Skip to content

Commit

Permalink
Make osc::Event virtual and start adding necessary infra for parsing …
Browse files Browse the repository at this point in the history
…etc.
  • Loading branch information
adamkewley committed Sep 15, 2024
1 parent 969024c commit 57fd286
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/OpenSimCreator/UI/MainUIScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ class osc::MainUIScreen::Impl final :

bool activeTabHandledEvent = false;
try {
activeTabHandledEvent = active->on_event(Event{e});
activeTabHandledEvent = active->on_event(ev);
}
catch (const std::exception& ex) {
log_error("MainUIScreen::on_event: exception thrown by tab: %s", ex.what());
Expand Down
1 change: 1 addition & 0 deletions src/oscar/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ add_library(oscar STATIC
Platform/AppSettings.cpp
Platform/AppSettings.h
Platform/AppSettingScope.h
Platform/Event.cpp
Platform/Event.h
Platform/FilesystemResourceLoader.cpp
Platform/FilesystemResourceLoader.h
Expand Down
10 changes: 5 additions & 5 deletions src/oscar/Platform/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,18 +254,18 @@ class osc::App::Impl final {
{
OSC_PERF("App/pump_events");

bool shouldWait = is_in_wait_mode_ and num_frames_to_poll_ <= 0;
bool should_wait = is_in_wait_mode_ and num_frames_to_poll_ <= 0;
num_frames_to_poll_ = max(0, num_frames_to_poll_ - 1);

for (SDL_Event e; shouldWait ? SDL_WaitEventTimeout(&e, 1000) : SDL_PollEvent(&e);) {
shouldWait = false;
for (SDL_Event e; should_wait ? SDL_WaitEventTimeout(&e, 1000) : SDL_PollEvent(&e);) {
should_wait = false;

// let screen handle the event
const bool screenHandledEvent = screen_->on_event(Event{e});
const bool screen_handled_event = screen_->on_event(*parse_into_event(e));

// if the active screen didn't handle the event, try to handle it here by following
// reasonable heuristics
if (not screenHandledEvent) {
if (not screen_handled_event) {
if (e.type == SDL_WINDOWEVENT) {
// window was resized and should be drawn a couple of times quickly
// to ensure any immediate UIs in screens are updated
Expand Down
10 changes: 10 additions & 0 deletions src/oscar/Platform/Event.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "Event.h"

#include <memory>

using namespace osc;

std::unique_ptr<Event> osc::parse_into_event(const SDL_Event& e)
{
return std::make_unique<RawEvent>(e);
}
21 changes: 20 additions & 1 deletion src/oscar/Platform/Event.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
#pragma once

#include <memory>

union SDL_Event;

namespace osc
{
// base class for application events
class Event {
public:
protected:
explicit Event(const SDL_Event& e) : inner_event_{&e} {}

Event(const Event&) = default;
Event(Event&&) noexcept = default;
Event& operator=(const Event&) = default;
Event& operator=(Event&&) noexcept = default;
public:
virtual ~Event() noexcept = default;

operator const SDL_Event& () const { return *inner_event_; }
private:
const SDL_Event* inner_event_;
};

// a "raw" uncategorized event from the underlying OS/OS abstraction layer
class RawEvent : public Event {
public:
explicit RawEvent(const SDL_Event& e) : Event{e} {}
};

std::unique_ptr<Event> parse_into_event(const SDL_Event&);
}

0 comments on commit 57fd286

Please sign in to comment.