1 // $Id: worldview_collider_tool.cxx,v 1.4 2003/01/11 16:11:36 grumbel Exp $
3 // Construo - A wire-frame construction gamee
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.
20 #include "construo.hxx"
21 #include "input_context.hxx"
22 #include "controller.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;
36 WorldViewColliderTool::~WorldViewColliderTool ()
41 WorldViewColliderTool::draw_background (ZoomGraphicContext
* gc
)
43 Vector2d mouse_pos
= WorldViewComponent::instance()->get_gc()->screen_to_world(input_context
->get_mouse_pos ());
46 gc
->GraphicContext::draw_rect(click_pos
, mouse_pos
, Colors::selection_rect
);
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
)
65 WorldViewColliderTool::draw_foreground (ZoomGraphicContext
* gc
)
68 = WorldViewComponent::instance()->get_gc()->screen_to_world(input_context
->get_mouse_pos ());
69 Collider
* collider
= get_collider (mouse_pos
);
72 collider
->draw_highlight(gc
);
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();
91 Controller::instance()->push_undo();
97 WorldViewColliderTool::on_primary_button_release (int x
, int y
)
99 WorldGUIManager::instance()->ungrab_mouse (WorldViewComponent::instance());
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
;
113 world
.add_rect_collider (click_pos
, pos2
);
117 creating_rect
= false;
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
));
128 move_collider
->set_pos(current_pos
- click_pos
);
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
)));
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;