eventmanager.h

00001 /***************************************************************************
00002  *   Copyright (C) 2005-2008 by the FIFE team                              *
00003  *   http://www.fifengine.de                                               *
00004  *   This file is part of FIFE.                                            *
00005  *                                                                         *
00006  *   FIFE is free software; you can redistribute it and/or                 *
00007  *   modify it under the terms of the GNU Lesser General Public            *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2.1 of the License, or (at your option) any later version.    *
00010  *                                                                         *
00011  *   This library 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 GNU     *
00014  *   Lesser General Public License for more details.                       *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Lesser General Public      *
00017  *   License along with this library; if not, write to the                 *
00018  *   Free Software Foundation, Inc.,                                       *
00019  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
00020  ***************************************************************************/
00021 
00022 #ifndef FIFE_EVENTCHANNEL_EVENTMANAGER_H
00023 #define FIFE_EVENTCHANNEL_EVENTMANAGER_H
00024 
00025 // Standard C++ library includes
00026 //
00027 #include <deque>
00028 #include <map>
00029 #include <list>
00030 
00031 // 3rd party library includes
00032 //
00033 
00034 // FIFE includes
00035 // These includes are split up in two parts, separated by one empty line
00036 // First block: files included from the FIFE root src directory
00037 // Second block: files included from the same folder
00038 //
00039 #include "util/base/fife_stdint.h"
00040 #include "eventchannel/command/ec_command.h"
00041 #include "eventchannel/command/ec_icommandcontroller.h"
00042 #include "eventchannel/command/ec_icommandlistener.h"
00043 
00044 #include "eventchannel/key/ec_ikeycontroller.h"
00045 #include "eventchannel/key/ec_ikeylistener.h"
00046 #include "eventchannel/key/ec_keyevent.h"
00047 #include "eventchannel/key/ec_key.h"
00048 
00049 #include "eventchannel/mouse/ec_imousecontroller.h"
00050 #include "eventchannel/mouse/ec_imouselistener.h"
00051 #include "eventchannel/mouse/ec_mouseevent.h"
00052 
00053 #include "eventchannel/sdl/ec_isdleventcontroller.h"
00054 #include "eventchannel/sdl/ec_isdleventlistener.h"
00055 
00056 namespace FIFE {
00057 
00058     class ICommandListener;
00059     class InputEvent;
00060     class MouseEvent;
00061     class KeyEvent;
00062     class IKeyFilter;
00063 
00066     class EventManager:
00067         public ICommandController,
00068         public IKeyController,
00069         public IMouseController,
00070         public ISdlEventController,
00071         public IEventSource {
00072     public:
00075         EventManager();
00076 
00079         virtual ~EventManager();
00080 
00081         void addCommandListener(ICommandListener* listener);
00082         void addCommandListenerFront(ICommandListener* listener);
00083         void removeCommandListener(ICommandListener* listener);
00084 
00085         void dispatchCommand(Command& command);
00086 
00087         void addKeyListener(IKeyListener* listener);
00088         void addKeyListenerFront(IKeyListener* listener);
00089         void removeKeyListener(IKeyListener* listener);
00090 
00091         void addMouseListener(IMouseListener* listener);
00092         void addMouseListenerFront(IMouseListener* listener);
00093         void removeMouseListener(IMouseListener* listener);
00094 
00095         void addSdlEventListener(ISdlEventListener* listener);
00096         void addSdlEventListenerFront(ISdlEventListener* listener);
00097         void removeSdlEventListener(ISdlEventListener* listener);
00098 
00099         EventSourceType getEventSourceType();
00100 
00105         void processEvents();
00106 
00107         void setKeyFilter(IKeyFilter* keyFilter);
00108 
00112         void setMouseSensitivity(float sensitivity);
00113 
00116         float getMouseSensitivity() const;
00117 
00122         void setMouseAcceleration(bool acceleration);
00123 
00126         bool getMouseAcceleration() const;
00127 
00128     private:
00129         // Helpers for processEvents
00130         void processActiveEvent(SDL_Event event);
00131         void processKeyEvent(SDL_Event event);
00132         void processMouseEvent(SDL_Event event);
00133         bool combineEvents(SDL_Event& event1, const SDL_Event& event2);
00134 
00135         // Events dispatchers - only dispatchSdlevent may reject the event.
00136         bool dispatchSdlEvent(SDL_Event& evt);
00137         void dispatchKeyEvent(KeyEvent& evt);
00138         void dispatchMouseEvent(MouseEvent& evt);
00139 
00140         // Translate events
00141         void fillModifiers(InputEvent& evt);
00142         void fillKeyEvent(const SDL_Event& sdlevt, KeyEvent& keyevt);
00143         void fillMouseEvent(const SDL_Event& sdlevt, MouseEvent& mouseevt);
00144 
00145         std::deque<ICommandListener*> m_commandlisteners;
00146         std::deque<ICommandListener*> m_pending_commandlisteners;
00147         std::deque<ICommandListener*> m_pending_commandlisteners_front;
00148         std::deque<ICommandListener*> m_pending_cldeletions;
00149 
00150         std::deque<IKeyListener*> m_keylisteners;
00151         std::deque<IKeyListener*> m_pending_keylisteners;
00152         std::deque<IKeyListener*> m_pending_keylisteners_front;
00153         std::deque<IKeyListener*> m_pending_kldeletions;
00154 
00155         std::deque<IMouseListener*> m_mouselisteners;
00156         std::deque<IMouseListener*> m_pending_mouselisteners;
00157         std::deque<IMouseListener*> m_pending_mouselisteners_front;
00158         std::deque<IMouseListener*> m_pending_mldeletions;
00159 
00160         std::deque<ISdlEventListener*> m_sdleventlisteners;
00161         std::deque<ISdlEventListener*> m_pending_sdleventlisteners;
00162         std::deque<ISdlEventListener*> m_pending_sdleventlisteners_front;
00163         std::deque<ISdlEventListener*> m_pending_sdldeletions;
00164 
00165         std::map<int32_t, bool> m_keystatemap;
00166         IKeyFilter* m_keyfilter;
00167         int32_t m_mousestate;
00168         MouseEvent::MouseButtonType m_mostrecentbtn;
00169 
00170         // m_mousesensitivity is the mouse speed factor - 1, so a value of 0 won't
00171         // influence mouse speed, a value of 1 would double the speed and
00172         // -.5 would make it half the speed
00173         float m_mousesensitivity;
00174         bool m_acceleration;
00175         bool m_warp;
00176         bool m_enter;
00177         uint16_t m_oldx;
00178         uint16_t m_oldy;
00179         uint32_t m_lastticks;
00180         float m_oldvelocity;
00181 
00182     };
00183 } //FIFE
00184 
00185 #endif