Changeset 2924


Ignore:
Timestamp:
Sep 14, 2013, 8:35:48 PM (6 years ago)
Author:
benlitz
Message:

input: added functions to bind keys over axis (not tested)

Location:
trunk/src/input
Files:
2 edited

Legend:

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

    r2841 r2924  
    2323void KeyBinding::Bind(const char* device_name, const char* key_name)
    2424{
     25    ClearBinding();
     26
    2527    m_device = InputDevice::Get(device_name);
    2628    if (!m_device)
     
    3638        Log::Warn("Trying to bind controller to key %s.%s which doesn't exist", device_name, key_name);
    3739    }
     40}
     41
     42void KeyBinding::ClearBinding()
     43{
     44    m_keyindex = -1;
    3845}
    3946
     
    4350void AxisBinding::Bind(const char* device_name, const char* axis_name)
    4451{
     52    ClearBinding();
     53
    4554    m_device = InputDevice::Get(device_name);
    4655    if (!m_device)
     
    5867}
    5968
     69void AxisBinding::BindKey(const char* device_name, const char* key_name)
     70{
     71    ClearBinding();
     72
     73    m_device = InputDevice::Get(device_name);
     74    if (!m_device)
     75    {
     76        Log::Warn("Trying to bind controller to device %s which doesn't exist", device_name);
     77        return;
     78    }
     79
     80    m_maxkeyindex = m_device->GetKeyIndex(key_name);
     81
     82    if (m_maxkeyindex < 0)
     83    {
     84        Log::Warn("Trying to bind controller to key %s.%s which doesn't exist", device_name, key_name);
     85    }
     86}
     87
     88void AxisBinding::BindKeys(const char* device_name, const char* min_key_name, const char* max_key_name)
     89{
     90    ClearBinding();
     91    m_device = InputDevice::Get(device_name);
     92    if (!m_device)
     93    {
     94        Log::Warn("Trying to bind controller to device %s which doesn't exist", device_name);
     95        return;
     96    }
     97
     98    m_minkeyindex = m_device->GetKeyIndex(min_key_name);
     99    m_maxkeyindex = m_device->GetKeyIndex(max_key_name);
     100
     101    if (m_minkeyindex < 0)
     102    {
     103        Log::Warn("Trying to bind controller to key %s.%s which doesn't exist", device_name, min_key_name);
     104        ClearBinding();
     105    }
     106
     107    if (m_maxkeyindex < 0)
     108    {
     109        Log::Warn("Trying to bind controller to key %s.%s which doesn't exist", device_name, max_key_name);
     110        ClearBinding();
     111    }
     112}
     113
     114void AxisBinding::ClearBinding()
     115{
     116    m_axisindex = -1;
     117    m_minkeyindex = -1;
     118    m_maxkeyindex = -1;
     119}
     120
     121float AxisBinding::RetrieveCurrentValue()
     122{
     123    if (m_axisindex != -1)
     124    {
     125        return m_device->GetAxis(m_axisindex);
     126    }
     127
     128    float value = 0.0f;
     129    if (m_minkeyindex != -1)
     130        value += m_device->GetKey(m_minkeyindex) ? -1.0f : 0.0f;
     131    if (m_maxkeyindex != -1)
     132        value += m_device->GetKey(m_maxkeyindex) ? 1.0f : 0.0f;
     133
     134    return value;
     135}
    60136
    61137///////////////////////////////////////////////////////////////////////////////
  • trunk/src/input/controller.h

    r2841 r2924  
    2020{
    2121public:
    22     KeyBinding() : m_device(nullptr), m_current(false), m_previous(false) {}
     22    KeyBinding() : m_device(nullptr), m_current(false), m_previous(false), m_keyindex(-1) {}
    2323
     24    /** Indicate wheither the key is currently down */
    2425    bool IsDown() const { return m_current; }
     26    /** Indicate wheither the key is currently up */
    2527    bool IsUp() const { return !m_current; }
     28    /** Indicate wheither the key has just been pressed */
    2629    bool IsPressed() const { return m_current && !m_previous; }
     30    /** Indicate wheither the key has just been released */
    2731    bool IsReleased() const { return !m_current && m_previous; }
    2832
     33    /** Bind a physical device and key */
    2934    void Bind(const char* device_name, const char* key_name);
     35    /** Clear current binding */
     36    void ClearBinding();
    3037
     38    /** Indicate wheither a physical device and key has been bound */
    3139    bool IsBound() { return m_device && m_keyindex != -1; }
    3240
     
    4553{
    4654public:
    47     AxisBinding() : m_device(nullptr), m_current(0.0f), m_previous(0.0f) {}
     55    AxisBinding() : m_device(nullptr), m_current(0.0f), m_previous(0.0f), m_axisindex(-1), m_minkeyindex(-1), m_maxkeyindex(-1) {}
    4856
     57    /** Gets the current absolute value of this axis */
    4958    float GetValue() const { return m_current; }
     59    /** Gets the current delta value of this axis */
    5060    float GetDelta() const { return m_current - m_previous; }
    5161
     62    /** Bind a physical device and axis */
    5263    void Bind(const char* device_name, const char* axis_name);
     64    /** Bind a physical device and key over this axis. The axis value will be 0 if the key is up and 1 if it's down */
     65    void BindKey(const char* device_name, const char* key_name);
     66    /** Bind physical device and keys over this axis. The axis value will be 0 if both the key are up, -1 if minkey is down, and 1 if maxkey is down */
     67    void BindKeys(const char* device_name, const char* min_key_name, const char* max_key_name);
     68    /** Clear current binding */
     69    void ClearBinding();
    5370
    54     bool IsBound() { return m_device && m_axisindex != -1; }
     71    /** Indicate wheither a physical device and axis has been bound */
     72    bool IsBound() { return m_device && m_axisindex != -1 || m_maxkeyindex != -1; }
    5573
    5674protected:
    57     void Update() { m_previous = m_current; m_current = IsBound() ? m_device->GetAxis(m_axisindex) : 0.0f; }
     75    void Update() { m_previous = m_current; m_current = IsBound() ? RetrieveCurrentValue() : 0.0f; }
     76        float RetrieveCurrentValue();
    5877
    5978    const InputDevice* m_device;
    6079    int m_axisindex;
     80    int m_minkeyindex;
     81    int m_maxkeyindex;
    6182    float m_current;
    6283    float m_previous;
Note: See TracChangeset for help on using the changeset viewer.