From 2648fc50e4a944a27af5a7fcf555f806a51ca44d Mon Sep 17 00:00:00 2001 From: Phillip Stephens Date: Sat, 19 Feb 2022 19:22:03 -0800 Subject: [PATCH] aurora: Implement all major input events (still missing scroll events) --- Runtime/CMain.cpp | 30 ++++---- aurora/include/aurora/aurora.hpp | 16 ++++ aurora/lib/aurora.cpp | 75 ++++++++++++++----- aurora/lib/input.cpp | 125 ++++++++++++++++++++++++++++++- aurora/lib/input.hpp | 14 +++- 5 files changed, 218 insertions(+), 42 deletions(-) diff --git a/Runtime/CMain.cpp b/Runtime/CMain.cpp index aa6af5cce..99b095617 100644 --- a/Runtime/CMain.cpp +++ b/Runtime/CMain.cpp @@ -393,7 +393,6 @@ public: m_imGuiConsole.ShowAboutWindow(false, m_errorString); } - if (m_quitRequested) { if (g_mainMP1) { g_mainMP1->Quit(); @@ -474,7 +473,6 @@ public: } void onCharKeyDown(uint8_t code, aurora::ModifierKey mods, bool isRepeat) noexcept override { - Log.report(logvisor::Info, FMT_STRING("DEBUG CHAR KEYS: '{}', isRepeat {}"), static_cast(code), isRepeat); // if (!ImGuiWindowCallback::m_keyboardCaptured && g_mainMP1) { if (g_mainMP1) { if (MP1::CGameArchitectureSupport* as = g_mainMP1->GetArchSupport()) { @@ -485,7 +483,6 @@ public: } void onCharKeyUp(uint8_t code, aurora::ModifierKey mods) noexcept override { - Log.report(logvisor::Info, FMT_STRING("DEBUG CHAR KEYS: '{}'"), static_cast(code)); if (g_mainMP1) { if (MP1::CGameArchitectureSupport* as = g_mainMP1->GetArchSupport()) { as->charKeyUp(code, mods); @@ -494,8 +491,6 @@ public: } void onSpecialKeyDown(aurora::SpecialKey key, aurora::ModifierKey mods, bool isRepeat) noexcept override { - Log.report(logvisor::Info, FMT_STRING("DEBUG KEYS: SpecialKey {}, isRepeat {}"), key, isRepeat); - /* TODO: Temporarily convert the aurora enum to boo's until we refactor everything */ if (g_mainMP1) { if (MP1::CGameArchitectureSupport* as = g_mainMP1->GetArchSupport()) { as->specialKeyDown(key, mods, isRepeat); @@ -511,7 +506,6 @@ public: } void onSpecialKeyUp(aurora::SpecialKey key, aurora::ModifierKey mods) noexcept override { - /* TODO: Temporarily convert the aurora enum to boo's until we refactor everything */ if (g_mainMP1) { if (MP1::CGameArchitectureSupport* as = g_mainMP1->GetArchSupport()) { as->specialKeyUp(key, mods); @@ -519,13 +513,17 @@ public: } } - void onImGuiInit(float scale) noexcept override { - ImGuiEngine_Initialize(scale); - } + void onTextInput(const std::string& text) noexcept override {} - void onImGuiAddTextures() noexcept override { - ImGuiEngine_AddTextures(); - } + void onModifierKeyDown(aurora::ModifierKey mods, bool isRepeat) noexcept override {} + void onModifierKeyUp(aurora::ModifierKey mods) noexcept override {} + + void onMouseMove(int32_t x, int32_t y, int32_t xrel, int32_t yrel, aurora::MouseButton state) noexcept override {} + void onMouseButtonDown(int32_t x, int32_t y, aurora::MouseButton button, int32_t clicks) noexcept override {} + void onMouseButtonUp(int32_t x, int32_t y, aurora::MouseButton button) noexcept override {} + void onImGuiInit(float scale) noexcept override { ImGuiEngine_Initialize(scale); } + + void onImGuiAddTextures() noexcept override { ImGuiEngine_AddTextures(); } [[nodiscard]] std::string getGraphicsApi() const { return m_cvarCommons.getGraphicsApi(); } @@ -584,10 +582,10 @@ static bool IsClientLoggingEnabled(int argc, char** argv) { #if !WINDOWS_STORE int main(int argc, char** argv) { - //TODO: This seems to fix a lot of weird issues with rounding - // but breaks animations, need to research why this is the case - // for now it's disabled - //fesetround(FE_TOWARDZERO); + // TODO: This seems to fix a lot of weird issues with rounding + // but breaks animations, need to research why this is the case + // for now it's disabled + // fesetround(FE_TOWARDZERO); if (argc > 1 && !hecl::StrCmp(argv[1], "--dlpackage")) { fmt::print(FMT_STRING("{}\n"), METAFORCE_DLPACKAGE); return 100; diff --git a/aurora/include/aurora/aurora.hpp b/aurora/include/aurora/aurora.hpp index b1d07b678..de6726f6c 100644 --- a/aurora/include/aurora/aurora.hpp +++ b/aurora/include/aurora/aurora.hpp @@ -175,6 +175,16 @@ struct WindowSize { uint32_t width; uint32_t height; }; +enum class MouseButton { + None = 0, + Primary = 1 << 0, + Middle = 1 << 1, + Secondary = 1 << 2, + Aux1 = 1 << 3, + Aux2 = 1 << 4, +}; +ENABLE_BITWISE_ENUM(MouseButton); + enum class Backend : uint8_t { Invalid, Vulkan, @@ -212,6 +222,12 @@ struct AppDelegate { virtual void onCharKeyUp(uint8_t charCode, ModifierKey mods) noexcept = 0; virtual void onSpecialKeyDown(SpecialKey key, ModifierKey mods, bool isRepeat) noexcept = 0; virtual void onSpecialKeyUp(SpecialKey key, ModifierKey mods) noexcept = 0; + virtual void onModifierKeyDown(ModifierKey mods, bool isRepeat) noexcept = 0; + virtual void onModifierKeyUp(ModifierKey mods) noexcept = 0; + virtual void onTextInput(const std::string& input) noexcept = 0; + virtual void onMouseMove(int32_t x, int32_t y, int32_t xrel, int32_t yrel, MouseButton state) noexcept = 0; + virtual void onMouseButtonDown(int32_t x, int32_t y, MouseButton button, int32_t clicks) noexcept = 0; + virtual void onMouseButtonUp(int32_t x, int32_t y, MouseButton button) noexcept = 0; // Controller virtual void onControllerAdded(uint32_t which) noexcept = 0; diff --git a/aurora/lib/aurora.cpp b/aurora/lib/aurora.cpp index 3605598ef..6f9b06d6b 100644 --- a/aurora/lib/aurora.cpp +++ b/aurora/lib/aurora.cpp @@ -64,16 +64,13 @@ static bool poll_events() noexcept { g_AppDelegate->onAppWindowMoved(event.window.data1, event.window.data2); break; } + case SDL_WINDOWEVENT_SIZE_CHANGED: case SDL_WINDOWEVENT_RESIZED: { gpu::resize_swapchain(event.window.data1, event.window.data2); g_AppDelegate->onAppWindowResized( {static_cast(event.window.data1), static_cast(event.window.data2)}); break; } - case SDL_WINDOWEVENT_SIZE_CHANGED: { - // TODO: handle size changed event - break; - } case SDL_WINDOWEVENT_MINIMIZED: { // TODO: handle minimized event break; @@ -136,31 +133,77 @@ static bool poll_events() noexcept { case SDL_CONTROLLERBUTTONUP: case SDL_CONTROLLERBUTTONDOWN: { g_AppDelegate->onControllerButton( - event.cbutton.which, input::translate_button(static_cast(event.cbutton.button)), + event.cbutton.which, + input::translate_controller_button(static_cast(event.cbutton.button)), event.cbutton.state == SDL_PRESSED); break; } case SDL_CONTROLLERAXISMOTION: { - g_AppDelegate->onControllerAxis(event.caxis.which, - input::translate_axis(static_cast(event.caxis.axis)), - event.caxis.value); + g_AppDelegate->onControllerAxis( + event.caxis.which, input::translate_controller_axis(static_cast(event.caxis.axis)), + event.caxis.value); break; } case SDL_KEYDOWN: { if (!ImGui::GetIO().WantCaptureKeyboard) { - // TODO + SpecialKey specialKey{}; + ModifierKey modifierKey{}; + char chr = input::translate_key(event.key.keysym, specialKey, modifierKey); + if (chr != 0) { + modifierKey = input::translate_modifiers(event.key.keysym.mod); + g_AppDelegate->onCharKeyDown(chr, modifierKey, event.key.repeat != 0u); + } else if (specialKey != SpecialKey::None) { + modifierKey = input::translate_modifiers(event.key.keysym.mod); + g_AppDelegate->onSpecialKeyDown(specialKey, modifierKey, event.key.repeat != 0u); + } else if (modifierKey != ModifierKey::None) { + g_AppDelegate->onModifierKeyDown(modifierKey, event.key.repeat != 0u); + } } break; } case SDL_KEYUP: { if (!ImGui::GetIO().WantCaptureKeyboard) { - // TODO + SpecialKey specialKey{}; + ModifierKey modifierKey{}; + char chr = input::translate_key(event.key.keysym, specialKey, modifierKey); + if (chr != 0) { + modifierKey = input::translate_modifiers(event.key.keysym.mod); + g_AppDelegate->onCharKeyUp(chr, modifierKey); + } else if (specialKey != SpecialKey::None) { + modifierKey = input::translate_modifiers(event.key.keysym.mod); + g_AppDelegate->onSpecialKeyUp(specialKey, modifierKey); + } else if (modifierKey != ModifierKey::None) { + g_AppDelegate->onModifierKeyUp(modifierKey); + } } break; } case SDL_TEXTINPUT: { if (!ImGui::GetIO().WantCaptureKeyboard) { - // TODO + std::string str; + str.assign(&event.text.text[0], SDL_TEXTINPUTEVENT_TEXT_SIZE); + g_AppDelegate->onTextInput(str); + } + break; + } + case SDL_MOUSEBUTTONDOWN: { + if (!ImGui::GetIO().WantCaptureMouse) { + g_AppDelegate->onMouseButtonDown(event.button.x, event.button.y, + input::translate_mouse_button(event.button.button), event.button.clicks); + } + break; + } + case SDL_MOUSEBUTTONUP: { + if (!ImGui::GetIO().WantCaptureMouse) { + g_AppDelegate->onMouseButtonUp(event.button.x, event.button.y, + input::translate_mouse_button(event.button.button)); + } + break; + } + case SDL_MOUSEMOTION: { + if (!ImGui::GetIO().WantCaptureMouse) { + g_AppDelegate->onMouseMove(event.motion.x, event.motion.y, event.motion.xrel, event.motion.yrel, + input::translate_mouse_button_state(event.motion.state)); } break; } @@ -334,13 +377,7 @@ Backend get_backend() noexcept { std::string_view get_backend_string() noexcept { return magic_enum::enum_name(gpu::g_backendType); } void set_fullscreen(bool fullscreen) noexcept { - auto flags = SDL_GetWindowFlags(g_Window); - if (fullscreen) { - flags |= SDL_WINDOW_FULLSCREEN; - } else { - flags &= ~SDL_WINDOW_FULLSCREEN; - } - SDL_SetWindowFullscreen(g_Window, flags); + SDL_SetWindowFullscreen(g_Window, fullscreen ? SDL_WINDOW_FULLSCREEN : 0); } int32_t get_controller_player_index(uint32_t which) noexcept { @@ -354,6 +391,6 @@ bool is_controller_gamecube(uint32_t which) noexcept { } std::string get_controller_name(uint32_t instance) noexcept { - return input::name(instance); // TODO + return input::controller_name(instance); // TODO } } // namespace aurora diff --git a/aurora/lib/input.cpp b/aurora/lib/input.cpp index 9b7d21a17..8a1369d39 100644 --- a/aurora/lib/input.cpp +++ b/aurora/lib/input.cpp @@ -52,7 +52,7 @@ void set_player_index(Uint32 which, Sint32 index) { } } -std::string name(Uint32 which) { +std::string controller_name(Uint32 which) { if (g_GameControllers.find(which) != g_GameControllers.end()) { auto* name = SDL_GameControllerName(g_GameControllers[which].m_controller); if (name) { @@ -62,7 +62,7 @@ std::string name(Uint32 which) { return {}; } -ControllerButton translate_button(SDL_GameControllerButton btn) { +ControllerButton translate_controller_button(SDL_GameControllerButton btn) { switch (btn) { case SDL_CONTROLLER_BUTTON_A: return ControllerButton::A; @@ -99,7 +99,7 @@ ControllerButton translate_button(SDL_GameControllerButton btn) { } } -ControllerAxis translate_axis(SDL_GameControllerAxis axis) { +ControllerAxis translate_controller_axis(SDL_GameControllerAxis axis) { switch (axis) { case SDL_CONTROLLER_AXIS_LEFTX: return ControllerAxis::LeftX; @@ -117,4 +117,123 @@ ControllerAxis translate_axis(SDL_GameControllerAxis axis) { return ControllerAxis::MAX; } } + +char translate_key(SDL_Keysym sym, SpecialKey& specialSym, ModifierKey& modifierSym) { + specialSym = SpecialKey::None; + modifierSym = ModifierKey::None; + if (sym.sym >= SDLK_F1 && sym.sym <= SDLK_F12) { + specialSym = SpecialKey(int(SpecialKey::F1) + sym.sym - SDLK_F1); + } else if (sym.sym == SDLK_ESCAPE) { + specialSym = SpecialKey::Esc; + } else if (sym.sym == SDLK_RETURN) { + specialSym = SpecialKey::Enter; + } else if (sym.sym == SDLK_KP_ENTER) { + specialSym = SpecialKey::KpEnter; + } else if (sym.sym == SDLK_BACKSPACE) { + specialSym = SpecialKey::Backspace; + } else if (sym.sym == SDLK_INSERT) { + specialSym = SpecialKey::Insert; + } else if (sym.sym == SDLK_DELETE) { + specialSym = SpecialKey::Delete; + } else if (sym.sym == SDLK_HOME) { + specialSym = SpecialKey::Home; + } else if (sym.sym == SDLK_END) { + specialSym = SpecialKey::End; + } else if (sym.sym == SDLK_PAGEUP) { + specialSym = SpecialKey::PgUp; + } else if (sym.sym == SDLK_PAGEDOWN) { + specialSym = SpecialKey::PgDown; + } else if (sym.sym == SDLK_LEFT) { + specialSym = SpecialKey::Left; + } else if (sym.sym == SDLK_RIGHT) { + specialSym = SpecialKey::Right; + } else if (sym.sym == SDLK_UP) { + specialSym = SpecialKey::Up; + } else if (sym.sym == SDLK_DOWN) { + specialSym = SpecialKey::Down; + } else if (sym.sym == SDLK_TAB) { + specialSym = SpecialKey::Tab; + } else if (sym.sym == SDLK_LSHIFT) { + modifierSym = ModifierKey::LeftShift; + } else if (sym.sym == SDLK_RSHIFT) { + modifierSym = ModifierKey::RightShift; + } else if (sym.sym == SDLK_LCTRL) { + modifierSym = ModifierKey::LeftControl; + } else if (sym.sym == SDLK_RCTRL) { + modifierSym = ModifierKey::RightControl; + } else if (sym.sym == SDLK_LALT) { + modifierSym = ModifierKey::LeftAlt; + } else if (sym.sym == SDLK_RALT) { + modifierSym = ModifierKey::RightAlt; + } else if (sym.sym >= ' ' && sym.sym <= 'z') { + return static_cast(sym.sym); + } + return 0; +} + +ModifierKey translate_modifiers(Uint16 mods) { + ModifierKey ret = ModifierKey::None; + if ((mods & SDLK_LSHIFT) != 0) { + ret |= ModifierKey::LeftShift; + } + if ((mods & SDLK_RSHIFT) != 0) { + ret |= ModifierKey::RightShift; + } + if ((mods & SDLK_LCTRL) != 0) { + ret |= ModifierKey::LeftControl; + } + if ((mods & SDLK_RCTRL) != 0) { + ret |= ModifierKey::RightControl; + } + if ((mods & SDLK_LALT) != 0) { + ret |= ModifierKey::LeftAlt; + } + if ((mods & SDLK_RALT) != 0) { + ret |= ModifierKey::RightAlt; + } + + return ret; +} + +MouseButton translate_mouse_button(Uint8 button) { + if (button == 1) { + return MouseButton::Primary; + } + if (button == 2) { + return MouseButton::Middle; + } + if (button == 3) { + return MouseButton::Secondary; + } + if (button == 4) { + return MouseButton::Aux1; + } + if (button == 5) { + return MouseButton::Aux2; + } + + return MouseButton::None; +} + +MouseButton translate_mouse_button_state(Uint8 state) { + auto ret = MouseButton::None; + if ((state & 0x01) != 0) { + ret |= MouseButton::Primary; + } + if ((state & 0x02) != 0) { + ret |= MouseButton::Middle; + } + if ((state & 0x04) != 0) { + ret |= MouseButton::Secondary; + } + if ((state & 0x08) != 0) { + ret |= MouseButton::Aux1; + } + if ((state & 0x10) != 0) { + ret |= MouseButton::Aux2; + } + + return ret; +} + } // namespace aurora::input \ No newline at end of file diff --git a/aurora/lib/input.hpp b/aurora/lib/input.hpp index 672e97d1d..1a2a61937 100644 --- a/aurora/lib/input.hpp +++ b/aurora/lib/input.hpp @@ -3,14 +3,20 @@ #include #include "aurora/aurora.hpp" #include "SDL_gamecontroller.h" - +#include "SDL_keyboard.h" +#include "SDL_keycode.h" +#include "SDL_mouse.h" namespace aurora::input { Sint32 add_controller(Uint32 which); void remove_controller(Uint32 which); Sint32 player_index(Uint32 which); void set_player_index(Uint32 which, Sint32 index); -std::string name(Uint32 which); +std::string controller_name(Uint32 which); bool is_gamecube(Uint32 which); -ControllerButton translate_button(SDL_GameControllerButton button); -ControllerAxis translate_axis(SDL_GameControllerAxis axis); +ControllerButton translate_controller_button(SDL_GameControllerButton button); +ControllerAxis translate_controller_axis(SDL_GameControllerAxis axis); +char translate_key(SDL_Keysym sym, SpecialKey& specialSym, ModifierKey& modifierSym); +ModifierKey translate_modifiers(Uint16 mods); +MouseButton translate_mouse_button(Uint8 button); +MouseButton translate_mouse_button_state(Uint8 state); } \ No newline at end of file