Browse Source

input: some refactoring in the action bindings.

legacy
Sam Hocevar sam 12 years ago
parent
commit
956657e86f
2 changed files with 93 additions and 90 deletions
  1. +52
    -52
      src/input/input.cpp
  2. +41
    -38
      src/input/input.h

+ 52
- 52
src/input/input.cpp View File

@@ -31,7 +31,7 @@ namespace lol
* Input implementation class * Input implementation class
*/ */


InputTracker* Input::m_input_tracker = NULL;
InputTracker* Input::m_input_tracker = NULL;


static class InputData static class InputData
{ {
@@ -64,10 +64,10 @@ static InputData * const data = &inputdata;
* ButtonSetting class * ButtonSetting class
*/ */


int ButtonSetting::GetActionSettingIdx(ACTION_TYPE SearchAction)
int ButtonSetting::GetActionSettingIdx(Action a)
{ {
for (int i = 0; i < m_associated_action_list.Count(); i++) for (int i = 0; i < m_associated_action_list.Count(); i++)
if (ACTION_CMP(m_associated_action_list[i].m_action, SearchAction))
if (ACTION_CMP(m_associated_action_list[i].m_action, a))
return i; return i;
return -1; return -1;
} }
@@ -87,27 +87,27 @@ InputTracker::InputTracker()
} }


//Internal //Internal
int InputTracker::GetButtonSettingIdx(Key Button)
int InputTracker::GetButtonSettingIdx(Key k)
{ {
for (int i = 0; i < m_input_assocation_list.Count(); i++) for (int i = 0; i < m_input_assocation_list.Count(); i++)
if (m_input_assocation_list[i].m_raw_button == Button)
if (m_input_assocation_list[i].m_raw_button == k)
return i; return i;
return -1; return -1;
} }


//----- //-----
int InputTracker::GetCurrentButtonStatus(Key Button)
int InputTracker::GetCurrentButtonStatus(Key k)
{ {
if (Button < m_input_status.Count())
return m_input_status[Button];
if (k < m_input_status.Count())
return m_input_status[k];
return 0; return 0;
} }


//----- //-----
int InputTracker::GetPreviousButtonStatus(Key Button)
int InputTracker::GetPreviousButtonStatus(Key k)
{ {
if (Button < m_input_status.Count())
return m_input_status[(int)Button + (int)Key::Last];
if (k < m_input_status.Count())
return m_input_status[(int)k + (int)Key::Last];
return 0; return 0;
} }


@@ -147,44 +147,44 @@ void InputTracker::UpdateActionStatus(float seconds)
} }


//Helps link a software input Action-Id to an hardware input Button-Id. //Helps link a software input Action-Id to an hardware input Button-Id.
void InputTracker::LinkActionToKey(ACTION_TYPE Action, Key Button)
void InputTracker::LinkActionToKey(Action a, Key k)
{ {
int ITIdx = GetButtonSettingIdx(Button);
int ITIdx = GetButtonSettingIdx(k);
if (ITIdx == -1) if (ITIdx == -1)
{ {
ITIdx = m_input_assocation_list.Count(); ITIdx = m_input_assocation_list.Count();
m_input_assocation_list << ButtonSetting(Button);
m_input_assocation_list << ButtonSetting(k);
} }


ButtonSetting &CurIT = m_input_assocation_list[ITIdx]; ButtonSetting &CurIT = m_input_assocation_list[ITIdx];


int ASIdx = CurIT.GetActionSettingIdx(Action);
int ASIdx = CurIT.GetActionSettingIdx(a);
if (ASIdx == -1) if (ASIdx == -1)
{ {
ASIdx = CurIT.m_associated_action_list.Count(); ASIdx = CurIT.m_associated_action_list.Count();
CurIT.m_associated_action_list << ActionSetting(Action);
CurIT.m_associated_action_list << ActionSetting(a);
} }
} }


//Helps unlink a software input Action-Id to an hardware input Button-Id.
void InputTracker::UnlinkAction(ACTION_TYPE Action)
//Helps unlink a software input Action-Id to an hardware input k-Id.
void InputTracker::UnlinkAction(Action a)
{ {
for (int i = 0; i < m_input_assocation_list.Count(); i++) for (int i = 0; i < m_input_assocation_list.Count(); i++)
{ {
ButtonSetting &CurIT = m_input_assocation_list[i]; ButtonSetting &CurIT = m_input_assocation_list[i];
int ASIdx = CurIT.GetActionSettingIdx(Action);
int ASIdx = CurIT.GetActionSettingIdx(a);
if (ASIdx != -1) if (ASIdx != -1)
CurIT.m_associated_action_list.Remove(ASIdx); CurIT.m_associated_action_list.Remove(ASIdx);
} }
} }


