Changeset 1897


Ignore:
Timestamp:
Sep 9, 2012, 11:47:45 PM (7 years ago)
Author:
sam
Message:

input: some refactoring in the action bindings.

Location:
trunk/src/input
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/input/input.cpp

    r1888 r1897  
    3232 */
    3333
    34 InputTracker*            Input::m_input_tracker = NULL;
     34InputTracker* Input::m_input_tracker = NULL;
    3535
    3636static class InputData
     
    6565 */
    6666
    67 int ButtonSetting::GetActionSettingIdx(ACTION_TYPE SearchAction)
     67int ButtonSetting::GetActionSettingIdx(Action a)
    6868{
    6969    for (int i = 0; i < m_associated_action_list.Count(); i++)
    70         if (ACTION_CMP(m_associated_action_list[i].m_action, SearchAction))
     70        if (ACTION_CMP(m_associated_action_list[i].m_action, a))
    7171            return i;
    7272    return -1;
     
    8888
    8989//Internal
    90 int InputTracker::GetButtonSettingIdx(Key Button)
    91 {
    92     for (int i = 0; i < m_input_assocation_list.Count(); i++)
    93         if (m_input_assocation_list[i].m_raw_button == Button)
     90int InputTracker::GetButtonSettingIdx(Key k)
     91{
     92    for (int i = 0; i < m_input_assocation_list.Count(); i++)
     93        if (m_input_assocation_list[i].m_raw_button == k)
    9494            return i;
    9595    return -1;
     
    9797
    9898//-----
    99 int InputTracker::GetCurrentButtonStatus(Key Button)
    100 {
    101     if (Button < m_input_status.Count())
    102         return m_input_status[Button];
     99int InputTracker::GetCurrentButtonStatus(Key k)
     100{
     101    if (k < m_input_status.Count())
     102        return m_input_status[k];
    103103    return 0;
    104104}
    105105
    106106//-----
    107 int InputTracker::GetPreviousButtonStatus(Key Button)
    108 {
    109     if (Button < m_input_status.Count())
    110         return m_input_status[(int)Button + (int)Key::Last];
     107int InputTracker::GetPreviousButtonStatus(Key k)
     108{
     109    if (k < m_input_status.Count())
     110        return m_input_status[(int)k + (int)Key::Last];
    111111    return 0;
    112112}
     
    148148
    149149//Helps link a software input Action-Id to an hardware input Button-Id.
    150 void InputTracker::LinkActionToKey(ACTION_TYPE Action, Key Button)
    151 {
    152     int ITIdx = GetButtonSettingIdx(Button);
     150void InputTracker::LinkActionToKey(Action a, Key k)
     151{
     152    int ITIdx = GetButtonSettingIdx(k);
    153153    if (ITIdx == -1)
    154154    {
    155155        ITIdx = m_input_assocation_list.Count();
    156         m_input_assocation_list << ButtonSetting(Button);
     156        m_input_assocation_list << ButtonSetting(k);
    157157    }
    158158
    159159    ButtonSetting &CurIT = m_input_assocation_list[ITIdx];
    160160
    161     int ASIdx = CurIT.GetActionSettingIdx(Action);
     161    int ASIdx = CurIT.GetActionSettingIdx(a);
    162162    if (ASIdx == -1)
    163163    {
    164164        ASIdx = CurIT.m_associated_action_list.Count();
    165         CurIT.m_associated_action_list << ActionSetting(Action);
    166     }
    167 }
    168 
    169 //Helps unlink a software input Action-Id to an hardware input Button-Id.
    170 void InputTracker::UnlinkAction(ACTION_TYPE Action)
     165        CurIT.m_associated_action_list << ActionSetting(a);
     166    }
     167}
     168
     169//Helps unlink a software input Action-Id to an hardware input k-Id.
     170void InputTracker::UnlinkAction(Action a)
    171171{
    172172    for (int i = 0; i < m_input_assocation_list.Count(); i++)
    173173    {
    174174        ButtonSetting &CurIT = m_input_assocation_list[i];
    175         int ASIdx = CurIT.GetActionSettingIdx(Action);
     175        int ASIdx = CurIT.GetActionSettingIdx(a);
    176176        if (ASIdx != -1)
    177177            CurIT.m_associated_action_list.Remove(ASIdx);
     
    180180
    181181//Returns the current status of a given action
    182 int InputTracker::GetStatus(ACTION_TYPE Action)
     182int InputTracker::GetStatus(Action a)
    183183{
    184184    for (int i = 0; i < m_input_assocation_list.Count(); i++)
    185185    {
    186186        ButtonSetting &CurIT = m_input_assocation_list[i];
    187         int ASIdx = CurIT.GetActionSettingIdx(Action);
     187        int ASIdx = CurIT.GetActionSettingIdx(a);
    188188        if (ASIdx != -1)
    189189        {
     
    199199
    200200//Returns TRUE if action status went from Active to Inactive this frame
    201 bool InputTracker::WasReleased(ACTION_TYPE Action)
     201bool InputTracker::WasReleased(Action a)
    202202{
    203203    for (int i = 0; i < m_input_assocation_list.Count(); i++)
    204204    {
    205205        ButtonSetting &CurIT = m_input_assocation_list[i];
    206         int ASIdx = CurIT.GetActionSettingIdx(Action);
     206        int ASIdx = CurIT.GetActionSettingIdx(a);
    207207        if (ASIdx != -1)
    208208        {
     
    218218
    219219//Returns TRUE if action status went from Inactive to Active this frame
    220 bool InputTracker::WasPressed(ACTION_TYPE Action)
     220bool InputTracker::WasPressed(Action a)
    221221{
    222222    for (int i = 0; i < m_input_assocation_list.Count(); i++)
    223223    {
    224224        ButtonSetting &CurIT = m_input_assocation_list[i];
    225         int ASIdx = CurIT.GetActionSettingIdx(Action);
     225        int ASIdx = CurIT.GetActionSettingIdx(a);
    226226        if (ASIdx != -1)
    227227        {
     
    236236
    237237//Returns the current status of a given action
    238 int InputTracker::GetStatus(Key Button)
    239 {
    240     return GetCurrentButtonStatus(Button);
     238int InputTracker::GetStatus(Key k)
     239{
     240    return GetCurrentButtonStatus(k);
    241241}
    242242
    243243//Returns TRUE if action status went from Active to Inactive this frame
    244 bool InputTracker::WasReleased(Key Button)
    245 {
    246     if (GetPreviousButtonStatus(Button) &&
    247         !GetCurrentButtonStatus(Button))
     244bool InputTracker::WasReleased(Key k)
     245{
     246    if (GetPreviousButtonStatus(k) &&
     247        !GetCurrentButtonStatus(k))
    248248        return true;
    249249    return false;
     
    251251
    252252//Returns TRUE if action status went from Inactive to Active this frame
    253 bool InputTracker::WasPressed(Key Button)
    254 {
    255     if (!GetPreviousButtonStatus(Button) &&
    256         GetCurrentButtonStatus(Button))
     253bool InputTracker::WasPressed(Key k)
     254{
     255    if (!GetPreviousButtonStatus(k) &&
     256        GetCurrentButtonStatus(k))
    257257        return true;
    258258    return false;
     
    303303{
    304304#if defined USE_SDL
    305 #if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION >= 3
     305#   if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION >= 3
    306306    Uint8 *keystate = SDL_GetKeyboardState(NULL);
    307 #else
     307#   else
    308308    Uint8 *keystate = SDL_GetKeyState(NULL);
    309 #endif
     309#   endif
    310310    return keystate[button];
    311311#else
     
    315315
    316316//Helps link a software input Action-Id to an hardware input Button-Id.
    317 void Input::LinkActionToKey(ACTION_TYPE Action, struct Key Button)
    318 {
    319     if (CheckInputTrackerInit())
    320         Input::m_input_tracker->LinkActionToKey(Action, Button);
     317void Input::LinkActionToKey(Action a, Key k)
     318{
     319    if (CheckInputTrackerInit())
     320        Input::m_input_tracker->LinkActionToKey(a, k);
    321321}
    322322
    323323//Helps unlink a software input Action-Id to an hardware input Button-Id.
    324 void Input::UnlinkAction(ACTION_TYPE Action)
    325 {
    326     if (CheckInputTrackerInit())
    327         Input::m_input_tracker->UnlinkAction(Action);
     324void Input::UnlinkAction(Action a)
     325{
     326    if (CheckInputTrackerInit())
     327        Input::m_input_tracker->UnlinkAction(a);
    328328}
    329329
    330330//Returns the current status of a given action
    331 int Input::GetStatus(ACTION_TYPE Action)
    332 {
    333     if (CheckInputTrackerInit())
    334         return Input::m_input_tracker->GetStatus(Action);
     331int Input::GetStatus(Action a)
     332{
     333    if (CheckInputTrackerInit())
     334        return Input::m_input_tracker->GetStatus(a);
    335335    return 0;
    336336}
    337337
    338338//Returns TRUE if action status when from Active to Inactive this frame
    339 bool Input::WasPressed(ACTION_TYPE Action)
    340 {
    341     if (CheckInputTrackerInit())
    342         return Input::m_input_tracker->WasPressed(Action);
     339bool Input::WasPressed(Action a)
     340{
     341    if (CheckInputTrackerInit())
     342        return Input::m_input_tracker->WasPressed(a);
    343343    return false;
    344344}
    345345
    346346//Returns TRUE if action status when from Active to Inactive this frame
    347 bool Input::WasReleased(ACTION_TYPE Action)
    348 {
    349     if (CheckInputTrackerInit())
    350         return Input::m_input_tracker->WasReleased(Action);
     347bool Input::WasReleased(Action a)
     348{
     349    if (CheckInputTrackerInit())
     350        return Input::m_input_tracker->WasReleased(a);
    351351    return false;
    352352}
    353353
    354354//Returns the current status of a given action
    355 int Input::GetStatus(Key Button)
    356 {
    357     if (CheckInputTrackerInit())
    358         return Input::m_input_tracker->GetStatus(Button);
     355int Input::GetStatus(Key k)
     356{
     357    if (CheckInputTrackerInit())
     358        return Input::m_input_tracker->GetStatus(k);
    359359    return 0;
    360360}
    361361
    362362//Returns TRUE if action status when from Active to Inactive this frame
    363 bool Input::WasPressed(Key Button)
    364 {
    365     if (CheckInputTrackerInit())
    366         return Input::m_input_tracker->WasPressed(Button);
     363bool Input::WasPressed(Key k)
     364{
     365    if (CheckInputTrackerInit())
     366        return Input::m_input_tracker->WasPressed(k);
    367367    return false;
    368368}
    369369
    370370//Returns TRUE if action status when from Active to Inactive this frame
    371 bool Input::WasReleased(Key Button)
    372 {
    373     if (CheckInputTrackerInit())
    374         return Input::m_input_tracker->WasReleased(Button);
     371bool Input::WasReleased(Key k)
     372{
     373    if (CheckInputTrackerInit())
     374        return Input::m_input_tracker->WasReleased(k);
    375375    return false;
    376376}
  • trunk/src/input/input.h

    r1888 r1897  
    1919#include <cstring>
    2020#include <string.h>
     21
    2122#include "core.h"
     23
    2224#include "lol/math/vector.h"
    2325#include "input/stick.h"
     
    2628{
    2729
    28 typedef std::string ACTION_TYPE;
    29 static inline int ACTION_CMP(ACTION_TYPE a, ACTION_TYPE b) { return a.compare(b) == 0; }
     30typedef std::string Action;
     31static inline int ACTION_CMP(Action a, Action b) { return a.compare(b) == 0; }
    3032
    3133class WorldEntity;
     
    328330struct ActionSetting
    329331{
    330     ACTION_TYPE                m_action;
    331     float                    m_buffering_time;
    332     float                    m_buffered_since;
    333 
    334     ActionSetting(ACTION_TYPE NewAction) :
    335         m_action(NewAction),
     332    Action m_action;
     333    float m_buffering_time;
     334    float m_buffered_since;
     335
     336    ActionSetting(Action NewAction)
     337      : m_action(NewAction),
    336338        m_buffering_time(.0f),
    337339        m_buffered_since(.0f)
     
    341343struct ButtonSetting
    342344{
    343     Key                        m_raw_button;
    344     Array<ActionSetting>    m_associated_action_list;
     345    Key m_raw_button;
     346    Array<ActionSetting> m_associated_action_list;
    345347
    346348    ButtonSetting(Key NewRawButton)
    347         : m_raw_button(NewRawButton) { }
    348     int GetActionSettingIdx(ACTION_TYPE SearchAction);
     349      : m_raw_button(NewRawButton)
     350    { }
     351
     352    int GetActionSettingIdx(Action SearchAction);
    349353};
    350354
    351355class InputTracker : public Entity
    352356{
    353 
    354357    friend class Input;
    355358
     
    358361
    359362private:
    360     Array<uint8_t>            m_input_status;
    361     Array<ButtonSetting>    m_input_assocation_list;
    362 
    363     int                        GetButtonSettingIdx(struct Key Button);
    364     int                        GetCurrentButtonStatus(struct Key Button);
    365     int                        GetPreviousButtonStatus(struct Key Button);
    366     void                    UpdateActionStatus(float seconds);
     363    Array<uint8_t> m_input_status;
     364    Array<ButtonSetting> m_input_assocation_list;
     365
     366    int GetButtonSettingIdx(struct Key k);
     367    int GetCurrentButtonStatus(struct Key k);
     368    int GetPreviousButtonStatus(struct Key k);
     369    void UpdateActionStatus(float seconds);
    367370
    368371protected:
    369     virtual char const *    GetName()
     372    virtual char const * GetName()
    370373    {
    371374        return "<InputTracker>";
    372375    }
    373     virtual void            TickGame(float seconds)
     376    virtual void TickGame(float seconds)
    374377    {
    375378        Entity::TickGame(seconds);
     
    378381    }
    379382
    380     void                    LinkActionToKey(ACTION_TYPE Action, struct Key Button);
    381     void                    UnlinkAction(ACTION_TYPE Action);
    382     int                        GetStatus(ACTION_TYPE Action);
    383     bool                    WasPressed(ACTION_TYPE Action);
    384     bool                    WasReleased(ACTION_TYPE Action);
     383    void LinkActionToKey(Action a, struct Key k);
     384    void UnlinkAction(Action a);
     385    int GetStatus(Action a);
     386    bool WasPressed(Action a);
     387    bool WasReleased(Action a);
    385388
    386389    //You should probably use the Action System
    387     int                        GetStatus(Key Button);
    388     bool                    WasPressed(Key Button);
    389     bool                    WasReleased(Key Button);
     390    int GetStatus(Key k);
     391    bool WasPressed(Key k);
     392    bool WasReleased(Key k);
    390393};
    391394
     
    393396{
    394397private:
    395     static InputTracker*        m_input_tracker;
    396 
    397     static bool                    CheckInputTrackerInit()
     398    static InputTracker* m_input_tracker;
     399
     400    static bool CheckInputTrackerInit()
    398401    {
    399402        if (Input::m_input_tracker)
     
    414417
    415418    /* Action management */
    416     static void LinkActionToKey(ACTION_TYPE Action, struct Key Button);
    417     static void UnlinkAction(ACTION_TYPE Action);
    418     static int GetStatus(ACTION_TYPE Action);
    419     static bool    WasPressed(ACTION_TYPE Action);
    420     static bool WasReleased(ACTION_TYPE Action);
     419    static void LinkActionToKey(Action a, struct Key k);
     420    static void UnlinkAction(Action a);
     421    static int GetStatus(Action a);
     422    static bool WasPressed(Action a);
     423    static bool WasReleased(Action a);
    421424
    422425    /* Raw Button management. You should use actions. */
    423     static int GetStatus(Key Button);
    424     static bool    WasPressed(Key Button);
    425     static bool WasReleased(Key Button);
     426    static int GetStatus(Key k);
     427    static bool WasPressed(Key k);
     428    static bool WasReleased(Key k);
    426429
    427430    /* Entities can subscribe to events */
Note: See TracChangeset for help on using the changeset viewer.