From b0a85960019ae1c8dfefe4b000a2d32bc8ed7b9a Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Mon, 4 Mar 2019 13:30:24 +0100 Subject: [PATCH] =?UTF-8?q?RIP=20lol::Controller=20and=20lol::InputProfile?= =?UTF-8?q?=20=E2=9A=B0=EF=B8=8F=20LOL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/tutorial/07_input.cpp | 51 ++-- doc/tutorial/11_fractal.cpp | 15 +- src/Makefile.am | 1 - src/lol-core.vcxproj | 2 - src/lol-core.vcxproj.filters | 6 - src/lol/extras.h | 1 - src/ui/controller.cpp | 444 ----------------------------------- src/ui/controller.h | 352 --------------------------- src/ui/d3d9-input.cpp | 19 +- src/ui/gui.cpp | 24 +- src/ui/gui.h | 2 - src/ui/input.cpp | 42 +--- src/ui/input.h | 166 +++++-------- src/ui/sdl-input.cpp | 40 ++-- src/ui/sdl-input.h | 2 +- 15 files changed, 128 insertions(+), 1039 deletions(-) delete mode 100644 src/ui/controller.cpp delete mode 100644 src/ui/controller.h diff --git a/doc/tutorial/07_input.cpp b/doc/tutorial/07_input.cpp index 71ab22ba..a791069d 100644 --- a/doc/tutorial/07_input.cpp +++ b/doc/tutorial/07_input.cpp @@ -26,16 +26,6 @@ class InputTutorial : public WorldEntity public: InputTutorial() { - m_controller = new Controller("Default"); - - m_profile - << InputProfile::JoystickAxis(1, AXIS_PITCH, "Axis2") - << InputProfile::JoystickAxis(1, AXIS_YAW, "Axis1") - << InputProfile::MouseAxis(AXIS_DRAG_PITCH, "Y") - << InputProfile::MouseAxis(AXIS_DRAG_YAW, "X"); - - m_controller->Init(m_profile); - m_pitch_angle = 0; m_yaw_angle = 0; m_autorot = true; @@ -63,7 +53,7 @@ public: m_lines_indices << 0 << 4 << 1 << 5 << 2 << 6 << 3 << 7; m_text = new Text("", "data/font/ascii.png"); - m_text->SetPos(vec3(5, 5, 1)); + m_text->SetPos(vec3(5, 30, 1)); Ticker::Ref(m_text); m_ready = false; @@ -86,10 +76,14 @@ public: m_autorot = !m_autorot; /* Handle joystick */ - if (lol::abs(m_controller->GetAxisValue(AXIS_PITCH)) > 0.2f) - m_pitch_angle += m_controller->GetAxisValue(AXIS_PITCH) * seconds; - if (lol::abs(m_controller->GetAxisValue(AXIS_YAW)) > 0.2f) - m_yaw_angle += m_controller->GetAxisValue(AXIS_YAW) * seconds; + auto joystick = InputDevice::Get(g_name_joystick(1).c_str()); + if ((bool)joystick) + { + if (lol::abs(joystick->axis(input::axis::LeftY)) > 0.2f) + m_pitch_angle += joystick->axis(input::axis::LeftY) * seconds; + if (lol::abs(joystick->axis(input::axis::LeftX)) > 0.2f) + m_yaw_angle += joystick->axis(input::axis::LeftX) * seconds; + } /* Handle mouse */ if (true) @@ -97,8 +91,8 @@ public: if (mouse->button(input::button::BTN_Left)) { mouse->capture(true); - m_pitch_angle -= m_controller->GetAxisValue(AXIS_DRAG_PITCH) * seconds * 0.1f; - m_yaw_angle += m_controller->GetAxisValue(AXIS_DRAG_YAW) * seconds * 0.1f; + m_pitch_angle -= mouse->axis(input::axis::MoveX) * seconds * 0.1f; + m_yaw_angle += mouse->axis(input::axis::MoveY) * seconds * 0.1f; } else { @@ -108,9 +102,14 @@ public: } m_text->SetText(lol::format( - "cursor: (%0.3f, %0.3f) - pixel (%d, %d)", - mouse->get_cursor_uv(0).x, mouse->get_cursor_uv(0).y, - mouse->get_cursor_pixel(0).x, mouse->get_cursor_pixel(0).y)); + "cursor: (%0.3f,%0.3f) - pixel (%d,%d)\n" + " move: (%0.3f,%0.3f) - pixel (%d,%d)", + mouse->axis(input::axis::X), mouse->axis(input::axis::Y), + (int)mouse->axis(input::axis::ScreenX), + (int)mouse->axis(input::axis::ScreenY), + mouse->axis(input::axis::MoveX), mouse->axis(input::axis::MoveY), + (int)mouse->axis(input::axis::ScreenMoveX), + (int)mouse->axis(input::axis::ScreenMoveY))); } else { @@ -180,18 +179,6 @@ public: } private: - enum - { - AXIS_DRAG_PITCH, - AXIS_DRAG_YAW, - AXIS_PITCH, - AXIS_YAW, - AXIS_MAX - }; - - Controller *m_controller; - InputProfile m_profile; - bool m_autorot; float m_pitch_angle; float m_yaw_angle; diff --git a/doc/tutorial/11_fractal.cpp b/doc/tutorial/11_fractal.cpp index 57a63529..e3c13ab1 100644 --- a/doc/tutorial/11_fractal.cpp +++ b/doc/tutorial/11_fractal.cpp @@ -153,7 +153,8 @@ public: auto mouse = input::mouse(); auto keyboard = input::keyboard(); - ivec2 mousepos = mouse->get_cursor_pixel(0); + vec2 mousepos(mouse->axis(input::axis::ScreenX), + mouse->axis(input::axis::ScreenY)); int prev_frame = (m_frame + 4) % 4; m_frame = (m_frame + 1) % 4; @@ -164,7 +165,7 @@ public: if (m_julia) { m_saved_view = m_view; - m_view.r0 = m_view.center + rcmplx(ScreenToWorldOffset((vec2)mousepos)); + m_view.r0 = m_view.center + rcmplx(ScreenToWorldOffset(mousepos)); } else { @@ -174,24 +175,24 @@ public: flag = 2; } - rcmplx worldmouse = m_view.center + rcmplx(ScreenToWorldOffset((vec2)mousepos)); + rcmplx worldmouse = m_view.center + rcmplx(ScreenToWorldOffset(mousepos)); if (mouse->button(input::button::BTN_Middle)) { if (!m_drag) { - m_oldmouse = mousepos; + m_oldmouse = (ivec2)mousepos; m_drag = true; } m_view.translate = rcmplx(ScreenToWorldOffset((vec2)m_oldmouse) - - ScreenToWorldOffset((vec2)mousepos)); + - ScreenToWorldOffset(mousepos)); /* XXX: the purpose of this hack is to avoid translating by * an exact number of pixels. If this were to happen, the step() * optimisation for i915 cards in our shader would behave * incorrectly because a quarter of the pixels in the image * would have tied rankings in the distance calculation. */ m_view.translate *= real(1023.0 / 1024.0); - m_oldmouse = mousepos; + m_oldmouse = (ivec2)mousepos; } else { @@ -239,7 +240,7 @@ public: m_view.center += m_view.translate; m_view.center = (m_view.center - worldmouse) * real(zoom) + worldmouse; worldmouse = m_view.center - + rcmplx(ScreenToWorldOffset((vec2)mousepos)); + + rcmplx(ScreenToWorldOffset(mousepos)); /* Store the transformation properties to go from m_frame - 1 * to m_frame. */ diff --git a/src/Makefile.am b/src/Makefile.am index a95d173a..2b9dbd11 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -102,7 +102,6 @@ liblol_core_sources = \ audio/audio.cpp audio/sample.cpp \ \ ui/input.cpp ui/input.h ui/keys.inc ui/buttons.inc \ - ui/controller.cpp ui/controller.h \ ui/gui.cpp ui/gui.h \ \ gpu/default-material.lolfx \ diff --git a/src/lol-core.vcxproj b/src/lol-core.vcxproj index 84d779cd..af4e4294 100644 --- a/src/lol-core.vcxproj +++ b/src/lol-core.vcxproj @@ -172,7 +172,6 @@ - @@ -288,7 +287,6 @@ - diff --git a/src/lol-core.vcxproj.filters b/src/lol-core.vcxproj.filters index 4bfc9f72..0a65eb08 100644 --- a/src/lol-core.vcxproj.filters +++ b/src/lol-core.vcxproj.filters @@ -239,9 +239,6 @@ - - ui - ui @@ -524,9 +521,6 @@ ui - - ui - ui diff --git a/src/lol/extras.h b/src/lol/extras.h index f5908675..7258d822 100644 --- a/src/lol/extras.h +++ b/src/lol/extras.h @@ -46,7 +46,6 @@ // UI #include -#include #include // Other objects diff --git a/src/ui/controller.cpp b/src/ui/controller.cpp deleted file mode 100644 index fda5d524..00000000 --- a/src/ui/controller.cpp +++ /dev/null @@ -1,444 +0,0 @@ -// -// Lol Engine -// -// Copyright © 2017—2019 Sam Hocevar -// © 2010—2015 Benjamin Litzelmann -// -// Lol Engine is free software. It comes without any warranty, to -// the extent permitted by applicable law. You can redistribute it -// and/or modify it under the terms of the Do What the Fuck You Want -// to Public License, Version 2, as published by the WTFPL Task Force. -// See http://www.wtfpl.net/ for more details. -// - -#include - -#include - -namespace lol -{ - -/////////////////////////////////////////////////////////////////////////////// -// KeyBinding - -void KeyBinding::Bind(const std::string& device_name, const std::string& key_name) -{ - const InputDevice* device = InputDevice::Get(device_name); - - if (!device) - { - msg::warn("trying to bind key to nonexistent input device %s\n", - device_name.c_str()); - return; - } - - int keyindex = (int)device->GetKeyIndex(key_name); - if (keyindex < 0) - { - msg::warn("trying to bind nonexistent key %s.%s\n", - device_name.c_str(), key_name.c_str()); - return; - } - - m_keybindings.push(device, keyindex); -} - -bool KeyBinding::Unbind(const std::string& device_name, const std::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)) - { - m_keybindings.remove(i); - return true; - } - } - } - return false; -} - -/////////////////////////////////////////////////////////////////////////////// -// AxisBinding - -void AxisBinding::Bind(const std::string& device_name, const std::string& axis_name) -{ - const InputDevice* device = InputDevice::Get(device_name); - if (!device) - { - msg::warn("trying to bind axis to nonexistent input device %s\n", - device_name.c_str()); - return; - } - - int axisindex = (int)device->GetAxisIndex(axis_name); - if (axisindex < 0) - { - msg::warn("trying to bind nonexistent axis %s.%s\n", - device_name.c_str(), axis_name.c_str()); - return; - } - - m_axisbindings.push(device, axisindex); -} - -void AxisBinding::BindKey(const std::string& device_name, const std::string& key_name) -{ - const InputDevice* device = InputDevice::Get(device_name); - if (!device) - { - msg::warn("trying to bind axis key to nonexistent input device %s\n", - device_name.c_str()); - return; - } - - int keyindex = (int)device->GetKeyIndex(key_name); - if (keyindex < 0) - { - msg::warn("trying to bind nonexistent axis key %s.%s\n", - device_name.c_str(), key_name.c_str()); - return; - } - - m_keybindings.push(device, -1, keyindex); -} - -void AxisBinding::BindKeys(const std::string& device_name, const std::string& min_key_name, const std::string& max_key_name) -{ - const InputDevice* device = InputDevice::Get(device_name); - if (!device) - { - msg::warn("trying to bind axis keys to nonexistent input device %s\n", - device_name.c_str()); - return; - } - - int minkeyindex = (int)device->GetKeyIndex(min_key_name); - if (minkeyindex < 0) - { - msg::warn("trying to bind nonexistent axis key %s.%s\n", - device_name.c_str(), min_key_name.c_str()); - return; - } - - int maxkeyindex = (int)device->GetKeyIndex(max_key_name); - if (maxkeyindex < 0) - { - msg::warn("trying to bind nonexistent axis key %s.%s\n", - device_name.c_str(), max_key_name.c_str()); - return; - } - - m_keybindings.push(device, minkeyindex, maxkeyindex); -} - -bool AxisBinding::Unbind(const std::string& device_name, const std::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)) - { - m_axisbindings.remove(i); - return true; - } - } - } - return false; -} - -bool AxisBinding::UnbindKey(const std::string& device_name, const std::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)) - { - m_keybindings.remove(i); - return true; - } - } - } - return false; -} - -bool AxisBinding::UnbindKeys(const std::string& device_name, const std::string& min_key_name, const std::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)) - { - m_keybindings.remove(i); - return true; - } - } - } - return false; - -} - -float AxisBinding::RetrieveCurrentValue() -{ - float max_positive = 0.0f; - float max_negative = 0.0f; - - for (int i = 0; i < m_axisbindings.count(); ++i) - { - float value = m_axisbindings[i].m1->GetAxis(m_axisbindings[i].m2); - if (value > max_positive) - max_positive = value; - if (value < max_negative) - max_negative = value; - } - - for (int i = 0; i < m_keybindings.count(); ++i) - { - float value = 0.0f; - m_keybindings[i].m1->key((input::key)m_keybindings[i].m2); - value += m_keybindings[i].m1->key((input::key)m_keybindings[i].m3) ? 1.0f : 0.0f; - if (m_keybindings[i].m2 != -1) - value += m_keybindings[i].m1->key((input::key)m_keybindings[i].m2) ? -1.0f : 0.0f; - - if (value > max_positive) - max_positive = value; - if (value < max_negative) - max_negative = value; - } - - return max_negative + max_positive; -} - -/////////////////////////////////////////////////////////////////////////////// -// Controller - -static std::map g_controllers; -static uint32_t g_active_layer = ~((uint32_t)0); - -//----------------------------------------------------------------------------- -Controller::Controller(std::string const &name) -{ - m_gamegroup = tickable::group::game::input; - m_name = name; - m_activate_nextframe = true; - m_deactivate_nextframe = false; - m_active = false; - if (g_controllers[name]) - msg::warn("controller “%s” has already been registered\n", name.c_str()); - g_controllers[name] = this; -} - -Controller::Controller(std::string const &name, InputProfile const& profile) - : Controller(name) -{ - Init(profile); -} - -Controller::~Controller() -{ - ClearProfile(); - if (g_controllers[m_name] == this) - g_controllers.erase(m_name); -} - -//Init mode 1: Input profile system ------------------------------------------- -void Controller::Init(InputProfile const& profile) -{ - UnbindProfile(); - BindProfile(profile); -} -void Controller::ClearProfile() -{ - UnbindProfile(); -} - -//Layer mask stuff ------------------------------------------------------------ -void Controller::SetLayerMask(uint32_t layer_mask) -{ - m_layer_mask = layer_mask; -} -uint32_t Controller::GetLayerMask() -{ - return m_layer_mask; -} -bool Controller::IsLayerActive() -{ - return !!(m_layer_mask & g_active_layer); -} - -//GetKeys/Axis stuff ---------------------------------------------------------- -KeyBinding& Controller::GetKey(int index) -{ - return m_key_bindings[index]; -} - -AxisBinding& Controller::GetAxis(int index) -{ - return m_axis_bindings[index]; -} - -//Axis methods: should not go directly to binding ----------------------------- -float Controller::GetAxisValue(int index) const -{ - auto axis = m_axis_bindings.find(index); - return axis != m_axis_bindings.end() ? axis->second.GetValue() : 0.f; -} - -float Controller::GetAxisDelta(int index) const -{ - auto axis = m_axis_bindings.find(index); - return axis != m_axis_bindings.end() ? axis->second.GetDelta() : 0.f; -} - -//Input profile system -------------------------------------------------------- -void Controller::UnbindProfile() -{ - if (m_profile.IsEmpty()) - return; - - m_mutex.lock(); - - //Keyboard - if (m_keyboard) - { - for (InputProfile::KeyboardKey& key : m_profile.m_keys) - GetKey(key.m_idx).UnbindKeyboard(key.m_name); - m_keyboard = nullptr; - } - - //Mouse - if (m_mouse) - { - for (InputProfile::MouseKey& key : m_profile.m_mouse_keys) - GetKey(key.m_idx).UnbindMouse(key.m_name); - for (InputProfile::MouseAxis& axis : m_profile.m_mouse_axis) - GetAxis(axis.m_idx).UnbindMouse(axis.m_name); - m_mouse = nullptr; - } - - //Joystick - for (InputProfile::JoystickKey& key : m_profile.m_joystick_keys) - { - if (m_joystick_idx.find(key.m_joy) != INDEX_NONE) - GetKey(key.m_idx).UnbindJoystick(key.m_joy, key.m_name); - } - for (InputProfile::JoystickAxis& axis : m_profile.m_joystick_axis) - { - if (m_joystick_idx.find(axis.m_joy) != INDEX_NONE) - GetAxis(axis.m_idx).UnbindJoystick(axis.m_joy, axis.m_name); - } - m_joysticks.clear(); - m_joystick_idx.clear(); - - m_mutex.unlock(); -} -//Input profile system -------------------------------------------------------- -void Controller::BindProfile(InputProfile const& setup) -{ - ASSERT(!setup.IsEmpty()); - - m_mutex.lock(); - m_profile = setup; - - // Keyboard - m_keyboard = input::keyboard(); - if (m_keyboard) - { - for (InputProfile::KeyboardKey& key : m_profile.m_keys) - GetKey(key.m_idx).BindKeyboard(key.m_name); - } - - // Mouse - m_mouse = input::mouse(); - if (m_mouse) - { - for (InputProfile::MouseKey& key : m_profile.m_mouse_keys) - GetKey(key.m_idx).BindMouse(key.m_name); - for (InputProfile::MouseAxis& axis : m_profile.m_mouse_axis) - GetAxis(axis.m_idx).BindMouse(axis.m_name); - } - - // Joystick - for (uint64_t joy_idx : m_profile.m_joystick) - { - class InputDevice* joystick = InputDevice::GetJoystick(joy_idx); - if (joystick) - { - m_joysticks << joystick; - m_joystick_idx << joy_idx; - } - } - for (InputProfile::JoystickKey& key : m_profile.m_joystick_keys) - { - if (m_joystick_idx.find(key.m_joy) != INDEX_NONE) - GetKey(key.m_idx).BindJoystick(key.m_joy, key.m_name); - } - for (InputProfile::JoystickAxis& axis : m_profile.m_joystick_axis) - { - if (m_joystick_idx.find(axis.m_joy) != INDEX_NONE) - GetAxis(axis.m_idx).BindJoystick(axis.m_joy, axis.m_name); - } - - m_mutex.unlock(); -} - -//----------------------------------------------------------------------------- -void Controller::tick_game(float seconds) -{ - Entity::tick_game(seconds); - - if (m_active) - { - for (auto &kv : m_key_bindings) - kv.second.Update(); - - for (auto &kv : m_axis_bindings) - kv.second.Update(); - } - - if (m_activate_nextframe) - m_active = true; - - if (m_deactivate_nextframe) - m_active = false; - - m_activate_nextframe = false; - m_deactivate_nextframe = false; -} - -//----------------------------------------------------------------------------- -void Controller::Activate() -{ - m_activate_nextframe = true; - m_deactivate_nextframe = false; -} - -void Controller::Deactivate() -{ - m_deactivate_nextframe = true; - m_activate_nextframe = false; -} - -//----------------------------------------------------------------------------- -array Controller::DeactivateAll() -{ - array result; - - for (auto it : g_controllers) - { - if (it.second->m_active || it.second->m_activate_nextframe) - { - result.push(it.second); - it.second->Deactivate(); - } - } - - return result; -} - -} /* namespace lol */ diff --git a/src/ui/controller.h b/src/ui/controller.h deleted file mode 100644 index 3fc6f6b2..00000000 --- a/src/ui/controller.h +++ /dev/null @@ -1,352 +0,0 @@ -// -// Lol Engine -// -// Copyright © 2017—2019 Sam Hocevar -// © 2010—2015 Benjamin Litzelmann -// -// Lol Engine is free software. It comes without any warranty, to -// the extent permitted by applicable law. You can redistribute it -// and/or modify it under the terms of the Do What the Fuck You Want -// to Public License, Version 2, as published by the WTFPL Task Force. -// See http://www.wtfpl.net/ for more details. -// - -#pragma once - -#include -#include - -namespace lol -{ - -//----------------------------------------------------------------------------- -class KeyBinding -{ - friend class Controller; - -protected: - //Status methods ---------------------------------------------------------- - /** Indicates wheither the key is currently down */ - bool IsPressed() const { return m_current; } - /** Indicates wheither the key is currently up */ - bool IsReleased() const { return !m_current; } - -public: - //Binding methods --------------------------------------------------------- - /** Bind a physical device and key */ - void Bind(const std::string& device_name, const std::string& key_name); - /** Unbind a previously bound physical device and key. Returns true if the binding was existing. */ - bool Unbind(const std::string& device_name, const std::string& key_name); - /* Small helpers */ - void BindMouse(const std::string& key_name) { Bind(g_name_mouse, key_name); } - void BindKeyboard(const std::string& key_name) { Bind(g_name_keyboard, key_name); } - void BindJoystick(const uint64_t num, const std::string& key_name) { Bind(g_name_joystick(num), key_name); } - bool UnbindMouse(const std::string& key_name) { return Unbind(g_name_mouse, key_name); } - bool UnbindKeyboard(const std::string& key_name) { return Unbind(g_name_keyboard, key_name); } - bool UnbindJoystick(const uint64_t num, const std::string& key_name) { return Unbind(g_name_joystick(num), key_name); } - -protected: - /** Update the binding value. Called internally by the controller, once per frame */ - void Update() - { - m_previous = m_current; - m_current = false; - for (int i = 0; i < m_keybindings.count(); ++i) - m_current = m_current || m_keybindings[i].m1->key((input::key)m_keybindings[i].m2); - } - - /** m1 is the InputDevice, m2 is the key index on the InputDevice */ - array m_keybindings; - /** Value at the previous frame */ - bool m_current = false; - /** Value at the current frame */ - bool m_previous = false; -}; - -//----------------------------------------------------------------------------- -class AxisBinding -{ - friend class Controller; - -protected: - //Status methods ---------------------------------------------------------- - /** Gets the current absolute value of this axis */ - float GetValue() const { return m_current; } - /** Gets the current delta value of this axis */ - float GetDelta() const { return m_current - m_previous; } - -public: - //Binding methods --------------------------------------------------------- - /** Bind a physical device and axis */ - void Bind(const std::string& device_name, const std::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 std::string& device_name, const std::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 std::string& device_name, const std::string& min_key_name, const std::string& max_key_name); - /** Unbind a previously bound physical device and axis. Returns true if the binding was existing. */ - bool Unbind(const std::string& device_name, const std::string& axis_name); - /** Unbind a previously bound physical device and axis. Returns true if the binding was existing. */ - bool UnbindKey(const std::string& device_name, const std::string& key_name); - /** Unbind a previously bound physical device and axis. Returns true if the binding was existing. */ - bool UnbindKeys(const std::string& device_name, const std::string& min_key_name, const std::string& max_key_name); - /* Small helpers */ - void BindMouse(const std::string& axis_name) { Bind(g_name_mouse, axis_name); } - bool UnbindMouse(const std::string& axis_name) { return Unbind(g_name_mouse, axis_name); } - /* */ - void BindJoystick(const uint64_t num, const std::string& axis_name) { Bind(g_name_joystick(num), axis_name); } - bool UnbindJoystick(const uint64_t num, const std::string& axis_name) { return Unbind(g_name_joystick(num), axis_name); } - -protected: - void Update() - { - m_previous = m_current; - m_current = RetrieveCurrentValue(); - } - float RetrieveCurrentValue(); - - /** m1 is the InputDevice, m2 is the axis index on the InputDevice */ - array m_axisbindings; - /** m1 is the InputDevice, m2 is the key index on the InputDevice for the negative value, m3 is the key index on the InputDevice for the positive value. Only one key is required to bind key over axis. */ - array m_keybindings; - float m_current = 0.0f; - float m_previous = 0.0f; -}; - -//------------------------------------------------------------------------- -class InputProfile -{ - friend class Controller; - -private: - class Key - { - friend class Controller; - friend class InputProfile; - public: - Key() { } - Key(int idx, std::string const& name) : m_idx(idx), m_name(name) { } - Key(const Key& other) : m_idx(other.m_idx), m_name(other.m_name) { } - ~Key() { } - bool operator==(const Key& other) { return m_name == other.m_name; } - private: - int m_idx = 0; - std::string m_name; - }; - - class Joystick - { - friend class Controller; - friend class InputProfile; - public: - Joystick() { } - Joystick(uint64_t joy, int idx, std::string const& name) : m_joy(joy), m_idx(idx), m_name(name) { } - Joystick(const Joystick& other) : m_joy(other.m_joy), m_idx(other.m_idx), m_name(other.m_name) { } - ~Joystick() { } - bool operator==(const Joystick& other) { return m_name == other.m_name; } - private: - uint64_t m_joy = 0; - int m_idx = 0; - std::string m_name; - }; - -public: - class KeyboardKey : public Key - { - friend class Controller; - friend class InputProfile; - public: - KeyboardKey(int idx, std::string const& name) : Key(idx, name) { } - }; - - class MouseKey : public Key - { - friend class Controller; - friend class InputProfile; - public: - MouseKey(int idx, std::string const& name) : Key(idx, name) { } - }; - - class MouseAxis : public Key - { - friend class Controller; - friend class InputProfile; - public: - MouseAxis(int idx, std::string const& name) : Key(idx, name) { } - }; - - class JoystickKey : public Joystick - { - friend class Controller; - friend class InputProfile; - public: - JoystickKey(uint64_t joy, int idx, std::string const& name) : Joystick(joy, idx, name) { } - }; - - class JoystickAxis : public Joystick - { - friend class Controller; - friend class InputProfile; - public: - JoystickAxis(uint64_t joy, int idx, std::string const& name) : Joystick(joy, idx, name) { } - }; - -public: - InputProfile() = default; - virtual ~InputProfile() = default; - - bool IsEmpty() const - { - return !GetKeyCount() && !GetAxisCount(); - } - int GetKeyCount() const - { - return m_keys.count() + m_mouse_keys.count() + m_joystick_keys.count(); - } - int GetAxisCount() const - { - return m_mouse_axis.count() + m_joystick_axis.count(); - } - //Keys -------------------------------------------------------------------- - InputProfile& operator<<(InputProfile::KeyboardKey const& binding) - { - m_keys.push_unique(binding); - return *this; - } - InputProfile& operator<<(array const& bindings) - { - m_keys += bindings; - return *this; - } - //------ - InputProfile& operator<<(InputProfile::MouseKey const& binding) - { - m_mouse_keys.push_unique(binding); - return *this; - } - InputProfile& operator<<(array const& bindings) - { - m_mouse_keys += bindings; - return *this; - } - //------ - InputProfile& operator<<(InputProfile::JoystickKey const& binding) - { - m_joystick.push_unique(binding.m_joy); - m_joystick_keys.push_unique(binding); - return *this; - } - InputProfile& operator<<(array const& bindings) - { - for (InputProfile::JoystickKey const& binding : bindings) - m_joystick.push_unique(binding.m_joy); - m_joystick_keys += bindings; - return *this; - } - //Axis -------------------------------------------------------------------- - InputProfile& operator<<(InputProfile::MouseAxis const& binding) - { - m_mouse_axis.push_unique(binding); - return *this; - } - InputProfile& operator<<(array const& bindings) - { - m_mouse_axis += bindings; - return *this; - } - //------ - InputProfile& operator<<(InputProfile::JoystickAxis const& binding) - { - m_joystick.push_unique(binding.m_joy); - m_joystick_axis.push_unique(binding); - return *this; - } - InputProfile& operator<<(array const& bindings) - { - for (InputProfile::JoystickAxis const& binding : bindings) - m_joystick.push_unique(binding.m_joy); - m_joystick_axis += bindings; - return *this; - } - - void register_default_keys() - { -#define _SC(id, str, name) *this << InputProfile::KeyboardKey(id, #name); -#include "ui/keys.inc" - } - -private: - array m_keys; - array m_mouse_keys; - array m_mouse_axis; - array m_joystick; - array m_joystick_keys; - array m_joystick_axis; -}; - -//----------------------------------------------------------------------------- -//TODO: Add mask|layer system to prevent several controllers from interfering with another. (input overlay in menus) -class Controller : public Entity -{ -public: - Controller(std::string const &name); - Controller(std::string const &name, InputProfile const& setup); - virtual ~Controller(); - - virtual void tick_game(float seconds); - - /** Activate the controller on next frame */ - void Activate(); - /** Deactivate the controller on next frame */ - void Deactivate(); - /** Deactivate every active controller on next frame and return an array of deactivated (previously active) controllers */ - static array DeactivateAll(); - - /** Init mode 1: Input profile system */ - void Init(InputProfile const& profile); - void ClearProfile(); - - /** layer mask stuff */ - void SetLayerMask(uint32_t layer_mask); - uint32_t GetLayerMask(); -protected: - bool IsLayerActive(); -public: - - /** GetKeys/Axis stuff */ - KeyBinding& GetKey(int index); - KeyBinding const& GetKey(int index) const; - AxisBinding& GetAxis(int index); - AxisBinding const& GetAxis(int index) const; - - /** Axis methods: should not go directly to binding */ - /** Gets the current absolute value of this axis */ - float GetAxisValue(int index) const; - /** Gets the current delta value of this axis */ - float GetAxisDelta(int index) const; - -protected: - /** Input profile system */ - void UnbindProfile(); - void BindProfile(InputProfile const& setup); - -private: - uint32_t m_layer_mask = 1; // plugged on the first by default - std::map m_key_bindings; - std::map m_axis_bindings; - - std::string m_name; - bool m_activate_nextframe; - bool m_deactivate_nextframe; - bool m_active; - - //Input profile stuff - mutex m_mutex; - class InputProfile m_profile; - std::shared_ptr m_keyboard; - std::shared_ptr m_mouse; - array m_joysticks; - array m_joystick_idx; -}; - -} /* namespace lol */ - diff --git a/src/ui/d3d9-input.cpp b/src/ui/d3d9-input.cpp index b284dec9..2db5bd5a 100644 --- a/src/ui/d3d9-input.cpp +++ b/src/ui/d3d9-input.cpp @@ -26,13 +26,6 @@ namespace lol { -static const std::string g_name_xbox_axis_left_x("Axis1"); -static const std::string g_name_xbox_axis_left_y("Axis2"); -static const std::string g_name_xbox_axis_right_x("Axis3"); -static const std::string g_name_xbox_axis_right_y("Axis4"); -static const std::string g_name_xbox_axis_left_trigger("Axis5"); -static const std::string g_name_xbox_axis_right_trigger("Axis6"); - /* * D3d9 Input implementation class */ @@ -63,12 +56,12 @@ D3d9Input::D3d9Input() // TODO: we can put more friendly name here, such as LeftAxisX, ButtonX... InputDevice* stick = new InputDevice(g_name_joystick(i + 1)); - stick->AddAxis(g_name_xbox_axis_left_x.c_str()); - stick->AddAxis(g_name_xbox_axis_left_y.c_str()); - stick->AddAxis(g_name_xbox_axis_right_x.c_str()); - stick->AddAxis(g_name_xbox_axis_right_y.c_str()); - stick->AddAxis(g_name_xbox_axis_left_trigger.c_str()); - stick->AddAxis(g_name_xbox_axis_right_trigger.c_str()); + stick->internal_add_axis(input::axis::LeftX, "LeftX"); + stick->internal_add_axis(input::axis::LeftY, "LeftY"); + stick->internal_add_axis(input::axis::RightX, "RightX"); + stick->internal_add_axis(input::axis::RightY, "RightY"); + stick->internal_add_axis(input::axis::LeftShoulder, "LeftShoulder"); + stick->internal_add_axis(input::axis::RightShoulder, "RightShoulder"); #define _BTN(id, name) stick->internal_add_button(input::button::BTN_##name, #name); #include "ui/buttons.inc" // FIXME: ignore mouse buttons here diff --git a/src/ui/gui.cpp b/src/ui/gui.cpp index 1613d85f..e10c1c7e 100644 --- a/src/ui/gui.cpp +++ b/src/ui/gui.cpp @@ -31,11 +31,6 @@ namespace MiddleClick, Focus, }; - - enum axis_enum - { - Scroll, - }; } static gui* g_gui = nullptr; @@ -88,19 +83,6 @@ gui::gui(ImFontAtlas *shared_font_atlas) m_builder << ShaderProgram::Vertex << imgui_vertex << ShaderProgram::Pixel << imgui_pixel; - - // Input Setup ------------------------------------------------------------- - m_profile.register_default_keys(); - - m_profile << InputProfile::MouseKey(key_enum::LeftClick, g_name_mouse_key_left); - m_profile << InputProfile::MouseKey(key_enum::RightClick, g_name_mouse_key_right); - m_profile << InputProfile::MouseKey(key_enum::MiddleClick, g_name_mouse_key_middle); - m_profile << InputProfile::MouseKey(key_enum::Focus, g_name_mouse_key_in_screen); - - m_profile << InputProfile::MouseAxis(axis_enum::Scroll, g_name_mouse_axis_scroll); - - Ticker::Ref(m_controller = new Controller("ImGui_Controller")); - m_controller->Init(m_profile); } gui::~gui() @@ -252,12 +234,12 @@ void gui::tick_game(float seconds) keyboard->capture_text(io.WantTextInput); // Update mouse - vec2 cursor = mouse->get_cursor_uv(0); - cursor.y = 1.f - cursor.y; + vec2 cursor(mouse->axis(input::axis::X), + 1.f - mouse->axis(input::axis::Y)); io.MousePos = cursor * video_size; //msg::debug("%.2f/%.2f\n", io.MousePos.x, io.MousePos.y); - io.MouseWheel = m_controller->GetAxisValue(axis_enum::Scroll); + io.MouseWheel = mouse->axis(input::axis::Wheel); io.MouseDown[0] = mouse->button(input::button::BTN_Left); io.MouseDown[1] = mouse->button(input::button::BTN_Right); diff --git a/src/ui/gui.h b/src/ui/gui.h index 9bf90e7c..32dd59bf 100644 --- a/src/ui/gui.h +++ b/src/ui/gui.h @@ -85,8 +85,6 @@ protected: Uniform m_texture; array m_attribs; std::shared_ptr m_vdecl; - Controller* m_controller = nullptr; - InputProfile m_profile; std::string m_clipboard; class primitive : public PrimitiveRenderer diff --git a/src/ui/input.cpp b/src/ui/input.cpp index 10b0bfd0..5c3457ab 100644 --- a/src/ui/input.cpp +++ b/src/ui/input.cpp @@ -49,12 +49,15 @@ input::input() m_mouse = std::make_shared(g_name_mouse.c_str()); #define _BTN(id, name) m_mouse->internal_add_button(input::button::BTN_##name, #name); #include "ui/buttons.inc" // FIXME: this will also add joystick buttons! - m_mouse->AddAxis(g_name_mouse_axis_x.c_str()); - m_mouse->AddAxis(g_name_mouse_axis_y.c_str()); - m_mouse->AddAxis(g_name_mouse_axis_xpixel.c_str()); - m_mouse->AddAxis(g_name_mouse_axis_ypixel.c_str()); - m_mouse->AddAxis(g_name_mouse_axis_scroll.c_str(), .0000001f); - m_mouse->AddCursor(g_name_mouse_cursor.c_str()); + m_mouse->internal_add_axis(input::axis::X, "X"); + m_mouse->internal_add_axis(input::axis::Y, "Y"); + m_mouse->internal_add_axis(input::axis::ScreenX, "ScreenX"); + m_mouse->internal_add_axis(input::axis::ScreenY, "ScreenY"); + m_mouse->internal_add_axis(input::axis::MoveX, "MoveX"); + m_mouse->internal_add_axis(input::axis::MoveY, "MoveY"); + m_mouse->internal_add_axis(input::axis::ScreenMoveX, "ScreenMoveX"); + m_mouse->internal_add_axis(input::axis::ScreenMoveY, "ScreenMoveY"); + m_mouse->internal_add_axis(input::axis::Wheel, "Wheel"); } // Lookup tables for scancode and key name lookups @@ -135,34 +138,15 @@ void InputDevice::internal_add_button(input::button button, const char* name) m_button_names[(int)button] = name; } -void InputDevice::AddAxis(int index, const char* name, float sensitivity) +void InputDevice::internal_add_axis(input::axis axis, const char* name) { - if (index == -1) - index = (int)m_axis_names.size(); - - while (index >= (int)m_axis_names.size()) + while ((int)axis >= (int)m_axis_names.size()) { m_axis_names.push_back(""); - m_axis.push(0.0f, 1.0f); - } - - m_axis_names[index] = name; - m_axis[index].m1 = 0.0f; - m_axis[index].m2 = sensitivity; -} - -void InputDevice::AddCursor(int index, const char* name) -{ - if (index == -1) - index = (int)m_cursor_names.size(); - - while (index >= (int)m_cursor_names.size()) - { - m_cursor_names.push_back(""); - m_cursors.push_back(cursor_state()); + m_axes.push_back(0.0f); } - m_cursor_names[index] = name; + m_axis_names[(int)axis] = name; } } /* namespace lol */ diff --git a/src/ui/input.h b/src/ui/input.h index 86cfe050..efd147b0 100644 --- a/src/ui/input.h +++ b/src/ui/input.h @@ -25,19 +25,6 @@ extern const std::string g_name_mouse; extern const std::string g_name_keyboard; extern std::string g_name_joystick(const uint64_t num); -// Mouse default buttons/axis -const std::string g_name_mouse_key_left("Left"); -const std::string g_name_mouse_key_middle("Middle"); -const std::string g_name_mouse_key_right("Right"); -const std::string g_name_mouse_key_in_screen("InScreen"); - -const std::string g_name_mouse_axis_x("X"); -const std::string g_name_mouse_axis_y("Y"); -const std::string g_name_mouse_axis_xpixel("XPixel"); -const std::string g_name_mouse_axis_ypixel("YPixel"); -const std::string g_name_mouse_axis_scroll("Scroll"); -const std::string g_name_mouse_cursor("Cursor"); - class input { public: @@ -62,6 +49,27 @@ public: #include "ui/buttons.inc" }; + enum class axis : uint16_t + { + // Mouse + Wheel, + X, + Y, + ScreenX, + ScreenY, + MoveX, + MoveY, + ScreenMoveX, + ScreenMoveY, + // Joystick + LeftX, + LeftY, + RightX, + RightY, + LeftShoulder, + RightShoulder, + }; + static std::vector const &all_keys(); static std::string const &key_to_name(key k); static key name_to_key(std::string const &name); @@ -125,6 +133,9 @@ public: void bind(input::button button, uint16_t event); void unbind(input::button button, uint16_t event); + void bind(input::axis axis, uint16_t event); + void unbind(input::axis axis, uint16_t event); + // // Key, button etc. state // @@ -155,6 +166,16 @@ public: return m_pressed_buttons.find(button) != m_pressed_buttons.end(); } + // Get the current state of the given axis + float axis(input::axis axis) const { return m_axes[(int)axis]; } + + bool axis_changed(input::axis axis) const + { + return m_changed_axes.find(axis) != m_changed_axes.end(); + } + + // TODO: axis sensitivity was removed + // // Keyboard-specific section // @@ -179,86 +200,24 @@ public: void capture(bool value) { m_capture = value; } bool capture() const { return m_capture; } - /** 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(int index) const - { - return m_axis[index].m1 * m_axis[index].m2; - } - - /** Gets the current value of the given cursor, 0,0 being the bottom-left - * corner and 1,1 being the top-right corner */ - vec2 get_cursor_uv(int index) const - { - return m_cursors[index].uv; - } - - /** Gets the coordinate of the pixel the cursor is currently over, - * 0,0 being the bottom-left corner. */ - ivec2 get_cursor_pixel(int index) const - { - return m_cursors[index].pixel; - } - - /** Sets a per-device-axis sensitivity factor. The value returned by - * the operating system will be multiplied by this value before being - * returned by GetAxis */ - void SetAxisSensitivity(int index, float sensitivity) - { - m_axis[index].m2 = sensitivity; - } - - /** Gets the per-device-axis sensitivity factor. The value returned by - * the operating system will be multiplied by this value before being - * returned by GetAxis */ - float GetAxisSensitivity(int index) const - { - return m_axis[index].m2; - } - - /** Gets a list of the name of all available axis in this device */ - const std::vector& GetAllAxis() const - { - return m_axis_names; - } - /** Gets a list of the name of all available cursors in this device */ - const std::vector& GetAllCursors() const - { - return m_cursor_names; - } - /** Gets an input device by its name */ static InputDevice* Get(std::string const &name) { - return GetDevice(name); - } - - /** Default helpers */ - static InputDevice* GetJoystick(const uint64_t num) - { - return GetDevice(g_name_joystick(num)); + for (int i = 0; i < devices.count(); ++i) + { + if (devices[i]->m_name == name) + return devices[i]; + } + return nullptr; } public: /** Internal functions that allow to construct an InputDevice * dynamically, when the keys, axis and cursors are not known at * compile time. */ - void internal_add_key(input::key, char const * name); - void internal_add_button(input::button, char const * name); - - void AddAxis(int id, char const * name, float sensitivity = 1.0f); - - inline void AddAxis(char const * name, float sensitivity = 1.0f) - { - AddAxis(-1, name, sensitivity); - } - - void AddCursor(int id, char const * name); - - inline void AddCursor(char const * name) - { - AddCursor(-1, name); - } + void internal_add_key(input::key, char const *name); + void internal_add_button(input::button, char const *name); + void internal_add_axis(input::axis, char const *name); /* Internal functions for the platform-specific drivers. */ void internal_begin_frame() @@ -267,12 +226,7 @@ public: m_released_keys.clear(); m_pressed_buttons.clear(); m_released_buttons.clear(); - } - - void internal_set_cursor(int id, vec2 const & position, ivec2 const & pixel) - { - m_cursors[id].uv = position; - m_cursors[id].pixel = pixel; + m_changed_axes.clear(); } void internal_set_key(input::key key, bool state) @@ -289,14 +243,16 @@ public: m_buttons[(int)button] = state; } - void internal_add_text(std::string const &text) + void internal_set_axis(input::axis axis, float value) { - m_text += text; + if (m_axes[(int)axis] != value) + m_changed_axes.insert(axis); + m_axes[(int)axis] = value; } - void internal_set_axis(int id, float value) + void internal_add_text(std::string const &text) { - m_axis[id].m1 = value; + m_text += text; } protected: @@ -307,25 +263,19 @@ protected: std::vector m_key_names; std::vector m_button_names; std::vector m_axis_names; - std::vector m_cursor_names; /** Key and button states (pressed/released) */ std::vector m_keys; std::unordered_set m_pressed_keys, m_released_keys; std::vector m_buttons; std::unordered_set m_pressed_buttons, m_released_buttons; + std::vector m_axes; + std::unordered_set m_changed_axes; - /** Text input state */ + // Text input state std::string m_text; bool m_input_active; - /** Axis states (value and sensitivity) */ - array m_axis; - - /** Cursor position */ - struct cursor_state { vec2 uv; ivec2 pixel; }; - std::vector m_cursors; - // Capture (for mouse devices) bool m_capture = false; @@ -340,16 +290,6 @@ private: return (int)i; return -1; } - - static InputDevice* GetDevice(std::string const &name) - { - for (int i = 0; i < devices.count(); ++i) - { - if (devices[i]->m_name == name) - return devices[i]; - } - return nullptr; - } }; } /* namespace lol */ diff --git a/src/ui/sdl-input.cpp b/src/ui/sdl-input.cpp index 58695ef6..0cd9e6d4 100644 --- a/src/ui/sdl-input.cpp +++ b/src/ui/sdl-input.cpp @@ -85,7 +85,7 @@ SdlInput::SdlInput(int app_w, int app_h, int screen_w, int screen_h) InputDevice* stick = new InputDevice(g_name_joystick(i + 1)); for (int j = 0; j < SDL_JoystickNumAxes(sdlstick); ++j) - stick->AddAxis(format("Axis%d", j + 1).c_str()); + stick->internal_add_axis((input::axis)(j + 1), format("Axis%d", j + 1).c_str()); for (int j = 0; j < SDL_JoystickNumButtons(sdlstick); ++j) stick->internal_add_button((input::button)(j + 1), format("Button%d", j + 1).c_str()); @@ -142,12 +142,12 @@ void SdlInput::tick(float seconds) for (int i = 0; i < SDL_JoystickNumButtons(m_joysticks[j].m1); i++) m_joysticks[j].m2->internal_set_button((input::button)i, SDL_JoystickGetButton(m_joysticks[j].m1, i) != 0); for (int i = 0; i < SDL_JoystickNumAxes(m_joysticks[j].m1); i++) - m_joysticks[j].m2->internal_set_axis(i, (float)SDL_JoystickGetAxis(m_joysticks[j].m1, i) / 32768.f); + m_joysticks[j].m2->internal_set_axis((input::axis)i, (float)SDL_JoystickGetAxis(m_joysticks[j].m1, i) / 32768.f); } # endif keyboard->internal_begin_frame(); - mouse->internal_set_axis(4, 0); + float mouse_wheel = 0.f; if (keyboard->capture_text()) SDL_StartTextInput(); @@ -204,7 +204,7 @@ void SdlInput::tick(float seconds) event.type == SDL_MOUSEBUTTONDOWN); break; case SDL_MOUSEWHEEL: - mouse->internal_set_axis(4, (float)event.button.y); + mouse_wheel += (float)event.button.y; break; case SDL_WINDOWEVENT: { @@ -239,6 +239,13 @@ void SdlInput::tick(float seconds) } /* Handle mouse input */ + mouse->internal_set_axis(input::axis::Wheel, mouse_wheel); + + ivec2 window_size; + // FIXME: get actual window size + //SDL_GetWindowSize(m_window, &window_size.x, &window_size.y); + window_size = (ivec2)m_app; + ivec2 mouse_pos(-1, -1); SDL_GetMouseState(&mouse_pos.x, &mouse_pos.y); mouse_pos.y = Video::GetSize().y - 1 - mouse_pos.y; @@ -247,32 +254,35 @@ void SdlInput::tick(float seconds) { m_mousecapture = mouse->capture(); SDL_SetRelativeMouseMode(m_mousecapture ? SDL_TRUE : SDL_FALSE); - mouse_pos = ivec2(m_app * .5f); - // FIXME: how do I warped mouse? - //SDL_WarpMouse((uint16_t)mouse_pos.x, (uint16_t)mouse_pos.y); - //SDL_ShowCursor(m_mousecapture ? SDL_DISABLE : SDL_ENABLE); + // FIXME: get handle to window + //SDL_WarpMouseInWindow(m_window, window_size.x / 2, window_size.y / 2); + SDL_ShowCursor(m_mousecapture ? SDL_DISABLE : SDL_ENABLE); } - if (mouse_pos.x >= 0 && mouse_pos.x < m_app.x && mouse_pos.y >= 0 && mouse_pos.y < m_app.y) + if (mouse_pos.x >= 0 && mouse_pos.x < window_size.x && mouse_pos.y >= 0 && mouse_pos.y < window_size.y) { //We need the max if we want coherent mouse speed between axis float max_screen_size = lol::max(m_screen.x, m_screen.y); vec2 vmouse = vec2(mouse_pos); vec2 vprevmouse = vec2(m_prev_mouse_pos); - mouse->internal_set_cursor(0, vmouse / m_app, mouse_pos); + mouse->internal_set_axis(input::axis::X, vmouse.x / window_size.x); + mouse->internal_set_axis(input::axis::Y, vmouse.y / window_size.y); + mouse->internal_set_axis(input::axis::ScreenX, mouse_pos.x); + mouse->internal_set_axis(input::axis::ScreenY, mouse_pos.y); + // Note: 100.0f is an arbitrary value that makes it feel about the same than an xbox controller joystick - mouse->internal_set_axis(0, (mouse_pos.x - vprevmouse.x) * MOUSE_SPEED_MOD / max_screen_size); + mouse->internal_set_axis(input::axis::MoveX, (mouse_pos.x - vprevmouse.x) * MOUSE_SPEED_MOD / max_screen_size); // Y Axis is also negated to match the usual joystick Y axis (negatives values are for the upper direction) - mouse->internal_set_axis(1,-(mouse_pos.y - vprevmouse.y) * MOUSE_SPEED_MOD / max_screen_size); + mouse->internal_set_axis(input::axis::MoveY,-(mouse_pos.y - vprevmouse.y) * MOUSE_SPEED_MOD / max_screen_size); //Pixel movement - mouse->internal_set_axis(2, (mouse_pos.x - vprevmouse.x)); - mouse->internal_set_axis(3,-(mouse_pos.y - vprevmouse.y)); + mouse->internal_set_axis(input::axis::ScreenMoveX, (mouse_pos.x - vprevmouse.x)); + mouse->internal_set_axis(input::axis::ScreenMoveY,-(mouse_pos.y - vprevmouse.y)); } if (m_mousecapture) { - mouse_pos = ivec2(m_app * .5f); + //mouse_pos = ivec2(window_size * .5f); //SDL_WarpMouse((uint16_t)mouse_pos.x, (uint16_t)mouse_pos.y); } diff --git a/src/ui/sdl-input.h b/src/ui/sdl-input.h index a5455305..bb08de19 100644 --- a/src/ui/sdl-input.h +++ b/src/ui/sdl-input.h @@ -48,10 +48,10 @@ private: array m_joysticks; - ivec2 m_prev_mouse_pos = ivec2::zero; vec2 m_app; vec2 m_screen; + ivec2 m_prev_mouse_pos = ivec2::zero; bool m_mousecapture = false; bool m_tick_in_draw_thread = false; };