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

gui_manager.cxx

Go to the documentation of this file.
00001 //  $Id: gui_manager.cxx,v 1.31 2003/01/11 19:07:48 grumbel Exp $
00002 //
00003 //  Construo - A wire-frame construction game
00004 //  Copyright (C) 2002 Ingo Ruhnke <[email protected]>
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 <config.h>
00021 #include "string_utils.hxx"
00022 #include "construo.hxx"
00023 #include "graphic_context.hxx"
00024 #include "input_context.hxx"
00025 #include "system_context.hxx"
00026 #include "controller.hxx"
00027 #include "keep_alive_mgr.hxx"
00028 #include "events.hxx"
00029 #include "gui_component.hxx"
00030 #include "gui_manager.hxx"
00031 #include "worldview_component.hxx"
00032 #include "worldview_insert_tool.hxx"
00033 #include "globals.hxx"
00034 #include "screen_manager.hxx"
00035 
00036 using namespace StringUtils;
00037 
00038 GUIManager::GUIManager ()
00039 {
00040   frame_count = 0;
00041   start_time  = system_context->get_time ();
00042 
00043   last_component     = 0;
00044   grabbing_component = 0;
00045   current_component  = 0;
00046 
00047 }
00048 
00049 GUIManager::~GUIManager ()
00050 {
00051 }
00052   
00053 void
00054 GUIManager::run_once ()
00055 { 
00056   frame_count += 1;
00057 
00058   if (start_time + 3000 < system_context->get_time ())
00059     {
00060       float passed_time = (system_context->get_time () - start_time) / 1000.0f;
00061       
00062       //std::cout << "FPS: " << frame_count / passed_time << std::endl;
00063 
00064       current_fps = frame_count / passed_time;
00065       
00066       frame_count = 0;
00067       start_time  = system_context->get_time ();
00068     }
00069 
00070   process_events ();
00071 
00072   update();
00073   
00074   graphic_context->clear ();
00075   draw ();
00076   draw_overlay ();
00077   graphic_context->flip ();
00078 }
00079 
00080 void
00081 GUIManager::draw ()
00082 {
00083   for (ComponentLst::iterator i = components.begin (); i != components.end (); ++i)
00084     {
00085       (*i)->draw (graphic_context);
00086     }
00087 }
00088 
00089 GUIComponent*
00090 GUIManager::find_component_at (int x, int y)
00091 {
00092   GUIComponent* component = 0;
00093   for (ComponentLst::iterator i = components.begin (); i != components.end (); ++i)
00094     {
00095       if ((*i)->is_at (x, y))
00096         component = *i;
00097     }
00098   return component;
00099 }
00100 
00101 void
00102 GUIManager::process_button_events (ButtonEvent& button)
00103 {
00104   int x = input_context->get_mouse_x();
00105   int y = input_context->get_mouse_y();
00106 
00107   if (button.pressed)
00108     {
00109       switch (button.id)
00110         {
00111         case BUTTON_START:
00112           Controller::instance()->start_simulation ();
00113           break;
00114 
00115         case BUTTON_PRIMARY:
00116           current_component->on_primary_button_press(x, y);
00117           break;
00118 
00119         case BUTTON_SECONDARY:
00120           current_component->on_secondary_button_press(x, y);
00121           break;
00122 
00123         case BUTTON_TERTIARY:
00124           current_component->on_tertiary_button_press(x, y);
00125           break;
00126 
00127         case BUTTON_FIX:
00128           current_component->on_fix_press (x, y);
00129           break;
00130         case BUTTON_DELETE:
00131           current_component->on_delete_press (x, y);
00132           break;
00133         case BUTTON_DUPLICATE:
00134           current_component->on_duplicate_press (x, y);
00135           break;
00136 
00137         case BUTTON_SCROLL_LEFT:
00138           current_component->scroll_left ();
00139           break;
00140 
00141         case BUTTON_SCROLL_RIGHT:
00142           current_component->scroll_right ();
00143           break;
00144 
00145         case BUTTON_SCROLL_UP:
00146           current_component->scroll_up ();
00147           break;
00148 
00149         case BUTTON_SCROLL_DOWN:
00150           current_component->scroll_down ();
00151           break;
00152 
00153         case BUTTON_CLEAR:
00154           Controller::instance()->clear_world ();
00155           break;
00156                   
00157         case BUTTON_UNDO:
00158           Controller::instance()->undo ();
00159           break;
00160                   
00161         case BUTTON_REDO:
00162           Controller::instance()->redo ();
00163           break;
00164 
00165         case BUTTON_ACTIONCAM:
00166           Controller::instance()->set_action_cam (!Controller::instance()->get_action_cam ());
00167           break;
00168 
00169         case BUTTON_HIDEDOTS:
00170           Controller::instance()->set_hide_dots (!Controller::instance()->get_hide_dots ());
00171           break;
00172 
00173         case BUTTON_ESCAPE:
00174           ScreenManager::instance()->quit();
00175           break;
00176 
00177         case BUTTON_MODE_CHANGE:
00178           if (WorldViewComponent::instance()->get_mode () == WorldViewComponent::INSERT_MODE)
00179             {
00180               WorldViewComponent::instance()->set_mode(WorldViewComponent::SELECT_MODE);
00181             }
00182           else
00183             {
00184               WorldViewComponent::instance()->set_mode(WorldViewComponent::INSERT_MODE);
00185             }
00186           break;
00187 
00188         case BUTTON_TOGGLESLOWMO:
00189           Controller::instance()->set_slow_down (!Controller::instance()->slow_down_active ());
00190           break;
00191 
00192         case BUTTON_RUN:
00193           Controller::instance()->start_simulation ();
00194           break;
00195 
00196         case BUTTON_QUICKSAVE0:
00197         case BUTTON_QUICKSAVE1:
00198         case BUTTON_QUICKSAVE2:
00199         case BUTTON_QUICKSAVE3:
00200         case BUTTON_QUICKSAVE4:
00201         case BUTTON_QUICKSAVE5:
00202         case BUTTON_QUICKSAVE6:
00203         case BUTTON_QUICKSAVE7:
00204         case BUTTON_QUICKSAVE8:
00205         case BUTTON_QUICKSAVE9:
00206           Controller::instance()->save_to_slot (button.id - BUTTON_QUICKSAVE0);
00207           break;
00208 
00209         case BUTTON_QUICKLOAD0:
00210         case BUTTON_QUICKLOAD1:
00211         case BUTTON_QUICKLOAD2:
00212         case BUTTON_QUICKLOAD3:
00213         case BUTTON_QUICKLOAD4:
00214         case BUTTON_QUICKLOAD5:
00215         case BUTTON_QUICKLOAD6:
00216         case BUTTON_QUICKLOAD7:
00217         case BUTTON_QUICKLOAD8:
00218         case BUTTON_QUICKLOAD9:
00219           Controller::instance()->load_from_slot (button.id - BUTTON_QUICKLOAD0);
00220           break;
00221 
00222         case BUTTON_ZOOM_OUT:
00223           current_component->wheel_down (x, y);
00224           break;
00225 
00226         case BUTTON_ZOOM_IN:
00227           current_component->wheel_up (x, y);
00228           break;
00229 
00230         default:
00231           current_component->on_button_press (button.id, x, y);
00232           break;
00233         }
00234     }
00235   else // button released
00236     {
00237       switch (button.id)
00238         {
00239         case BUTTON_PRIMARY:
00240           current_component->on_primary_button_release(x, y);
00241           break;
00242 
00243         case BUTTON_SECONDARY:
00244           current_component->on_secondary_button_release(x, y);
00245           break;
00246 
00247         case BUTTON_TERTIARY:
00248           current_component->on_tertiary_button_release(x, y);
00249           break;
00250 
00251         default:
00252           std::cout << "GUIManager:process_button_events: Got unhandled BUTTON_EVENT release: "
00253                     << button.id << std::endl;
00254           break;
00255         }
00256     }
00257 }
00258 
00259 void
00260 GUIManager::process_events ()
00261 {
00262   int x = input_context->get_mouse_x();
00263   int y = input_context->get_mouse_y();
00264       
00265   if (grabbing_component && (last_x != x || last_y != y))
00266     {
00267       grabbing_component->on_mouse_move (x, y, x - last_x, y - last_y);
00268     }
00269   if (current_component != grabbing_component)
00270     {
00271       current_component->on_mouse_move (x, y, x - last_x, y - last_y);
00272     }
00273 
00274   if (!grabbing_component)
00275     {
00276       current_component = find_component_at (x, y);
00277       
00278       if (last_component != current_component)
00279         {
00280           if (current_component) current_component->on_mouse_enter ();
00281           if (last_component)
00282             last_component->on_mouse_leave ();
00283 
00284           last_component = current_component;
00285         }
00286     }
00287   else
00288     {
00289       GUIComponent* comp = find_component_at (x, y);
00290 
00291       if (comp != grabbing_component)
00292         {
00293           grabbing_component->on_mouse_leave();
00294           last_component = comp;
00295         }
00296       else if (last_component != grabbing_component)
00297         {
00298           grabbing_component->on_mouse_enter();
00299         }
00300     }
00301 
00302   Event event;
00303   while (input_context->get_event (&event))
00304     {
00305       if (current_component)
00306         {
00307           switch (event.type)
00308             {
00309             case BUTTON_EVENT:
00310               //std::cout << "BUTTON_EVENT: " << event.button.id  << " state: " << event.button.pressed << std::endl;
00311               process_button_events (event.button);
00312               break;
00313             default: 
00314               std::cout << "GUIManager: Unhandled event type" << std::endl;
00315               break;
00316             }
00317         }
00318     }
00319 
00320   last_x = x;
00321   last_y = y;
00322 }
00323 
00324 void
00325 GUIManager::grab_mouse (GUIComponent* comp)
00326 {
00327   grabbing_component = comp;
00328   current_component  = comp;
00329 }
00330 
00331 void
00332 GUIManager::ungrab_mouse (GUIComponent* comp)
00333 {
00334   grabbing_component = 0;
00335 }
00336 
00337 void
00338 GUIManager::add (GUIComponent* c)
00339 {
00340   assert(c);
00341   components.push_back(c);
00342 }
00343 
00344 /* EOF */

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