diff --git a/src/ui/controller.cpp b/src/ui/controller.cpp index f99a34a0..cb6d9ce5 100644 --- a/src/ui/controller.cpp +++ b/src/ui/controller.cpp @@ -217,8 +217,8 @@ float AxisBinding::RetrieveCurrentValue() /////////////////////////////////////////////////////////////////////////////// // Controller -array Controller::controllers; -uint32_t Controller::m_active_layer = ~((uint32_t)0); +static std::map g_controllers; +static uint32_t g_active_layer = ~((uint32_t)0); //----------------------------------------------------------------------------- Controller::Controller(std::string const &name) @@ -228,11 +228,9 @@ Controller::Controller(std::string const &name) m_activate_nextframe = true; m_deactivate_nextframe = false; m_active = false; - if (Get(name) != nullptr) - { + if (g_controllers[name]) msg::warn("controller ā€œ%sā€ has already been registered\n", name.c_str()); - } - controllers.push(this); + g_controllers[name] = this; } Controller::Controller(std::string const &name, InputProfile const& profile) @@ -244,14 +242,8 @@ Controller::Controller(std::string const &name, InputProfile const& profile) Controller::~Controller() { ClearProfile(); - for (int i = 0; i < controllers.count(); ++i) - { - if (controllers[i] == this) - { - controllers.remove(i); - break; - } - } + if (g_controllers[m_name] == this) + g_controllers.erase(m_name); } //Init mode 1: Input profile system ------------------------------------------- @@ -276,7 +268,7 @@ uint32_t Controller::GetLayerMask() } bool Controller::IsLayerActive() { - return !!(m_layer_mask & m_active_layer); + return !!(m_layer_mask & g_active_layer); } //GetKeys/Axis stuff ---------------------------------------------------------- @@ -287,7 +279,7 @@ KeyBinding& Controller::GetKey(int index) AxisBinding& Controller::GetAxis(int index) { - return m_axis[index]; + return m_axis_bindings[index]; } // Key methods: should not go directly to binding @@ -318,23 +310,14 @@ bool Controller::WasKeyReleasedThisFrame(int index) const //Axis methods: should not go directly to binding ----------------------------- float Controller::GetAxisValue(int index) const { - auto axis = m_axis.find(index); - return axis != m_axis.end() ? axis->second.GetValue() : 0.f; + 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.find(index); - return axis != m_axis.end() ? axis->second.GetDelta() : 0.f; -} - -//----------------------------------------------------------------------------- -Controller* Controller::Get(std::string const &name) -{ - for (auto controller : controllers) - if (controller->m_name == name) - return controller; - return nullptr; + auto axis = m_axis_bindings.find(index); + return axis != m_axis_bindings.end() ? axis->second.GetDelta() : 0.f; } //Input profile system -------------------------------------------------------- @@ -374,7 +357,7 @@ void Controller::UnbindProfile() if (m_joystick_idx.find(axis.m_joy) != INDEX_NONE) GetAxis(axis.m_idx).UnbindJoystick(axis.m_joy, axis.m_name); } - m_joystick.clear(); + m_joysticks.clear(); m_joystick_idx.clear(); m_mutex.unlock(); @@ -411,7 +394,7 @@ void Controller::BindProfile(InputProfile const& setup) class InputDevice* joystick = InputDevice::GetJoystick(joy_idx); if (joystick) { - m_joystick << joystick; + m_joysticks << joystick; m_joystick_idx << joy_idx; } } @@ -439,7 +422,7 @@ void Controller::tick_game(float seconds) for (auto &kv : m_key_bindings) kv.second.Update(); - for (auto &kv : m_axis) + for (auto &kv : m_axis_bindings) kv.second.Update(); } @@ -471,12 +454,12 @@ array Controller::DeactivateAll() { array result; - for (int i = 0; i < controllers.count(); ++i) + for (auto it : g_controllers) { - if (controllers[i]->m_active || controllers[i]->m_activate_nextframe) + if (it.second->m_active || it.second->m_activate_nextframe) { - result.push(controllers[i]); - controllers[i]->Deactivate(); + result.push(it.second); + it.second->Deactivate(); } } diff --git a/src/ui/controller.h b/src/ui/controller.h index aa6301e4..9a41b8a6 100644 --- a/src/ui/controller.h +++ b/src/ui/controller.h @@ -23,11 +23,6 @@ namespace lol class KeyBinding { friend class Controller; -public: - KeyBinding() - : m_current(false), - m_previous(false) - {} protected: //Status methods ---------------------------------------------------------- @@ -61,28 +56,21 @@ protected: 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(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; + bool m_current = false; /** Value at the current frame */ - bool m_previous; + bool m_previous = false; }; //----------------------------------------------------------------------------- class AxisBinding { friend class Controller; -public: - AxisBinding() - : m_current(0.0f), - m_previous(0.0f) - {} protected: //Status methods ---------------------------------------------------------- @@ -120,20 +108,20 @@ protected: } float RetrieveCurrentValue(); - /** m1 is the InputDevice, m2 is the axis index on the InputDevice, m3 and m4 are an optional key indices to bind one or two keys over the axis */ + /** 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; - float m_previous; + float m_current = 0.0f; + float m_previous = 0.0f; }; //------------------------------------------------------------------------- class InputProfile { friend class Controller; + private: - //--------------------------------------------------------------------- class Key { friend class Controller; @@ -148,7 +136,7 @@ private: int m_idx = 0; std::string m_name; }; - //--------------------------------------------------------------------- + class Joystick { friend class Controller; @@ -164,8 +152,8 @@ private: int m_idx = 0; std::string m_name; }; + public: - //--------------------------------------------------------------------- class KeyboardKey : public Key { friend class Controller; @@ -173,7 +161,7 @@ public: public: KeyboardKey(int idx, std::string const& name) : Key(idx, name) { } }; - //--------------------------------------------------------------------- + class MouseKey : public Key { friend class Controller; @@ -181,7 +169,7 @@ public: public: MouseKey(int idx, std::string const& name) : Key(idx, name) { } }; - //--------------------------------------------------------------------- + class MouseAxis : public Key { friend class Controller; @@ -189,7 +177,7 @@ public: public: MouseAxis(int idx, std::string const& name) : Key(idx, name) { } }; - //--------------------------------------------------------------------- + class JoystickKey : public Joystick { friend class Controller; @@ -197,7 +185,7 @@ public: public: JoystickKey(uint64_t joy, int idx, std::string const& name) : Joystick(joy, idx, name) { } }; - //--------------------------------------------------------------------- + class JoystickAxis : public Joystick { friend class Controller; @@ -205,6 +193,7 @@ public: public: JoystickAxis(uint64_t joy, int idx, std::string const& name) : Joystick(joy, idx, name) { } }; + public: InputProfile() = default; virtual ~InputProfile() = default; @@ -349,9 +338,6 @@ public: /** Gets the current delta value of this axis */ float GetAxisDelta(int index) const; - /** Get named controller */ - static Controller* Get(std::string const &name); - protected: /** Input profile system */ void UnbindProfile(); @@ -360,10 +346,8 @@ protected: private: uint32_t m_layer_mask = 1; // plugged on the first by default std::map m_key_bindings; - std::map m_axis; + std::map m_axis_bindings; - static uint32_t m_active_layer; // All active by default - static array controllers; std::string m_name; bool m_activate_nextframe; bool m_deactivate_nextframe; @@ -374,7 +358,7 @@ private: class InputProfile m_profile; class InputDevice* m_keyboard = nullptr; class InputDevice* m_mouse = nullptr; - array m_joystick; + array m_joysticks; array m_joystick_idx; };