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

# OgreEventDispatcher.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 "OgreMouseEvent.h"
    00028 #include "OgreEventDispatcher.h"
    00029 #include "OgreGuiContainer.h"
    00030 #include "OgreTargetManager.h"
    00031 #include "OgreEventProcessor.h"
    00032 #include "OgrePositionTarget.h"
    00033 #include "OgreKeyEvent.h"
    00034
    00035 namespace Ogre {
    00036
    00037     EventDispatcher::EventDispatcher(TargetManager* pTargetManager)
    00038         :mTargetManager(pTargetManager) // abstract this out TODO
    00039     {
    00040         mFocus = 0;
    00041         mMouseDragSource = 0;
    00042         mKeyCursorOn = 0;
    00043         mEventMask = 0;
    00044         mTargetLastEntered = 0;
    00045         mMouseX = 0;
    00046         mMouseY = 0;
    00047         mDragging = false;
    00048         mDragDropOn = false;
    00049         mDragDropActive = false;
    00050     }
    00051
    00052     //---------------------------------------------------------------------
    00053     EventDispatcher::~EventDispatcher()
    00054     {
    00055     }
    00056
    00057     //---------------------------------------------------------------------
    00058     bool EventDispatcher::dispatchEvent(InputEvent* e)
    00059     {
    00060         bool ret = false;
    00061         if (e->isEventBetween(MouseEvent::ME_FIRST_EVENT, MouseEvent::ME_LAST_EVENT))   // i am open to suggestions for a better way to do this
    00062                                                                                         // maybe a method e->isEvent(InputEvent::MouseEvent) ??
    00063         {
    00064             MouseEvent* me = static_cast<MouseEvent*> (e);
    00065             ret = processMouseEvent(me);
    00066         }
    00067         else if (e->isEventBetween(KeyEvent::KE_FIRST_EVENT, KeyEvent::KE_LAST_EVENT))
    00068         {
    00069             KeyEvent* ke = static_cast<KeyEvent*> (e);
    00070             ret = processKeyEvent(ke);
    00071
    00072         }
    00073         return ret;
    00074     }
    00075
    00076     //---------------------------------------------------------------------
    00077     void EventDispatcher::setDragDrop(bool dragDropOn)
    00078     {
    00079         mDragDropOn = dragDropOn;
    00080     }
    00081
    00082     //---------------------------------------------------------------------
    00083     bool EventDispatcher::processKeyEvent(KeyEvent* e)
    00084     {
    00085         if (mKeyCursorOn != 0)
    00086         {
    00087             mKeyCursorOn->processEvent(e);
    00088         }
    00089         return e->isConsumed();
    00090     }
    00091
    00092     //---------------------------------------------------------------------
    00093     bool EventDispatcher::processMouseEvent(MouseEvent* e)
    00094     {
    00095         PositionTarget* targetOver;
    00096
    00097         mMouseX = e->getX();
    00098         mMouseY = e->getY();
    00099
    00100         targetOver = mTargetManager->getPositionTargetAt(e->getX(), e->getY());
    00101         trackMouseEnterExit(targetOver, e);
    00102
    00103         switch (e->getID())
    00104         {
    00105         case MouseEvent::ME_MOUSE_PRESSED:
    00106             mDragging = true;
    00107             if (mDragDropOn)
    00108                 mDragDropActive = true;
    00109             mMouseDragSource = targetOver;
    00110             retargetMouseEvent(targetOver, e);
    00111             trackKeyEnterExit(targetOver, e);
    00112             break;
    00113
    00114         case MouseEvent::ME_MOUSE_RELEASED:
    00115             if (targetOver != 0)
    00116             {
    00117                 if (targetOver == mMouseDragSource)
    00118                 {
    00119                     retargetMouseEvent(mMouseDragSource, MouseEvent::ME_MOUSE_CLICKED, e);
    00120                     retargetMouseEvent(mMouseDragSource, e);
    00121                 }
    00122                 else // i.e. targetOver != mMouseDragSource
    00123                 {
    00124                     if (mDragDropActive)
    00125                         retargetMouseEvent(targetOver, MouseEvent::ME_MOUSE_DRAGDROPPED, e);
    00126                     retargetMouseEvent(mMouseDragSource, e);
    00127                     retargetMouseEvent(targetOver, MouseEvent::ME_MOUSE_ENTERED, e);
    00128                 }
    00129             }
    00130             else
    00131                 retargetMouseEvent(mMouseDragSource, e);
    00132
    00133             mDragging = false;
    00134             mDragDropActive = false;
    00135             mMouseDragSource = 0;
    00136             break;
    00137
    00138         case MouseEvent::ME_MOUSE_MOVED:
    00139         case MouseEvent::ME_MOUSE_DRAGGED:
    00140             if (!mDragging || targetOver == mMouseDragSource)
    00141             {
    00142                 retargetMouseEvent(targetOver, e);
    00143             }
    00144             else // i.e. mDragging && targetOver != mMouseDragSource
    00145             {
    00146                 retargetMouseEvent(mMouseDragSource, MouseEvent::ME_MOUSE_DRAGGED, e, true);
    00147                 if (mDragDropActive)
    00148                     retargetMouseEvent(targetOver, MouseEvent::ME_MOUSE_DRAGMOVED, e);
    00149             }
    00150             break;
    00151         }
    00152
    00153         return e->isConsumed();
    00154     }
    00155
    00156     //---------------------------------------------------------------------
    00157     void EventDispatcher::retargetMouseEvent(PositionTarget* target, MouseEvent* e)
    00158     {
    00159         if (target == NULL)
    00160         {
    00161             return;
    00162         }
    00163
    00164         MouseEvent* retargeted = new MouseEvent(target,
    00165                                                e->getID(),
    00166                                                e->getButtonID(),
    00167                                                e->getWhen(),
    00168                                                e->getModifiers(),
    00169                                                e->getX(),
    00170                                                e->getY(),
    00171                                                e->getZ(),
    00172                                                e->getClickCount());
    00173
    00174         target->processEvent(retargeted);
    00175         delete retargeted;
    00176
    00177         e->consume();
    00178     }
    00179
    00180     //---------------------------------------------------------------------
    00181     void EventDispatcher::retargetMouseEvent(PositionTarget* target, int id, MouseEvent* e, bool consume)
    00182     {
    00183         if (target == NULL)
    00184         {
    00185             return;
    00186         }
    00187
    00188         MouseEvent* retargeted = new MouseEvent(target,
    00189                                                id,
    00190                                                e->getButtonID(),
    00191                                                e->getWhen(),
    00192                                                e->getModifiers(),
    00193                                                e->getX(),
    00194                                                e->getY(),
    00195                                                e->getZ(),
    00196                                                e->getClickCount());
    00197
    00198         target->processEvent(retargeted);
    00199         delete retargeted;
    00200
    00201         if (consume)
    00202             e->consume();
    00203     }
    00204
    00205     //---------------------------------------------------------------------
    00206     void EventDispatcher::retargetKeyEvent(PositionTarget* target, int id, MouseEvent* e)
    00207     {
    00208         if (target == NULL)
    00209         {
    00210             return;
    00211         }
    00212
    00213         KeyEvent* retargeted = new KeyEvent(target,
    00214                                                id,
    00215                                                0,
    00216                                                e->getWhen(),
    00217                                                e->getModifiers());
    00218
    00219         target->processEvent(retargeted);
    00220         delete retargeted;
    00221     }
    00222
    00223     //---------------------------------------------------------------------
    00224     void EventDispatcher::trackMouseEnterExit(PositionTarget* targetOver, MouseEvent* e)
    00225     {
    00226         if (mTargetLastEntered == targetOver)
    00227         {
    00228             return;
    00229         }
    00230
    00231         if (mTargetLastEntered != 0)
    00232         {
    00233             if (!mDragging || mTargetLastEntered == mMouseDragSource)
    00234             {
    00235                 retargetMouseEvent(mTargetLastEntered, MouseEvent::ME_MOUSE_EXITED, e);
    00236             }
    00237             else if (mDragDropActive) // i.e. mDragging && mTargetLastEntered != mMouseDragSource && mDragDropActive
    00238             {
    00239                 retargetMouseEvent(mTargetLastEntered, MouseEvent::ME_MOUSE_DRAGEXITED, e);
    00240             }
    00241         }
    00242
    00243         if (targetOver != 0)
    00244         {
    00245             if (!mDragging || targetOver == mMouseDragSource)
    00246             {
    00247                 retargetMouseEvent(targetOver, MouseEvent::ME_MOUSE_ENTERED, e);
    00248             }
    00249             else if (mDragDropActive) // i.e. mDragging && targetOver != mMouseDragSource && mDragDropActive
    00250             {
    00251                 retargetMouseEvent(targetOver, MouseEvent::ME_MOUSE_DRAGENTERED, e);
    00252             }
    00253         }
    00254
    00255         mTargetLastEntered = targetOver;
    00256     }
    00257
    00258     //---------------------------------------------------------------------
    00259     void EventDispatcher::trackKeyEnterExit(PositionTarget* targetOver, MouseEvent* e)
    00260     {
    00261         if (targetOver != mKeyCursorOn)
    00262         {
    00263             if (mKeyCursorOn != 0)
    00264             {
    00265                 retargetKeyEvent(mKeyCursorOn, KeyEvent::KE_KEY_FOCUSOUT, e);
    00266             }
    00267
    00268             if (targetOver != 0 && targetOver->isKeyEnabled())
    00269             {
    00270                 mKeyCursorOn = targetOver;
    00271                 retargetKeyEvent(targetOver, KeyEvent::KE_KEY_FOCUSIN, e);
    00272             }
    00273             else
    00274             {
    00275                 mKeyCursorOn = NULL;
    00276             }
    00277         }
    00278     }
    00279
    00280 }
    00281
    00282
    00283

Copyright © 2002-2003 by The OGRE Team
Last modified Sun Nov 28 19:48:23 2004