From a82278587918dba4e9ddd2a83a66b66a33efdfd5 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Mon, 11 Feb 2019 13:43:31 +0100 Subject: [PATCH] sdl: improve input support stability and handle return/tab/backspace in text input mode. --- src/input/controller.cpp | 58 +++++++++++++++-------------------- src/input/controller.h | 11 +++---- src/platform/sdl/sdlinput.cpp | 13 ++++++-- 3 files changed, 40 insertions(+), 42 deletions(-) diff --git a/src/input/controller.cpp b/src/input/controller.cpp index f51814ed..262c83d5 100644 --- a/src/input/controller.cpp +++ b/src/input/controller.cpp @@ -276,13 +276,6 @@ void Controller::ClearProfile() UnbindProfile(); } -//Init mode 2: By hand, key/axis by key/axis ---------------------------------- -void Controller::SetInputCount(int nb_keys, int nb_axis) -{ - m_keys.resize(nb_keys); - m_axis.resize(nb_axis); -} - //Layer mask stuff ------------------------------------------------------------ void Controller::SetLayerMask(uint32_t layer_mask) { @@ -302,46 +295,48 @@ KeyBinding& Controller::GetKey(int index) { return m_keys[index]; } -KeyBinding const& Controller::GetKey(int index) const -{ - return m_keys[index]; -} + AxisBinding& Controller::GetAxis(int index) { return m_axis[index]; } -AxisBinding const& Controller::GetAxis(int index) const -{ - return m_axis[index]; -} -//Key methods: should not go directly to binding ------------------------------ +// Key methods: should not go directly to binding bool Controller::IsKeyPressed(int index) const { -//#error do something better IsLayerActive() - return GetKey(index).IsPressed(); + auto key = m_keys.find(index); + return key != m_keys.end() && key->second.IsPressed(); } + bool Controller::IsKeyReleased(int index) const { - return GetKey(index).IsReleased(); + auto key = m_keys.find(index); + return key != m_keys.end() && key->second.IsReleased(); } + bool Controller::WasKeyPressedThisFrame(int index) const { - return GetKey(index).WasPressedThisFrame(); + auto key = m_keys.find(index); + return key != m_keys.end() && key->second.WasPressedThisFrame(); } + bool Controller::WasKeyReleasedThisFrame(int index) const { - return GetKey(index).WasReleasedThisFrame(); + auto key = m_keys.find(index); + return key != m_keys.end() && key->second.WasReleasedThisFrame(); } //Axis methods: should not go directly to binding ----------------------------- float Controller::GetAxisValue(int index) const { - return GetAxis(index).GetValue(); + auto axis = m_axis.find(index); + return axis != m_axis.end() ? axis->second.GetValue() : 0.f; } + float Controller::GetAxisDelta(int index) const { - return GetAxis(index).GetDelta(); + auto axis = m_axis.find(index); + return axis != m_axis.end() ? axis->second.GetDelta() : 0.f; } //----------------------------------------------------------------------------- @@ -403,10 +398,7 @@ void Controller::BindProfile(InputProfile const& setup) m_mutex.lock(); m_profile = setup; - m_keys.resize(m_profile.GetKeyCount()); - m_axis.resize(m_profile.GetAxisCount()); - - //Keyboard + // Keyboard m_keyboard = InputDevice::GetKeyboard(); if (m_keyboard) { @@ -414,7 +406,7 @@ void Controller::BindProfile(InputProfile const& setup) GetKey(key.m_idx).BindKeyboard(key.m_name); } - //Mouse + // Mouse m_mouse = InputDevice::GetMouse(); if (m_mouse) { @@ -424,7 +416,7 @@ void Controller::BindProfile(InputProfile const& setup) GetAxis(axis.m_idx).BindMouse(axis.m_name); } - //Joystick + // Joystick for (uint64_t joy_idx : m_profile.m_joystick) { class InputDevice* joystick = InputDevice::GetJoystick(joy_idx); @@ -455,11 +447,11 @@ void Controller::tick_game(float seconds) if (m_active) { - for (int i = 0; i < m_keys.count(); ++i) - m_keys[i].Update(); + for (auto &kv : m_keys) + kv.second.Update(); - for (int i = 0; i < m_axis.count(); ++i) - m_axis[i].Update(); + for (auto &kv : m_axis) + kv.second.Update(); } if (m_activate_nextframe) diff --git a/src/input/controller.h b/src/input/controller.h index eae671c2..39eb86c2 100644 --- a/src/input/controller.h +++ b/src/input/controller.h @@ -386,9 +386,6 @@ public: void Init(InputProfile const& profile); void ClearProfile(); - /** Init mode 2: By hand, key/axis by key/axis */ - void SetInputCount(int nb_keys, int nb_axis); - /** layer mask stuff */ void SetLayerMask(uint32_t layer_mask); uint32_t GetLayerMask(); @@ -427,11 +424,11 @@ protected: void BindProfile(InputProfile const& setup); private: - uint32_t m_layer_mask = 1; //plugged on the first by default - array m_keys; - array m_axis; + uint32_t m_layer_mask = 1; // plugged on the first by default + std::map m_keys; + std::map m_axis; - static uint32_t m_active_layer; //All active by default + static uint32_t m_active_layer; // All active by default static array controllers; std::string m_name; bool m_activate_nextframe; diff --git a/src/platform/sdl/sdlinput.cpp b/src/platform/sdl/sdlinput.cpp index 283bdb4b..f06a3f03 100644 --- a/src/platform/sdl/sdlinput.cpp +++ b/src/platform/sdl/sdlinput.cpp @@ -165,9 +165,7 @@ SdlInput::SdlInput(int app_w, int app_h, int screen_w, int screen_h) * - HDAPS, it's not a real joystick. * - X360 controllers, Xinput handles them better since * it won't think there is only one trigger axis. */ -# if LOL_USE_SDL char const *name = SDL_JoystickName(sdlstick); -# endif if (strstr(name, "HDAPS") # if LOL_USE_XINPUT || strstr(name, "XBOX 360 For Windows") @@ -302,6 +300,17 @@ void SdlInputData::Tick(float seconds) (int)m_keyboard, ScanCodeToText(sc).C(), ScanCodeToName(sc).C(), event.type == SDL_KEYDOWN ? "up" : "down", event.key.repeat); */ + + // These are arguably text input characters, too. + if (event.type == SDL_KEYDOWN && m_keyboard->IsTextInputActive()) + { + switch (sc) + { + case SDLOL_Return: m_keyboard->AddText("\n"); break; + case SDLOL_Tab: m_keyboard->AddText("\t"); break; + case SDLOL_Backspace: m_keyboard->AddText("\x08"); break; + } + } } /* DEBUG STUFF else