Browse Source

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

undefined
Benlitz Sam Hocevar <sam@hocevar.net> 11 years ago
parent
commit
b3d306d22e
2 changed files with 101 additions and 4 deletions
  1. +76
    -0
      src/input/controller.cpp
  2. +25
    -4
      src/input/controller.h

+ 76
- 0
src/input/controller.cpp View File

@@ -22,6 +22,8 @@ namespace lol


void KeyBinding::Bind(const char* device_name, const char* key_name) void KeyBinding::Bind(const char* device_name, const char* key_name)
{ {
ClearBinding();

m_device = InputDevice::Get(device_name); m_device = InputDevice::Get(device_name);
if (!m_device) if (!m_device)
{ {
@@ -37,11 +39,18 @@ void KeyBinding::Bind(const char* device_name, const char* key_name)
} }
} }


void KeyBinding::ClearBinding()
{
m_keyindex = -1;
}

/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// AxisBinding // AxisBinding


void AxisBinding::Bind(const char* device_name, const char* axis_name) void AxisBinding::Bind(const char* device_name, const char* axis_name)
{ {
ClearBinding();

m_device = InputDevice::Get(device_name); m_device = InputDevice::Get(device_name);
if (!m_device) if (!m_device)
{ {
@@ -57,6 +66,73 @@ void AxisBinding::Bind(const char* device_name, const char* axis_name)
} }
} }


void AxisBinding::BindKey(const char* device_name, const char* key_name)
{
ClearBinding();

m_device = InputDevice::Get(device_name);
if (!m_device)
{
Log::Warn("Trying to bind controller to device %s which doesn't exist", device_name);
return;
}

m_maxkeyindex = m_device->GetKeyIndex(key_name);

if (m_maxkeyindex < 0)
{
Log::Warn("Trying to bind controller to key %s.%s which doesn't exist", device_name, key_name);
}
}

void AxisBinding::BindKeys(const char* device_name, const char* min_key_name, const char* max_key_name)
{
ClearBinding();
m_device = InputDevice::Get(device_name);
if (!m_device)
{
Log::Warn("Trying to bind controller to device %s which doesn't exist", device_name);
return;
}

m_minkeyindex = m_device->GetKeyIndex(min_key_name);
m_maxkeyindex = m_device->GetKeyIndex(max_key_name);

if (m_minkeyindex < 0)
{
Log::Warn("Trying to bind controller to key %s.%s which doesn't exist", device_name, min_key_name);
ClearBinding();
}

if (m_maxkeyindex < 0)
{
Log::Warn("Trying to bind controller to key %s.%s which doesn't exist", device_name, max_key_name);
ClearBinding();
}
}

void AxisBinding::ClearBinding()
{
m_axisindex = -1;
m_minkeyindex = -1;
m_maxkeyindex = -1;
}

float AxisBinding::RetrieveCurrentValue()
{
if (m_axisindex != -1)
{
return m_device->GetAxis(m_axisindex);
}

float value = 0.0f;
if (m_minkeyindex != -1)
value += m_device->GetKey(m_minkeyindex) ? -1.0f : 0.0f;
if (m_maxkeyindex != -1)
value += m_device->GetKey(m_maxkeyindex) ? 1.0f : 0.0f;

return value;
}


/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// Controller // Controller


+ 25
- 4
src/input/controller.h View File

@@ -19,15 +19,23 @@ namespace lol
class KeyBinding class KeyBinding
{ {
public: public:
KeyBinding() : m_device(nullptr), m_current(false), m_previous(false) {}
KeyBinding() : m_device(nullptr), m_current(false), m_previous(false), m_keyindex(-1) {}


/** Indicate wheither the key is currently down */
bool IsDown() const { return m_current; } bool IsDown() const { return m_current; }
/** Indicate wheither the key is currently up */
bool IsUp() const { return !m_current; } bool IsUp() const { return !m_current; }
/** Indicate wheither the key has just been pressed */
bool IsPressed() const { return m_current && !m_previous; } bool IsPressed() const { return m_current && !m_previous; }
/** Indicate wheither the key has just been released */
bool IsReleased() const { return !m_current && m_previous; } bool IsReleased() const { return !m_current && m_previous; }


/** Bind a physical device and key */
void Bind(const char* device_name, const char* key_name); void Bind(const char* device_name, const char* key_name);
/** Clear current binding */
void ClearBinding();


/** Indicate wheither a physical device and key has been bound */
bool IsBound() { return m_device && m_keyindex != -1; } bool IsBound() { return m_device && m_keyindex != -1; }


protected: protected:
@@ -44,20 +52,33 @@ protected:
class AxisBinding class AxisBinding
{ {
public: public:
AxisBinding() : m_device(nullptr), m_current(0.0f), m_previous(0.0f) {}
AxisBinding() : m_device(nullptr), m_current(0.0f), m_previous(0.0f), m_axisindex(-1), m_minkeyindex(-1), m_maxkeyindex(-1) {}


/** Gets the current absolute value of this axis */
float GetValue() const { return m_current; } float GetValue() const { return m_current; }
/** Gets the current delta value of this axis */
float GetDelta() const { return m_current - m_previous; } float GetDelta() const { return m_current - m_previous; }


/** Bind a physical device and axis */
void Bind(const char* device_name, const char* axis_name); void Bind(const char* device_name, const char* axis_name);
/** 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 */
void BindKey(const char* device_name, const char* key_name);
/** 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 */
void BindKeys(const char* device_name, const char* min_key_name, const char* max_key_name);
/** Clear current binding */
void ClearBinding();


bool IsBound() { return m_device && m_axisindex != -1; }
/** Indicate wheither a physical device and axis has been bound */
bool IsBound() { return m_device && m_axisindex != -1 || m_maxkeyindex != -1; }


protected: protected:
void Update() { m_previous = m_current; m_current = IsBound() ? m_device->GetAxis(m_axisindex) : 0.0f; }
void Update() { m_previous = m_current; m_current = IsBound() ? RetrieveCurrentValue() : 0.0f; }
float RetrieveCurrentValue();


const InputDevice* m_device; const InputDevice* m_device;
int m_axisindex; int m_axisindex;
int m_minkeyindex;
int m_maxkeyindex;
float m_current; float m_current;
float m_previous; float m_previous;




Loading…
Cancel
Save