Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

OgreEventProcessor.cpp

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004     (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright © 2000-2002 The OGRE Team
00008 Also see acknowledgements in Readme.html
00009 
00010 This program is free software; you can redistribute it and/or modify it under
00011 the terms of the GNU Lesser General Public License as published by the Free Software
00012 Foundation; either version 2 of the License, or (at your option) any later
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT
00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with
00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 -----------------------------------------------------------------------------
00024 */
00025 #include "OgreStableHeaders.h"
00026 
00027 #include "OgreEventProcessor.h"
00028 #include "OgreOverlayManager.h"
00029 #include "OgreEventQueue.h"
00030 #include "OgreRoot.h"
00031 #include "OgreMouseEvent.h"
00032 #include "OgreKeyEvent.h"
00033 #include "OgreActionEvent.h"
00034 #include "OgreInput.h"
00035 #include "OgreCursor.h"
00036 
00037 
00038 namespace Ogre {
00039     //-----------------------------------------------------------------------
00040     template<> EventProcessor* Singleton<EventProcessor>::ms_Singleton = 0;
00041     //-----------------------------------------------------------------------
00042     //-----------------------------------------------------------------------
00043     EventProcessor& EventProcessor::getSingleton(void)
00044     {
00045         return Singleton<EventProcessor>::getSingleton();
00046     }
00047 
00048 //-----------------------------------------------------------------------------
00049     EventProcessor::EventProcessor() :
00050         MouseTarget(),
00051         MouseMotionTarget()
00052     {
00053         mEventQueue = 0;
00054         mInputDevice = 0;
00055         mRegisteredAsFrameListener = false;
00056     }
00057 
00058 //-----------------------------------------------------------------------------
00059     EventProcessor::~EventProcessor()
00060     {
00061 
00062         cleanup();
00063     }
00064 
00065 //-----------------------------------------------------------------------------
00066     void EventProcessor::cleanup()
00067     {
00068         if (mEventQueue)
00069             delete mEventQueue;
00070 
00071         for(DispatcherList::iterator i = mDispatcherList.begin(); i != mDispatcherList.end(); ++i )                 
00072         {
00073             delete *i;
00074         }
00075         mDispatcherList.clear();
00076 
00077         PlatformManager::getSingleton().destroyInputReader(mInputDevice);
00078 
00079     }
00080 
00081 //-----------------------------------------------------------------------------
00082     void EventProcessor::stopProcessingEvents()
00083     {
00084 
00085         mEventQueue->activateEventQueue(false);
00086 
00087         if(mRegisteredAsFrameListener)
00088         {
00089             Root::getSingleton().removeFrameListener(this);
00090             mRegisteredAsFrameListener = false;
00091         }
00092 
00093     }
00094 
00095 //-----------------------------------------------------------------------------
00096     void EventProcessor::initialise(RenderWindow* ren)
00097     {
00098         cleanup();
00099 
00100 
00101         mEventQueue = new EventQueue();
00102 
00103         mInputDevice = PlatformManager::getSingleton().createInputReader();
00104         mInputDevice->useBufferedInput(mEventQueue);
00105         mInputDevice->initialise(ren,true, true, false);    
00106 
00107     }
00108 //-----------------------------------------------------------------------------
00109 
00110     void EventProcessor::addTargetManager(TargetManager* targetManager)
00111     {
00112         EventDispatcher* pDispatcher = new EventDispatcher(targetManager);  
00113         mDispatcherList.push_back(pDispatcher);
00114     }
00115 
00116     //-----------------------------------------------------------------------------
00117     void EventProcessor::addEventTarget(EventTarget* eventTarget)
00118     {
00119         mEventTargetList.push_back(eventTarget);
00120     }
00121 
00122 
00123 //-----------------------------------------------------------------------------
00124     void EventProcessor::startProcessingEvents(bool registerListener)
00125     {
00126         if(registerListener)
00127         {
00128             Root::getSingleton().addFrameListener(this);
00129             mRegisteredAsFrameListener = true;
00130         }
00131 
00132         mEventQueue->activateEventQueue(true);
00133     }
00134 
00135 
00136 //-----------------------------------------------------------------------------
00137     bool EventProcessor::frameStarted(const FrameEvent& evt)
00138     {
00139         mInputDevice->capture();
00140 
00141         while (mEventQueue->getSize() > 0)
00142         {
00143             InputEvent* e = mEventQueue->pop();
00144             processEvent(e);
00145             delete e;
00146         }
00147 
00148         return true;
00149     }
00150 
00151 //-----------------------------------------------------------------------------
00152     void EventProcessor::processEvent(InputEvent* e)
00153     {
00154             // try the event dispatcher list
00155         for (DispatcherList::iterator i = mDispatcherList.begin(); i != mDispatcherList.end(); ++i )                 
00156         {
00157             (*i)->dispatchEvent(e);
00158         }
00159 
00160             // try the event target list
00161         if (!e->isConsumed())
00162         {
00163             EventTargetList::iterator i, iEnd;
00164 
00165             iEnd = mEventTargetList.end();
00166             for (i = mEventTargetList.begin(); i != iEnd; ++i )                 
00167             {
00168                 (*i)->processEvent(e);
00169             }
00170         }
00171 
00172         if (!e->isConsumed())
00173         {
00174             switch(e->getID()) 
00175             {
00176             case MouseEvent::ME_MOUSE_PRESSED:
00177             case MouseEvent::ME_MOUSE_RELEASED:
00178             case MouseEvent::ME_MOUSE_CLICKED:
00179             case MouseEvent::ME_MOUSE_ENTERED:
00180             case MouseEvent::ME_MOUSE_EXITED:
00181             case MouseEvent::ME_MOUSE_DRAGENTERED:
00182             case MouseEvent::ME_MOUSE_DRAGEXITED:
00183             case MouseEvent::ME_MOUSE_DRAGDROPPED:
00184                 processMouseEvent(static_cast<MouseEvent*>(e));
00185                 break;
00186             case MouseEvent::ME_MOUSE_MOVED:
00187             case MouseEvent::ME_MOUSE_DRAGGED:
00188             case MouseEvent::ME_MOUSE_DRAGMOVED:
00189                 processMouseMotionEvent(static_cast<MouseEvent*>(e));
00190                 break;
00191             case KeyEvent::KE_KEY_PRESSED:
00192             case KeyEvent::KE_KEY_RELEASED:
00193             case KeyEvent::KE_KEY_CLICKED:
00194                 processKeyEvent(static_cast<KeyEvent*>(e));
00195                 break;
00196             }
00197         }
00198     }
00199 
00200 //-----------------------------------------------------------------------------
00201     void EventProcessor::addCursorMoveListener(MouseMotionListener* c)
00202     {
00203         mInputDevice->addCursorMoveListener(c);
00204     }
00205 //-----------------------------------------------------------------------------
00206     void EventProcessor::removeCursorMoveListener(MouseMotionListener* c)
00207     {
00208         mInputDevice->removeCursorMoveListener(c);
00209     }
00210 
00211 //-----------------------------------------------------------------------------
00212     Real EventProcessor::getLeft() const
00213     {
00214         return 0;
00215     }
00216 
00217 //-----------------------------------------------------------------------------
00218     Real EventProcessor::getTop() const
00219     {
00220         return 0;
00221     }
00222     
00223 //-----------------------------------------------------------------------------
00224     PositionTarget* EventProcessor::getPositionTargetParent() 
00225     {
00226         return NULL;
00227     }
00228 //-----------------------------------------------------------------------------
00229 
00230 }
00231 

Copyright © 2002-2003 by The OGRE Team
Last modified Wed Jan 21 00:10:10 2004