FIFE  6e1afdbeda11afe9ac53e6023a4be96ef88f1dc6
eventmanager.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2017 by the FIFE team *
3  * http://www.fifengine.net *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 #ifndef FIFE_EVENTCHANNEL_EVENTMANAGER_H
23 #define FIFE_EVENTCHANNEL_EVENTMANAGER_H
24 
25 // Standard C++ library includes
26 //
27 #include <deque>
28 #include <map>
29 #include <list>
30 
31 // 3rd party library includes
32 //
33 
34 // FIFE includes
35 // These includes are split up in two parts, separated by one empty line
36 // First block: files included from the FIFE root src directory
37 // Second block: files included from the same folder
38 //
39 #include "util/base/fife_stdint.h"
43 
47 
51 #include "eventchannel/key/key.h"
52 
56 #include "eventchannel/text/text.h"
57 
61 
64 
65 namespace FIFE {
66 
67  class ICommandListener;
68  class InputEvent;
69  class IJoystickListener;
70  class Joystick;
71  class JoystickManager;
72  class MouseEvent;
73  class KeyEvent;
74  class IKeyFilter;
75  class DropEvent;
76  class IMouseFilter;
77 
80  class EventManager:
81  public ICommandController,
82  public IKeyController,
83  public ITextController,
84  public IMouseController,
85  public ISdlEventController,
86  public IDropController,
87  public IEventSource {
88  public:
91  EventManager();
92 
95  virtual ~EventManager();
96 
97  void addCommandListener(ICommandListener* listener);
100 
101  void dispatchCommand(Command& command);
102 
103  void addKeyListener(IKeyListener* listener);
104  void addKeyListenerFront(IKeyListener* listener);
105  void removeKeyListener(IKeyListener* listener);
106 
107  void addTextListener(ITextListener* listener);
108  void addTextListenerFront(ITextListener* listener);
109  void removeTextListener(ITextListener* listener);
110 
111  void addMouseListener(IMouseListener* listener);
112  void addMouseListenerFront(IMouseListener* listener);
113  void removeMouseListener(IMouseListener* listener);
114 
115  void addSdlEventListener(ISdlEventListener* listener);
118 
119  void addDropListener(IDropListener* listener);
120  void addDropListenerFront(IDropListener* listener);
121  void removeDropListener(IDropListener* listener);
122 
123  void addJoystickListener(IJoystickListener* listener);
126 
128 
133  void processEvents();
134 
135  void setKeyFilter(IKeyFilter* keyFilter);
136 
137  void setMouseFilter(IMouseFilter* mouseFilter);
138 
142  void setMouseSensitivity(float sensitivity);
143 
146  float getMouseSensitivity() const;
147 
152  void setMouseAccelerationEnabled(bool acceleration);
153 
158  bool isMouseAccelerationEnabled() const;
159 
164  bool isClipboardText() const;
165 
170  std::string getClipboardText() const;
171 
176  void setClipboardText(const std::string& text);
177 
180  void setJoystickSupport(bool support);
181 
184  Joystick* getJoystick(int32_t instanceId);
185 
188  uint8_t getJoystickCount() const;
189 
192  void loadGamepadMapping(const std::string& file);
193 
196  void saveGamepadMapping(const std::string guid, const std::string& file);
197 
200  void saveGamepadMappings(const std::string& file);
201 
204  std::string getGamepadStringMapping(const std::string& guid);
205 
208  void setGamepadStringMapping(const std::string& mapping);
209 
210  private:
211  // Helpers for processEvents
212  void processWindowEvent(SDL_Event event);
213  void processKeyEvent(SDL_Event event);
214  void processTextEvent(SDL_Event event);
215  void processMouseEvent(SDL_Event event);
216  void processDropEvent(SDL_Event event);
217  bool combineEvents(SDL_Event& event1, const SDL_Event& event2);
218 
219  // Events dispatchers - only dispatchSdlevent may reject the event.
220  bool dispatchSdlEvent(SDL_Event& evt);
221  void dispatchKeyEvent(KeyEvent& evt);
222  void dispatchTextEvent(TextEvent& evt);
223  void dispatchMouseEvent(MouseEvent& evt);
224  void dispatchDropEvent(DropEvent& evt);
225 
226  // Translate events
227  void fillModifiers(InputEvent& evt);
228  void fillKeyEvent(const SDL_Event& sdlevt, KeyEvent& keyevt);
229  void fillTextEvent(const SDL_Event& sdlevt, TextEvent& txtevt);
230  void fillMouseEvent(const SDL_Event& sdlevt, MouseEvent& mouseevt);
231 
232  std::deque<ICommandListener*> m_commandlisteners;
233  std::deque<ICommandListener*> m_pending_commandlisteners;
234  std::deque<ICommandListener*> m_pending_commandlisteners_front;
235  std::deque<ICommandListener*> m_pending_cldeletions;
236 
237  std::deque<IKeyListener*> m_keylisteners;
238  std::deque<IKeyListener*> m_pending_keylisteners;
239  std::deque<IKeyListener*> m_pending_keylisteners_front;
240  std::deque<IKeyListener*> m_pending_kldeletions;
241 
242  std::deque<ITextListener*> m_textListeners;
243  std::deque<ITextListener*> m_pendingTextListeners;
244  std::deque<ITextListener*> m_pendingTextListenersFront;
245  std::deque<ITextListener*> m_pendingTlDeletions;
246 
247  std::deque<IMouseListener*> m_mouselisteners;
248  std::deque<IMouseListener*> m_pending_mouselisteners;
249  std::deque<IMouseListener*> m_pending_mouselisteners_front;
250  std::deque<IMouseListener*> m_pending_mldeletions;
251 
252  std::deque<ISdlEventListener*> m_sdleventlisteners;
253  std::deque<ISdlEventListener*> m_pending_sdleventlisteners;
254  std::deque<ISdlEventListener*> m_pending_sdleventlisteners_front;
255  std::deque<ISdlEventListener*> m_pending_sdldeletions;
256 
257  std::deque<IDropListener*> m_dropListeners;
258  std::deque<IDropListener*> m_pendingDropListeners;
259  std::deque<IDropListener*> m_pendingDropListenersFront;
260  std::deque<IDropListener*> m_pendingDlDeletions;
261 
262  std::map<int32_t, bool> m_keystatemap;
265  int32_t m_mousestate;
267 
268  // m_mouseSensitivity is the mouse speed factor - 1, so a value of 0 won't
269  // influence mouse speed, a value of 1 would double the speed and
270  // -.5 would make it half the speed
273  bool m_warp;
274  bool m_enter;
279 
281 
282  };
283 } //FIFE
284 
285 #endif
std::deque< ITextListener * > m_pendingTextListenersFront
Definition: eventmanager.h:244
void removeMouseListener(IMouseListener *listener)
Removes an added listener from the controller.
float getMouseSensitivity() const
Gets mouse sensitivity.
void setClipboardText(const std::string &text)
Sets clipboard text.
std::deque< ITextListener * > m_textListeners
Definition: eventmanager.h:242
Listener of SDL events.
std::string getClipboardText() const
Returns the clipboard text as UTF-8 string.
void addKeyListenerFront(IKeyListener *listener)
Adds a listener to the front of the listener deque Listener will be notified via the corresponding ev...
void saveGamepadMappings(const std::string &file)
Saves all controller mappings that were used during the season.
Class for mouse events.
Definition: mouseevent.h:42
void saveGamepadMapping(const std::string guid, const std::string &file)
Saves controller mapping for given GUID in the specified file.
Controller provides a way to receive events from the system Using this interface, clients can subscri...
uint8_t getJoystickCount() const
Return the number of joysticks / gamecontrollers.
void addTextListenerFront(ITextListener *listener)
Adds a listener to the front of the listener deque Listener will be notified via the corresponding ev...
Controller provides a way to receive events from the system Using this interface, clients can subscri...
IKeyFilter * m_keyfilter
Definition: eventmanager.h:263
Listener of text events.
Definition: itextlistener.h:44
Controller provides a way to receive events from the system Using this interface, clients can subscri...
Controller provides a way to receive events from the system Using this interface, clients can subscri...
Definition: ikeyfilter.h:42
Base class for input events (like mouse and keyboard)
Definition: inputevent.h:42
std::deque< IKeyListener * > m_pending_keylisteners
Definition: eventmanager.h:238
std::deque< IMouseListener * > m_pending_mouselisteners
Definition: eventmanager.h:248
Listener of drop events.
Definition: idroplistener.h:44
std::deque< ISdlEventListener * > m_pending_sdleventlisteners_front
Definition: eventmanager.h:254
Controller provides a way to receive events from the system Using this interface, clients can subscri...
bool combineEvents(SDL_Event &event1, const SDL_Event &event2)
Listener of command events.
void addJoystickListenerFront(IJoystickListener *listener)
void setMouseFilter(IMouseFilter *mouseFilter)
void removeJoystickListener(IJoystickListener *listener)
Class for text events.
Definition: textevent.h:45
void addSdlEventListener(ISdlEventListener *listener)
Adds a listener to the back of the listener deque Listener will be notified via the corresponding eve...
void dispatchCommand(Command &command)
Use this method to send command to command listeners.
void addDropListener(IDropListener *listener)
Adds a listener to the back of the listener deque Listener will be notified via the corresponding eve...
void addCommandListenerFront(ICommandListener *listener)
Adds a listener to the front of the listener deque Listener will be notified via the corresponding ev...
Listener of mouse events.
void setMouseAccelerationEnabled(bool acceleration)
Sets mouse acceleration if mouse acceleration is enabled, then the mouse sensitivity is used as speed...
std::deque< IKeyListener * > m_pending_kldeletions
Definition: eventmanager.h:240
Listener of joystick events.
void dispatchDropEvent(DropEvent &evt)
void removeDropListener(IDropListener *listener)
Removes an added listener from the controller.
std::deque< IDropListener * > m_dropListeners
Definition: eventmanager.h:257
void addDropListenerFront(IDropListener *listener)
Adds a listener to the front of the listener deque Listener will be notified via the corresponding ev...
Represents a Joystick and if available the Gamecontroller.
Definition: joystick.h:39
std::deque< IDropListener * > m_pendingDropListenersFront
Definition: eventmanager.h:259
Joystick * getJoystick(int32_t instanceId)
Return the joystick with the given instance id.
unsigned char uint8_t
Definition: core.h:38
void removeCommandListener(ICommandListener *listener)
Removes an added listener from the controller.
IMouseFilter * m_mousefilter
Definition: eventmanager.h:264
void dispatchMouseEvent(MouseEvent &evt)
void processWindowEvent(SDL_Event event)
std::deque< ISdlEventListener * > m_pending_sdleventlisteners
Definition: eventmanager.h:253
std::string getGamepadStringMapping(const std::string &guid)
Return the controller mapping for given GUID as string.
void addCommandListener(ICommandListener *listener)
Adds a listener to the back of the listener deque Listener will be notified via the corresponding eve...
std::deque< IKeyListener * > m_keylisteners
Definition: eventmanager.h:237
std::deque< ICommandListener * > m_pending_cldeletions
Definition: eventmanager.h:235
Class for commands Commands are arbitrary events e.g.
Definition: command.h:44
Joystick Manager manages all events related to Joysticks and Gamecontrollers.
bool isClipboardText() const
Returns if clipboard have text or not.
void processDropEvent(SDL_Event event)
void setGamepadStringMapping(const std::string &mapping)
Sets controller mapping from string and adds or updates the related controllers.
Controller provides a way to receive events from the system Using this interface, clients can subscri...
std::map< int32_t, bool > m_keystatemap
Definition: eventmanager.h:262
void fillMouseEvent(const SDL_Event &sdlevt, MouseEvent &mouseevt)
Controller provides a way to receive events from the system Using this interface, clients can subscri...
Definition: imousefilter.h:42
EventSourceType getEventSourceType()
Gets the source type of this event.
std::deque< IMouseListener * > m_pending_mouselisteners_front
Definition: eventmanager.h:249
unsigned short uint16_t
Definition: core.h:39
void setKeyFilter(IKeyFilter *keyFilter)
std::deque< IMouseListener * > m_mouselisteners
Definition: eventmanager.h:247
void dispatchTextEvent(TextEvent &evt)
void processTextEvent(SDL_Event event)
void removeKeyListener(IKeyListener *listener)
Removes an added listener from the controller.
EventManager()
Constructor.
Event Manager manages all events related to FIFE.
Definition: eventmanager.h:80
std::deque< ITextListener * > m_pendingTextListeners
Definition: eventmanager.h:243
void addJoystickListener(IJoystickListener *listener)
void fillKeyEvent(const SDL_Event &sdlevt, KeyEvent &keyevt)
JoystickManager * m_joystickManager
Definition: eventmanager.h:280
std::deque< IMouseListener * > m_pending_mldeletions
Definition: eventmanager.h:250
MouseButtonType
Mouse button types.
Definition: mouseevent.h:66
MouseEvent::MouseButtonType m_mostrecentbtn
Definition: eventmanager.h:266
std::deque< IDropListener * > m_pendingDropListeners
Definition: eventmanager.h:258
std::deque< ITextListener * > m_pendingTlDeletions
Definition: eventmanager.h:245
void fillTextEvent(const SDL_Event &sdlevt, TextEvent &txtevt)
std::deque< IKeyListener * > m_pending_keylisteners_front
Definition: eventmanager.h:239
void setJoystickSupport(bool support)
Sets the joystick support to enabled or disabled.
Class for drop events.
Definition: dropevent.h:43
bool isMouseAccelerationEnabled() const
Returns if mouse acceleration is enabled or not.
Class for key events.
Definition: keyevent.h:45
void addMouseListener(IMouseListener *listener)
Adds a listener to the back of the listener deque Listener will be notified via the corresponding eve...
std::deque< ISdlEventListener * > m_sdleventlisteners
Definition: eventmanager.h:252
void processKeyEvent(SDL_Event event)
virtual ~EventManager()
Destructor.
bool dispatchSdlEvent(SDL_Event &evt)
Listener of key events.
Definition: ikeylistener.h:44
EventSourceType
Types for different event sources.
void processMouseEvent(SDL_Event event)
void fillModifiers(InputEvent &evt)
std::deque< IDropListener * > m_pendingDlDeletions
Definition: eventmanager.h:260
void addSdlEventListenerFront(ISdlEventListener *listener)
Adds a listener to the front of the listener deque Listener will be notified via the corresponding ev...
void setMouseSensitivity(float sensitivity)
Sets mouse sensitivity The sensitivity is limited to the range -0.99 - 10.0.
unsigned int uint32_t
Definition: core.h:40
Representation of event source (a thing sending events)
Definition: ieventsource.h:42
std::deque< ICommandListener * > m_commandlisteners
Definition: eventmanager.h:232
void processEvents()
Process the SDL event queue.
void addTextListener(ITextListener *listener)
Adds a listener to the back of the listener deque Listener will be notified via the corresponding eve...
void addMouseListenerFront(IMouseListener *listener)
Adds a listener to the front of the listener deque Listener will be notified via the corresponding ev...
std::deque< ICommandListener * > m_pending_commandlisteners_front
Definition: eventmanager.h:234
std::deque< ISdlEventListener * > m_pending_sdldeletions
Definition: eventmanager.h:255
std::deque< ICommandListener * > m_pending_commandlisteners
Definition: eventmanager.h:233
Controller provides a way to receive events from the system Using this interface, clients can subscri...
void loadGamepadMapping(const std::string &file)
Loads controller mappings from given file and if possible, it opens the related controllers.
void addKeyListener(IKeyListener *listener)
Adds a listener to the back of the listener deque Listener will be notified via the corresponding eve...
void dispatchKeyEvent(KeyEvent &evt)
void removeTextListener(ITextListener *listener)
Removes an added listener from the controller.
void removeSdlEventListener(ISdlEventListener *listener)
Removes an added listener from the controller.