The Game Engine  1
Input Class Reference

Input handler to separate input from system. More...

#include <Input.h>

+ Collaboration diagram for Input:

Classes

struct  MouseState
 Helper container to handle mouse clicks. More...
 

Public Member Functions

 Input (bool isJoy=false)
 
Point2D getJoyMovement (JoyStick s=JoyLeft)
 Obtain the movement Value from this input. More...
 
bool pressed (SDLKey key, SDLMod modifier=KMOD_NONE)
 Whether a key is pressed (ends up being the same as gathering entire current keyboard state and polling for specific key. More...
 
bool triggered (SDLKey key, int ms=-1)
 Whether a key is triggered. More...
 
bool doubleTapped (SDLKey key, int ms=-1)
 Whether a key is double tapped. More...
 
bool pressed (JoyButton button)
 Whether a button is pressed (ends up being the same as gathering entire current buttonboard state and polling for specific button. More...
 
bool triggered (JoyButton button, int ms=-1)
 Whether a button is triggered. More...
 
bool doubleTapped (JoyButton button, int ms=-1)
 Whether a button is double tapped. More...
 

Static Public Member Functions

static int getNumInputs ()
 Returns number of inputs. More...
 
static int updateJoysticks ()
 Initializes the Joystick subsystem to update connected joysticks. More...
 
static InputgetInput (unsigned int index=0)
 Provides access to the specified Input at given index (0 is always the keyboard, 1-n are joysticks) More...
 
static void updateInputs (SDL_Event event)
 Store input from SDL. More...
 
static pair< int, int > getDeviceCounts ()
 gets the counts of keyboards and joysticks More...
 
static int getNumJoysticks ()
 gets the number of joysticks on the system More...
 
static void addInput (Input *i)
 Adds an input device. More...
 
static void removeInput (Input *i)
 Removes an input device. More...
 
static void addKeyDownCallback (void(*fn)(SDLKey, SDLMod))
 Add a keyboard button down callback. More...
 
static void removeKeyDownCallback (void(*fn)(SDLKey, SDLMod))
 remove a keyboard button down callback More...
 
static void addKeyUpCallback (void(*fn)(SDLKey, SDLMod))
 Add a keyboard button down callback. More...
 
static void removeKeyUpCallback (void(*fn)(SDLKey, SDLMod))
 remove a keyboard button down callback More...
 
static void addMouseDownCallback (void(*fn)(Uint8, MouseState))
 Add a keyboard button down callback. More...
 
static void removeMouseDownCallback (void(*fn)(Uint8, MouseState))
 remove a keyboard button down callback More...
 
static void addMouseUpCallback (void(*fn)(Uint8, MouseState))
 Add a keyboard button down callback. More...
 
static void removeMouseUpCallback (void(*fn)(Uint8, MouseState))
 remove a keyboard button down callback More...
 
static void addMouseMoveCallback (void(*fn)(MouseState))
 Add a keyboard button down callback. More...
 
static void removeMouseMoveCallback (void(*fn)(MouseState))
 remove a keyboard button down callback More...
 
static void addJoyButtonDownCallback (void(*fn)(Uint8, JoyButton))
 Add a keyboard button down callback. More...
 
static void removeJoyButtonDownCallback (void(*fn)(Uint8, JoyButton))
 remove a keyboard button down callback More...
 
static void addJoyButtonUpCallback (void(*fn)(Uint8, JoyButton))
 Add a keyboard button down callback. More...
 
static void removeJoyButtonUpCallback (void(*fn)(Uint8, JoyButton))
 remove a keyboard button down callback More...
 
static void addJoyMoveCallback (void(*fn)(Uint8, JoyStick, Point2D))
 Add a keyboard button down callback. More...
 
static void removeJoyMoveCallback (void(*fn)(Uint8, JoyStick, Point2D))
 remove a keyboard button down callback More...
 
static bool getAllPressed (JoyButton b)
 Check all joypads for values of act being down. More...
 
static bool getAllTriggered (JoyButton b)
 Check all joypads for values for act being triggered (down then up) More...
 
static Point2D getAllJoyMovement (JoyStick s=JoyLeft)
 Check all joypads for values of movement. More...
 
static Point2D getMousePos ()
 Provides an interface to obtain Mouse Position. More...
 
static Point2D getMouseRelPos ()
 Provides an interface to obtain Mouse Relative movement. More...
 
static bool pressed (MouseButton b, int button=0)
 Whether a mouse button is pressed (ends up being the same as gathering entire current Buttonboard state and polling for specific Button. More...
 
static bool triggered (MouseButton b, int button=0, int ms=-1)
 Whether a mouse button is triggered. More...
 
static bool doubleTapped (MouseButton b, int button=0, int ms=-1)
 Whether a mouse button is double tapped. More...
 

Protected Attributes

int mIndex
 input index More...
 
bool isJoystick
 Represents Keyboard or Joystick. If true Joystick. More...
 
SDL_Joystick * mJoystick
 Pointer to the Joystick. More...
 
Point2D mJoyLeftPos
 Represents the axis values of a joystick. More...
 
Point2D mJoyRightPos
 Represents JoyRight Joy pos. More...
 
Point2D mJoyDPos
 Represents Dpad Joy pos. More...
 
map< SDLKey, pair< bool, SDLMod > > mKeyStates
 list of key's state (active if the input is a keyboard) More...
 
map< SDLKey, Uint32 > mLastKeyStates
 list of key's state trigger times (active if the input is a keyboard) More...
 
map< SDLKey, Uint32 > mLastKeyDoubleStates
 list of key's state trigger times (for double click) (active if the input is a keyboard) More...
 
vector< bool > mJoyStates
 list of button's state (active if the input is a Joystick) Corresponds to joyButton More...
 
vector< Uint32 > mLastJoyStates
 list of button's state trigger times (active if the input is a Joystick) Corresponds to joyButton More...
 
vector< Uint32 > mLastJoyDoubleStates
 list of button's double clicked state (active if the input is a Joystick) Corresponds to joyButton More...
 
vector< MouseStatemMouseButtonStates
 list of mouse's state More...
 
vector< MouseStatemLastMouseStates
 list of mouse's state trigger times More...
 
vector< MouseStatemLastMouseDoubleStates
 list of mouse's doube click state More...
 

Static Protected Attributes

static vector< Input * > inputs = vector<Input*>()
 list of all inputdevices we have 0 is always the keyboard (not taking into acount multiples atm) joysticks are 1..JoyCount More...
 
static set< void(*)(SDLKey,
SDLMod)> 
mKeyDownCallbacks = set<void (*)(SDLKey, SDLMod)>()
 list of callback functions for Button press More...
 
static set< void(*)(SDLKey,
SDLMod)> 
mKeyUpCallbacks = set<void (*)(SDLKey, SDLMod)>()
 list of callback functions for Button release More...
 
static set< void(*)(Uint8,
MouseState)> 
mMouseDownCallbacks = set<void (*)(Uint8, MouseState)>()
 list of callback functions for Mouse press More...
 
static set< void(*)(Uint8,
MouseState)> 
mMouseUpCallbacks = set<void (*)(Uint8, MouseState)>()
 list of callback functions for Mouse release More...
 
static set< void(*)(MouseState)> mMouseMoveCallbacks = set<void (*)(MouseState)>()
 list of callback functions for Mouse movement More...
 
static set< void(*)(Uint8,
JoyButton)> 
mJoyButtonDownCallbacks = set<void (*)(Uint8, JoyButton)>()
 list of callback functions for JoyButton press requiring which joystick passed the data More...
 
static set< void(*)(Uint8,
JoyButton)> 
mJoyButtonUpCallbacks = set<void (*)(Uint8, JoyButton)>()
 list of callback functions for JoyButton release requiring which joystick passed the data More...
 
static set< void(*)(Uint8,
JoyStick, Point2D)> 
mJoyMoveCallbacks = set<void (*)(Uint8, JoyStick, Point2D)>()
 list of callback functions for JoyStick movement requiring which joystick passed the data More...
 
static MouseState mMouseState = MouseState()
 Mouse state. More...
 

Detailed Description

Input handler to separate input from system.

This class is tasked with the following:

  • obtaining and handling all input in a consistent form.
  • Stores current key/button states for a given input
  • Stores the last time a button was releassed (for Triggered)
  • Allows queries for and individual input device
  • Allows queries for all input devices
  • Provide access to callback fns
  • Obtain input device for a given player
    Todo:
    move to Player class

Definition at line 62 of file Input.h.

Constructor & Destructor Documentation

Input::Input ( bool  isJoy = false)
Todo:
this should at some point store the defaults for both keyboard and joystick (player's personal preferences)and just use the one that is selected

Definition at line 4 of file Input.cpp.

+ Here is the call graph for this function:

Member Function Documentation

void Input::addInput ( Input i)
static

Adds an input device.

Definition at line 570 of file Input.cpp.

+ Here is the caller graph for this function:

void Input::addJoyButtonDownCallback ( void(*)(Uint8, JoyButton fn)
static

Add a keyboard button down callback.

Parameters
fnThe function to add

Definition at line 626 of file Input.cpp.

+ Here is the caller graph for this function:

void Input::addJoyButtonUpCallback ( void(*)(Uint8, JoyButton fn)
static

Add a keyboard button down callback.

Parameters
fnThe function to add

Definition at line 634 of file Input.cpp.

+ Here is the caller graph for this function:

void Input::addJoyMoveCallback ( void(*)(Uint8, JoyStick, Point2D fn)
static

Add a keyboard button down callback.

Parameters
fnThe function to add

Definition at line 642 of file Input.cpp.

+ Here is the caller graph for this function:

void Input::addKeyDownCallback ( void(*)(SDLKey, SDLMod)  fn)
static

Add a keyboard button down callback.

Parameters
fnThe function to add

Definition at line 585 of file Input.cpp.

+ Here is the caller graph for this function:

void Input::addKeyUpCallback ( void(*)(SDLKey, SDLMod)  fn)
static

Add a keyboard button down callback.

Parameters
fnThe function to add

Definition at line 594 of file Input.cpp.

+ Here is the caller graph for this function:

void Input::addMouseDownCallback ( void(*)(Uint8, MouseState fn)
static

Add a keyboard button down callback.

Parameters
fnThe function to add

Definition at line 602 of file Input.cpp.

+ Here is the caller graph for this function:

void Input::addMouseMoveCallback ( void(*)(MouseState fn)
static

Add a keyboard button down callback.

Parameters
fnThe function to add

Definition at line 618 of file Input.cpp.

+ Here is the caller graph for this function:

void Input::addMouseUpCallback ( void(*)(Uint8, MouseState fn)
static

Add a keyboard button down callback.

Parameters
fnThe function to add

Definition at line 610 of file Input.cpp.

+ Here is the caller graph for this function:

bool Input::doubleTapped ( SDLKey  key,
int  ms = -1 
)

Whether a key is double tapped.

Parameters
keySDLKey to check for double tapped
msThis value is an optional value that checks against the given ms instead of TRIGGERTIME (this value cannot be larger than 2^31)
Returns
whether the key has been released within TRIGGERTIME (this value cannot be larger than 2^31) ticks

<

Todo:
this should reeeally be tested to make sure - values of ms don't break.

Definition at line 692 of file Input.cpp.

+ Here is the caller graph for this function:

bool Input::doubleTapped ( JoyButton  button,
int  ms = -1 
)

Whether a button is double tapped.

Todo:
make TRIGGERTIME (this value cannot be larger than 2^31) settable
Parameters
buttonJoyButton to check for double tapped
msThis value is an optional value that checks against the given ms instead of TRIGGERTIME (this value cannot be larger than 2^31)
Returns
whether the button has been pressed within TRIGGERTIME (this value cannot be larger than 2^31) ticks

Definition at line 711 of file Input.cpp.

bool Input::doubleTapped ( MouseButton  b,
int  button = 0,
int  ms = -1 
)
static

Whether a mouse button is double tapped.

Parameters
bMouseButton to check for double tapped
buttonMouseButton (i.e. not left, right, middle, or scroll) index to check for trigger
msThis value is an optional value that checks against the given ms instead of TRIGGERTIME (this value cannot be larger than 2^31)
Returns
whether the Button has been pressed within TRIGGERTIME ticks

Definition at line 734 of file Input.cpp.

Point2D Input::getAllJoyMovement ( JoyStick  s = JoyLeft)
static

Check all joypads for values of movement.

Returns
the values of the l/r and u/d values of the requested Joystick

Definition at line 668 of file Input.cpp.

+ Here is the caller graph for this function:

bool Input::getAllPressed ( JoyButton  b)
static

Check all joypads for values of act being down.

Parameters
bThe button to be pressed

Definition at line 650 of file Input.cpp.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool Input::getAllTriggered ( JoyButton  b)
static

Check all joypads for values for act being triggered (down then up)

Parameters
bButton to be triggered

Definition at line 659 of file Input.cpp.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

pair< int, int > Input::getDeviceCounts ( )
static

gets the counts of keyboards and joysticks

Returns
pair where first is keyboards and second is joysticks
Todo:
determine number of keyboards on linux

Definition at line 559 of file Input.cpp.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Input * Input::getInput ( unsigned int  index = 0)
static

Provides access to the specified Input at given index (0 is always the keyboard, 1-n are joysticks)

Parameters
indexIndex to obtain

Definition at line 31 of file Input.cpp.

+ Here is the caller graph for this function:

Point2D Input::getJoyMovement ( JoyStick  s = JoyLeft)

Obtain the movement Value from this input.

Parameters
sThe action/Joystick to obtain movement data for
Returns
  • For keyboard this would generate a vector based on movement keys
  • For joysticks the vetor will be the raw vector of the up/down left/right axes

Definition at line 743 of file Input.cpp.

+ Here is the caller graph for this function:

Point2D Input::getMousePos ( )
static

Provides an interface to obtain Mouse Position.

Definition at line 676 of file Input.cpp.

+ Here is the caller graph for this function:

Point2D Input::getMouseRelPos ( )
static

Provides an interface to obtain Mouse Relative movement.

Definition at line 678 of file Input.cpp.

+ Here is the caller graph for this function:

int Input::getNumInputs ( )
static

Returns number of inputs.

Definition at line 27 of file Input.cpp.

+ Here is the caller graph for this function:

int Input::getNumJoysticks ( )
static

gets the number of joysticks on the system

Returns
count of joysticks

Definition at line 565 of file Input.cpp.

+ Here is the caller graph for this function:

bool Input::pressed ( SDLKey  key,
SDLMod  modifier = KMOD_NONE 
)

Whether a key is pressed (ends up being the same as gathering entire current keyboard state and polling for specific key.

Parameters
keySDLKey to check
modifierthe SDLMod representing modifier keys
Returns
Whether the SDLKey is currently down

Definition at line 680 of file Input.cpp.

+ Here is the caller graph for this function:

bool Input::pressed ( JoyButton  button)

Whether a button is pressed (ends up being the same as gathering entire current buttonboard state and polling for specific button.

Parameters
buttonJoyButton to check
Returns
Whether the JoyButton is currently down

Definition at line 698 of file Input.cpp.

bool Input::pressed ( MouseButton  b,
int  button = 0 
)
static

Whether a mouse button is pressed (ends up being the same as gathering entire current Buttonboard state and polling for specific Button.

Parameters
bMouseButton to check
buttonMouseButton (i.e. not left, right, middle, or scroll) index to check for trigger
Returns
Whether the SDLButton is currently down

Definition at line 717 of file Input.cpp.

void Input::removeInput ( Input i)
static

Removes an input device.

Definition at line 575 of file Input.cpp.

+ Here is the caller graph for this function:

void Input::removeJoyButtonDownCallback ( void(*)(Uint8, JoyButton fn)
static

remove a keyboard button down callback

Parameters
fnThe function to add

Definition at line 630 of file Input.cpp.

+ Here is the caller graph for this function:

void Input::removeJoyButtonUpCallback ( void(*)(Uint8, JoyButton fn)
static

remove a keyboard button down callback

Parameters
fnThe function to add

Definition at line 638 of file Input.cpp.

+ Here is the caller graph for this function:

void Input::removeJoyMoveCallback ( void(*)(Uint8, JoyStick, Point2D fn)
static

remove a keyboard button down callback

Parameters
fnThe function to add

Definition at line 646 of file Input.cpp.

+ Here is the caller graph for this function:

void Input::removeKeyDownCallback ( void(*)(SDLKey, SDLMod)  fn)
static

remove a keyboard button down callback

Parameters
fnThe function to add
Todo:
fix all of these so they don't delete themselves instantaneously

Definition at line 589 of file Input.cpp.

+ Here is the caller graph for this function:

void Input::removeKeyUpCallback ( void(*)(SDLKey, SDLMod)  fn)
static

remove a keyboard button down callback

Parameters
fnThe function to add

Definition at line 598 of file Input.cpp.

+ Here is the caller graph for this function:

void Input::removeMouseDownCallback ( void(*)(Uint8, MouseState fn)
static

remove a keyboard button down callback

Parameters
fnThe function to add

Definition at line 606 of file Input.cpp.

+ Here is the caller graph for this function:

void Input::removeMouseMoveCallback ( void(*)(MouseState fn)
static

remove a keyboard button down callback

Parameters
fnThe function to add

Definition at line 622 of file Input.cpp.

+ Here is the caller graph for this function:

void Input::removeMouseUpCallback ( void(*)(Uint8, MouseState fn)
static

remove a keyboard button down callback

Parameters
fnThe function to add

Definition at line 614 of file Input.cpp.

+ Here is the caller graph for this function:

bool Input::triggered ( SDLKey  key,
int  ms = -1 
)

Whether a key is triggered.

Parameters
keySDLKey to check for trigger
msThis value is an optional value that checks against the given ms instead of TRIGGERTIME (this value cannot be larger than 2^31)
Returns
whether the key has been released within TRIGGERTIME (this value cannot be larger than 2^31) ticks

<

Todo:
this should reeeally be tested to make sure - values of ms don't break.

Definition at line 686 of file Input.cpp.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool Input::triggered ( JoyButton  button,
int  ms = -1 
)

Whether a button is triggered.

Todo:
make TRIGGERTIME (this value cannot be larger than 2^31) settable
Parameters
buttonJoyButton to check for double tapped
msThis value is an optional value that checks against the given ms instead of TRIGGERTIME (this value cannot be larger than 2^31)
Returns
whether the button has been pressed within TRIGGERTIME (this value cannot be larger than 2^31) ticks

Definition at line 703 of file Input.cpp.

+ Here is the call graph for this function:

bool Input::triggered ( MouseButton  b,
int  button = 0,
int  ms = -1 
)
static

Whether a mouse button is triggered.

Parameters
bMouseButton to check for trigger
buttonMouseButton (i.e. not left, right, middle, or scroll) index to check for trigger
msThis value is an optional value that checks against the given ms instead of TRIGGERTIME (this value cannot be larger than 2^31)
Returns
whether the Button has been pressed within TRIGGERTIME (this value cannot be larger than 2^31) ticks

Definition at line 725 of file Input.cpp.

+ Here is the call graph for this function:

void Input::updateInputs ( SDL_Event  event)
static

Store input from SDL.

Todo:
see if it makes more sense for mousebutton and other things to allow the user to provide a processing function for each type of thing (makes lots of sense for mouse so that each event can be handled by a custom handler)
Todo:
make this have a mapping from a joybutton to storage button (after getting a non-ps3 and non-360 controller) to make configurable

Definition at line 51 of file Input.cpp.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int Input::updateJoysticks ( )
static

Initializes the Joystick subsystem to update connected joysticks.

Definition at line 35 of file Input.cpp.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Data Documentation

vector< Input * > Input::inputs = vector<Input*>()
staticprotected

list of all inputdevices we have 0 is always the keyboard (not taking into acount multiples atm) joysticks are 1..JoyCount

Todo:
define NumKeyboards if we care about more than one

Definition at line 338 of file Input.h.

bool Input::isJoystick
protected

Represents Keyboard or Joystick. If true Joystick.

Definition at line 365 of file Input.h.

int Input::mIndex
protected

input index

Todo:
have this represent the joypad or keyboard index depending on isJoystick

Definition at line 335 of file Input.h.

set< void(*)(Uint8, JoyButton)> Input::mJoyButtonDownCallbacks = set<void (*)(Uint8, JoyButton)>()
staticprotected

list of callback functions for JoyButton press requiring which joystick passed the data

Definition at line 356 of file Input.h.

set< void(*)(Uint8, JoyButton)> Input::mJoyButtonUpCallbacks = set<void (*)(Uint8, JoyButton)>()
staticprotected

list of callback functions for JoyButton release requiring which joystick passed the data

Definition at line 359 of file Input.h.

Point2D Input::mJoyDPos
protected

Represents Dpad Joy pos.

Definition at line 377 of file Input.h.

Point2D Input::mJoyLeftPos
protected

Represents the axis values of a joystick.

Definition at line 371 of file Input.h.

set< void(*)(Uint8, JoyStick, Point2D)> Input::mJoyMoveCallbacks = set<void (*)(Uint8, JoyStick, Point2D)>()
staticprotected

list of callback functions for JoyStick movement requiring which joystick passed the data

Definition at line 362 of file Input.h.

Point2D Input::mJoyRightPos
protected

Represents JoyRight Joy pos.

Definition at line 374 of file Input.h.

vector<bool> Input::mJoyStates
protected

list of button's state (active if the input is a Joystick) Corresponds to joyButton

Definition at line 392 of file Input.h.

SDL_Joystick* Input::mJoystick
protected

Pointer to the Joystick.

Definition at line 368 of file Input.h.

set< void(*)(SDLKey, SDLMod)> Input::mKeyDownCallbacks = set<void (*)(SDLKey, SDLMod)>()
staticprotected

list of callback functions for Button press

Definition at line 341 of file Input.h.

map<SDLKey, pair<bool,SDLMod> > Input::mKeyStates
protected

list of key's state (active if the input is a keyboard)

Definition at line 383 of file Input.h.

set< void(*)(SDLKey, SDLMod)> Input::mKeyUpCallbacks = set<void (*)(SDLKey, SDLMod)>()
staticprotected

list of callback functions for Button release

Definition at line 344 of file Input.h.

vector<Uint32> Input::mLastJoyDoubleStates
protected

list of button's double clicked state (active if the input is a Joystick) Corresponds to joyButton

Definition at line 398 of file Input.h.

vector<Uint32> Input::mLastJoyStates
protected

list of button's state trigger times (active if the input is a Joystick) Corresponds to joyButton

Definition at line 395 of file Input.h.

map<SDLKey, Uint32> Input::mLastKeyDoubleStates
protected

list of key's state trigger times (for double click) (active if the input is a keyboard)

Definition at line 389 of file Input.h.

map<SDLKey, Uint32> Input::mLastKeyStates
protected

list of key's state trigger times (active if the input is a keyboard)

Definition at line 386 of file Input.h.

vector<MouseState> Input::mLastMouseDoubleStates
protected

list of mouse's doube click state

Definition at line 407 of file Input.h.

vector<MouseState> Input::mLastMouseStates
protected

list of mouse's state trigger times

Definition at line 404 of file Input.h.

vector<MouseState> Input::mMouseButtonStates
protected

list of mouse's state

Definition at line 401 of file Input.h.

set< void(*)(Uint8, Input::MouseState)> Input::mMouseDownCallbacks = set<void (*)(Uint8, MouseState)>()
staticprotected

list of callback functions for Mouse press

Definition at line 347 of file Input.h.

set< void(*)(Input::MouseState)> Input::mMouseMoveCallbacks = set<void (*)(MouseState)>()
staticprotected

list of callback functions for Mouse movement

Definition at line 353 of file Input.h.

Input::MouseState Input::mMouseState = MouseState()
staticprotected

Mouse state.

Definition at line 380 of file Input.h.

set< void(*)(Uint8, Input::MouseState)> Input::mMouseUpCallbacks = set<void (*)(Uint8, MouseState)>()
staticprotected

list of callback functions for Mouse release

Definition at line 350 of file Input.h.


The documentation for this class was generated from the following files: