1 // $Id: worldview_component.cxx,v 1.35 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 "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 ())
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
;
53 WorldViewComponent::set_mode (Mode m
)
55 current_tool
->deactivate ();
59 current_tool
= insert_tool
;
61 graphic_context
->set_cursor(CURSOR_INSERT
);
63 else if (m
== SELECT_MODE
)
65 current_tool
= select_tool
;
67 graphic_context
->set_cursor(CURSOR_SELECT
);
69 else if (m
== ZOOM_MODE
)
71 current_tool
= zoom_tool
;
73 graphic_context
->set_cursor(CURSOR_ZOOM
);
75 else if (m
== COLLIDER_MODE
)
77 current_tool
= collider_tool
;
79 graphic_context
->set_cursor(CURSOR_COLLIDER
);
83 std::cout
<< "Unknown Mode" << std::endl
;
87 current_tool
->activate ();
90 WorldViewComponent::~WorldViewComponent ()
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
,
113 for(int x
= start_x
; x
< end_x
; x
+= 10)
114 gc
.draw_line(x
, start_y
,
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),
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
;
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
;
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
,
150 for(int x
= start_x
; x
< end_x
; x
+= step_size
)
151 gc
.draw_line(x
, start_y
,
156 gc
.draw_rect(gc
.screen_to_world_x(0),
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
);
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
);
177 World
& world
= *Controller::instance()->get_world();
179 if (Controller::instance()->get_action_cam()
180 && Controller::instance()->is_running())
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
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
);
214 parent_gc
->draw_string (10, parent_gc
->get_height () - 15, "[ Zoom Mode ]");
217 parent_gc
->draw_string (10, parent_gc
->get_height () - 15, "[ Insert Mode ]");
220 parent_gc
->draw_string (10, parent_gc
->get_height () - 15, "[ Select Mode ]");
223 parent_gc
->draw_string (10, parent_gc
->get_height () - 15, "[Collider Mode]");
228 //const WorldBoundingBox& box = world.calc_bounding_box();
229 //gc.flip (int(box.x1), int(box.y1), int(box.x2), int(box.y2));
233 WorldViewComponent::wheel_up (int x
, int y
)
239 WorldViewComponent::wheel_down (int x
, int y
)
245 WorldViewComponent::on_button_press (int button_id
, int x
, int y
)
247 current_tool
->on_button_press (button_id
, x
, y
);
251 WorldViewComponent::on_primary_button_press (int screen_x
, int screen_y
)
253 current_tool
->on_primary_button_press (screen_x
, screen_y
);
257 WorldViewComponent::on_primary_button_release (int screen_x
, int screen_y
)
259 current_tool
->on_primary_button_release (screen_x
, screen_y
);
263 WorldViewComponent::on_secondary_button_press (int screen_x
, int screen_y
)
265 current_tool
->on_secondary_button_press (screen_x
, screen_y
);
269 WorldViewComponent::on_secondary_button_release (int screen_x
, int screen_y
)
271 current_tool
->on_secondary_button_release (screen_x
, screen_y
);
275 WorldViewComponent::on_delete_press (int screen_x
, int screen_y
)
277 current_tool
->on_delete_press (screen_x
, screen_y
);
281 WorldViewComponent::on_duplicate_press (int screen_x
, int screen_y
)
283 current_tool
->on_duplicate_press (screen_x
, screen_y
);
287 WorldViewComponent::on_join_press (int x
, int y
)
289 current_tool
->on_join_press (x
, y
);
293 WorldViewComponent::on_fix_press (int screen_x
, int screen_y
)
295 current_tool
->on_fix_press (screen_x
, screen_y
);
299 WorldViewComponent::scroll_left ()
301 gc
.translate_offset (-20, 0);
305 WorldViewComponent::scroll_right ()
307 gc
.translate_offset (20, 0);
311 WorldViewComponent::scroll_up ()
313 gc
.translate_offset (0, -20);
317 WorldViewComponent::scroll_down ()
319 gc
.translate_offset (0, 20);
323 WorldViewComponent::on_tertiary_button_press (int x
, int y
)
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
);
338 WorldViewComponent::on_tertiary_button_release (int x
, int y
)
340 graphic_context
->pop_cursor();
342 WorldGUIManager::instance()->ungrab_mouse (this);
346 WorldViewComponent::on_mouse_move (int x
, int y
, int of_x
, int of_y
)
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
));
359 current_tool
->on_mouse_move (x
, y
, of_x
, of_y
);
364 WorldViewComponent::on_scale_press(int x
, int y
)
366 current_tool
->on_scale_press(x
,y
);
370 WorldViewComponent::on_grid_press(int x
, int y
)
372 use_grid
= !use_grid
;
376 WorldViewComponent::get_zoom ()
378 return gc
.get_zoom ();
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
393 gc
.zoom_out (get_width()/2, get_height()/2);
394 gc
.zoom_out (get_width()/2, get_height()/2);