1
// $Id: context_menu.cpp 3142 2007-09-14 15:39:32Z grumbel $
3
// Pingus - A free Lemmings clone
4
// Copyright (C) 2007 Jason Green <jave27@gmail.com>,
5
// Ingo Ruhnke <grumbel@gmx.de>
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.
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.
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.
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"
31
// Determine which actions are available for these objects
32
ContextMenu::ContextMenu(std::vector<LevelObj*> o, Vector2i p, EditorViewport* vp, bool base_menu)
36
selected_action_offset(0),
38
item_height(Fonts::verdana11.get_height())
42
// Create all available child menus
53
total_height = item_height * actions.size();
57
ContextMenu::~ContextMenu()
62
// Keep track of where the mouse is for highlighting
64
ContextMenu::on_pointer_move(int x, int y)
69
selected_action_offset = (unsigned)((mouse_at.y - pos.y) / item_height);
73
ContextMenu::draw(DrawingContext &gc)
78
gc.draw_fillrect(pos.x, pos.y, pos.x + width, pos.y + total_height,
79
Color(211,211,211,100));
81
gc.draw_rect(pos.x, pos.y, pos.x + width, pos.y + total_height,
83
// Draw the highlighted action if the mouse is in the box
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));
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);
99
ContextMenu::is_at(int x, int y)
102
return (x > pos.x && x < pos.x + (int)width &&
103
y > pos.y && y < pos.y + (int)total_height);
109
ContextMenu::on_primary_button_click(int x, int y)
111
if (!actions[selected_action_offset].child)
113
for (unsigned i = 0; i < objs.size(); i++)
115
switch (actions[selected_action_offset].modifier)
121
objs[i]->set_modifier(actions[selected_action_offset].parameter);
124
objs[i]->set_owner(StringUtil::to<int>(actions[selected_action_offset].parameter));
126
case (SET_DIRECTION) :
127
objs[i]->set_direction(actions[selected_action_offset].parameter);
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());
138
// FIXME: should be handled differently: viewport->remove_context_menu();
143
ContextMenu::on_secondary_button_click(int x, int y)
145
// Does the same as the primary button
146
on_primary_button_click(x, y);
150
ContextMenu::create_child_menus()
152
// Create Remove button - available to all objects
153
actions.push_back(ContextItem("Remove", "", REMOVE, 0));
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();
161
if (available_attribs & CAN_ROTATE)
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));
175
if (available_attribs & HAS_OWNER)
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));
185
if (available_attribs & HAS_DIRECTION)
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));
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));
203
// TODO - Add more menu options here
207
ContextMenu::add_action(ContextItem item)
209
actions.push_back(item);
210
total_height += item_height;
214
ContextMenu::display(bool should_display)
216
show = should_display;
217
if (!show && displayed_child)
218
displayed_child->display(false);
222
ContextMenu::update(float delta)
226
if (displayed_child != actions[selected_action_offset].child)
230
displayed_child->display(false);
233
if (actions[selected_action_offset].child)
235
displayed_child = actions[selected_action_offset].child;
236
displayed_child->display(true);
241
} // Editor namespace