From 747da72c4445a6f0cc350cfecb86b5c030b6cc73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20=E2=80=98Touky=E2=80=99=20Huet?= Date: Mon, 7 Oct 2013 15:38:46 +0000 Subject: [PATCH] Input : Small refactor, for clarity, easiness and beautification. Constants : Added minus one. --- src/input/controller.cpp | 62 +++++++++++++++++------------------ src/input/controller.h | 28 +++++++++++----- src/input/input.cpp | 6 ++-- src/input/input.h | 3 ++ src/input/input_internal.h | 1 + src/lol/math/vector.h | 3 ++ src/lolcore.vcxproj | 26 ++++++++++++++- src/lolcore.vcxproj.filters | 28 ++++++++++++++++ src/math/constants.cpp | 3 ++ src/platform/sdl/sdlinput.cpp | 24 +++++++------- 10 files changed, 129 insertions(+), 55 deletions(-) diff --git a/src/input/controller.cpp b/src/input/controller.cpp index 33dedf37..ba4f2cde 100644 --- a/src/input/controller.cpp +++ b/src/input/controller.cpp @@ -20,33 +20,33 @@ namespace lol /////////////////////////////////////////////////////////////////////////////// // KeyBinding -void KeyBinding::Bind(const char* device_name, const char* key_name) +void KeyBinding::Bind(const String& device_name, const String& key_name) { - const InputDevice* device = InputDevice::Get(device_name); + const InputDevice* device = InputDevice::Get(device_name.C()); if (!device) { - Log::Warn("Trying to bind controller to device %s which doesn't exist", device_name); + Log::Warn("Trying to bind controller to device %s which doesn't exist", device_name.C()); return; } - int keyindex = device->GetKeyIndex(key_name); + int keyindex = device->GetKeyIndex(key_name.C()); if (keyindex < 0) { - Log::Warn("Trying to bind controller to key %s.%s which doesn't exist", device_name, key_name); + Log::Warn("Trying to bind controller to key %s.%s which doesn't exist", device_name.C(), key_name.C()); return; } m_keybindings.Push(device, keyindex); } -bool KeyBinding::Unbind(const char* device_name, const char* key_name) +bool KeyBinding::Unbind(const String& device_name, const String& key_name) { for (int i = 0; i < m_keybindings.Count(); ++i) { if (m_keybindings[i].m1->GetName() == device_name) { - if (m_keybindings[i].m2 == m_keybindings[i].m1->GetKeyIndex(key_name)) + if (m_keybindings[i].m2 == m_keybindings[i].m1->GetKeyIndex(key_name.C())) { m_keybindings.Remove(i); return true; @@ -64,77 +64,77 @@ void KeyBinding::ClearBindings() /////////////////////////////////////////////////////////////////////////////// // AxisBinding -void AxisBinding::Bind(const char* device_name, const char* axis_name) +void AxisBinding::Bind(const String& device_name, const String& axis_name) { - const InputDevice* device = InputDevice::Get(device_name); + const InputDevice* device = InputDevice::Get(device_name.C()); if (!device) { - Log::Warn("Trying to bind controller to device %s which doesn't exist", device_name); + Log::Warn("Trying to bind controller to device %s which doesn't exist", device_name.C()); return; } - int axisindex = device->GetAxisIndex(axis_name); + int axisindex = device->GetAxisIndex(axis_name.C()); if (axisindex < 0) { - Log::Warn("Trying to bind controller to axis %s.%s which doesn't exist", device_name, axis_name); + Log::Warn("Trying to bind controller to axis %s.%s which doesn't exist", device_name.C(), axis_name.C()); return; } m_axisbindings.Push(device, axisindex); } -void AxisBinding::BindKey(const char* device_name, const char* key_name) +void AxisBinding::BindKey(const String& device_name, const String& key_name) { - const InputDevice* device = InputDevice::Get(device_name); + const InputDevice* device = InputDevice::Get(device_name.C()); if (!device) { - Log::Warn("Trying to bind controller to device %s which doesn't exist", device_name); + Log::Warn("Trying to bind controller to device %s which doesn't exist", device_name.C()); return; } - int keyindex = device->GetKeyIndex(key_name); + int keyindex = device->GetKeyIndex(key_name.C()); if (keyindex < 0) { - Log::Warn("Trying to bind controller to key %s.%s which doesn't exist", device_name, key_name); + Log::Warn("Trying to bind controller to key %s.%s which doesn't exist", device_name.C(), key_name.C()); return; } m_keybindings.Push(device, -1, keyindex); } -void AxisBinding::BindKeys(const char* device_name, const char* min_key_name, const char* max_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); + const InputDevice* device = InputDevice::Get(device_name.C()); if (!device) { - Log::Warn("Trying to bind controller to device %s which doesn't exist", device_name); + Log::Warn("Trying to bind controller to device %s which doesn't exist", device_name.C()); return; } - int minkeyindex = device->GetKeyIndex(min_key_name); + int minkeyindex = device->GetKeyIndex(min_key_name.C()); if (minkeyindex < 0) { - Log::Warn("Trying to bind controller to key %s.%s which doesn't exist", device_name, min_key_name); + Log::Warn("Trying to bind controller to key %s.%s which doesn't exist", device_name, min_key_name.C()); return; } - int maxkeyindex = device->GetKeyIndex(max_key_name); + int maxkeyindex = device->GetKeyIndex(max_key_name.C()); if (maxkeyindex < 0) { - Log::Warn("Trying to bind controller to key %s.%s which doesn't exist", device_name, max_key_name); + Log::Warn("Trying to bind controller to key %s.%s which doesn't exist", device_name, max_key_name.C()); return; } m_keybindings.Push(device, minkeyindex, maxkeyindex); } -bool AxisBinding::Unbind(const char* device_name, const char* axis_name) +bool AxisBinding::Unbind(const String& device_name, const String& axis_name) { for (int i = 0; i < m_keybindings.Count(); ++i) { if (m_axisbindings[i].m1->GetName() == device_name) { - if (m_axisbindings[i].m2 == m_axisbindings[i].m1->GetAxisIndex(axis_name)) + if (m_axisbindings[i].m2 == m_axisbindings[i].m1->GetAxisIndex(axis_name.C())) { m_axisbindings.Remove(i); return true; @@ -144,13 +144,13 @@ bool AxisBinding::Unbind(const char* device_name, const char* axis_name) return false; } -bool AxisBinding::UnbindKey(const char* device_name, const char* key_name) +bool AxisBinding::UnbindKey(const String& device_name, const String& key_name) { for (int i = 0; i < m_keybindings.Count(); ++i) { 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)) + if (m_keybindings[i].m2 == -1 && m_keybindings[i].m3 == m_keybindings[i].m1->GetKeyIndex(key_name.C())) { m_keybindings.Remove(i); return true; @@ -160,14 +160,14 @@ bool AxisBinding::UnbindKey(const char* device_name, const char* key_name) return false; } -bool AxisBinding::UnbindKeys(const char* device_name, const char* min_key_name, const char* max_key_name) +bool AxisBinding::UnbindKeys(const String& device_name, const String& min_key_name, const String& max_key_name) { for (int i = 0; i < m_keybindings.Count(); ++i) { if (m_keybindings[i].m1->GetName() == device_name) { - 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)) + 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())) { m_keybindings.Remove(i); return true; diff --git a/src/input/controller.h b/src/input/controller.h index b7e6db21..c9fbcd77 100644 --- a/src/input/controller.h +++ b/src/input/controller.h @@ -34,9 +34,14 @@ public: 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 String& device_name, const String& key_name); /** Unbind a previously bound physical device and key. Returns true if the binding was existing. */ - bool Unbind(const char* device_name, const char* key_name); + bool Unbind(const String& device_name, const String& key_name); + /* Small helpers */ + void BindMouse(const String& key_name) { Bind(g_name_mouse, key_name); } + void BindKeyboard(const String& key_name) { Bind(g_name_keyboard, key_name); } + bool UnbindMouse(const String& key_name) { return Unbind(g_name_mouse, key_name); } + bool UnbindKeyboard(const String& key_name) { return Unbind(g_name_mouse, key_name); } /** Clear current binding */ void ClearBindings(); /** Indicate wheither a physical device and key has been bound. Returns the number of bindings set. */ @@ -78,17 +83,24 @@ public: 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 String& device_name, const String& 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); + void BindKey(const String& device_name, const String& 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); + void BindKeys(const String& device_name, const String& min_key_name, const String& max_key_name); /** Unbind a previously bound physical device and axis. Returns true if the binding was existing. */ - bool Unbind(const char* device_name, const char* axis_name); + bool Unbind(const String& device_name, const String& axis_name); /** Unbind a previously bound physical device and axis. Returns true if the binding was existing. */ - bool UnbindKey(const char* device_name, const char* key_name); + bool UnbindKey(const String& device_name, const String& key_name); /** Unbind a previously bound physical device and axis. Returns true if the binding was existing. */ - bool UnbindKeys(const char* device_name, const char* min_key_name, const char* max_key_name); + bool UnbindKeys(const String& device_name, const String& min_key_name, const String& max_key_name); + /* Small helpers */ + void BindMouse(const String& axis_name) { Bind(g_name_mouse, axis_name); } + void BindMouseKey(const String& key_name) { BindKey(g_name_mouse, key_name); } + void BindMouseKeys(const String& min_key_name, const String& max_key_name) { BindKeys(g_name_mouse, min_key_name, max_key_name); } + bool UnbindMouse(const String& axis_name) { return Unbind(g_name_mouse, axis_name); } + bool UnbindMouseKey(const String& key_name) { return UnbindKey(g_name_mouse, key_name); } + bool UnbindMouseKeys(const String& min_key_name, const String& max_key_name){ return UnbindKeys(g_name_mouse, min_key_name, max_key_name); } /** Clear current binding */ void ClearBindings(); /** Indicate wheither a physical device and axis has been bound. Returns the number of bindings set. */ diff --git a/src/input/input.cpp b/src/input/input.cpp index d492882c..06c3e1f3 100644 --- a/src/input/input.cpp +++ b/src/input/input.cpp @@ -52,7 +52,7 @@ void InputDeviceInternal::AddCursor(const char* name) InputDeviceInternal* InputDeviceInternal::CreateStandardKeyboard() { - InputDeviceInternal* keyboard = new InputDeviceInternal("Keyboard"); + InputDeviceInternal* keyboard = new InputDeviceInternal(g_name_keyboard.C()); /* "value" is unused, what matters is the index. */ # define KEY_FUNC(key, value) \ keyboard->AddKey(#key); @@ -63,10 +63,12 @@ InputDeviceInternal* InputDeviceInternal::CreateStandardKeyboard() InputDeviceInternal* InputDeviceInternal::CreateStandardMouse() { - InputDeviceInternal* mouse = new InputDeviceInternal("Mouse"); + InputDeviceInternal* mouse = new InputDeviceInternal(g_name_mouse.C()); mouse->AddKey("Left"); mouse->AddKey("Middle"); mouse->AddKey("Right"); + //Added to manage if mouse is in the screen or not. + mouse->AddKey("InScreen"); mouse->AddAxis("X"); mouse->AddAxis("Y"); diff --git a/src/input/input.h b/src/input/input.h index a56244f4..70d14db2 100644 --- a/src/input/input.h +++ b/src/input/input.h @@ -16,6 +16,9 @@ namespace lol { +static const String g_name_mouse("Mouse"); +static const String g_name_keyboard("Keyboard"); + class InputDevice { public: diff --git a/src/input/input_internal.h b/src/input/input_internal.h index 803a0168..0d554052 100644 --- a/src/input/input_internal.h +++ b/src/input/input_internal.h @@ -29,6 +29,7 @@ public: void SetKey(int index, bool state) { m_keys[index] = state; } void SetAxis(int index, float value) { m_axis[index].m1 = value; } void SetCursor(int index, const vec2& position, const ivec2& pixel) { m_cursors[index].m1 = position; m_cursors[index].m2 = pixel; } + ivec2 GetCursorPixelPos(int index) { return m_cursors[index].m2; } static bool GetMouseCapture() { return m_capturemouse; } diff --git a/src/lol/math/vector.h b/src/lol/math/vector.h index c8e36dc5..21d4f1eb 100644 --- a/src/lol/math/vector.h +++ b/src/lol/math/vector.h @@ -251,6 +251,7 @@ template struct Vec2 : BVec2 LOL_MEMBER_OPS(Vec2, x) + static const Vec2 mone; static const Vec2 one; static const Vec2 zero; static const Vec2 axis_x; @@ -525,6 +526,7 @@ template struct Vec3 : BVec3 LOL_MEMBER_OPS(Vec3, x) + static const Vec3 mone; static const Vec3 one; static const Vec3 zero; static const Vec3 axis_x; @@ -948,6 +950,7 @@ template struct Vec4 : BVec4 LOL_MEMBER_OPS(Vec4, x) + static const Vec4 mone; static const Vec4 one; static const Vec4 zero; static const Vec4 axis_x; diff --git a/src/lolcore.vcxproj b/src/lolcore.vcxproj index 5f78bc01..3e248047 100644 --- a/src/lolcore.vcxproj +++ b/src/lolcore.vcxproj @@ -159,6 +159,18 @@ + + true + + + true + + + true + + + true + @@ -259,6 +271,18 @@ + + true + + + true + + + true + + + true + @@ -315,4 +339,4 @@ - + \ No newline at end of file diff --git a/src/lolcore.vcxproj.filters b/src/lolcore.vcxproj.filters index d88c6d5c..3ca630b5 100644 --- a/src/lolcore.vcxproj.filters +++ b/src/lolcore.vcxproj.filters @@ -79,6 +79,9 @@ {c6c6b597-ed6c-4d82-a166-964beeeeb525} + + {f6cc3470-c841-4581-969b-e60cea841c27} + @@ -312,6 +315,19 @@ ... + + + platform\NaCl + + + platform\NaCl + + + platform\NaCl + + + platform\NaCl + @@ -617,6 +633,18 @@ ... + + platform\NaCl + + + platform\NaCl + + + platform\NaCl + + + platform\NaCl + diff --git a/src/math/constants.cpp b/src/math/constants.cpp index ed82e933..c0dcde56 100644 --- a/src/math/constants.cpp +++ b/src/math/constants.cpp @@ -30,17 +30,20 @@ namespace lol { #define LOL_ALL_VECTOR_CONST_INNER(type) \ LOL_VEC_2_CONST(type, one, 1, 1) \ + LOL_VEC_2_CONST(type, mone,-1,-1) \ LOL_VEC_2_CONST(type, zero, 0, 0) \ LOL_VEC_2_CONST(type, axis_x, 1, 0) \ LOL_VEC_2_CONST(type, axis_y, 0, 1) \ \ LOL_VEC_3_CONST(type, one, 1, 1, 1) \ + LOL_VEC_3_CONST(type, mone,-1,-1,-1) \ LOL_VEC_3_CONST(type, zero, 0, 0, 0) \ LOL_VEC_3_CONST(type, axis_x, 1, 0, 0) \ LOL_VEC_3_CONST(type, axis_y, 0, 1, 0) \ LOL_VEC_3_CONST(type, axis_z, 0, 0, 1) \ \ LOL_VEC_4_CONST(type, one, 1, 1, 1, 1) \ + LOL_VEC_4_CONST(type, mone,-1,-1,-1,-1) \ LOL_VEC_4_CONST(type, zero, 0, 0, 0, 0) \ LOL_VEC_4_CONST(type, axis_x, 1, 0, 0, 0) \ LOL_VEC_4_CONST(type, axis_y, 0, 1, 0, 0) \ diff --git a/src/platform/sdl/sdlinput.cpp b/src/platform/sdl/sdlinput.cpp index 0d82ed60..969c5609 100644 --- a/src/platform/sdl/sdlinput.cpp +++ b/src/platform/sdl/sdlinput.cpp @@ -49,10 +49,8 @@ private: #if USE_SDL SdlInputData(int app_w, int app_h, int screen_w, int screen_h) : m_prevmouse(ivec2::zero), - m_app_w((float)app_w), - m_app_h((float)app_h), - m_screen_w((float)screen_w), - m_screen_h((float)screen_h), + m_app(vec2((float)app_w, (float)app_h)), + m_screen(vec2((float)screen_w, (float)screen_h)), m_mousecapture(false) { } @@ -61,10 +59,8 @@ private: InputDeviceInternal* m_keyboard; ivec2 m_prevmouse; - float m_app_w; - float m_app_h; - float m_screen_w; - float m_screen_h; + vec2 m_app; + vec2 m_screen; bool m_mousecapture; #endif // USE_SDL }; @@ -222,18 +218,20 @@ void SdlInputData::Tick(float seconds) //SDL_ShowCursor(m_mousecapture ? SDL_DISABLE : SDL_ENABLE); } - if (mouse.x >= 0 && mouse.x < m_app_w && mouse.y >= 0 && mouse.y < m_app_h) + if (mouse.x >= 0 && mouse.x < m_app.x && mouse.y >= 0 && mouse.y < m_app.y) { - m_mouse->SetCursor(0, vec2((float)(mouse.x) / m_app_w, (float)(mouse.y) / m_app_h), mouse); + vec2 vmouse = vec2(mouse); + vec2 vprevmouse = vec2(m_prevmouse); + m_mouse->SetCursor(0, vmouse / m_app, mouse); // Note: 100.0f is an arbitrary value that makes it feel about the same than an xbox controller joystick - m_mouse->SetAxis(0, (float)(mouse.x - m_prevmouse.x) * 100.0f / m_screen_w); + m_mouse->SetAxis(0, (mouse.x - vprevmouse.x) * 100.0f / m_screen.x); // Y Axis is also negated to match the usual joystick Y axis (negatives values are for the upper direction) - m_mouse->SetAxis(1, -(float)(mouse.y - m_prevmouse.y) * 100.0f / m_screen_h); + m_mouse->SetAxis(1,-(mouse.y - vprevmouse.y) * 100.0f / m_screen.y); } if (m_mousecapture) { - mouse = ivec2((int)m_app_w / 2, (int)m_app_h / 2); + mouse = ivec2(m_app * .5f); SdlInputData::SetMousePos(mouse); }