Skip to content

Commit

Permalink
Add osc::DisplayStateChangeEvent and osc::TextInputEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkewley committed Sep 21, 2024
1 parent 17c41e5 commit 630a285
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 14 deletions.
22 changes: 22 additions & 0 deletions src/oscar/Platform/Event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
#include <oscar/Utils/EnumHelpers.h>

#include <SDL_events.h>
#include <SDL_version.h>

#include <array>
#include <memory>
#include <utility>

#define SDL_HAS_DISPLAY_EVENT SDL_VERSION_ATLEAST(2,0,9)

using namespace osc;

template<>
Expand Down Expand Up @@ -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)}
{
Expand Down Expand Up @@ -254,6 +268,14 @@ std::unique_ptr<Event> osc::parse_into_event(const SDL_Event& e)
else if (e.type == SDL_MOUSEWHEEL) {
return std::make_unique<MouseWheelEvent>(e);
}
else if (e.type == SDL_TEXTINPUT) {
return std::make_unique<TextInputEvent>(e);
}
#if SDL_HAS_DISPLAY_EVENT
else if (e.type == SDL_DISPLAYEVENT) {
return std::make_unique<DisplayStateChangeEvent>(e);
}
#endif
else {
return std::make_unique<RawEvent>(e);
}
Expand Down
29 changes: 27 additions & 2 deletions src/oscar/Platform/Event.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
#include <oscar/Maths/Vec2.h>
#include <oscar/Platform/Key.h>
#include <oscar/Shims/Cpp23/utility.h>
#include <oscar/Utils/CStringView.h>

#include <filesystem>
#include <memory>
#include <string>
#include <string_view>

union SDL_Event;

Expand All @@ -16,10 +19,12 @@ namespace osc
DropFile,
KeyDown,
KeyUp,
TextInput,
MouseButtonDown,
MouseButtonUp,
MouseMove,
MouseWheel,
DisplayStateChange,
Quit,
NUM_OPTIONS,
};
Expand Down Expand Up @@ -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 {
Expand Down
22 changes: 10 additions & 12 deletions src/oscar/UI/ui_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<const SDL_Event&>(e); event->type) {
case SDL_TEXTINPUT:
{
io.AddInputCharactersUTF8(event->text.text);
case EventType::TextInput: {
const auto& text_event = dynamic_cast<const TextInputEvent&>(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<const SDL_Event&>(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.
Expand Down

0 comments on commit 630a285

Please sign in to comment.