From 24348f6990db7be95e62347d9175d44aed2088bc Mon Sep 17 00:00:00 2001 From: kalaposfos13 <153381648+kalaposfos13@users.noreply.github.com> Date: Fri, 18 Oct 2024 10:44:19 +0200 Subject: [PATCH] Encapsulated globals and new classes in a new namespace --- src/sdl_window.cpp | 145 +++++++++++++++++++++++---------------------- src/sdl_window.h | 16 ++++- 2 files changed, 87 insertions(+), 74 deletions(-) diff --git a/src/sdl_window.cpp b/src/sdl_window.cpp index d2379511b..7cbc58646 100644 --- a/src/sdl_window.cpp +++ b/src/sdl_window.cpp @@ -53,61 +53,11 @@ Uint32 getMouseWheelEvent(const SDL_Event* event) { return 0; } -namespace Frontend { +namespace KBMConfig { using Libraries::Pad::OrbisPadButtonDataOffset; -KeyBinding::KeyBinding(const SDL_Event* event) { - modifier = getCustomModState(); - key = 0; - // std::cout << "Someone called the new binding ctor!\n"; - if (event->type == SDL_EVENT_KEY_DOWN || event->type == SDL_EVENT_KEY_UP) { - key = event->key.key; - } else if (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN || - event->type == SDL_EVENT_MOUSE_BUTTON_UP) { - key = event->button.button; - } else if (event->type == SDL_EVENT_MOUSE_WHEEL) { - key = getMouseWheelEvent(event); - } else { - std::cout << "We don't support this event type!\n"; - } -} - -bool KeyBinding::operator<(const KeyBinding& other) const { - return std::tie(key, modifier) < std::tie(other.key, other.modifier); -} - -// modifiers are bitwise or-d together, so we need to check if ours is in that -template -typename std::map::const_iterator FindKeyAllowingPartialModifiers( - const std::map& map, KeyBinding binding) { - for (typename std::map::const_iterator it = map.cbegin(); it != map.cend(); - it++) { - if ((it->first.key == binding.key) && (it->first.modifier & binding.modifier) != 0) { - return it; - } - } - return map.end(); // Return end if no match is found -} -template -typename std::map::const_iterator FindKeyAllowingOnlyNoModifiers( - const std::map& map, KeyBinding binding) { - for (typename std::map::const_iterator it = map.cbegin(); it != map.cend(); - it++) { - if (it->first.key == binding.key && it->first.modifier == SDL_KMOD_NONE) { - return it; - } - } - return map.end(); // Return end if no match is found -} - -// Axis map: maps key+modifier to controller axis and axis value -struct AxisMapping { - Input::Axis axis; - int value; // Value to set for key press (+127 or -127 for movement) -}; - // i strongly suggest you collapse these maps -std::map string_to_cbutton_map = { +const std::map string_to_cbutton_map = { {"triangle", OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_TRIANGLE}, {"circle", OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_CIRCLE}, {"cross", OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_CROSS}, @@ -127,7 +77,7 @@ std::map string_to_cbutton_map = { {"leftjoystick_halfmode", LEFTJOYSTICK_HALFMODE}, {"rightjoystick_halfmode", RIGHTJOYSTICK_HALFMODE}, }; -std::map string_to_axis_map = { +const std::map string_to_axis_map = { {"axis_left_x_plus", {Input::Axis::LeftX, 127}}, {"axis_left_x_minus", {Input::Axis::LeftX, -127}}, {"axis_left_y_plus", {Input::Axis::LeftY, 127}}, @@ -137,7 +87,7 @@ std::map string_to_axis_map = { {"axis_right_y_plus", {Input::Axis::RightY, 127}}, {"axis_right_y_minus", {Input::Axis::RightY, -127}}, }; -std::map string_to_keyboard_key_map = { +const std::map string_to_keyboard_key_map = { {"a", SDLK_A}, {"b", SDLK_B}, {"c", SDLK_C}, @@ -234,7 +184,7 @@ std::map string_to_keyboard_key_map = { {"kpequals", SDLK_KP_EQUALS}, {"capslock", SDLK_CAPSLOCK}, }; -std::map string_to_keyboard_mod_key_map = { +const std::map string_to_keyboard_mod_key_map = { {"lshift", SDL_KMOD_LSHIFT}, {"rshift", SDL_KMOD_RSHIFT}, {"lctrl", SDL_KMOD_LCTRL}, {"rctrl", SDL_KMOD_RCTRL}, {"lalt", SDL_KMOD_LALT}, {"ralt", SDL_KMOD_RALT}, @@ -251,22 +201,6 @@ std::map button_map = {}; std::map axis_map = {}; std::map> key_to_modkey_toggle_map = {}; -SDL_Keymod KeyBinding::getCustomModState() { - SDL_Keymod state = SDL_GetModState(); - for (auto mod_flag : key_to_modkey_toggle_map) { - if (mod_flag.second.second) { - state |= mod_flag.second.first; - } - } - return state; -} - -// Flags and values for varying purposes -int mouse_joystick_binding = 0; -float mouse_deadzone_offset = 0.5, mouse_speed = 1, mouse_speed_offset = 0.125; -Uint32 mouse_polling_id = 0; -bool mouse_enabled = false, leftjoystick_halfmode = false, rightjoystick_halfmode = false; - // i wrapped it in a function so I can collapse it std::string getDefaultKeyboardConfig() { std::string default_config = @@ -334,6 +268,75 @@ axis_left_y_plus = s; return default_config; } +// Flags and values for varying purposes +int mouse_joystick_binding = 0; +float mouse_deadzone_offset = 0.5, mouse_speed = 1, mouse_speed_offset = 0.125; +Uint32 mouse_polling_id = 0; +bool mouse_enabled = false, leftjoystick_halfmode = false, rightjoystick_halfmode = false; + +KeyBinding::KeyBinding(const SDL_Event* event) { + modifier = getCustomModState(); + key = 0; + // std::cout << "Someone called the new binding ctor!\n"; + if (event->type == SDL_EVENT_KEY_DOWN || event->type == SDL_EVENT_KEY_UP) { + key = event->key.key; + } else if (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN || + event->type == SDL_EVENT_MOUSE_BUTTON_UP) { + key = event->button.button; + } else if (event->type == SDL_EVENT_MOUSE_WHEEL) { + key = getMouseWheelEvent(event); + } else { + std::cout << "We don't support this event type!\n"; + } +} + +bool KeyBinding::operator<(const KeyBinding& other) const { + return std::tie(key, modifier) < std::tie(other.key, other.modifier); +} + +SDL_Keymod KeyBinding::getCustomModState() { + SDL_Keymod state = SDL_GetModState(); + for (auto mod_flag : KBMConfig::key_to_modkey_toggle_map) { + if (mod_flag.second.second) { + state |= mod_flag.second.first; + } + } + return state; +} + +} // namespace KBMConfig + +namespace Frontend { +using Libraries::Pad::OrbisPadButtonDataOffset; + +using namespace KBMConfig; +using KBMConfig::AxisMapping; +using KBMConfig::KeyBinding; + +// modifiers are bitwise or-d together, so we need to check if ours is in that +template +typename std::map::const_iterator FindKeyAllowingPartialModifiers( + const std::map& map, KeyBinding binding) { + for (typename std::map::const_iterator it = map.cbegin(); it != map.cend(); + it++) { + if ((it->first.key == binding.key) && (it->first.modifier & binding.modifier) != 0) { + return it; + } + } + return map.end(); // Return end if no match is found +} +template +typename std::map::const_iterator FindKeyAllowingOnlyNoModifiers( + const std::map& map, KeyBinding binding) { + for (typename std::map::const_iterator it = map.cbegin(); it != map.cend(); + it++) { + if (it->first.key == binding.key && it->first.modifier == SDL_KMOD_NONE) { + return it; + } + } + return map.end(); // Return end if no match is found +} + void WindowSDL::parseInputConfig(const std::string& filename) { // Read configuration file. const auto config_file = Common::FS::GetUserPath(Common::FS::PathType::UserDir) / filename; diff --git a/src/sdl_window.h b/src/sdl_window.h index 87ba43bd4..4cee2369f 100644 --- a/src/sdl_window.h +++ b/src/sdl_window.h @@ -5,6 +5,7 @@ #include #include "common/types.h" +#include "input/controller.h" #include @@ -16,7 +17,8 @@ namespace Input { class GameController; } -namespace Frontend { +namespace KBMConfig { +std::string getDefaultKeyboardConfig(); class KeyBinding { public: @@ -29,6 +31,14 @@ public: static SDL_Keymod getCustomModState(); }; +struct AxisMapping { + Input::Axis axis; + int value; // Value to set for key press (+127 or -127 for movement) +}; +} // namespace KBMConfig + +namespace Frontend { + enum class WindowSystemType : u8 { Headless, Windows, @@ -88,8 +98,8 @@ private: void onGamepadEvent(const SDL_Event* event); int sdlGamepadToOrbisButton(u8 button); - void updateModKeyedInputsManually(KeyBinding& binding); - void updateButton(KeyBinding& binding, u32 button, bool isPressed); + void updateModKeyedInputsManually(KBMConfig::KeyBinding& binding); + void updateButton(KBMConfig::KeyBinding& binding, u32 button, bool isPressed); static Uint32 keyRepeatCallback(void* param, Uint32 id, Uint32 interval); static Uint32 mousePolling(void* param, Uint32 id, Uint32 interval);