Browse Source

input: prepare a small API for direct text input.

undefined
Sam Hocevar 10 years ago
parent
commit
9ba22b8a58
4 changed files with 60 additions and 33 deletions
  1. +32
    -23
      src/input/controller.cpp
  2. +7
    -0
      src/input/input.cpp
  3. +16
    -10
      src/input/input.h
  4. +5
    -0
      src/input/input_internal.h

+ 32
- 23
src/input/controller.cpp View File

@@ -18,18 +18,20 @@ namespace lol

void KeyBinding::Bind(const String& device_name, const String& key_name)
{
const InputDevice* device = InputDevice::Get(device_name.C());
const InputDevice* device = InputDevice::Get(device_name);

if (!device)
{
Log::Warn("Trying to bind key to nonexistent device %s", device_name.C());
Log::Warn("Trying to bind key to nonexistent input device %s",
device_name.C());
return;
}

int keyindex = device->GetKeyIndex(key_name.C());
int keyindex = device->GetKeyIndex(key_name);
if (keyindex < 0)
{
Log::Warn("Trying to bind nonexistent key %s.%s", device_name.C(), key_name.C());
Log::Warn("Trying to bind nonexistent key %s.%s",
device_name.C(), key_name.C());
return;
}

@@ -42,7 +44,7 @@ bool KeyBinding::Unbind(const String& device_name, const String& key_name)
{
if (m_keybindings[i].m1->GetName() == device_name)
{
if (m_keybindings[i].m2 == m_keybindings[i].m1->GetKeyIndex(key_name.C()))
if (m_keybindings[i].m2 == m_keybindings[i].m1->GetKeyIndex(key_name))
{
m_keybindings.Remove(i);
return true;
@@ -62,17 +64,19 @@ void KeyBinding::ClearBindings()

void AxisBinding::Bind(const String& device_name, const String& axis_name)
{
const InputDevice* device = InputDevice::Get(device_name.C());
const InputDevice* device = InputDevice::Get(device_name);
if (!device)
{
Log::Warn("Trying to bind axis to nonexistent device %s", device_name.C());
Log::Warn("Trying to bind axis to nonexistent input device %s",
device_name.C());
return;
}

int axisindex = device->GetAxisIndex(axis_name.C());
int axisindex = device->GetAxisIndex(axis_name);
if (axisindex < 0)
{
Log::Warn("Trying to bind nonexistent axis %s.%s", device_name.C(), axis_name.C());
Log::Warn("Trying to bind nonexistent axis %s.%s",
device_name.C(), axis_name.C());
return;
}

@@ -81,17 +85,19 @@ void AxisBinding::Bind(const String& device_name, const String& axis_name)

void AxisBinding::BindKey(const String& device_name, const String& key_name)
{
const InputDevice* device = InputDevice::Get(device_name.C());
const InputDevice* device = InputDevice::Get(device_name);
if (!device)
{
Log::Warn("Trying to bind axis key to nonexistent device %s", device_name.C());
Log::Warn("Trying to bind axis key to nonexistent input device %s",
device_name.C());
return;
}

int keyindex = device->GetKeyIndex(key_name.C());
int keyindex = device->GetKeyIndex(key_name);
if (keyindex < 0)
{
Log::Warn("Trying to bind nonexistent axis key %s.%s", device_name.C(), key_name.C());
Log::Warn("Trying to bind nonexistent axis key %s.%s",
device_name.C(), key_name.C());
return;
}

@@ -100,24 +106,27 @@ void AxisBinding::BindKey(const String& device_name, const String& key_name)

void AxisBinding::BindKeys(const String& device_name, const String& min_key_name, const String& max_key_name)
{
const InputDevice* device = InputDevice::Get(device_name.C());
const InputDevice* device = InputDevice::Get(device_name);
if (!device)
{
Log::Warn("Trying to bind axis keys to nonexistent device %s", device_name.C());
Log::Warn("Trying to bind axis keys to nonexistent input device %s",
device_name.C());
return;
}

int minkeyindex = device->GetKeyIndex(min_key_name.C());
int minkeyindex = device->GetKeyIndex(min_key_name);
if (minkeyindex < 0)
{
Log::Warn("Trying to bind nonexistent axis key %s.%s", device_name.C(), min_key_name.C());
Log::Warn("Trying to bind nonexistent axis key %s.%s",
device_name.C(), min_key_name.C());
return;
}

int maxkeyindex = device->GetKeyIndex(max_key_name.C());
int maxkeyindex = device->GetKeyIndex(max_key_name);
if (maxkeyindex < 0)
{
Log::Warn("Trying to bind nonexistent axis key %s.%s", device_name.C(), max_key_name.C());
Log::Warn("Trying to bind nonexistent axis key %s.%s",
device_name.C(), max_key_name.C());
return;
}

@@ -130,7 +139,7 @@ bool AxisBinding::Unbind(const String& device_name, const String& axis_name)
{
if (m_axisbindings[i].m1->GetName() == device_name)
{
if (m_axisbindings[i].m2 == m_axisbindings[i].m1->GetAxisIndex(axis_name.C()))
if (m_axisbindings[i].m2 == m_axisbindings[i].m1->GetAxisIndex(axis_name))
{
m_axisbindings.Remove(i);
return true;
@@ -146,7 +155,7 @@ bool AxisBinding::UnbindKey(const String& device_name, const String& key_name)
{
if (m_keybindings[i].m1->GetName() == device_name)
{
if (m_keybindings[i].m2 == -1 && m_keybindings[i].m3 == m_keybindings[i].m1->GetKeyIndex(key_name.C()))
if (m_keybindings[i].m2 == -1 && m_keybindings[i].m3 == m_keybindings[i].m1->GetKeyIndex(key_name))
{
m_keybindings.Remove(i);
return true;
@@ -162,8 +171,8 @@ bool AxisBinding::UnbindKeys(const String& device_name, const String& min_key_na
{
if (m_keybindings[i].m1->GetName() == device_name)
{
if (m_keybindings[i].m2 == m_keybindings[i].m1->GetKeyIndex(min_key_name.C())
&& m_keybindings[i].m3 == m_keybindings[i].m1->GetKeyIndex(max_key_name.C()))
if (m_keybindings[i].m2 == m_keybindings[i].m1->GetKeyIndex(min_key_name)
&& m_keybindings[i].m3 == m_keybindings[i].m1->GetKeyIndex(max_key_name))
{
m_keybindings.Remove(i);
return true;


+ 7
- 0
src/input/input.cpp View File

@@ -28,6 +28,13 @@ array<String> InputDevice::GetAvailableDevices()
return result;
}

String InputDevice::GetText()
{
String ret = m_text;
m_text = "";
return ret;
}

void InputDeviceInternal::AddKey(int index, const char* name)
{
if (index == -1)


+ 16
- 10
src/input/input.h View File

@@ -26,19 +26,19 @@ public:
}

/** Gets the index of the corresponding key, needed to call GetKey */
ptrdiff_t GetKeyIndex(const char* name) const
ptrdiff_t GetKeyIndex(String const &name) const
{
return GetItemIndex(name, m_keynames);
}

/** Gets the index of the corresponding axis, needed to call GetAxis */
ptrdiff_t GetAxisIndex(const char* name) const
ptrdiff_t GetAxisIndex(String const &name) const
{
return GetItemIndex(name, m_axisnames);
}

/** Gets the index of the corresponding cursor, needed to call GetCursor */
ptrdiff_t GetCursorIndex(const char* name) const
ptrdiff_t GetCursorIndex(String const &name) const
{
return GetItemIndex(name, m_cursornames);
}
@@ -50,6 +50,9 @@ public:
return m_keys[index];
}

/** Gets the latest contents of text input. */
String GetText();

/** Gets the current value of the given axis. Devices should try to
* clamp this value between -1 and 1, though it is not guaranteed. */
float GetAxis(ptrdiff_t index) const
@@ -107,7 +110,7 @@ public:
static array<String> GetAvailableDevices();

/** Gets an input device by its name */
static InputDevice* Get(const char* name)
static InputDevice* Get(String const &name)
{
return GetDevice(name);
}
@@ -127,18 +130,21 @@ protected:
array<String> m_axisnames;
array<String> m_cursornames;

/** key states (pressed/released) */
/** Key states (pressed/released) */
array<bool> m_keys;

/** axis states (value and sensitivity) */
/** Text input state */
String m_text;

/** Axis states (value and sensitivity) */
array<float, float> m_axis;

/** cursor position */
/** Cursor position */
array<vec2, ivec2> m_cursors;

static bool m_capturemouse;

InputDevice(const char* name) : m_name(name)
InputDevice(String const &name) : m_name(name)
{
devices.Push(this);
}
@@ -159,7 +165,7 @@ private:
static array<InputDevice*> devices;

template <typename... T>
ptrdiff_t GetItemIndex(const char* name, const array<String, T...>& a) const
ptrdiff_t GetItemIndex(String const &name, const array<String, T...>& a) const
{
for (ptrdiff_t i = 0; i < a.Count(); ++i)
{
@@ -169,7 +175,7 @@ private:
return -1;
}

static InputDevice* GetDevice(const char* name)
static InputDevice* GetDevice(String const &name)
{
for (ptrdiff_t i = 0; i < devices.Count(); ++i)
{


+ 5
- 0
src/input/input_internal.h View File

@@ -47,6 +47,11 @@ public:
m_keys[id] = state;
}

void AddText(String const &text)
{
m_text += text;
}

void SetAxis(int id, float value)
{
m_axis[id].m1 = value;


Loading…
Cancel
Save