Fixed includes and bugs so that it compiles
[construo.git] / worldview_collider_tool.cxx
blobcb4d751e50c852df6c26cccefaac4035f5a9ee8b
1 // $Id: worldview_collider_tool.cxx,v 1.4 2003/01/11 16:11:36 grumbel Exp $
2 //
3 // Construo - A wire-frame construction gamee
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 "construo.hxx"
21 #include "input_context.hxx"
22 #include "controller.hxx"
23 #include "colors.hxx"
24 #include "world.hxx"
25 #include "world_gui_manager.hxx"
26 #include "worldview_component.hxx"
27 #include "worldview_collider_tool.hxx"
29 WorldViewColliderTool::WorldViewColliderTool ()
31 creating_rect = false;
32 to_delete_collider = 0;
33 move_collider = 0;
36 WorldViewColliderTool::~WorldViewColliderTool ()
40 void
41 WorldViewColliderTool::draw_background (ZoomGraphicContext* gc)
43 Vector2d mouse_pos = WorldViewComponent::instance()->get_gc()->screen_to_world(input_context->get_mouse_pos ());
44 if (creating_rect)
46 gc->GraphicContext::draw_rect(click_pos, mouse_pos, Colors::selection_rect);
50 Collider*
51 WorldViewColliderTool::get_collider (const Vector2d& pos)
53 World& world = *Controller::instance()->get_world();
54 World::Colliders& colliders = world.get_colliders();
55 for (World::Colliders::reverse_iterator i = colliders.rbegin ();
56 i != colliders.rend(); ++i)
58 if ((*i)->is_at(pos))
59 return *i;
61 return 0;
64 void
65 WorldViewColliderTool::draw_foreground (ZoomGraphicContext* gc)
67 Vector2d mouse_pos
68 = WorldViewComponent::instance()->get_gc()->screen_to_world(input_context->get_mouse_pos ());
69 Collider* collider = get_collider (mouse_pos);
71 if (collider)
72 collider->draw_highlight(gc);
75 void
76 WorldViewColliderTool::on_primary_button_press (int x, int y)
78 WorldGUIManager::instance()->grab_mouse (WorldViewComponent::instance());
80 click_pos = WorldViewComponent::instance()->get_gc()->screen_to_world(input_context->get_mouse_pos ());
82 if ((move_collider = get_collider (click_pos)) != 0)
84 // click_pos Offset, not position
85 click_pos = click_pos - move_collider->get_pos();
86 creating_rect = false;
87 Controller::instance()->push_undo();
89 else
91 Controller::instance()->push_undo();
92 creating_rect = true;
96 void
97 WorldViewColliderTool::on_primary_button_release (int x, int y)
99 WorldGUIManager::instance()->ungrab_mouse (WorldViewComponent::instance());
101 if (creating_rect)
103 Vector2d pos2 = WorldViewComponent::instance()->get_gc()->screen_to_world(input_context->get_mouse_pos ());
104 World& world = *Controller::instance()->get_world();
106 if (fabs(pos2.x - click_pos.x) < 15
107 || fabs(pos2.y - click_pos.y) < 15)
109 std::cout << "Rect collider to small, not inserting" << std::endl;
111 else
113 world.add_rect_collider (click_pos, pos2);
117 creating_rect = false;
118 move_collider = 0;
121 void
122 WorldViewColliderTool::on_mouse_move (int x, int y, int of_x, int of_y)
124 Vector2d current_pos = WorldViewComponent::instance()->get_gc()->screen_to_world(Vector2d(x,y));
126 if (move_collider)
128 move_collider->set_pos(current_pos - click_pos);
132 void
133 WorldViewColliderTool::on_secondary_button_press (int x, int y)
135 to_delete_collider = get_collider(WorldViewComponent::instance()->get_gc()->screen_to_world(Vector2d(x, y)));
138 void
139 WorldViewColliderTool::on_secondary_button_release (int x, int y)
141 World& world = *Controller::instance()->get_world();
143 if (to_delete_collider
144 == get_collider(WorldViewComponent::instance()->get_gc()->screen_to_world(Vector2d(x, y))))
146 Controller::instance()->push_undo();
147 world.remove_collider(to_delete_collider);
149 to_delete_collider = 0;
152 /* EOF */