1 // $Id: gui_manager.cxx,v 1.37 2003/07/28 22:46:48 grumbel Exp $
3 // Construo - A wire-frame construction game
4 // Copyright (C) 2002 Ingo Ruhnke <grumbel@gmx.de>
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.
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"
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 ()
42 start_time
= system_context
->get_time ();
45 grabbing_component
= 0;
46 current_component
= 0;
50 GUIManager::~GUIManager ()
55 GUIManager::run_once ()
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
;
68 start_time
= system_context
->get_time ();
75 graphic_context
->clear ();
78 graphic_context
->flip ();
84 for (ComponentLst::iterator i
= components
.begin (); i
!= components
.end (); ++i
)
86 (*i
)->draw (graphic_context
);
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
))
103 GUIManager::process_button_events (ButtonEvent
& button
)
105 int x
= input_context
->get_mouse_x();
106 int y
= input_context
->get_mouse_y();
112 case BUTTON_FULLSCREEN
:
113 // graphic_centext->toggle_fullscreen();
117 Controller::instance()->start_simulation ();
121 current_component
->on_primary_button_press(x
, y
);
124 case BUTTON_SECONDARY
:
125 current_component
->on_secondary_button_press(x
, y
);
128 case BUTTON_TERTIARY
:
129 current_component
->on_tertiary_button_press(x
, y
);
133 current_component
->on_scale_press (x
, y
);
137 current_component
->on_fix_press (x
, y
);
141 current_component
->on_join_press (x
, y
);
145 current_component
->on_grid_press(x
, y
);
149 current_component
->on_delete_press (x
, y
);
151 case BUTTON_DUPLICATE
:
152 current_component
->on_duplicate_press (x
, y
);
155 case BUTTON_SCROLL_LEFT
:
156 current_component
->scroll_left ();
159 case BUTTON_SCROLL_RIGHT
:
160 current_component
->scroll_right ();
163 case BUTTON_SCROLL_UP
:
164 current_component
->scroll_up ();
167 case BUTTON_SCROLL_DOWN
:
168 current_component
->scroll_down ();
172 Controller::instance()->clear_world ();
176 Controller::instance()->undo ();
180 Controller::instance()->redo ();
183 case BUTTON_ACTIONCAM
:
184 Controller::instance()->set_action_cam (!Controller::instance()->get_action_cam ());
187 case BUTTON_HIDEDOTS
:
188 Controller::instance()->set_hide_dots (!Controller::instance()->get_hide_dots ());
192 ScreenManager::instance()->quit();
195 case BUTTON_MODE_CHANGE
:
196 if (WorldViewComponent::instance()->get_mode () == WorldViewComponent::INSERT_MODE
)
198 WorldViewComponent::instance()->set_mode(WorldViewComponent::SELECT_MODE
);
202 WorldViewComponent::instance()->set_mode(WorldViewComponent::INSERT_MODE
);
206 case BUTTON_TOGGLESLOWMO
:
207 Controller::instance()->set_slow_down (!Controller::instance()->slow_down_active ());
211 Controller::instance()->start_simulation ();
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
);
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
);
240 case BUTTON_ZOOM_OUT
:
241 current_component
->wheel_down (x
, y
);
245 current_component
->wheel_up (x
, y
);
249 current_component
->on_button_press (button
.id
, x
, y
);
253 else // button released
258 current_component
->on_primary_button_release(x
, y
);
261 case BUTTON_SECONDARY
:
262 current_component
->on_secondary_button_release(x
, y
);
265 case BUTTON_TERTIARY
:
266 current_component
->on_tertiary_button_release(x
, y
);
271 std::cout
<< "GUIManager:process_button_events: Got unhandled BUTTON_EVENT release: "
272 << button
.id
<< std::endl
;
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 ();
302 last_component
->on_mouse_leave ();
304 last_component
= current_component
;
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();
323 while (input_context
->get_event (&event
))
325 if (current_component
)
330 //std::cout << "BUTTON_EVENT: " << event.button.id << " state: " << event.button.pressed << std::endl;
331 process_button_events (event
.button
);
334 std::cout
<< "GUIManager: Unhandled event type" << std::endl
;
345 GUIManager::grab_mouse (GUIComponent
* comp
)
347 grabbing_component
= comp
;
348 current_component
= comp
;
352 GUIManager::ungrab_mouse (GUIComponent
* comp
)
354 grabbing_component
= 0;
358 GUIManager::add (GUIComponent
* c
)
361 components
.push_back(c
);