From 49bc9d5d8bba57611d8f5a1a2d85c215c4ba7ea5 Mon Sep 17 00:00:00 2001 From: superp00t Date: Fri, 24 Nov 2023 23:25:58 -0500 Subject: [PATCH] feat(gx): GLSDL window dispatches mouse button events --- src/gx/glsdl/GLSDLWindow.cpp | 42 ++++++++++++++++++++++++++++++++++++ src/gx/glsdl/GLSDLWindow.hpp | 1 + 2 files changed, 43 insertions(+) diff --git a/src/gx/glsdl/GLSDLWindow.cpp b/src/gx/glsdl/GLSDLWindow.cpp index 00d3f72..43a1e48 100644 --- a/src/gx/glsdl/GLSDLWindow.cpp +++ b/src/gx/glsdl/GLSDLWindow.cpp @@ -118,6 +118,25 @@ static const std::map s_keyConversion = { {SDL_SCANCODE_F19, KEY_F19} }; +static MOUSEBUTTON s_buttonConversion[16] = { + MOUSE_BUTTON_NONE, + MOUSE_BUTTON_LEFT, + MOUSE_BUTTON_RIGHT, + MOUSE_BUTTON_MIDDLE, + MOUSE_BUTTON_XBUTTON1, + MOUSE_BUTTON_XBUTTON2, + MOUSE_BUTTON_XBUTTON3, + MOUSE_BUTTON_XBUTTON4, + MOUSE_BUTTON_XBUTTON5, + MOUSE_BUTTON_XBUTTON6, + MOUSE_BUTTON_XBUTTON7, + MOUSE_BUTTON_XBUTTON8, + MOUSE_BUTTON_XBUTTON9, + MOUSE_BUTTON_XBUTTON10, + MOUSE_BUTTON_XBUTTON11, + MOUSE_BUTTON_XBUTTON12 +}; + void GLSDLWindow::Create(const char* title, const GLSDLWindowRect& rect, GLTextureFormat depthFormat, uint32_t sampleCount) { BLIZZARD_ASSERT(this->m_sdlWindow == nullptr); @@ -255,6 +274,10 @@ void GLSDLWindow::DispatchSDLEvent(const SDL_Event& event) { case SDL_EVENT_KEY_UP: this->DispatchSDLKeyboardEvent(event); break; + case SDL_EVENT_MOUSE_BUTTON_DOWN: + case SDL_EVENT_MOUSE_BUTTON_UP: + this->DispatchSDLMouseButtonEvent(event); + break; case SDL_EVENT_MOUSE_MOTION: this->DispatchSDLMouseMotionEvent(event); break; @@ -285,3 +308,22 @@ void GLSDLWindow::DispatchSDLMouseMotionEvent(const SDL_Event& event) { OsQueuePut(OS_INPUT_MOUSE_MOVE, 0, x, y, 0); } + +void GLSDLWindow::DispatchSDLMouseButtonEvent(const SDL_Event& event) { + // Is this an up or down mouse click? + OSINPUT inputclass = event.type == SDL_EVENT_MOUSE_BUTTON_UP ? OS_INPUT_MOUSE_UP : OS_INPUT_MOUSE_DOWN; + + // XY click coordinates + auto x = static_cast(event.button.x); + auto y = static_cast(event.button.y); + + // Convert SDL button index into internal MOUSEBUTTON ID + auto buttonIndex = event.button.button; + if (buttonIndex > 15) { + return; + } + auto button = s_buttonConversion[buttonIndex]; + + // Push mousebutton event into input queue + OsQueuePut(inputclass, button, x, y, 0); +} diff --git a/src/gx/glsdl/GLSDLWindow.hpp b/src/gx/glsdl/GLSDLWindow.hpp index a238c07..ce79135 100644 --- a/src/gx/glsdl/GLSDLWindow.hpp +++ b/src/gx/glsdl/GLSDLWindow.hpp @@ -33,6 +33,7 @@ class GLSDLWindow { void DispatchSDLEvent(const SDL_Event& event); void DispatchSDLKeyboardEvent(const SDL_Event& event); void DispatchSDLMouseMotionEvent(const SDL_Event& event); + void DispatchSDLMouseButtonEvent(const SDL_Event& event); void Resize(const GLSDLWindowRect& rect); GLSDLWindowRect GetRect();