//Returns the current status of a given action //Returns the current status of a given action
int InputTracker::GetStatus(ACTION_TYPE Action)
int InputTracker::GetStatus(Action a)
{ {
for (int i = 0; i < m_input_assocation_list.Count(); i++) for (int i = 0; i < m_input_assocation_list.Count(); i++)
{ {
ButtonSetting &CurIT = m_input_assocation_list[i]; ButtonSetting &CurIT = m_input_assocation_list[i];
int ASIdx = CurIT.GetActionSettingIdx(Action);
int ASIdx = CurIT.GetActionSettingIdx(a);
if (ASIdx != -1) if (ASIdx != -1)
{ {
ActionSetting &CurAS = CurIT.m_associated_action_list[ASIdx]; ActionSetting &CurAS = CurIT.m_associated_action_list[ASIdx];
@@ -198,12 +198,12 @@ int InputTracker::GetStatus(ACTION_TYPE Action)
} }


//Returns TRUE if action status went from Active to Inactive this frame //Returns TRUE if action status went from Active to Inactive this frame
bool InputTracker::WasReleased(ACTION_TYPE Action)
bool InputTracker::WasReleased(Action a)
{ {
for (int i = 0; i < m_input_assocation_list.Count(); i++) for (int i = 0; i < m_input_assocation_list.Count(); i++)
{ {
ButtonSetting &CurIT = m_input_assocation_list[i]; ButtonSetting &CurIT = m_input_assocation_list[i];
int ASIdx = CurIT.GetActionSettingIdx(Action);
int ASIdx = CurIT.GetActionSettingIdx(a);
if (ASIdx != -1) if (ASIdx != -1)
{ {


@@ -217,12 +217,12 @@ bool InputTracker::WasReleased(ACTION_TYPE Action)
} }


//Returns TRUE if action status went from Inactive to Active this frame //Returns TRUE if action status went from Inactive to Active this frame
bool InputTracker::WasPressed(ACTION_TYPE Action)
bool InputTracker::WasPressed(Action a)
{ {
for (int i = 0; i < m_input_assocation_list.Count(); i++) for (int i = 0; i < m_input_assocation_list.Count(); i++)
{ {
ButtonSetting &CurIT = m_input_assocation_list[i]; ButtonSetting &CurIT = m_input_assocation_list[i];
int ASIdx = CurIT.GetActionSettingIdx(Action);
int ASIdx = CurIT.GetActionSettingIdx(a);
if (ASIdx != -1) if (ASIdx != -1)
{ {
if (!GetPreviousButtonStatus(CurIT.m_raw_button) && if (!GetPreviousButtonStatus(CurIT.m_raw_button) &&
@@ -235,25 +235,25 @@ bool InputTracker::WasPressed(ACTION_TYPE Action)
} }


//Returns the current status of a given action //Returns the current status of a given action
int InputTracker::GetStatus(Key Button)
int InputTracker::GetStatus(Key k)
{ {
return GetCurrentButtonStatus(Button);
return GetCurrentButtonStatus(k);
} }


//Returns TRUE if action status went from Active to Inactive this frame //Returns TRUE if action status went from Active to Inactive this frame
bool InputTracker::WasReleased(Key Button)
bool InputTracker::WasReleased(Key k)
{ {
if (GetPreviousButtonStatus(Button) &&
!GetCurrentButtonStatus(Button))
if (GetPreviousButtonStatus(k) &&
!GetCurrentButtonStatus(k))
return true; return true;
return false; return false;
} }


//Returns TRUE if action status went from Inactive to Active this frame //Returns TRUE if action status went from Inactive to Active this frame
bool InputTracker::WasPressed(Key Button)
bool InputTracker::WasPressed(Key k)
{ {
if (!GetPreviousButtonStatus(Button) &&
GetCurrentButtonStatus(Button))
if (!GetPreviousButtonStatus(k) &&
GetCurrentButtonStatus(k))
return true; return true;
return false; return false;
} }
@@ -302,11 +302,11 @@ ivec3 Input::GetMouseButtons()
int Input::GetButtonState(int button) int Input::GetButtonState(int button)
{ {
#if defined USE_SDL #if defined USE_SDL
#if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION >= 3
# if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION >= 3
Uint8 *keystate = SDL_GetKeyboardState(NULL); Uint8 *keystate = SDL_GetKeyboardState(NULL);
#else
# else
Uint8 *keystate = SDL_GetKeyState(NULL); Uint8 *keystate = SDL_GetKeyState(NULL);
#endif
# endif
return keystate[button]; return keystate[button];
#else #else
return 0; return 0;
@@ -314,64 +314,64 @@ int Input::GetButtonState(int button)
} }


//Helps link a software input Action-Id to an hardware input Button-Id. //Helps link a software input Action-Id to an hardware input Button-Id.
void Input::LinkActionToKey(ACTION_TYPE Action, struct Key Button)
void Input::LinkActionToKey(Action a, Key k)
{ {
if (CheckInputTrackerInit()) if (CheckInputTrackerInit())
Input::m_input_tracker->LinkActionToKey(Action, Button);
Input::m_input_tracker->LinkActionToKey(a, k);
} }


//Helps unlink a software input Action-Id to an hardware input Button-Id. //Helps unlink a software input Action-Id to an hardware input Button-Id.
void Input::UnlinkAction(ACTION_TYPE Action)
void Input::UnlinkAction(Action a)
{ {
if (CheckInputTrackerInit()) if (CheckInputTrackerInit())
Input::m_input_tracker->UnlinkAction(Action);
Input::m_input_tracker->UnlinkAction(a);
} }


//Returns the current status of a given action //Returns the current status of a given action
int Input::GetStatus(ACTION_TYPE Action)
int Input::GetStatus(Action a)
{ {
if (CheckInputTrackerInit()) if (CheckInputTrackerInit())
return Input::m_input_tracker->GetStatus(Action);
return Input::m_input_tracker->GetStatus(a);
return 0; return 0;
} }


//Returns TRUE if action status when from Active to Inactive this frame //Returns TRUE if action status when from Active to Inactive this frame
bool Input::WasPressed(ACTION_TYPE Action)
bool Input::WasPressed(Action a)
{ {
if (CheckInputTrackerInit()) if (CheckInputTrackerInit())
return Input::m_input_tracker->WasPressed(Action);
return Input::m_input_tracker->WasPressed(a);
return false; return false;
} }


//Returns TRUE if action status when from Active to Inactive this frame //Returns TRUE if action status when from Active to Inactive this frame
bool Input::WasReleased(ACTION_TYPE Action)
bool Input::WasReleased(Action a)
{ {
if (CheckInputTrackerInit()) if (CheckInputTrackerInit())
return Input::m_input_tracker->WasReleased(Action);
return Input::m_input_tracker->WasReleased(a);
return false; return false;
} }


//Returns the current status of a given action //Returns the current status of a given action
int Input::GetStatus(Key Button)
int Input::GetStatus(Key k)
{ {
if (CheckInputTrackerInit()) if (CheckInputTrackerInit())
return Input::m_input_tracker->GetStatus(Button);
return Input::m_input_tracker->GetStatus(k);
return 0; return 0;
} }


//Returns TRUE if action status when from Active to Inactive this frame //Returns TRUE if action status when from Active to Inactive this frame
bool Input::WasPressed(Key Button)
bool Input::WasPressed(Key k)
{ {
if (CheckInputTrackerInit()) if (CheckInputTrackerInit())
return Input::m_input_tracker->WasPressed(Button);
return Input::m_input_tracker->WasPressed(k);
return false; return false;
} }


//Returns TRUE if action status when from Active to Inactive this frame //Returns TRUE if action status when from Active to Inactive this frame
bool Input::WasReleased(Key Button)
bool Input::WasReleased(Key k)
{ {
if (CheckInputTrackerInit()) if (CheckInputTrackerInit())
return Input::m_input_tracker->WasReleased(Button);
return Input::m_input_tracker->WasReleased(k);
return false; return false;
} }




+ 41
- 38
src/input/input.h View File

@@ -18,15 +18,17 @@


#include <cstring> #include <cstring>
#include <string.h> #include <string.h>

#include "core.h" #include "core.h"

#include "lol/math/vector.h" #include "lol/math/vector.h"
#include "input/stick.h" #include "input/stick.h"


namespace lol namespace lol
{ {


typedef std::string ACTION_TYPE;
static inline int ACTION_CMP(ACTION_TYPE a, ACTION_TYPE b) { return a.compare(b) == 0; }
typedef std::string Action;
static inline int ACTION_CMP(Action a, Action b) { return a.compare(b) == 0; }


class WorldEntity; class WorldEntity;


@@ -327,12 +329,12 @@ struct Key


struct ActionSetting struct ActionSetting
{ {
ACTION_TYPE m_action;
float m_buffering_time;
float m_buffered_since;
Action m_action;
float m_buffering_time;
float m_buffered_since;


ActionSetting(ACTION_TYPE NewAction) :
m_action(NewAction),
ActionSetting(Action NewAction)
: m_action(NewAction),
m_buffering_time(.0f), m_buffering_time(.0f),
m_buffered_since(.0f) m_buffered_since(.0f)
{ } { }
@@ -340,61 +342,62 @@ struct ActionSetting


struct ButtonSetting struct ButtonSetting
{ {
Key m_raw_button;
Array<ActionSetting> m_associated_action_list;
Key m_raw_button;
Array<ActionSetting> m_associated_action_list;


ButtonSetting(Key NewRawButton) ButtonSetting(Key NewRawButton)
: m_raw_button(NewRawButton) { }
int GetActionSettingIdx(ACTION_TYPE SearchAction);
: m_raw_button(NewRawButton)
{ }

int GetActionSettingIdx(Action SearchAction);
}; };


class InputTracker : public Entity class InputTracker : public Entity
{ {

friend class Input; friend class Input;


public: public:
InputTracker(); InputTracker();


private: private:
Array<uint8_t> m_input_status;
Array<ButtonSetting> m_input_assocation_list;
Array<uint8_t> m_input_status;
Array<ButtonSetting> m_input_assocation_list;


int GetButtonSettingIdx(struct Key Button);
int GetCurrentButtonStatus(struct Key Button);
int GetPreviousButtonStatus(struct Key Button);
void UpdateActionStatus(float seconds);
int GetButtonSettingIdx(struct Key k);
int GetCurrentButtonStatus(struct Key k);
int GetPreviousButtonStatus(struct Key k);
void UpdateActionStatus(float seconds);


protected: protected:
virtual char const * GetName()
virtual char const * GetName()
{ {
return "<InputTracker>"; return "<InputTracker>";
} }
virtual void TickGame(float seconds)
virtual void TickGame(float seconds)
{ {
Entity::TickGame(seconds); Entity::TickGame(seconds);


UpdateActionStatus(seconds); UpdateActionStatus(seconds);
} }


void LinkActionToKey(ACTION_TYPE Action, struct Key Button);
void UnlinkAction(ACTION_TYPE Action);
int GetStatus(ACTION_TYPE Action);
bool WasPressed(ACTION_TYPE Action);
bool WasReleased(ACTION_TYPE Action);
void LinkActionToKey(Action a, struct Key k);
void UnlinkAction(Action a);
int GetStatus(Action a);
bool WasPressed(Action a);
bool WasReleased(Action a);


//You should probably use the Action System //You should probably use the Action System
int GetStatus(Key Button);
bool WasPressed(Key Button);
bool WasReleased(Key Button);
int GetStatus(Key k);
bool WasPressed(Key k);
bool WasReleased(Key k);
}; };


class Input class Input
{ {
private: private:
static InputTracker* m_input_tracker;
static InputTracker* m_input_tracker;


static bool CheckInputTrackerInit()
static bool CheckInputTrackerInit()
{ {
if (Input::m_input_tracker) if (Input::m_input_tracker)
return true; return true;
@@ -413,16 +416,16 @@ public:
static int GetButtonState(int button); static int GetButtonState(int button);


/* Action management */ /* Action management */
static void LinkActionToKey(ACTION_TYPE Action, struct Key Button);
static void UnlinkAction(ACTION_TYPE Action);
static int GetStatus(ACTION_TYPE Action);
static bool WasPressed(ACTION_TYPE Action);
static bool WasReleased(ACTION_TYPE Action);
static void LinkActionToKey(Action a, struct Key k);
static void UnlinkAction(Action a);
static int GetStatus(Action a);
static bool WasPressed(Action a);
static bool WasReleased(Action a);


/* Raw Button management. You should use actions. */ /* Raw Button management. You should use actions. */
static int GetStatus(Key Button);
static bool WasPressed(Key Button);
static bool WasReleased(Key Button);
static int GetStatus(Key k);
static bool WasPressed(Key k);
static bool WasReleased(Key k);


/* Entities can subscribe to events */ /* Entities can subscribe to events */
static void TrackMouse(WorldEntity *e); static void TrackMouse(WorldEntity *e);


Loading…
Cancel
Save