Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

worldview_collider_tool.cxx

Go to the documentation of this file.
00001 //  $Id: worldview_collider_tool.cxx,v 1.4 2003/01/11 16:11:36 grumbel Exp $
00002 //
00003 //  Construo - A wire-frame construction gamee
00004 //  Copyright (C) 2002 Ingo Ruhnke <grumbel@gmx.de>
00005 //
00006 //  This program is free software; you can redistribute it and/or
00007 //  modify it under the terms of the GNU General Public License
00008 //  as published by the Free Software Foundation; either version 2
00009 //  of the License, or (at your option) any later version.
00010 //
00011 //  This program is distributed in the hope that it will be useful,
00012 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //  GNU General Public License for more details.
00015 //
00016 //  You should have received a copy of the GNU General Public License
00017 //  along with this program; if not, write to the Free Software
00018 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
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       // click_pos Offset, not position
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 /* EOF */

Generated on Thu Jul 24 10:24:31 2003 for Construo by doxygen1.3-rc3