Fixed autotools stuff
[construo.git] / worldview_component.cxx
blobfe53b6307ace325387cb43b004ca584866d2aa4c
1 // $Id: worldview_component.cxx,v 1.35 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 "colors.hxx"
21 #include "controller.hxx"
22 #include "root_graphic_context.hxx"
23 #include "world_gui_manager.hxx"
24 #include "worldview_tool.hxx"
25 #include "worldview_insert_tool.hxx"
26 #include "worldview_select_tool.hxx"
27 #include "worldview_zoom_tool.hxx"
28 #include "worldview_collider_tool.hxx"
29 #include "worldview_component.hxx"
31 WorldViewComponent* WorldViewComponent::instance_;
33 WorldViewComponent::WorldViewComponent ()
34 : GUIComponent(0, 0, graphic_context->get_width (), graphic_context->get_height ())
36 instance_ = this;
38 scrolling = false;
39 use_grid = false;
41 select_tool = new WorldViewSelectTool ();
42 insert_tool = new WorldViewInsertTool ();
43 zoom_tool = new WorldViewZoomTool ();
44 collider_tool = new WorldViewColliderTool ();
46 current_tool = insert_tool;
47 mode = INSERT_MODE;
49 on_world_change();
52 void
53 WorldViewComponent::set_mode (Mode m)
55 current_tool->deactivate ();
57 if (m == INSERT_MODE)
59 current_tool = insert_tool;
60 mode = INSERT_MODE;
61 graphic_context->set_cursor(CURSOR_INSERT);
63 else if (m == SELECT_MODE)
65 current_tool = select_tool;
66 mode = SELECT_MODE;
67 graphic_context->set_cursor(CURSOR_SELECT);
69 else if (m == ZOOM_MODE)
71 current_tool = zoom_tool;
72 mode = ZOOM_MODE;
73 graphic_context->set_cursor(CURSOR_ZOOM);
75 else if (m == COLLIDER_MODE)
77 current_tool = collider_tool;
78 mode = COLLIDER_MODE;
79 graphic_context->set_cursor(CURSOR_COLLIDER);
81 else
83 std::cout << "Unknown Mode" << std::endl;
84 assert (false);
87 current_tool->activate ();
90 WorldViewComponent::~WorldViewComponent ()
92 instance_ = 0;
95 void
96 WorldViewComponent::draw_grid()
98 if (gc.get_zoom() > 0.4f)
100 Color color = Colors::grid_color;
101 int start_x = Math::round_to(gc.screen_to_world_x(0), 10) - 10;
102 int end_x = Math::round_to(gc.screen_to_world_x(gc.get_width()), 10) + 10;
104 int start_y = Math::round_to(gc.screen_to_world_y(0), 10) - 10;
105 int end_y = Math::round_to(gc.screen_to_world_y(gc.get_height()), 10) + 10;
107 gc.push_quick_draw();
108 for(int y = start_y; y < end_y; y += 10)
109 gc.draw_line(start_x, y,
110 end_x, y,
111 color, 1);
113 for(int x = start_x; x < end_x; x += 10)
114 gc.draw_line(x, start_y,
115 x, end_y,
116 color, 1);
117 gc.pop_quick_draw();
121 void
122 WorldViewComponent::draw_ground()
124 GraphicContext* parent_gc = gc.get_parent_gc();
126 if (gc.screen_to_world_y(parent_gc->get_height()) >= 599)
128 gc.draw_fill_rect(gc.screen_to_world_x(0),
129 599,
130 gc.screen_to_world_x(parent_gc->get_width()),
131 gc.screen_to_world_y(parent_gc->get_height()),
132 Colors::ground_color);
134 Color color = Colors::ground_grid_color;
136 int step_size = 100;
138 int start_x = Math::round_to(gc.screen_to_world_x(0), step_size) - step_size;
139 int end_x = Math::round_to(gc.screen_to_world_x(gc.get_width()), step_size) + step_size;
141 int start_y = 599;
142 int end_y = Math::round_to(gc.screen_to_world_y(gc.get_height()), step_size) + step_size;
144 gc.push_quick_draw();
145 for(int y = start_y; y < end_y; y += step_size)
146 gc.draw_line(start_x, y,
147 end_x, y,
148 color, 1);
150 for(int x = start_x; x < end_x; x += step_size)
151 gc.draw_line(x, start_y,
152 x, end_y,
153 color, 1);
154 gc.pop_quick_draw();
156 gc.draw_rect(gc.screen_to_world_x(0),
157 599,
158 gc.screen_to_world_x(parent_gc->get_width()),
159 gc.screen_to_world_y(parent_gc->get_height()),
160 Colors::rect_collider_bg);
164 void
165 WorldViewComponent::draw (GraphicContext* parent_gc)
167 //int x = gc.screen_to_world_x (input_context->get_mouse_x ());
168 //int y = gc.screen_to_world_y (input_context->get_mouse_y ());
170 gc.set_parent_gc (parent_gc);
172 if (use_grid)
173 draw_grid();
175 draw_ground();
177 World& world = *Controller::instance()->get_world();
179 if (Controller::instance()->get_action_cam()
180 && Controller::instance()->is_running())
182 // Live Action Cam
183 const BoundingBox& box = world.calc_bounding_box();
184 // Zoom to the bounding box
185 gc.zoom_to((int) box.x1, (int)box.y1,
186 (int)box.x2, (int)box.y2);
187 // Zoom out two times so that the area isn't covered up by the
188 // GUI
189 gc.zoom_out (get_width()/2, get_height()/2);
190 gc.zoom_out (get_width()/2, get_height()/2);
193 current_tool->draw_background (&gc);
195 if (0) // draw bounding box
197 const BoundingBox& box = world.calc_bounding_box();
198 gc.draw_rect(box.x1, box.y1, box.x2, box.y2,
199 Color(1.0f, 1.0f, 1.0f));
202 world.draw_colliders (&gc);
203 world.draw_springs (&gc);
204 if (!Controller::instance()->get_hide_dots())
205 world.draw_particles (&gc);
207 current_tool->draw_foreground (&gc);
209 if (0)
211 switch (mode)
213 case ZOOM_MODE:
214 parent_gc->draw_string (10, parent_gc->get_height () - 15, "[ Zoom Mode ]");
215 break;
216 case INSERT_MODE:
217 parent_gc->draw_string (10, parent_gc->get_height () - 15, "[ Insert Mode ]");
218 break;
219 case SELECT_MODE:
220 parent_gc->draw_string (10, parent_gc->get_height () - 15, "[ Select Mode ]");
221 break;
222 case COLLIDER_MODE:
223 parent_gc->draw_string (10, parent_gc->get_height () - 15, "[Collider Mode]");
224 break;
228 //const WorldBoundingBox& box = world.calc_bounding_box();
229 //gc.flip (int(box.x1), int(box.y1), int(box.x2), int(box.y2));
232 void
233 WorldViewComponent::wheel_up (int x, int y)
235 gc.zoom_in (x, y);
238 void
239 WorldViewComponent::wheel_down (int x, int y)
241 gc.zoom_out (x, y);
244 void
245 WorldViewComponent::on_button_press (int button_id, int x, int y)
247 current_tool->on_button_press (button_id, x, y);
250 void
251 WorldViewComponent::on_primary_button_press (int screen_x, int screen_y)
253 current_tool->on_primary_button_press (screen_x, screen_y);
256 void
257 WorldViewComponent::on_primary_button_release (int screen_x, int screen_y)
259 current_tool->on_primary_button_release (screen_x, screen_y);
262 void
263 WorldViewComponent::on_secondary_button_press (int screen_x, int screen_y)
265 current_tool->on_secondary_button_press (screen_x, screen_y);
268 void
269 WorldViewComponent::on_secondary_button_release (int screen_x, int screen_y)
271 current_tool->on_secondary_button_release (screen_x, screen_y);
274 void
275 WorldViewComponent::on_delete_press (int screen_x, int screen_y)
277 current_tool->on_delete_press (screen_x, screen_y);
280 void
281 WorldViewComponent::on_duplicate_press (int screen_x, int screen_y)
283 current_tool->on_duplicate_press (screen_x, screen_y);
286 void
287 WorldViewComponent::on_join_press (int x, int y)
289 current_tool->on_join_press (x, y);
292 void
293 WorldViewComponent::on_fix_press (int screen_x, int screen_y)
295 current_tool->on_fix_press (screen_x, screen_y);
298 void
299 WorldViewComponent::scroll_left ()
301 gc.translate_offset (-20, 0);
304 void
305 WorldViewComponent::scroll_right ()
307 gc.translate_offset (20, 0);
310 void
311 WorldViewComponent::scroll_up ()
313 gc.translate_offset (0, -20);
316 void
317 WorldViewComponent::scroll_down ()
319 gc.translate_offset (0, 20);
322 void
323 WorldViewComponent::on_tertiary_button_press (int x, int y)
325 scrolling = true;
326 graphic_context->push_cursor();
327 graphic_context->set_cursor(CURSOR_SCROLL);
329 x_offset = gc.get_x_offset ();
330 y_offset = gc.get_y_offset ();
331 WorldGUIManager::instance()->grab_mouse (this);
333 scroll_pos_x = gc.screen_to_world_x(x);
334 scroll_pos_y = gc.screen_to_world_y(y);
337 void
338 WorldViewComponent::on_tertiary_button_release (int x, int y)
340 graphic_context->pop_cursor();
341 scrolling = false;
342 WorldGUIManager::instance()->ungrab_mouse (this);
345 void
346 WorldViewComponent::on_mouse_move (int x, int y, int of_x, int of_y)
348 if (scrolling)
350 int new_scroll_pos_x = int(x/gc.get_zoom() - x_offset);
351 int new_scroll_pos_y = int(y/gc.get_zoom() - y_offset);
353 gc.set_offset (x_offset + (new_scroll_pos_x - scroll_pos_x),
354 y_offset + (new_scroll_pos_y - scroll_pos_y));
357 else
359 current_tool->on_mouse_move (x, y, of_x, of_y);
363 void
364 WorldViewComponent::on_scale_press(int x, int y)
366 current_tool->on_scale_press(x,y);
369 void
370 WorldViewComponent::on_grid_press(int x, int y)
372 use_grid = !use_grid;
375 float
376 WorldViewComponent::get_zoom ()
378 return gc.get_zoom ();
381 void
382 WorldViewComponent::on_world_change()
384 std::cout << "Fitting world into view" << std::endl;
385 World& world = *Controller::instance()->get_world();
387 const BoundingBox& box = world.calc_bounding_box();
388 // Zoom to the bounding box
389 gc.zoom_to((int) box.x1, (int)box.y1,
390 (int)box.x2, (int)box.y2);
391 // Zoom out two times so that the area isn't covered up by the
392 // GUI
393 gc.zoom_out (get_width()/2, get_height()/2);
394 gc.zoom_out (get_width()/2, get_height()/2);
397 /* EOF */