00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "construo.hxx"
00021 #include "input_context.hxx"
00022 #include "controller.hxx"
00023 #include "colors.hxx"
00024 #include "world.hxx"
00025 #include "world_gui_manager.hxx"
00026 #include "worldview_component.hxx"
00027 #include "worldview_collider_tool.hxx"
00028
00029 WorldViewColliderTool::WorldViewColliderTool ()
00030 {
00031 creating_rect = false;
00032 to_delete_collider = 0;
00033 move_collider = 0;
00034 }
00035
00036 WorldViewColliderTool::~WorldViewColliderTool ()
00037 {
00038 }
00039
00040 void
00041 WorldViewColliderTool::draw_background (ZoomGraphicContext* gc)
00042 {
00043 Vector2d mouse_pos = WorldViewComponent::instance()->get_gc()->screen_to_world(input_context->get_mouse_pos ());
00044 if (creating_rect)
00045 {
00046 gc->GraphicContext::draw_rect(click_pos, mouse_pos, Colors::selection_rect);
00047 }
00048 }
00049
00050 Collider*
00051 WorldViewColliderTool::get_collider (const Vector2d& pos)
00052 {
00053 World& world = *Controller::instance()->get_world();
00054 World::Colliders& colliders = world.get_colliders();
00055 for (World::Colliders::reverse_iterator i = colliders.rbegin ();
00056 i != colliders.rend(); ++i)
00057 {
00058 if ((*i)->is_at(pos))
00059 return *i;
00060 }
00061 return 0;
00062 }
00063
00064 void
00065 WorldViewColliderTool::draw_foreground (ZoomGraphicContext* gc)
00066 {
00067 Vector2d mouse_pos
00068 = WorldViewComponent::instance()->get_gc()->screen_to_world(input_context->get_mouse_pos ());
00069 Collider* collider = get_collider (mouse_pos);
00070
00071 if (collider)
00072 collider->draw_highlight(gc);
00073 }
00074
00075 void
00076 WorldViewColliderTool::on_primary_button_press (int x, int y)
00077 {
00078 WorldGUIManager::instance()->grab_mouse (WorldViewComponent::instance());
00079
00080 click_pos = WorldViewComponent::instance()->get_gc()->screen_to_world(input_context->get_mouse_pos ());
00081
00082 if ((move_collider = get_collider (click_pos)) != 0)
00083 {
00084
00085 click_pos = click_pos - move_collider->get_pos();
00086 creating_rect = false;
00087 Controller::instance()->push_undo();
00088 }
00089 else
00090 {
00091 Controller::instance()->push_undo();
00092 creating_rect = true;
00093 }
00094 }
00095
00096 void
00097 WorldViewColliderTool::on_primary_button_release (int x, int y)
00098 {
00099 WorldGUIManager::instance()->ungrab_mouse (WorldViewComponent::instance());
00100
00101 if (creating_rect)
00102 {
00103 Vector2d pos2 = WorldViewComponent::instance()->get_gc()->screen_to_world(input_context->get_mouse_pos ());
00104 World& world = *Controller::instance()->get_world();
00105
00106 if (fabs(pos2.x - click_pos.x) < 15
00107 || fabs(pos2.y - click_pos.y) < 15)
00108 {
00109 std::cout << "Rect collider to small, not inserting" << std::endl;
00110 }
00111 else
00112 {
00113 world.add_rect_collider (click_pos, pos2);
00114 }
00115 }
00116
00117 creating_rect = false;
00118 move_collider = 0;
00119 }
00120
00121 void
00122 WorldViewColliderTool::on_mouse_move (int x, int y, int of_x, int of_y)
00123 {
00124 Vector2d current_pos = WorldViewComponent::instance()->get_gc()->screen_to_world(Vector2d(x,y));
00125
00126 if (move_collider)
00127 {
00128 move_collider->set_pos(current_pos - click_pos);
00129 }
00130 }
00131
00132 void
00133 WorldViewColliderTool::on_secondary_button_press (int x, int y)
00134 {
00135 to_delete_collider = get_collider(WorldViewComponent::instance()->get_gc()->screen_to_world(Vector2d(x, y)));
00136 }
00137
00138 void
00139 WorldViewColliderTool::on_secondary_button_release (int x, int y)
00140 {
00141 World& world = *Controller::instance()->get_world();
00142
00143 if (to_delete_collider
00144 == get_collider(WorldViewComponent::instance()->get_gc()->screen_to_world(Vector2d(x, y))))
00145 {
00146 Controller::instance()->push_undo();
00147 world.remove_collider(to_delete_collider);
00148 }
00149 to_delete_collider = 0;
00150 }
00151
00152