00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 #include "colors.hxx"
00021 #include "particle_factory.hxx"
00022 #include "input_context.hxx"
00023 #include "graphic_context.hxx"
00024 #include "world.hxx"
00025 #include "controller.hxx"
00026 #include "worldview_component.hxx"
00027 #include "worldview_insert_tool.hxx"
00028 #include "world_gui_manager.hxx"
00029 
00030 WorldViewInsertTool::WorldViewInsertTool ()
00031 {
00032   current_particle = 0;
00033   particle_mass = 0.1f;
00034 }
00035 
00036 WorldViewInsertTool::~WorldViewInsertTool () 
00037 {
00038 }
00039 
00040 void
00041 WorldViewInsertTool::draw_background (ZoomGraphicContext* gc) 
00042 {
00043   float x = WorldViewComponent::instance()->get_gc()->screen_to_world_x (input_context->get_mouse_x ());
00044   float y = WorldViewComponent::instance()->get_gc()->screen_to_world_y (input_context->get_mouse_y ());
00045 
00046   World& world = *Controller::instance()->get_world();
00047 
00048   Particle* selected_particle = world.get_particle (x, y);
00049   if (selected_particle)
00050     {
00051       selected_particle->draw_highlight (gc);
00052     }
00053 }
00054 
00055 void
00056 WorldViewInsertTool::draw_foreground (ZoomGraphicContext* gc) 
00057 {
00058   World& world = *Controller::instance()->get_world();
00059 
00060   Vector2d click_pos = WorldViewComponent::instance()->get_gc()->screen_to_world (input_context->get_mouse_pos ());
00061   
00062   Spring* selected_spring = world.get_spring (click_pos.x, click_pos.y);
00063   if (selected_spring)
00064     {
00065       selected_spring->draw_highlight (gc);
00066     }
00067       
00068   if (current_particle)
00069     {
00070       gc->GraphicContext::draw_line (current_particle->pos, click_pos,
00071                                      Colors::new_spring, 2);
00072     }
00073 
00074   float x = WorldViewComponent::instance()->get_gc()->screen_to_world_x (input_context->get_mouse_x ());
00075   float y = WorldViewComponent::instance()->get_gc()->screen_to_world_y (input_context->get_mouse_y ());
00076 
00077   Particle* selected_particle = world.get_particle (x, y);
00078   if (selected_particle)
00079     {
00080       selected_particle->draw_infos (gc);
00081     }
00082 }
00083 
00084 void
00085 WorldViewInsertTool::on_primary_button_press (int screen_x, int screen_y) 
00086 {
00087   World& world = *Controller::instance()->get_world ();
00088   float x = WorldViewComponent::instance()->get_gc()->screen_to_world_x (screen_x);
00089   float y = WorldViewComponent::instance()->get_gc()->screen_to_world_y (screen_y);
00090 
00091   if (current_particle)
00092     {
00093       Particle* new_current_particle = world.get_particle (x, y);
00094       if (new_current_particle != current_particle)
00095         {
00096           if (new_current_particle) 
00097             {
00098               world.add_spring (current_particle, new_current_particle);
00099             }
00100           else 
00101             {
00102               new_current_particle = world.get_particle_mgr()->add_particle (Vector2d(x, y), Vector2d(), particle_mass);
00103               world.add_spring (current_particle, new_current_particle);
00104             }
00105           current_particle = 0;
00106         }
00107       WorldGUIManager::instance()->ungrab_mouse (WorldViewComponent::instance());
00108     }
00109   else
00110     {
00111       current_particle = world.get_particle (x, y);
00112       if (!current_particle)
00113         {
00114           Particle* p = world.get_particle_mgr()->add_particle (Vector2d(x, y), Vector2d(), 
00115                                                                 particle_mass);
00116           current_particle = p;
00117           WorldGUIManager::instance()->grab_mouse (WorldViewComponent::instance());
00118         }
00119     }
00120 }
00121 
00122 void
00123 WorldViewInsertTool::on_primary_button_release (int x, int y) 
00124 {
00125 }
00126 
00127 void
00128 WorldViewInsertTool::on_secondary_button_press (int screen_x, int screen_y)
00129 {
00130   on_delete_press (screen_x, screen_y);
00131 }
00132 
00133 void
00134 WorldViewInsertTool::on_secondary_button_release (int screen_x, int screen_y) 
00135 {
00136 }
00137 
00138 void
00139 WorldViewInsertTool::on_delete_press (int screen_x, int screen_y) 
00140 {
00141   World& world = *Controller::instance()->get_world ();
00142 
00143   float x = WorldViewComponent::instance()->get_gc()->screen_to_world_x (screen_x);
00144   float y = WorldViewComponent::instance()->get_gc()->screen_to_world_y (screen_y);
00145 
00146   if (current_particle) 
00147     { 
00148       current_particle = 0;
00149       WorldGUIManager::instance()->ungrab_mouse (WorldViewComponent::instance());
00150     }
00151   else
00152     {
00153       Spring*   spring   = world.get_spring (x, y);
00154       Particle* particle = world.get_particle (x, y);
00155 
00156       if (particle)
00157         {
00158           Controller::instance()->push_undo();
00159           world.remove_particle (particle);
00160         }
00161       else if (spring)
00162         {
00163           Controller::instance()->push_undo();
00164           world.remove_spring(spring);
00165         }
00166     }
00167 }
00168 
00169 void
00170 WorldViewInsertTool::on_fix_press (int screen_x, int screen_y) 
00171 {
00172   float x = WorldViewComponent::instance()->get_gc()->screen_to_world_x (screen_x);
00173   float y = WorldViewComponent::instance()->get_gc()->screen_to_world_y (screen_y);
00174 
00175   Particle* particle = Controller::instance()->get_world ()->get_particle (x, y);
00176   if (particle)
00177     {
00178       particle->set_fixed (!particle->get_fixed ());
00179     }
00180 }
00181 
00182