FIFE  be64c707dea6b3250bd4355bf5c825d25920087d
eventmanager.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2019 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 
79  class EventManager:
80  public ICommandController,
81  public IKeyController,
82  public ITextController,
83  public IMouseController,
84  public ISdlEventController,
85  public IDropController,
86  public IEventSource {
87  public:
90  EventManager();
91 
94  virtual ~EventManager();
95 
96  void addCommandListener(ICommandListener* listener);
99 
100  void dispatchCommand(Command& command);
101 
102  void addKeyListener(IKeyListener* listener);
103  void addKeyListenerFront(IKeyListener* listener);
104  void removeKeyListener(IKeyListener* listener);
105 
106  void addTextListener(ITextListener* listener);
107  void addTextListenerFront(ITextListener* listener);
108  void removeTextListener(ITextListener* listener);
109 
110  void addMouseListener(IMouseListener* listener);
111  void addMouseListenerFront(IMouseListener* listener);
112  void removeMouseListener(IMouseListener* listener);
113 
114  void addSdlEventListener(ISdlEventListener* listener);
117 
118  void addDropListener(IDropListener* listener);
119  void addDropListenerFront(IDropListener* listener);
120  void removeDropListener(IDropListener* listener);
121 
122  void addJoystickListener(IJoystickListener* listener);
125 
127 
132  void processEvents();
133 
134  void setKeyFilter(IKeyFilter* keyFilter);
135 
139  void setMouseSensitivity(float sensitivity);
140 
143  float getMouseSensitivity() const;
144 
149  void setMouseAccelerationEnabled(bool acceleration);
150 
155  bool isMouseAccelerationEnabled() const;
156 
161  bool isClipboardText() const;
162 
167  std::string getClipboardText() const;
168 
173  void setClipboardText(const std::string& text);
174 
177  void setJoystickSupport(bool support);
178 
181  Joystick* getJoystick(int32_t instanceId);
182 
185  uint8_t getJoystickCount() const;
186 
189  void loadGamepadMapping(const std::string& file);
190 
193  void saveGamepadMapping(const std::string guid, const std::string& file);
194 
197  void saveGamepadMappings(const std::string& file);
198 
201  std::string getGamepadStringMapping(const std::string& guid);
202 
205  void setGamepadStringMapping(const std::string& mapping);
206 
207  private:
208  // Helpers for processEvents
209  void processWindowEvent(SDL_Event event);
210  void processKeyEvent(SDL_Event event);
211  void processTextEvent(SDL_Event event);
212  void processMouseEvent(SDL_Event event);
213  void processDropEvent(SDL_Event event);
214  bool combineEvents(SDL_Event& event1, const SDL_Event& event2);
215 
216  // Events dispatchers - only dispatchSdlevent may reject the event.
217  bool dispatchSdlEvent(SDL_Event& evt);
218  void dispatchKeyEvent(KeyEvent& evt);
219  void dispatchTextEvent(TextEvent& evt);
220  void dispatchMouseEvent(MouseEvent& evt);
221  void dispatchDropEvent(DropEvent& evt);
222 
223  // Translate events
224  void fillModifiers(InputEvent& evt);
225  void fillKeyEvent(const SDL_Event& sdlevt, KeyEvent& keyevt);
226  void fillTextEvent(const SDL_Event& sdlevt, TextEvent& txtevt);
227  void fillMouseEvent(const SDL_Event& sdlevt, MouseEvent& mouseevt);
228 
229  // Listeners
230  std::deque<ICommandListener*> m_commandListeners;
231  std::deque<IKeyListener*> m_keyListeners;
232  std::deque<ITextListener*> m_textListeners;
233  std::deque<IMouseListener*> m_mouseListeners;
234  std::deque<ISdlEventListener*> m_sdleventListeners;
235  std::deque<IDropListener*> m_dropListeners;
236 
237  std::map<int32_t, bool> m_keystatemap;
239  int32_t m_mousestate;
241 
242  // m_mouseSensitivity is the mouse speed factor - 1, so a value of 0 won't
243  // influence mouse speed, a value of 1 would double the speed and
244  // -.5 would make it half the speed
247  bool m_warp;
248  bool m_enter;
253 
255 
256  };
257 } //FIFE
258 
259 #endif
bool isMouseAccelerationEnabled() const
Returns if mouse acceleration is enabled or not.
void removeMouseListener(IMouseListener *listener)
Removes an added listener from the controller.
void setClipboardText(const std::string &text)
Sets clipboard text.
std::deque< ITextListener * > m_textListeners
Definition: eventmanager.h:232
Listener of SDL events.
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...
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:238
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
Listener of drop events.
Definition: idroplistener.h:44
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)
std::deque< ICommandListener * > m_commandListeners
Definition: eventmanager.h:230
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...
uint8_t getJoystickCount() const
Return the number of joysticks / gamecontrollers.
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:235
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
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.
void dispatchMouseEvent(MouseEvent &evt)
void processWindowEvent(SDL_Event event)
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...
Class for commands Commands are arbitrary events e.g.
Definition: command.h:44
Joystick Manager manages all events related to Joysticks and Gamecontrollers.
void processDropEvent(SDL_Event event)
std::deque< IMouseListener * > m_mouseListeners
Definition: eventmanager.h:233
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...
float getMouseSensitivity() const
Gets mouse sensitivity.
std::map< int32_t, bool > m_keystatemap
Definition: eventmanager.h:237
void fillMouseEvent(const SDL_Event &sdlevt, MouseEvent &mouseevt)
EventSourceType getEventSourceType()
Gets the source type of this event.
unsigned short uint16_t
Definition: core.h:39
void setKeyFilter(IKeyFilter *keyFilter)
std::string getClipboardText() const
Returns the clipboard text as UTF-8 string.
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:79
void addJoystickListener(IJoystickListener *listener)
void fillKeyEvent(const SDL_Event &sdlevt, KeyEvent &keyevt)
JoystickManager * m_joystickManager
Definition: eventmanager.h:254
MouseButtonType
Mouse button types.
Definition: mouseevent.h:66
MouseEvent::MouseButtonType m_mostrecentbtn
Definition: eventmanager.h:240
void fillTextEvent(const SDL_Event &sdlevt, TextEvent &txtevt)
std::deque< ISdlEventListener * > m_sdleventListeners
Definition: eventmanager.h:234
void setJoystickSupport(bool support)
Sets the joystick support to enabled or disabled.
Class for drop events.
Definition: dropevent.h:43
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...
void processKeyEvent(SDL_Event event)
virtual ~EventManager()
Destructor.
bool isClipboardText() const
Returns if clipboard have text or not.
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)
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
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...
Controller provides a way to receive events from the system Using this interface, clients can subscri...
std::deque< IKeyListener * > m_keyListeners
Definition: eventmanager.h:231
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.