From 917f45188aeea1760c083076762e7664224fc930 Mon Sep 17 00:00:00 2001 From: mrkubax10 Date: Sun, 22 Oct 2023 14:46:30 +0200 Subject: [PATCH] HaikuWindow: Keyboard and mouse input events --- src/game/renderer/gl/haiku_gl_context.cpp | 4 +- src/game/renderer/gl/haiku_gl_context.hpp | 2 +- src/game/window/haiku_window.cpp | 189 +++++++++++++++++++++- src/game/window/haiku_window.hpp | 7 +- 4 files changed, 188 insertions(+), 14 deletions(-) diff --git a/src/game/renderer/gl/haiku_gl_context.cpp b/src/game/renderer/gl/haiku_gl_context.cpp index a19594a..3c0fd52 100644 --- a/src/game/renderer/gl/haiku_gl_context.cpp +++ b/src/game/renderer/gl/haiku_gl_context.cpp @@ -29,8 +29,8 @@ SOFTWARE. using namespace polygun::renderer; HaikuGLContext::HaikuGLContext(window::HaikuWindow* window) : - GLContext(), - BGLView(window->Bounds(), "glView", B_FOLLOW_ALL_SIDES, 0, BGL_RGB|BGL_DEPTH|BGL_DOUBLE) + BGLView(window->Bounds(), "glView", B_FOLLOW_ALL_SIDES, 0, BGL_RGB|BGL_DEPTH|BGL_DOUBLE), + GLContext() { LockGL(); } diff --git a/src/game/renderer/gl/haiku_gl_context.hpp b/src/game/renderer/gl/haiku_gl_context.hpp index b34bfd4..960b5f3 100644 --- a/src/game/renderer/gl/haiku_gl_context.hpp +++ b/src/game/renderer/gl/haiku_gl_context.hpp @@ -33,7 +33,7 @@ namespace polygun::window { } namespace polygun::renderer { - class HaikuGLContext final : public GLContext, public BGLView { + class HaikuGLContext final : public BGLView, public GLContext { public: HaikuGLContext(window::HaikuWindow* window); diff --git a/src/game/window/haiku_window.cpp b/src/game/window/haiku_window.cpp index 1cc5217..991d9db 100644 --- a/src/game/window/haiku_window.cpp +++ b/src/game/window/haiku_window.cpp @@ -30,11 +30,123 @@ SOFTWARE. using namespace polygun::window; +static const KeyCode g_haiku_keycode_to_keycode[] = { + KeyCode::KEY_NONE, + KeyCode::KEY_ESCAPE, + KeyCode::KEY_F1, + KeyCode::KEY_F2, + KeyCode::KEY_F3, + KeyCode::KEY_F4, + KeyCode::KEY_F5, + KeyCode::KEY_F6, + KeyCode::KEY_F7, + KeyCode::KEY_F8, + KeyCode::KEY_F9, + KeyCode::KEY_F10, + KeyCode::KEY_F11, + KeyCode::KEY_F12, + KeyCode::KEY_PRINT_SCREEN, + KeyCode::KEY_SCROLL_LOCK, + KeyCode::KEY_PAUSE, + KeyCode::KEY_TILDE, + KeyCode::KEY_1, + KeyCode::KEY_2, + KeyCode::KEY_3, + KeyCode::KEY_4, + KeyCode::KEY_5, + KeyCode::KEY_6, + KeyCode::KEY_7, + KeyCode::KEY_8, + KeyCode::KEY_9, + KeyCode::KEY_0, + KeyCode::KEY_MINUS, + KeyCode::KEY_EQUALS, + KeyCode::KEY_BACKSPACE, + KeyCode::KEY_INSERT, + KeyCode::KEY_HOME, + KeyCode::KEY_PAGE_UP, + KeyCode::KEY_NUM_LOCK, + KeyCode::KEY_NUM_SLASH, + KeyCode::KEY_NUM_MULTIPLY, + KeyCode::KEY_NUM_MINUS, + KeyCode::KEY_TAB, + KeyCode::KEY_Q, + KeyCode::KEY_W, + KeyCode::KEY_E, + KeyCode::KEY_R, + KeyCode::KEY_T, + KeyCode::KEY_Y, + KeyCode::KEY_U, + KeyCode::KEY_I, + KeyCode::KEY_O, + KeyCode::KEY_P, + KeyCode::KEY_LEFT_SQUARE_BRACKET, + KeyCode::KEY_RIGHT_SQUARE_BRACKET, + KeyCode::KEY_BACKSLASH, + KeyCode::KEY_DELETE, + KeyCode::KEY_END, + KeyCode::KEY_PAGE_DOWN, + KeyCode::KEY_NUM_7, + KeyCode::KEY_NUM_8, + KeyCode::KEY_NUM_9, + KeyCode::KEY_NUM_PLUS, + KeyCode::KEY_CAPS_LOCK, + KeyCode::KEY_A, + KeyCode::KEY_S, + KeyCode::KEY_D, + KeyCode::KEY_F, + KeyCode::KEY_G, + KeyCode::KEY_H, + KeyCode::KEY_J, + KeyCode::KEY_K, + KeyCode::KEY_L, + KeyCode::KEY_SEMICOLON, + KeyCode::KEY_QUOTE, + KeyCode::KEY_RETURN, + KeyCode::KEY_NUM_4, + KeyCode::KEY_NUM_5, + KeyCode::KEY_NUM_6, + KeyCode::KEY_LEFT_SHIFT, + KeyCode::KEY_Z, + KeyCode::KEY_X, + KeyCode::KEY_C, + KeyCode::KEY_V, + KeyCode::KEY_B, + KeyCode::KEY_N, + KeyCode::KEY_M, + KeyCode::KEY_COMMA, + KeyCode::KEY_DOT, + KeyCode::KEY_SLASH, + KeyCode::KEY_RIGHT_SHIFT, + KeyCode::KEY_UP, + KeyCode::KEY_NUM_1, + KeyCode::KEY_NUM_2, + KeyCode::KEY_NUM_3, + KeyCode::KEY_NUM_RETURN, + KeyCode::KEY_LEFT_CONTROL, + KeyCode::KEY_LEFT_ALT, + KeyCode::KEY_SPACE, + KeyCode::KEY_RIGHT_ALT, + KeyCode::KEY_RIGHT_CONTROL, + KeyCode::KEY_LEFT, + KeyCode::KEY_DOWN, + KeyCode::KEY_RIGHT, + KeyCode::KEY_NUM_0, + KeyCode::KEY_NUM_DOT, + KeyCode::KEY_LEFT_SUPER, + KeyCode::KEY_RIGHT_SUPER, + KeyCode::KEY_CONTEXT +}; + static BApplication* g_app = nullptr; HaikuWindow::HaikuWindow(const std::string& title, unsigned width, unsigned height, renderer::RendererType renderer_type) : - BDirectWindow(BRect(10, 10, width, height), title.c_str(), B_TITLED_WINDOW, 0), - Window() + BWindow(BRect(10, 10, width, height), title.c_str(), B_TITLED_WINDOW, 0), + Window(), +#if defined(RENDERER_GL) || defined(RENDERER_LEGACY_GL) + m_gl_context(nullptr), +#endif + m_prev_mouse_buttons(0) { m_type = WindowType::WINDOW_TYPE_HAIKU; switch(renderer_type) { @@ -78,14 +190,75 @@ bool HaikuWindow::QuitRequested() { return true; } -void HaikuWindow::MessageReceived(BMessage* msg) { - msg->PrintToStream(); +void HaikuWindow::DispatchMessage(BMessage* msg, BHandler* target) { + Event event; + bool handled = false; switch(msg->what) { - case B_KYD: - LOG_VERBOSE("Key down!"); - default: - BDirectWindow::MessageReceived(msg); + case B_KEY_DOWN: + case B_KEY_UP: { + int key; + const bool pressed = msg->what==B_KEY_DOWN; + if(msg->FindInt32("key", &key)==B_OK && static_cast(key)what==B_MOUSE_DOWN; + if(msg->FindInt32("buttons", &buttons)==B_OK) { + event.m_type = pressed?EventType::EVENT_TYPE_MOUSE_BUTTON_DOWN:EVENT_TYPE_MOUSE_BUTTON_UP; + if(buttons&B_PRIMARY_MOUSE_BUTTON || m_prev_mouse_buttons&B_PRIMARY_MOUSE_BUTTON) { + event.m_data.m_mouse_event.m_button = MouseButton::MOUSE_BUTTON_LEFT; + push_event(event); + handled = true; + } + if(buttons&B_SECONDARY_MOUSE_BUTTON || m_prev_mouse_buttons&B_SECONDARY_MOUSE_BUTTON) { + event.m_data.m_mouse_event.m_button = MouseButton::MOUSE_BUTTON_RIGHT; + push_event(event); + handled = true; + } + if(buttons&B_TERTIARY_MOUSE_BUTTON || m_prev_mouse_buttons&B_TERTIARY_MOUSE_BUTTON) { + event.m_data.m_mouse_event.m_button = MouseButton::MOUSE_BUTTON_MIDDLE; + push_event(event); + handled = true; + } + if(handled) { + m_pressed_mouse_buttons[event.m_data.m_mouse_event.m_button] = pressed; + handled = false; + } + m_prev_mouse_buttons = buttons; + } + break; + } + case B_MOUSE_MOVED: { + BPoint where; + if(msg->FindPoint("where", &where)==B_OK) { + event.m_type = EventType::EVENT_TYPE_MOUSE_MOVE; + m_mouse_x = event.m_data.m_mouse_event.m_x = where.x; + m_mouse_y = event.m_data.m_mouse_event.m_x = where.y; + handled = true; + } + break; + } + case B_MOUSE_WHEEL_CHANGED: { + float y; + if(msg->FindFloat("be:wheel_delta_y", &y)==B_OK) { + event.m_type = EventType::EVENT_TYPE_MOUSE_WHEEL; + event.m_data.m_mouse_event.m_wheel_offset = y; + handled = true; + } + break; + } + } + if(handled) + push_event(event); + else + BWindow::DispatchMessage(msg, target); } void HaikuWindow::init_application() { diff --git a/src/game/window/haiku_window.hpp b/src/game/window/haiku_window.hpp index 9f0a112..e0f9399 100644 --- a/src/game/window/haiku_window.hpp +++ b/src/game/window/haiku_window.hpp @@ -26,14 +26,14 @@ SOFTWARE. #define POLYGUN_WINDOW_HAIKU_WINDOW_HPP #include "game/window/window.hpp" -#include +#include #if defined(RENDERER_GL) || defined(RENDERER_LEGACY_GL) #include "game/renderer/gl/haiku_gl_context.hpp" #endif namespace polygun::window { - class HaikuWindow final : public BDirectWindow, public Window { + class HaikuWindow final : public BWindow, public Window { public: HaikuWindow(const std::string& title, unsigned width, unsigned height, renderer::RendererType renderer_type); virtual ~HaikuWindow() override; @@ -46,7 +46,7 @@ namespace polygun::window { #endif // BDirectWindow methods virtual bool QuitRequested() override; - virtual void MessageReceived(BMessage* msg) override; + virtual void DispatchMessage(BMessage* msg, BHandler* target) override; static void init_application(); @@ -54,6 +54,7 @@ namespace polygun::window { #if defined(RENDERER_GL) || defined(RENDERER_LEGACY_GL) renderer::HaikuGLContext* m_gl_context; #endif + int m_prev_mouse_buttons; }; }