From 9ba22b8a5850fa3d8f115d6cd8b6d2fa6db862be Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Tue, 16 Sep 2014 17:11:31 +0000 Subject: [PATCH] input: prepare a small API for direct text input. --- src/input/controller.cpp | 55 ++++++++++++++++++++++---------------- src/input/input.cpp | 7 +++++ src/input/input.h | 26 +++++++++++------- src/input/input_internal.h | 5 ++++ 4 files changed, 60 insertions(+), 33 deletions(-) diff --git a/src/input/controller.cpp b/src/input/controller.cpp index 913c1432..35c8fc61 100644 --- a/src/input/controller.cpp +++ b/src/input/controller.cpp @@ -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; diff --git a/src/input/input.cpp b/src/input/input.cpp index e23a8d7c..313e181c 100644 --- a/src/input/input.cpp +++ b/src/input/input.cpp @@ -28,6 +28,13 @@ array 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) diff --git a/src/input/input.h b/src/input/input.h index 3bebe4bb..35e8013e 100644 --- a/src/input/input.h +++ b/src/input/input.h @@ -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 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 m_axisnames; array m_cursornames; - /** key states (pressed/released) */ + /** Key states (pressed/released) */ array m_keys; - /** axis states (value and sensitivity) */ + /** Text input state */ + String m_text; + + /** Axis states (value and sensitivity) */ array m_axis; - /** cursor position */ + /** Cursor position */ array 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 devices; template - ptrdiff_t GetItemIndex(const char* name, const array& a) const + ptrdiff_t GetItemIndex(String const &name, const array& 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) { diff --git a/src/input/input_internal.h b/src/input/input_internal.h index 880dde6b..c0bdd9bb 100644 --- a/src/input/input_internal.h +++ b/src/input/input_internal.h @@ -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;