Imported vanilla contents from upstream
[construo.git] / gui_manager.cxx
blob67916c2ac623630593ebc3d6a62b87e5a8c6030b
1 // $Id: gui_manager.cxx,v 1.37 2003/07/28 22:46:48 grumbel Exp $
2 //
3 // Construo - A wire-frame construction game
4 // Copyright (C) 2002 Ingo Ruhnke <grumbel@gmx.de>
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #include <config.h>
21 #include "string_utils.hxx"
22 #include "construo.hxx"
23 #include "graphic_context.hxx"
24 #include "input_context.hxx"
25 #include "system_context.hxx"
26 #include "controller.hxx"
27 #include "keep_alive_mgr.hxx"
28 #include "events.hxx"
29 #include "gui_component.hxx"
30 #include "gui_manager.hxx"
31 #include "worldview_component.hxx"
32 #include "worldview_insert_tool.hxx"
33 #include "globals.hxx"
34 #include "root_graphic_context.hxx"
35 #include "screen_manager.hxx"
37 using namespace StringUtils;
39 GUIManager::GUIManager ()
41 frame_count = 0;
42 start_time = system_context->get_time ();
44 last_component = 0;
45 grabbing_component = 0;
46 current_component = 0;
50 GUIManager::~GUIManager ()
54 void
55 GUIManager::run_once ()
57 frame_count += 1;
59 if (start_time + 3000 < system_context->get_time ())
61 float passed_time = (system_context->get_time () - start_time) / 1000.0f;
63 //std::cout << "FPS: " << frame_count / passed_time << std::endl;
65 current_fps = frame_count / passed_time;
67 frame_count = 0;
68 start_time = system_context->get_time ();
71 process_events ();
73 update();
75 graphic_context->clear ();
76 draw ();
77 draw_overlay ();
78 graphic_context->flip ();
81 void
82 GUIManager::draw ()
84 for (ComponentLst::iterator i = components.begin (); i != components.end (); ++i)
86 (*i)->draw (graphic_context);
90 GUIComponent*
91 GUIManager::find_component_at (int x, int y)
93 GUIComponent* component = 0;
94 for (ComponentLst::iterator i = components.begin (); i != components.end (); ++i)
96 if ((*i)->is_at (x, y))
97 component = *i;
99 return component;
102 void
103 GUIManager::process_button_events (ButtonEvent& button)
105 int x = input_context->get_mouse_x();
106 int y = input_context->get_mouse_y();
108 if (button.pressed)
110 switch (button.id)
112 case BUTTON_FULLSCREEN:
113 // graphic_centext->toggle_fullscreen();
114 break;
116 case BUTTON_START:
117 Controller::instance()->start_simulation ();
118 break;
120 case BUTTON_PRIMARY:
121 current_component->on_primary_button_press(x, y);
122 break;
124 case BUTTON_SECONDARY:
125 current_component->on_secondary_button_press(x, y);
126 break;
128 case BUTTON_TERTIARY:
129 current_component->on_tertiary_button_press(x, y);
130 break;
132 case BUTTON_SCALE:
133 current_component->on_scale_press (x, y);
134 break;
136 case BUTTON_FIX:
137 current_component->on_fix_press (x, y);
138 break;
140 case BUTTON_JOIN:
141 current_component->on_join_press (x, y);
142 break;
144 case BUTTON_GRID:
145 current_component->on_grid_press(x, y);
146 break;
148 case BUTTON_DELETE:
149 current_component->on_delete_press (x, y);
150 break;
151 case BUTTON_DUPLICATE:
152 current_component->on_duplicate_press (x, y);
153 break;
155 case BUTTON_SCROLL_LEFT:
156 current_component->scroll_left ();
157 break;
159 case BUTTON_SCROLL_RIGHT:
160 current_component->scroll_right ();
161 break;
163 case BUTTON_SCROLL_UP:
164 current_component->scroll_up ();
165 break;
167 case BUTTON_SCROLL_DOWN:
168 current_component->scroll_down ();
169 break;
171 case BUTTON_CLEAR:
172 Controller::instance()->clear_world ();
173 break;
175 case BUTTON_UNDO:
176 Controller::instance()->undo ();
177 break;
179 case BUTTON_REDO:
180 Controller::instance()->redo ();
181 break;
183 case BUTTON_ACTIONCAM:
184 Controller::instance()->set_action_cam (!Controller::instance()->get_action_cam ());
185 break;
187 case BUTTON_HIDEDOTS:
188 Controller::instance()->set_hide_dots (!Controller::instance()->get_hide_dots ());
189 break;
191 case BUTTON_ESCAPE:
192 ScreenManager::instance()->quit();
193 break;
195 case BUTTON_MODE_CHANGE:
196 if (WorldViewComponent::instance()->get_mode () == WorldViewComponent::INSERT_MODE)
198 WorldViewComponent::instance()->set_mode(WorldViewComponent::SELECT_MODE);
200 else
202 WorldViewComponent::instance()->set_mode(WorldViewComponent::INSERT_MODE);
204 break;
206 case BUTTON_TOGGLESLOWMO:
207 Controller::instance()->set_slow_down (!Controller::instance()->slow_down_active ());
208 break;
210 case BUTTON_RUN:
211 Controller::instance()->start_simulation ();
212 break;
214 case BUTTON_QUICKSAVE0:
215 case BUTTON_QUICKSAVE1:
216 case BUTTON_QUICKSAVE2:
217 case BUTTON_QUICKSAVE3:
218 case BUTTON_QUICKSAVE4:
219 case BUTTON_QUICKSAVE5:
220 case BUTTON_QUICKSAVE6:
221 case BUTTON_QUICKSAVE7:
222 case BUTTON_QUICKSAVE8:
223 case BUTTON_QUICKSAVE9:
224 Controller::instance()->save_to_slot (button.id - BUTTON_QUICKSAVE0);
225 break;
227 case BUTTON_QUICKLOAD0:
228 case BUTTON_QUICKLOAD1:
229 case BUTTON_QUICKLOAD2:
230 case BUTTON_QUICKLOAD3:
231 case BUTTON_QUICKLOAD4:
232 case BUTTON_QUICKLOAD5:
233 case BUTTON_QUICKLOAD6:
234 case BUTTON_QUICKLOAD7:
235 case BUTTON_QUICKLOAD8:
236 case BUTTON_QUICKLOAD9:
237 Controller::instance()->load_from_slot (button.id - BUTTON_QUICKLOAD0);
238 break;
240 case BUTTON_ZOOM_OUT:
241 current_component->wheel_down (x, y);
242 break;
244 case BUTTON_ZOOM_IN:
245 current_component->wheel_up (x, y);
246 break;
248 default:
249 current_component->on_button_press (button.id, x, y);
250 break;
253 else // button released
255 switch (button.id)
257 case BUTTON_PRIMARY:
258 current_component->on_primary_button_release(x, y);
259 break;
261 case BUTTON_SECONDARY:
262 current_component->on_secondary_button_release(x, y);
263 break;
265 case BUTTON_TERTIARY:
266 current_component->on_tertiary_button_release(x, y);
267 break;
269 default:
270 #ifdef DEBUG
271 std::cout << "GUIManager:process_button_events: Got unhandled BUTTON_EVENT release: "
272 << button.id << std::endl;
273 #endif
274 break;
279 void
280 GUIManager::process_events ()
282 int x = input_context->get_mouse_x();
283 int y = input_context->get_mouse_y();
285 if (grabbing_component && (last_x != x || last_y != y))
287 grabbing_component->on_mouse_move (x, y, x - last_x, y - last_y);
289 if (current_component != grabbing_component)
291 current_component->on_mouse_move (x, y, x - last_x, y - last_y);
294 if (!grabbing_component)
296 current_component = find_component_at (x, y);
298 if (last_component != current_component)
300 if (current_component) current_component->on_mouse_enter ();
301 if (last_component)
302 last_component->on_mouse_leave ();
304 last_component = current_component;
307 else
309 GUIComponent* comp = find_component_at (x, y);
311 if (comp != grabbing_component)
313 grabbing_component->on_mouse_leave();
314 last_component = comp;
316 else if (last_component != grabbing_component)
318 grabbing_component->on_mouse_enter();
322 Event event;
323 while (input_context->get_event (&event))
325 if (current_component)
327 switch (event.type)
329 case BUTTON_EVENT:
330 //std::cout << "BUTTON_EVENT: " << event.button.id << " state: " << event.button.pressed << std::endl;
331 process_button_events (event.button);
332 break;
333 default:
334 std::cout << "GUIManager: Unhandled event type" << std::endl;
335 break;
340 last_x = x;
341 last_y = y;
344 void
345 GUIManager::grab_mouse (GUIComponent* comp)
347 grabbing_component = comp;
348 current_component = comp;
351 void
352 GUIManager::ungrab_mouse (GUIComponent* comp)
354 grabbing_component = 0;
357 void
358 GUIManager::add (GUIComponent* c)
360 assert(c);
361 components.push_back(c);
364 /* EOF */