From 630a285d605ee24f30d4132aa8d6a344d2f25fc8 Mon Sep 17 00:00:00 2001 From: Adam Kewley Date: Sat, 21 Sep 2024 11:39:51 +0200 Subject: [PATCH] Add osc::DisplayStateChangeEvent and osc::TextInputEvent --- src/oscar/Platform/Event.cpp | 22 ++++++++++++++++++++++ src/oscar/Platform/Event.h | 29 +++++++++++++++++++++++++++-- src/oscar/UI/ui_context.cpp | 22 ++++++++++------------ 3 files changed, 59 insertions(+), 14 deletions(-) diff --git a/src/oscar/Platform/Event.cpp b/src/oscar/Platform/Event.cpp index fb2f0454c..d8b5e7d92 100644 --- a/src/oscar/Platform/Event.cpp +++ b/src/oscar/Platform/Event.cpp @@ -6,11 +6,14 @@ #include #include +#include #include #include #include +#define SDL_HAS_DISPLAY_EVENT SDL_VERSION_ATLEAST(2,0,9) + using namespace osc; template<> @@ -207,6 +210,17 @@ osc::QuitEvent::QuitEvent(const SDL_Event& e) : OSC_ASSERT(e.type == SDL_QUIT); } +osc::TextInputEvent::TextInputEvent(const SDL_Event& e) : + Event{e, EventType::TextInput}, + utf8_text_{e.text.text} +{ + OSC_ASSERT(e.type == SDL_TEXTINPUT); +} + +osc::DisplayStateChangeEvent::DisplayStateChangeEvent(const SDL_Event& e) : + Event{e, EventType::DisplayStateChange} +{} + osc::MouseEvent::MouseEvent(const SDL_Event& e) : Event{e, e.type == SDL_MOUSEBUTTONDOWN ? EventType::MouseButtonDown : (e.type == SDL_MOUSEBUTTONUP ? EventType::MouseButtonUp : EventType::MouseMove)} { @@ -254,6 +268,14 @@ std::unique_ptr osc::parse_into_event(const SDL_Event& e) else if (e.type == SDL_MOUSEWHEEL) { return std::make_unique(e); } + else if (e.type == SDL_TEXTINPUT) { + return std::make_unique(e); + } +#if SDL_HAS_DISPLAY_EVENT + else if (e.type == SDL_DISPLAYEVENT) { + return std::make_unique(e); + } +#endif else { return std::make_unique(e); } diff --git a/src/oscar/Platform/Event.h b/src/oscar/Platform/Event.h index 5dddad0eb..f5a02c2ba 100644 --- a/src/oscar/Platform/Event.h +++ b/src/oscar/Platform/Event.h @@ -3,9 +3,12 @@ #include #include #include +#include #include #include +#include +#include union SDL_Event; @@ -16,10 +19,12 @@ namespace osc DropFile, KeyDown, KeyUp, + TextInput, MouseButtonDown, MouseButtonUp, MouseMove, MouseWheel, + DisplayStateChange, Quit, NUM_OPTIONS, }; @@ -127,8 +132,28 @@ namespace osc TouchScreen, }; - // TODO: textinput - // TODO: displayevent + class TextInputEvent final : public Event { + public: + explicit TextInputEvent(const SDL_Event&); + + CStringView utf8_text() const { return utf8_text_; } + private: + std::string utf8_text_; + }; + + // fired off when the state of a display changes, such as: + // + // - display connected + // - display disconnected + // - display reoriented + // - display resolution changed (maybe DPI change) + class DisplayStateChangeEvent final : public Event { + public: + explicit DisplayStateChangeEvent(const SDL_Event&); + }; + + // WindowStateChangeEvent - None, Minimized, Maximized, Fullscreen, Active + // TODO: windowevent class MouseEvent final : public Event { diff --git a/src/oscar/UI/ui_context.cpp b/src/oscar/UI/ui_context.cpp index 60eefd6c7..56316beda 100644 --- a/src/oscar/UI/ui_context.cpp +++ b/src/oscar/UI/ui_context.cpp @@ -53,7 +53,6 @@ namespace rgs = std::ranges; #else #define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE 0 #endif -#define SDL_HAS_DISPLAY_EVENT SDL_VERSION_ATLEAST(2,0,9) // SDL Data struct ImGui_ImplSDL2_Data @@ -176,24 +175,23 @@ static bool ImGui_ImplSDL2_ProcessEvent(const Event& e) io.AddKeyEvent(ui::toImGuiKey(key_event.key()), key_event.type() == EventType::KeyDown); return true; } - default: break; - } - - switch (const SDL_Event* event = &static_cast(e); event->type) { - case SDL_TEXTINPUT: - { - io.AddInputCharactersUTF8(event->text.text); + case EventType::TextInput: { + const auto& text_event = dynamic_cast(e); + io.AddInputCharactersUTF8(text_event.utf8_text().c_str()); return true; } -#if SDL_HAS_DISPLAY_EVENT - case SDL_DISPLAYEVENT: - { + case EventType::DisplayStateChange: { // 2.0.26 has SDL_DISPLAYEVENT_CONNECTED/SDL_DISPLAYEVENT_DISCONNECTED/SDL_DISPLAYEVENT_ORIENTATION, // so change of DPI/Scaling are not reflected in this event. (SDL3 has it) bd->WantUpdateMonitors = true; return true; } -#endif + default: { + break; + } + } + + switch (const SDL_Event* event = &static_cast(e); event->type) { case SDL_WINDOWEVENT: { // - When capturing mouse, SDL will send a bunch of conflicting LEAVE/ENTER event on every mouse move, but the final ENTER tends to be right.