~ubuntu-branches/ubuntu/precise/pingus/precise

« back to all changes in this revision

Viewing changes to src/editor/context_menu.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cyril Brulebois
  • Date: 2008-02-28 19:44:25 UTC
  • mfrom: (4.1.4 hardy)
  • Revision ID: james.westby@ubuntu.com-20080228194425-e8ilohlijv02kgcf
Tags: 0.7.2-2
* Fix FTBFS with gcc-4.3 by adding the missing include in
  src/input/evdev_device.cpp (Closes: #462238):
   + debian/patches/20_fix_FTBFS_with_gcc-4.3.
* Rename former patch so that the filename reflects the order in which
  the patches are applied:
   - debian/patches/data_dir.patch
   + debian/patches/10_fix_data_directory.
* Bump Standards-Version from 3.7.2 to 3.7.3, no changes needed.
* Add a dh_desktop call in the arch-dep part of debian/rules.
* Adjust the “missing-dep-for-interpreter guile” override since lintian
  now lists an alternative for that dependency.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  $Id: context_menu.cpp 3142 2007-09-14 15:39:32Z grumbel $
 
2
//
 
3
//  Pingus - A free Lemmings clone
 
4
//  Copyright (C) 2007 Jason Green <jave27@gmail.com>,
 
5
//                     Ingo Ruhnke <grumbel@gmx.de>
 
6
//
 
7
//  This program is free software; you can redistribute it and/or
 
8
//  modify it under the terms of the GNU General Public License
 
9
//  as published by the Free Software Foundation; either version 2
 
10
//  of the License, or (at your option) any later version.
 
11
//
 
12
//  This program is distributed in the hope that it will be useful,
 
13
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
//  GNU General Public License for more details.
 
16
//
 
17
//  You should have received a copy of the GNU General Public License
 
18
//  along with this program; if not, write to the Free Software
 
19
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
20
 
 
21
#include "context_menu.hpp"
 
22
#include "level_objs.hpp"
 
23
#include "editor_viewport.hpp"
 
24
#include "editor_screen.hpp"
 
25
#include "../gui/gui_manager.hpp"
 
26
#include "../string_util.hpp"
 
27
#include "../fonts.hpp"
 
28
 
 
29
namespace Editor {
 
30
 
 
31
// Determine which actions are available for these objects
 
32
ContextMenu::ContextMenu(std::vector<LevelObj*> o, Vector2i p, EditorViewport* vp, bool base_menu)
 
33
  : objs(o), 
 
34
    viewport(vp),
 
35
    pos(p),
 
36
    selected_action_offset(0),
 
37
    displayed_child(0),
 
38
    item_height(Fonts::verdana11.get_height())
 
39
{
 
40
  if (base_menu)
 
41
    {
 
42
      // Create all available child menus
 
43
      width = 110;
 
44
      show = true;
 
45
      create_child_menus();
 
46
    }
 
47
  else
 
48
    {
 
49
      width = 200;
 
50
      show = false;
 
51
    }
 
52
 
 
53
  total_height  = item_height * actions.size();
 
54
}
 
55
 
 
56
 
 
57
ContextMenu::~ContextMenu()
 
58
{
 
59
}
 
60
 
 
61
 
 
62
// Keep track of where the mouse is for highlighting
 
63
void
 
64
ContextMenu::on_pointer_move(int x, int y)
 
65
{
 
66
  mouse_at.x = x; 
 
67
  mouse_at.y = y;
 
68
 
 
69
  selected_action_offset = (unsigned)((mouse_at.y - pos.y) / item_height);
 
70
}
 
71
 
 
72
void
 
73
ContextMenu::draw(DrawingContext &gc)
 
74
{
 
75
  if (show)
 
76
    {
 
77
      // Draw the box
 
78
      gc.draw_fillrect(pos.x, pos.y, pos.x + width, pos.y + total_height, 
 
79
                       Color(211,211,211,100));
 
80
      // Draw the border
 
81
      gc.draw_rect(pos.x, pos.y, pos.x + width, pos.y + total_height, 
 
82
                   Color(0,0,0));
 
83
      // Draw the highlighted action if the mouse is in the box
 
84
      if (hover)
 
85
        {
 
86
          gc.draw_fillrect(pos.x, pos.y + selected_action_offset * 
 
87
                           item_height, pos.x + width, pos.y + (selected_action_offset + 1) * item_height,
 
88
                           Color(128,128,128,150));
 
89
        }
 
90
 
 
91
      // Draw the action names
 
92
      for (unsigned i = 0; i < actions.size(); i++)
 
93
        gc.print_left(Fonts::verdana11, pos.x, pos.y + 
 
94
                      (i * item_height), actions[i].friendly_name);
 
95
    }
 
96
}
 
97
 
 
98
bool
 
99
ContextMenu::is_at(int x, int y)
 
100
{
 
101
  if (show)
 
102
    return (x > pos.x && x < pos.x + (int)width &&
 
103
            y > pos.y && y < pos.y + (int)total_height);
 
104
  else
 
105
    return false;
 
106
}
 
107
 
 
108
void 
 
109
ContextMenu::on_primary_button_click(int x, int y)
 
110
{
 
111
  if (!actions[selected_action_offset].child)
 
112
    {
 
113
      for (unsigned i = 0; i < objs.size(); i++)
 
114
        {
 
115
          switch (actions[selected_action_offset].modifier)
 
116
            {
 
117
              case (REMOVE) : 
 
118
                objs[i]->remove();
 
119
                break;
 
120
              case (ROTATE) :
 
121
                objs[i]->set_modifier(actions[selected_action_offset].parameter);
 
122
                break;
 
123
              case (SET_OWNER) :
 
124
                objs[i]->set_owner(StringUtil::to<int>(actions[selected_action_offset].parameter));
 
125
                break;
 
126
              case (SET_DIRECTION) :
 
127
                objs[i]->set_direction(actions[selected_action_offset].parameter);
 
128
                break;
 
129
              case (SET_Z_POS) :
 
130
                objs[i]->set_pos(Vector3f(objs[i]->get_pos().x, objs[i]->get_pos().y, 
 
131
                                          (float)StringUtil::to<int>(actions[selected_action_offset].parameter)));
 
132
                objs[i]->set_orig_pos(objs[i]->get_pos());
 
133
                break;
 
134
              default :
 
135
                break;
 
136
            }
 
137
        }
 
138
      // FIXME: should be handled differently: viewport->remove_context_menu();
 
139
    }
 
140
}
 
141
 
 
142
void
 
143
ContextMenu::on_secondary_button_click(int x, int y)
 
144
{
 
145
  // Does the same as the primary button
 
146
  on_primary_button_click(x, y);
 
147
}
 
148
 
 
149
void
 
150
ContextMenu::create_child_menus()
 
151
{
 
152
  // Create Remove button - available to all objects
 
153
  actions.push_back(ContextItem("Remove", "", REMOVE, 0));
 
154
 
 
155
  // Determine which actions are available to the selected objects
 
156
  unsigned available_attribs = 0xffff;
 
157
  for (unsigned i = 0; i < (unsigned)objs.size(); i++)
 
158
    available_attribs = available_attribs & objs[i]->get_attribs();
 
159
 
 
160
  ContextMenu* menu;
 
161
  if (available_attribs & CAN_ROTATE)
 
162
    {
 
163
      menu = new ContextMenu(objs, Vector2i(pos.x + width, pos.y), viewport, false);
 
164
      viewport->get_screen()->get_gui_manager()->add(menu, true);
 
165
      menu->add_action(ContextItem("0 degrees", "ROT0", ROTATE, 0));
 
166
      menu->add_action(ContextItem("90 Degrees", "ROT90", ROTATE, 0));
 
167
      menu->add_action(ContextItem("180 Degrees", "ROT180", ROTATE, 0));
 
168
      menu->add_action(ContextItem("270 Degrees", "ROT270", ROTATE, 0));
 
169
      menu->add_action(ContextItem("0 Degrees + Flip", "ROT0FLIP", ROTATE, 0));
 
170
      menu->add_action(ContextItem("90 Degrees + Flip", "ROT90FLIP", ROTATE, 0));
 
171
      menu->add_action(ContextItem("180 Degrees + Flip", "ROT180FLIP", ROTATE, 0));
 
172
      menu->add_action(ContextItem("270 Degrees + Flip", "ROT270FLIP", ROTATE, 0));
 
173
      add_action(ContextItem("Rotate >", "", ROTATE, menu));
 
174
    }
 
175
  if (available_attribs & HAS_OWNER)
 
176
    {
 
177
      menu = new ContextMenu(objs, Vector2i(pos.x + width, pos.y), viewport, false);
 
178
      viewport->get_screen()->get_gui_manager()->add(menu, true);
 
179
      menu->add_action(ContextItem("0", "0", SET_OWNER, 0));
 
180
      menu->add_action(ContextItem("1", "1", SET_OWNER, 0));
 
181
      menu->add_action(ContextItem("2", "2", SET_OWNER, 0));
 
182
      menu->add_action(ContextItem("3", "3", SET_OWNER, 0));
 
183
      add_action(ContextItem("Set Owner >", "", SET_OWNER, menu));
 
184
    }
 
185
  if (available_attribs & HAS_DIRECTION)
 
186
    {
 
187
      menu = new ContextMenu(objs, Vector2i(pos.x + width, pos.y), viewport, false);
 
188
      viewport->get_screen()->get_gui_manager()->add(menu, true);
 
189
      menu->add_action(ContextItem("Left", "left", SET_DIRECTION, 0));
 
190
      menu->add_action(ContextItem("Right", "right", SET_DIRECTION, 0));
 
191
      menu->add_action(ContextItem("Misc.", "misc", SET_DIRECTION, 0));
 
192
      add_action(ContextItem("Direction >", "", SET_DIRECTION, menu));
 
193
    }
 
194
  menu = new ContextMenu(objs, Vector2i(pos.x + width, pos.y), viewport, false);
 
195
  viewport->get_screen()->get_gui_manager()->add(menu, true);
 
196
  menu->add_action(ContextItem("-50", "-50", SET_Z_POS, 0));
 
197
  menu->add_action(ContextItem("-25", "-25", SET_Z_POS, 0));
 
198
  menu->add_action(ContextItem("0", "0", SET_Z_POS, 0));
 
199
  menu->add_action(ContextItem("25", "25", SET_Z_POS, 0));
 
200
  menu->add_action(ContextItem("50", "50", SET_Z_POS, 0));
 
201
  add_action(ContextItem("Set Z Pos. >", "", SET_Z_POS, menu));
 
202
                
 
203
  // TODO - Add more menu options here
 
204
}
 
205
 
 
206
void
 
207
ContextMenu::add_action(ContextItem item)
 
208
{
 
209
  actions.push_back(item);
 
210
  total_height += item_height;
 
211
}
 
212
 
 
213
void
 
214
ContextMenu::display(bool should_display)
 
215
{
 
216
  show = should_display;
 
217
  if (!show && displayed_child)
 
218
    displayed_child->display(false);
 
219
}
 
220
 
 
221
void
 
222
ContextMenu::update(float delta)
 
223
{
 
224
  UNUSED_ARG(delta);
 
225
 
 
226
  if (displayed_child != actions[selected_action_offset].child)
 
227
    {
 
228
      if (displayed_child)
 
229
        {
 
230
          displayed_child->display(false);
 
231
          displayed_child = 0;
 
232
        }
 
233
      if (actions[selected_action_offset].child)
 
234
        {
 
235
          displayed_child = actions[selected_action_offset].child;
 
236
          displayed_child->display(true);
 
237
        }
 
238
    }
 
239
}
 
240
 
 
241
} // Editor namespace
 
242
 
 
243
/* EOF */