2
// C++ Interface: Input
7
// Author: Erik Hjortsberg <erik.hjortsberg@gmail.com>, (C) 2005
9
// This program is free software; you can redistribute it and/or modify
10
// it under the terms of the GNU General Public License as published by
11
// the Free Software Foundation; either version 2 of the License, or
12
// (at your option) any later version.
14
// This program is distributed in the hope that it will be useful,
15
// but WITHOUT ANY WARRANTY; without even the implied warranty of
16
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
// GNU General Public License for more details.
19
// You should have received a copy of the GNU General Public License
20
// along with this program; if not, write to the Free Software
21
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.//
23
#ifndef EMBEROGREINPUT_H
24
#define EMBEROGREINPUT_H
26
#include <sigc++/slot.h>
27
#include <sigc++/signal.h>
28
#include <SDL_events.h>
29
#include "framework/ConsoleObject.h"
30
#include "framework/Singleton.h"
39
class InputCommandMapper;
41
typedef std::set<SDLKey> KeysSet;
42
typedef std::list<IInputAdapter*> IInputAdapterStore;
47
@brief Struct for a mouse movement.
48
@author Erik Hjortsberg <erik.hjortsberg@gmail.com>
53
@brief The horizontal position of the mouse in pixels.
57
@brief The vertical position of the mouse in pixels.
62
@brief The relative horizontal position of the mouse.
65
float xRelativeMovement;
67
@brief The relative vertical position of the mouse.
70
float yRelativeMovement;
73
@brief The relative horizontal movement in pixels.
74
The amount of pixels the cursor has moved since our last sampling point.
76
int xRelativeMovementInPixels;
78
@brief The relative vertical movement in pixels.
79
The amount of pixels the cursor has moved since our last sampling point.
81
int yRelativeMovementInPixels;
84
@brief The time since last sampling point.
85
Expressed as full seconds.
87
float timeSinceLastMovement;
91
@brief Expresses the position of the mouse, both in terms of pixels and relative.
93
Positions are from the upper left corner.
94
@author Erik Hjortsberg <erik.hjortsberg@gmail.com>
99
@brief The horizontal position of the mouse in pixels.
103
@brief The vertical position of the mouse in pixels.
108
@brief The relative horizontal position of the mouse.
109
0 is to the left, 1 is to the right.
111
float xRelativePosition;
113
@brief The relative vertical position of the mouse.
114
0 is at the top, 1 is at the bottom.
116
float yRelativePosition;
121
@author Erik Hjortsberg <erik.hjortsberg@gmail.com>
123
@brief This class takes care of all input and routes it to the correct place in Ember.
124
Right now that means that when in GUI mode, all input will be routed to the registered list of @see IInputAdapter, and when in non-gui mode (ie. movement mode), all input will be routed directly to Ember, where it can be handled by the camera and movement system.
126
Note that while keyboard input is buffered, mouse input is not.
128
You can listen to input updates either by listening directly to the events, or by registering an instance of IInputAdapter through the addAdapter and removeAdapter methods.
130
This class also provides some methods useful for standard windowing and event system integration, such as isApplicationVisible().
132
class Input : public Ember::ConsoleObject, public Ember::Singleton<Input>
134
friend class InputCommandMapper;
139
@brief Command for binding keys to commands.
141
static const std::string BINDCOMMAND;
144
@brief Command for unbinding keys to commands.
146
static const std::string UNBINDCOMMAND;
159
@brief Describes different input modes.
164
@brief In gui mode, the mouse will move the cursor and allow interaction with the GUI system
169
@brief In movement mode, the mouse will move the camera and the keys will move the player. Interaction with the gui is not possible.
179
* @brief Initializes the input object. Call this before you want to recieve input.
180
* @param width The width of the window, in pixels.
181
* @param heigh The height of the window, in pixels.
183
void initialize(int width, int height);
186
* @brief Starts processing all input for a frame.
187
* Call this once every frame.
193
* @brief Checks whether the application is visible and nor minimized.
194
* @return True if the application is in normal shown mode, false if it's minimized.
196
bool isApplicationVisible();
198
/** @brief Emitted when a key has been pressed in movement mode.
200
@param true if the application is in gui mode
202
sigc::signal<void, const SDL_keysym&, Input::InputMode> EventKeyPressed;
204
/** @brief Emitted when a key has been released in movement mode.
206
@param true if the application is in gui mode
208
sigc::signal<void, const SDL_keysym&, Input::InputMode> EventKeyReleased;
210
/** @brief Emitted when the mouse has moved.
211
Note that when in non-gui mode, the x and y position for the mouse will always be the same for consecutive signals although the relative position will have changed.
212
@param the mouse motion
213
@param true if ember is in gui mode
215
sigc::signal<void, const MouseMotion&, InputMode> EventMouseMoved;
219
@brief Emitted when a mouse button is pressed.
220
@param the mouse button
221
@param true if ember is in gui mode
223
sigc::signal<void, MouseButton, InputMode> EventMouseButtonPressed;
226
@brief Emitted when a mouse button is released.
227
@param the mouse button
228
@param true if ember is in gui mode
230
sigc::signal<void, MouseButton, InputMode> EventMouseButtonReleased;
233
@brief Emitted when the input mode has been changed.
234
@param the new input mode
236
sigc::signal<void, InputMode> EventChangedInputMode;
239
@brief Emitted when the window is minimized or un-mininized.
240
@param True if the window is active, false it it's minimized.
242
sigc::signal<void, bool> EventWindowActive;
245
* @brief Returns true if the supplied key is down
246
* @param key The key to check for.
247
* @return True if the key is down
249
const bool isKeyDown(const SDLKey& key) const;
254
* @brief Sets the window geometry. Call this whenever the size of the window has changed.
255
* @param width The new width, in pixels.
256
* @param heigh The new height, in pixels.
258
void setGeometry(int width, int heigh);
261
* @brief Sets the new input mode.
262
* @param mode The new mode.
264
void setInputMode(InputMode mode);
267
* @brief Gets the current input mode.
268
* @return The current input mode.
270
InputMode getInputMode() const;
274
* @brief Toggles between different input modes, returning the new mode.
275
* @return The new input mode.
277
InputMode toggleInputMode();
280
* @brief Adds an adaptor to which input event will be sent.
281
* Note that event will be sent to adapters added later first, allowing them to decide whether events should be sent to previous added adapters. This allows later added adapters to override current behaviour.
283
* @param adaptor An adapter instance to add. Note that ownership isn't transferred. Note that if the adapter already has been added, this will add a duplicate entry.
285
void addAdapter(IInputAdapter* adapter);
289
* @brief Remove an adaptor from the list of adaptors.
290
* @param adaptor The adapter to remove. If the adapter isn't present already nothing will happen.
292
void removeAdapter(IInputAdapter* adapter);
297
* @brief Reimplements the ConsoleObject::runCommand method
301
virtual void runCommand(const std::string &command, const std::string &args);
304
@brief Suppress all further event handling of the current event. Call this inside event handling methods to prevent further event handling.
306
void suppressFurtherHandlingOfCurrentEvent();
309
@brief Gets whether the movement mode is enabled, at which all mouse right click events will toggle between movement mode and mouse mode.
310
@return True if movement mode is enabled.
312
bool getMovementModeEnabled() const;
315
@brief Sets whether the movement mode is enabled, at which all mouse right click events will toggle between movement mode and mouse mode.
316
@param value Whether to enable movement mode or not.
318
void setMovementModeEnabled(bool value);
321
* @brief Writes the supplied text to the system clipboard.
322
* This works on all platform, by using the scrap.c code found in framework.
323
* @param text The text to write.
325
void writeToClipboard(const std::string& text);
328
* @brief Gets the current mouse position.
329
* @return The last mouse position.
331
const MousePosition& getMousePosition() const;
335
typedef std::map<std::string, InputCommandMapper*> InputCommandMapperStore;
339
* @brief Registers a command mapper.
340
* @param mapper The mapper instance to register.
342
void registerCommandMapper(InputCommandMapper* mapper);
345
* @brief Deregisters a command mapper.
346
* @param mapper The mapper to deregister.
348
void deregisterCommandMapper(InputCommandMapper* mapper);
352
@brief The current input mode.
354
InputMode mCurrentInputMode;
358
* @brief Polls all input for the mouse.
359
* Call this each frame.
360
* @param secondsSinceLast In whole seconds, the time since the last polling.
362
void pollMouse(float secondsSinceLast);
365
* @brief Polls all needed events from the system.
366
* Call this each frame.
367
* @param secondsSinceLast In whole seconds, the time since the last polling.
369
void pollEvents(float secondsSinceLast);
371
void keyChanged(const SDL_KeyboardEvent &keyEvent);
373
void keyPressed(const SDL_KeyboardEvent &keyEvent);
374
void keyReleased(const SDL_KeyboardEvent &keyEvent);
378
@brief Keys which should not be injected as chars, ie. enter, backspace etc.
380
KeysSet mNonCharKeys;
384
@brief A set of the keys that are currently pressed.
386
KeysSet mKeysPressed;
389
@brief Saves the last mouse state.
391
unsigned int mMouseState;
394
@brief The last positions of the mouse.
396
MousePosition mMousePosition;
399
@brief The amount of time since the last right mouse click.
400
Used for detecting double clicks.
402
float mTimeSinceLastRightMouseClick;
405
@brief Store the last tick count, to use for looking up how much time has elapsed since our last event polling.
410
* @brief Gets the text in the clipboard and pastes it to the gui system.
412
void pasteFromClipboard();
415
@brief A store of adapters to which input event will be sent, in order.
417
IInputAdapterStore mAdapters;
420
@brief The dimensions of the window.
422
float mScreenWidth, mScreenHeight;
426
@brief A store of InputCommandMappers with their state as the key.
428
InputCommandMapperStore mInputCommandMappers;
431
@brief Whether no more event should be processed.
432
This is checked within the event handling code only.
434
bool mSuppressForCurrentEvent;
437
@brief Whether movement mode is enabled or not.
439
bool mMovementModeEnabled;