| 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" |
| 40 |
#include "eventchannel/command/command.h" |
| 41 |
#include "eventchannel/command/icommandcontroller.h" |
| 42 |
#include "eventchannel/command/icommandlistener.h" |
| 43 |
|
| 44 |
#include "eventchannel/drop/idropcontroller.h" |
| 45 |
#include "eventchannel/drop/idroplistener.h" |
| 46 |
#include "eventchannel/drop/dropevent.h" |
| 47 |
|
| 48 |
#include "eventchannel/key/ikeycontroller.h" |
| 49 |
#include "eventchannel/key/ikeylistener.h" |
| 50 |
#include "eventchannel/key/keyevent.h" |
| 51 |
#include "eventchannel/key/key.h" |
| 52 |
|
| 53 |
#include "eventchannel/text/itextcontroller.h" |
| 54 |
#include "eventchannel/text/itextlistener.h" |
| 55 |
#include "eventchannel/text/textevent.h" |
| 56 |
#include "eventchannel/text/text.h" |
| 57 |
|
| 58 |
#include "eventchannel/mouse/imousecontroller.h" |
| 59 |
#include "eventchannel/mouse/imouselistener.h" |
| 60 |
#include "eventchannel/mouse/mouseevent.h" |
| 61 |
|
| 62 |
#include "eventchannel/sdl/isdleventcontroller.h" |
| 63 |
#include "eventchannel/sdl/isdleventlistener.h" |
| 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 |
|
| 77 |
/** Event Manager manages all events related to FIFE |
| 78 |
*/ |
| 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: |
| 88 |
/** Constructor. |
| 89 |
*/ |
| 90 |
EventManager(); |
| 91 |
|
| 92 |
/** Destructor |
| 93 |
*/ |
| 94 |
virtual ~EventManager(); |
| 95 |
|
| 96 |
void addCommandListener(ICommandListener* listener); |
| 97 |
void addCommandListenerFront(ICommandListener* listener); |
| 98 |
void removeCommandListener(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); |
| 115 |
void addSdlEventListenerFront(ISdlEventListener* listener); |
| 116 |
void removeSdlEventListener(ISdlEventListener* listener); |
| 117 |
|
| 118 |
void addDropListener(IDropListener* listener); |
| 119 |
void addDropListenerFront(IDropListener* listener); |
| 120 |
void removeDropListener(IDropListener* listener); |
| 121 |
|
| 122 |
void addJoystickListener(IJoystickListener* listener); |
| 123 |
void addJoystickListenerFront(IJoystickListener* listener); |
| 124 |
void removeJoystickListener(IJoystickListener* listener); |
| 125 |
|
| 126 |
EventSourceType getEventSourceType(); |
| 127 |
|
| 128 |
/** Process the SDL event queue. |
| 129 |
* This is to be called only by the engine itself once per frame. |
| 130 |
* It passes appropriate events to their listeners |
| 131 |
*/ |
| 132 |
void processEvents(); |
| 133 |
|
| 134 |
void setKeyFilter(IKeyFilter* keyFilter); |
| 135 |
|
| 136 |
/** Sets mouse sensitivity |
| 137 |
* The sensitivity is limited to the range -0.99 - 10.0. |
| 138 |
*/ |
| 139 |
void setMouseSensitivity(float sensitivity); |
| 140 |
|
| 141 |
/** Gets mouse sensitivity |
| 142 |
*/ |
| 143 |
float getMouseSensitivity() const; |
| 144 |
|
| 145 |
/** Sets mouse acceleration |
| 146 |
* if mouse acceleration is enabled, |
| 147 |
* then the mouse sensitivity is used as speed max. |
| 148 |
*/ |
| 149 |
void setMouseAccelerationEnabled(bool acceleration); |
| 150 |
|
| 151 |
/** Returns if mouse acceleration is enabled or not. |
| 152 |
* |
| 153 |
* @return True if mouse acceleration is enabled, false if not. |
| 154 |
*/ |
| 155 |
bool isMouseAccelerationEnabled() const; |
| 156 |
|
| 157 |
/** Returns if clipboard have text or not. |
| 158 |
* |
| 159 |
* @return True if the clipboard contains text, false if not. |
| 160 |
*/ |
| 161 |
bool isClipboardText() const; |
| 162 |
|
| 163 |
/** Returns the clipboard text as UTF-8 string. |
| 164 |
* |
| 165 |
* @return UTF-8 string or an empty string if the cliboard have no text. |
| 166 |
*/ |
| 167 |
std::string getClipboardText() const; |
| 168 |
|
| 169 |
/** Sets clipboard text. |
| 170 |
* |
| 171 |
* @param text The text in UTF-8 format. |
| 172 |
*/ |
| 173 |
void setClipboardText(const std::string& text); |
| 174 |
|
| 175 |
/** Sets the joystick support to enabled or disabled. |
| 176 |
*/ |
| 177 |
void setJoystickSupport(bool support); |
| 178 |
|
| 179 |
/** Return the joystick with the given instance id. |
| 180 |
*/ |
| 181 |
Joystick* getJoystick(int32_t instanceId); |
| 182 |
|
| 183 |
/** Return the number of joysticks / gamecontrollers. |
| 184 |
*/ |
| 185 |
uint8_t getJoystickCount() const; |
| 186 |
|
| 187 |
/** Loads controller mappings from given file and if possible, it opens the related controllers. |
| 188 |
*/ |
| 189 |
void loadGamepadMapping(const std::string& file); |
| 190 |
|
| 191 |
/** Saves controller mapping for given GUID in the specified file. |
| 192 |
*/ |
| 193 |
void saveGamepadMapping(const std::string guid, const std::string& file); |
| 194 |
|
| 195 |
/** Saves all controller mappings that were used during the season. |
| 196 |
*/ |
| 197 |
void saveGamepadMappings(const std::string& file); |
| 198 |
|
| 199 |
/** Return the controller mapping for given GUID as string. |
| 200 |
*/ |
| 201 |
std::string getGamepadStringMapping(const std::string& guid); |
| 202 |
|
| 203 |
/** Sets controller mapping from string and adds or updates the related controllers. |
| 204 |
*/ |
| 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; |
| 238 |
IKeyFilter* m_keyfilter; |
| 239 |
int32_t m_mousestate; |
| 240 |
MouseEvent::MouseButtonType m_mostrecentbtn; |
| 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 |
| 245 |
float m_mouseSensitivity; |
| 246 |
bool m_acceleration; |
| 247 |
bool m_warp; |
| 248 |
bool m_enter; |
| 249 |
uint16_t m_oldX; |
| 250 |
uint16_t m_oldY; |
| 251 |
uint32_t m_lastTicks; |
| 252 |
float m_oldVelocity; |
| 253 |
|
| 254 |
JoystickManager* m_joystickManager; |
| 255 |
|
| 256 |
}; |
| 257 |
} //FIFE |
| 258 |
|
| 259 |
#endif |