HoverRace  2.0
Classes | Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Private Member Functions | Private Attributes | List of all members
HoverRace::Control::InputEventController Class Reference

Translates input events into actions. More...

#include <Controller.h>

Classes

struct  actions_t
 

Public Types

using ActionMap = std::map< int, ControlActionPtr >
 
enum  ActionMapId : size_t {
  ActionMapId::CAMERA = 0x01, ActionMapId::CONSOLE = 0x02, ActionMapId::CONSOLE_TOGGLE = 0x04, ActionMapId::MENU = 0x08,
  ActionMapId::PLAYER = 0x10
}
 
enum  axis_t {
  AXIS_X = 1, AXIS_Y, AXIS_Z, AXIS_WHEEL_X,
  AXIS_WHEEL_Y
}
 
using MouseClickActionPtr = std::shared_ptr< Action< mouseClickSignal_t, const Mouse::Click & >>
 
using MouseScrollActionPtr = std::shared_ptr< Action< mouseScrollSignal_t, const Mouse::Scroll & >>
 
using StringActionPtr = std::shared_ptr< Action< stringSignal_t, const std::string & >>
 
using TextControlActionPtr = std::shared_ptr< Action< textControlSignal_t, TextControl::key_t >>
 
using ValueActionPtr = std::shared_ptr< Action< valueSignal_t >>
 
using Vec2ActionPtr = std::shared_ptr< Action< vec2Signal_t, const Vec2 & >>
 
using VoidActionPtr = std::shared_ptr< Action< voidSignal_t >>
 

Public Member Functions

 InputEventController ()
 
 ~InputEventController ()
 
bool AddActionMap (const std::string &mapname, ActionMapId mapId)
 Add an action map into the current action map. More...
 
void AddCameraMaps ()
 Enable camera controls. More...
 
void AddConsoleMaps ()
 Enable console scroll controls. More...
 
void AddConsoleToggleMaps ()
 Enable console toggle. More...
 
void AddMenuMaps ()
 Enable menu controls. More...
 
void AddPlayerMaps (int numPlayers, MainCharacter::MainCharacter **mcs)
 Update player mappings to point to correct MainCharacter objects, then add them to the active action map. More...
 
void CaptureNextInput (int oldhash, std::string mapname)
 Capture the next user input event and reassign hash. More...
 
void ClearActionMap ()
 Clears all of the active control bindings. More...
 
void DisableCaptureInput ()
 Assign the next disabled hash to the current capture control. More...
 
ActionMapGetActionMap (std::string key)
 Return the map with the given key. More...
 
std::vector< std::string > GetAvailableMaps ()
 Return a vector containing the names of all the available maps. More...
 
void HandleEvent (int hash, int value)
 
VoidActionPtr Hotkey (const std::string &key)
 Assign an action to a hotkey. More...
 
bool IsCapturing ()
 Check if the controller is currently capturing input. More...
 
bool IsMapActive (ActionMapId id)
 Check if a control map is active. More...
 
void LoadCameraMap ()
 Set up camera controls. More...
 
void LoadConfig ()
 Load the configuration from the Config object. More...
 
void LoadConsoleMap ()
 Set up the console navigation controls. More...
 
void LoadConsoleToggleMap ()
 Set up the console toggle control. More...
 
void LoadMenuMap ()
 Set up menu controls. More...
 
bool OnKeyPressed (const SDL_KeyboardEvent &arg)
 
bool OnKeyReleased (const SDL_KeyboardEvent &arg)
 
bool OnMouseMoved (const SDL_MouseMotionEvent &evt)
 
bool OnMousePressed (const SDL_MouseButtonEvent &evt)
 
bool OnMouseReleased (const SDL_MouseButtonEvent &evt)
 
bool OnMouseWheel (const SDL_MouseWheelEvent &evt)
 
bool OnTextInput (const SDL_TextInputEvent &evt)
 
void ProcessInputEvent (const SDL_Event &evt)
 
void ReloadConfig ()
 Clear and reload entire configuration. More...
 
void SaveConfig ()
 Save the controller configuration to the Config object. More...
 
void StopCapture ()
 Exit capture mode. More...
 
SDL_Keycode StringToKey (const std::string &s)
 Convert a canonical key name into a keycode. More...
 

Static Public Member Functions

static int HashKeyboardEvent (const SDL_Keycode &arg)
 
static int HashMouseAxisEvent (axis_t axis, int direction)
 
static int HashMouseButtonEvent (const SDL_MouseButtonEvent &arg)
 
static std::string HashToString (int hash)
 Convert a hash into an internationalized string. More...
 

Public Attributes

struct HoverRace::Control::InputEventController::actions_t actions
 

Private Member Functions

int GetNextAvailableDisabledHash ()
 
void RebindKey (std::string mapname, int oldhash, int newhash)
 

Private Attributes

ActionMap actionMap
 We store several different action maps which we can choose from. More...
 
size_t activeMaps
 
std::map< std::string, ActionMapallActionMaps
 
std::string captureMap
 name of the map we are capturing for More...
 
bool captureNextInput
 
int captureOldHash
 stores the value of the hash we will be replacing when capturing input More...
 
std::unordered_map< SDL_Keycode, VoidActionPtrhotkeys
 
Vec2 mousePos
 Track most-recent mouse position for lookup later. More...
 
int nextAvailableDisabledHash
 

Detailed Description

Translates input events into actions.

The app class (the one running the SDL event loop), passes along input events to this class, which then fires the mapped action. Listeners can subscribe to the actions via the InputEventController::actions struct.

                          [Util::Config]
                               |
                               |
  [app] --(SDL_Event)--> [InputEventController] --(actions)--> [listeners]

Typically, to listen to an action, the listener must call the appropriate Add* function to enable the group of actions then call Action::Connect on the desired action to bind a function to the action. In subclasses of Client::Scene this is done in Client::Scene::AttachController.

Example:

  controller.AddCameraMaps();
  cameraZoomInConn = controller.actions.camera.zoomIn->Connect(
    std::bind(&GameScene::OnCameraZoom, this, 1));

Action::Connect returns a boost::signals2::connection. Be sure to call disconnect() on this connection to clean up. In subclasses of Client::Scene this is done in Client::Scene::DetachController.

Author
Ryan Curtin
Michael Imamura

Member Typedef Documentation

Member Enumeration Documentation

Enumerator
CAMERA 
CONSOLE 
CONSOLE_TOGGLE 
MENU 
PLAYER 
Enumerator
AXIS_X 
AXIS_Y 
AXIS_Z 
AXIS_WHEEL_X 
AXIS_WHEEL_Y 

Constructor & Destructor Documentation

HoverRace::Control::InputEventController::InputEventController ( )
HoverRace::Control::InputEventController::~InputEventController ( )

Member Function Documentation

bool HoverRace::Control::InputEventController::AddActionMap ( const std::string &  mapname,
ActionMapId  mapId 
)

Add an action map into the current action map.

The available maps are referenced by string. Maps include:

"player1" ... "player4"

Parameters
mapnameThe action map name.
mapIdThe action map ID.
Returns
false if the map is not found
void HoverRace::Control::InputEventController::AddCameraMaps ( )

Enable camera controls.

void HoverRace::Control::InputEventController::AddConsoleMaps ( )

Enable console scroll controls.

void HoverRace::Control::InputEventController::AddConsoleToggleMaps ( )

Enable console toggle.

void HoverRace::Control::InputEventController::AddMenuMaps ( )

Enable menu controls.

void HoverRace::Control::InputEventController::AddPlayerMaps ( int  numPlayers,
MainCharacter::MainCharacter **  mcs 
)

Update player mappings to point to correct MainCharacter objects, then add them to the active action map.

If nullptr is passed as any of the pointers that map will not be added. The "console-toggle" map will also be added.

Parameters
numPlayersThe number of players to update.
mcsA pointer to the list of MainCharacter objects.
void HoverRace::Control::InputEventController::CaptureNextInput ( int  oldhash,
std::string  mapname 
)

Capture the next user input event and reassign hash.

This function tells the InputEventController to capture the next user input event and assign the action currently residing at 'oldhash' to the hash of the new input. Behavior is undefined if there is no action assigned to the old hash, so don't screw it up! This is meant to be called by the control assignment dialog box.

Parameters
oldhashOld hash.
mapnameString representing the name of the map.
void HoverRace::Control::InputEventController::ClearActionMap ( )

Clears all of the active control bindings.

Does not delete the bindings but simply removes them from the active action map.

void HoverRace::Control::InputEventController::DisableCaptureInput ( )

Assign the next disabled hash to the current capture control.

Nothing will be done if the InputEventController is not in capture mode. This function will also disable capture mode.

InputEventController::ActionMap & HoverRace::Control::InputEventController::GetActionMap ( std::string  key)

Return the map with the given key.

vector< string > HoverRace::Control::InputEventController::GetAvailableMaps ( )

Return a vector containing the names of all the available maps.

int HoverRace::Control::InputEventController::GetNextAvailableDisabledHash ( )
private
void HoverRace::Control::InputEventController::HandleEvent ( int  hash,
int  value 
)
int HoverRace::Control::InputEventController::HashKeyboardEvent ( const SDL_Keycode &  arg)
static
int HoverRace::Control::InputEventController::HashMouseAxisEvent ( axis_t  axis,
int  direction 
)
static
int HoverRace::Control::InputEventController::HashMouseButtonEvent ( const SDL_MouseButtonEvent &  arg)
static
std::string HoverRace::Control::InputEventController::HashToString ( int  hash)
static

Convert a hash into an internationalized string.

Parameters
hashThe control hash, from one of the hash functions, e.g. HashKeyboardEvent.
Returns
The translated string.
InputEventController::VoidActionPtr HoverRace::Control::InputEventController::Hotkey ( const std::string &  key)

Assign an action to a hotkey.

Hotkey actions are activated in *addition* to any normal action assigned to the key.

Parameters
keyThe string representation of the key.
Returns
The action for the hotkey, or nullptr if the key is an invalid key for a hotkey.
bool HoverRace::Control::InputEventController::IsCapturing ( )

Check if the controller is currently capturing input.

It can be used to check whether or not an input has been captured.

Returns
true if in capture mode, false otherwise.
See also
CaptureNextInput(int, std::string)
bool HoverRace::Control::InputEventController::IsMapActive ( ActionMapId  id)
inline

Check if a control map is active.

Parameters
idThe map to look up.
Returns
true if active, false if inactive.
void HoverRace::Control::InputEventController::LoadCameraMap ( )

Set up camera controls.

void HoverRace::Control::InputEventController::LoadConfig ( )

Load the configuration from the Config object.

void HoverRace::Control::InputEventController::LoadConsoleMap ( )

Set up the console navigation controls.

void HoverRace::Control::InputEventController::LoadConsoleToggleMap ( )

Set up the console toggle control.

void HoverRace::Control::InputEventController::LoadMenuMap ( )

Set up menu controls.

bool HoverRace::Control::InputEventController::OnKeyPressed ( const SDL_KeyboardEvent &  arg)
bool HoverRace::Control::InputEventController::OnKeyReleased ( const SDL_KeyboardEvent &  arg)
bool HoverRace::Control::InputEventController::OnMouseMoved ( const SDL_MouseMotionEvent &  evt)
bool HoverRace::Control::InputEventController::OnMousePressed ( const SDL_MouseButtonEvent &  evt)
bool HoverRace::Control::InputEventController::OnMouseReleased ( const SDL_MouseButtonEvent &  evt)
bool HoverRace::Control::InputEventController::OnMouseWheel ( const SDL_MouseWheelEvent &  evt)
bool HoverRace::Control::InputEventController::OnTextInput ( const SDL_TextInputEvent &  evt)
void HoverRace::Control::InputEventController::ProcessInputEvent ( const SDL_Event &  evt)
void HoverRace::Control::InputEventController::RebindKey ( std::string  mapname,
int  oldhash,
int  newhash 
)
private
void HoverRace::Control::InputEventController::ReloadConfig ( )

Clear and reload entire configuration.

void HoverRace::Control::InputEventController::SaveConfig ( )

Save the controller configuration to the Config object.

void HoverRace::Control::InputEventController::StopCapture ( )

Exit capture mode.

SDL_Keycode HoverRace::Control::InputEventController::StringToKey ( const std::string &  s)

Convert a canonical key name into a keycode.

The canonical key name is used in scripts and isn't translated.

Parameters
sThe key name.
Returns
The keycode, or SDLK_UNKNOWN if the key name is unknown.

Member Data Documentation

ActionMap HoverRace::Control::InputEventController::actionMap
private

We store several different action maps which we can choose from.

They are referenced by string. See ClearActionMap(), AddActionMap().

struct HoverRace::Control::InputEventController::actions_t HoverRace::Control::InputEventController::actions
size_t HoverRace::Control::InputEventController::activeMaps
private
std::map<std::string, ActionMap> HoverRace::Control::InputEventController::allActionMaps
private
std::string HoverRace::Control::InputEventController::captureMap
private

name of the map we are capturing for

bool HoverRace::Control::InputEventController::captureNextInput
private
int HoverRace::Control::InputEventController::captureOldHash
private

stores the value of the hash we will be replacing when capturing input

std::unordered_map<SDL_Keycode, VoidActionPtr> HoverRace::Control::InputEventController::hotkeys
private
Vec2 HoverRace::Control::InputEventController::mousePos
private

Track most-recent mouse position for lookup later.

int HoverRace::Control::InputEventController::nextAvailableDisabledHash
private

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