Skip to content

Commit

Permalink
Refactor more of ui_context to the osc::Event API
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkewley committed Sep 20, 2024
1 parent 1fedad0 commit 98d73e3
Show file tree
Hide file tree
Showing 8 changed files with 401 additions and 325 deletions.
2 changes: 1 addition & 1 deletion src/OpenSimCreator/UI/MeshImporter/MeshImporterTab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1605,7 +1605,7 @@ class osc::mi::MeshImporterTab::Impl final : public IMeshImporterUILayerHost {


// context menu should be closed under these conditions
if (ui::any_of_keys_pressed({Key::Enter, Key::Escape}))
if (ui::any_of_keys_pressed({Key::Return, Key::Escape}))
{
m_MaybeOpenedContextMenu.reset();
ui::close_current_popup();
Expand Down
148 changes: 118 additions & 30 deletions src/oscar/Platform/Event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,67 @@ template<>
struct osc::Converter<SDL_Keycode, Key> final {
Key operator()(SDL_Keycode code) const
{
static_assert(num_options<Key>() == 67);
static_assert(num_options<Key>() == 120);

switch (code) {
case SDLK_ESCAPE: return Key::Escape;
case SDLK_RETURN: return Key::Enter;
case SDLK_SPACE: return Key::Space;
case SDLK_DELETE: return Key::Delete;
case SDLK_TAB: return Key::Tab;
case SDLK_BACKSPACE: return Key::Backspace;
case SDLK_LEFT: return Key::LeftArrow;
case SDLK_RIGHT: return Key::RightArrow;
case SDLK_UP: return Key::UpArrow;
case SDLK_DOWN: return Key::DownArrow;
case SDLK_PAGEUP: return Key::PageUp;
case SDLK_PAGEDOWN: return Key::PageDown;
case SDLK_F1: return Key::F1;
case SDLK_F2: return Key::F2;
case SDLK_F3: return Key::F3;
case SDLK_F4: return Key::F4;
case SDLK_F5: return Key::F5;
case SDLK_F6: return Key::F6;
case SDLK_F7: return Key::F7;
case SDLK_F8: return Key::F8;
case SDLK_F9: return Key::F9;
case SDLK_F10: return Key::F10;
case SDLK_F11: return Key::F11;
case SDLK_F12: return Key::F12;
case SDLK_HOME: return Key::Home;
case SDLK_END: return Key::End;
case SDLK_INSERT: return Key::Insert;
case SDLK_DELETE: return Key::Delete;
case SDLK_BACKSPACE: return Key::Backspace;
case SDLK_SPACE: return Key::Space;
case SDLK_RETURN: return Key::Return;
case SDLK_ESCAPE: return Key::Escape;
case SDLK_QUOTE: return Key::Quote;
case SDLK_COMMA: return Key::Comma;
case SDLK_MINUS: return Key::Minus;
case SDLK_PERIOD: return Key::Period;
case SDLK_SLASH: return Key::Slash;
case SDLK_SEMICOLON: return Key::Semicolon;
case SDLK_EQUALS: return Key::Equals;
case SDLK_LEFTBRACKET: return Key::LeftBracket;
case SDLK_BACKSLASH: return Key::Backslash;
case SDLK_RIGHTBRACKET: return Key::RightBracket;
case SDLK_BACKQUOTE: return Key::Backquote;
case SDLK_CAPSLOCK: return Key::CapsLock;
case SDLK_SCROLLLOCK: return Key::ScrollLock;
case SDLK_NUMLOCKCLEAR: return Key::NumLockClear;
case SDLK_PRINTSCREEN: return Key::PrintScreen;
case SDLK_PAUSE: return Key::Pause;
case SDLK_KP_0: return Key::Keypad0;
case SDLK_KP_1: return Key::Keypad1;
case SDLK_KP_2: return Key::Keypad2;
case SDLK_KP_3: return Key::Keypad3;
case SDLK_KP_4: return Key::Keypad4;
case SDLK_KP_5: return Key::Keypad5;
case SDLK_KP_6: return Key::Keypad6;
case SDLK_KP_7: return Key::Keypad7;
case SDLK_KP_8: return Key::Keypad8;
case SDLK_KP_9: return Key::Keypad9;
case SDLK_KP_PERIOD: return Key::KeypadPeriod;
case SDLK_KP_DIVIDE: return Key::KeypadDivide;
case SDLK_KP_MULTIPLY: return Key::KeypadMultiply;
case SDLK_KP_MINUS: return Key::KeypadMinus;
case SDLK_KP_PLUS: return Key::KeypadPlus;
case SDLK_KP_ENTER: return Key::KeypadEnter;
case SDLK_KP_EQUALS: return Key::KeypadEquals;
case SDLK_LCTRL: return Key::LeftCtrl;
case SDLK_LSHIFT: return Key::LeftShift;
case SDLK_LALT: return Key::LeftAlt;
case SDLK_LGUI: return Key::LeftGui;
case SDLK_RCTRL: return Key::RightCtrl;
case SDLK_RSHIFT: return Key::RightShift;
case SDLK_RALT: return Key::RightAlt;
case SDLK_RGUI: return Key::RightGui;
case SDLK_APPLICATION: return Key::Application;
case SDLK_0: return Key::_0;
case SDLK_1: return Key::_1;
case SDLK_2: return Key::_2;
case SDLK_3: return Key::_3;
Expand All @@ -75,13 +113,6 @@ struct osc::Converter<SDL_Keycode, Key> final {
case SDLK_7: return Key::_7;
case SDLK_8: return Key::_8;
case SDLK_9: return Key::_9;
case SDLK_0: return Key::_0;
case SDLK_UP: return Key::UpArrow;
case SDLK_DOWN: return Key::DownArrow;
case SDLK_LEFT: return Key::LeftArrow;
case SDLK_RIGHT: return Key::RightArrow;
case SDLK_MINUS: return Key::Minus;
case SDLK_EQUALS: return Key::Equal;
case SDLK_a: return Key::A;
case SDLK_b: return Key::B;
case SDLK_c: return Key::C;
Expand All @@ -108,7 +139,48 @@ struct osc::Converter<SDL_Keycode, Key> final {
case SDLK_x: return Key::X;
case SDLK_y: return Key::Y;
case SDLK_z: return Key::Z;
default: return Key::Unknown;
case SDLK_F1: return Key::F1;
case SDLK_F2: return Key::F2;
case SDLK_F3: return Key::F3;
case SDLK_F4: return Key::F4;
case SDLK_F5: return Key::F5;
case SDLK_F6: return Key::F6;
case SDLK_F7: return Key::F7;
case SDLK_F8: return Key::F8;
case SDLK_F9: return Key::F9;
case SDLK_F10: return Key::F10;
case SDLK_F11: return Key::F11;
case SDLK_F12: return Key::F12;
case SDLK_F13: return Key::F13;
case SDLK_F14: return Key::F14;
case SDLK_F15: return Key::F15;
case SDLK_F16: return Key::F16;
case SDLK_F17: return Key::F17;
case SDLK_F18: return Key::F18;
case SDLK_F19: return Key::F19;
case SDLK_F20: return Key::F20;
case SDLK_F21: return Key::F21;
case SDLK_F22: return Key::F22;
case SDLK_F23: return Key::F23;
case SDLK_F24: return Key::F24;
case SDLK_AC_BACK: return Key::AppBack;
case SDLK_AC_FORWARD: return Key::AppForward;
default: return Key::Unknown;
}
}
};

template<>
struct osc::Converter<Uint8, osc::MouseButton> final {
osc::MouseButton operator()(Uint8 sdlval) const
{
switch (sdlval) {
case SDL_BUTTON_LEFT: return MouseButton::Left;
case SDL_BUTTON_RIGHT: return MouseButton::Right;
case SDL_BUTTON_MIDDLE: return MouseButton::Middle;
case SDL_BUTTON_X1: return MouseButton::Back;
case SDL_BUTTON_X2: return MouseButton::Forward;
default: return MouseButton::None;
}
}
};
Expand Down Expand Up @@ -136,15 +208,31 @@ osc::QuitEvent::QuitEvent(const SDL_Event& e) :
}

osc::MouseEvent::MouseEvent(const SDL_Event& e) :
Event{e, e.type == SDL_MOUSEBUTTONDOWN ? EventType::MouseButtonDown : (e.type == SDL_MOUSEBUTTONUP ? EventType::MouseButtonUp : EventType::MouseMove)},
relative_delta_{static_cast<float>(e.motion.xrel), static_cast<float>(e.motion.yrel)}
Event{e, e.type == SDL_MOUSEBUTTONDOWN ? EventType::MouseButtonDown : (e.type == SDL_MOUSEBUTTONUP ? EventType::MouseButtonUp : EventType::MouseMove)}
{
OSC_ASSERT(e.type == SDL_MOUSEBUTTONDOWN or e.type == SDL_MOUSEBUTTONUP or e.type == SDL_MOUSEMOTION);
switch (e.type) {
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP: {
input_source_ = e.button.which == SDL_TOUCH_MOUSEID ? MouseInputSource::TouchScreen : MouseInputSource::Mouse;
button_ = to<MouseButton>(e.button.button);
break;
}
case SDL_MOUSEMOTION: {
relative_delta_ = {static_cast<float>(e.motion.xrel), static_cast<float>(e.motion.yrel)};
position_in_window_ = {static_cast<float>(e.motion.x), static_cast<float>(e.motion.y)};
input_source_ = e.motion.which == SDL_TOUCH_MOUSEID ? MouseInputSource::TouchScreen : MouseInputSource::Mouse;
break;
}
default: {
throw std::runtime_error{"unknown mouse event type passed into an osc::MouseEvent"};
}
}
}

osc::MouseWheelEvent::MouseWheelEvent(const SDL_Event& e) :
Event{e, EventType::MouseWheel},
delta_{e.wheel.preciseX, e.wheel.preciseY}
delta_{e.wheel.preciseX, e.wheel.preciseY},
input_source_{e.wheel.which == SDL_TOUCH_MOUSEID ? MouseInputSource::TouchScreen : MouseInputSource::Mouse}
{
OSC_ASSERT(e.type == SDL_MOUSEWHEEL);
}
Expand Down
28 changes: 27 additions & 1 deletion src/oscar/Platform/Event.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,37 @@ namespace osc
Key key_;
};

enum class MouseButton : unsigned {
None = 0, // no button is associated with the `MouseEvent` (e.g. moving the mouse)
Left = 1<<0,
Right = 1<<1,
Middle = 1<<2,
Back = 1<<3, // sometimes called X1 (SDL), ExtraButton1 (Qt)
Forward = 1<<4, // sometimes called X2 (SDL), ExtraButton2 (Qt)
};

enum class MouseInputSource {
Mouse,
TouchScreen,
};

// TODO: textinput
// TODO: displayevent
// TODO: windowevent

class MouseEvent final : public Event {
public:
explicit MouseEvent(const SDL_Event&);

MouseInputSource input_source() const { return input_source_; }
MouseButton button() const { return button_; }
Vec2 relative_delta() const { return relative_delta_; }
Vec2 position_in_window() const { return position_in_window_; }
private:
Vec2 relative_delta_;
Vec2 relative_delta_{};
Vec2 position_in_window_{};
MouseInputSource input_source_ = MouseInputSource::Mouse;
MouseButton button_ = MouseButton::None;
};

class QuitEvent final : public Event {
Expand All @@ -131,9 +155,11 @@ namespace osc
public:
explicit MouseWheelEvent(const SDL_Event&);

MouseInputSource input_source() const { return input_source_; }
Vec2 delta() const { return delta_; }
private:
Vec2 delta_;
MouseInputSource input_source_ = MouseInputSource::TouchScreen;
};

std::unique_ptr<Event> parse_into_event(const SDL_Event&);
Expand Down
111 changes: 81 additions & 30 deletions src/oscar/Platform/Key.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,64 @@
namespace osc
{
enum class Key {
Escape,
Enter,
Space,
Delete,
Tab,
LeftCtrl,
RightCtrl,
Backspace,
LeftArrow,
RightArrow,
UpArrow,
DownArrow,
PageUp,
PageDown,
F1,
F2,
F3,
F4,
F5,
F6,
F7,
F8,
F9,
F10,
F11,
F12,
Home,
End,
Insert,
Delete,
Backspace,
Space,
Return,
Escape,
Quote,
Comma,
Minus,
Period,
Slash,
Semicolon,
Equals,
LeftBracket,
Backslash,
RightBracket,
Backquote,
CapsLock,
ScrollLock,
NumLockClear,
PrintScreen,
Pause,
Keypad0,
Keypad1,
Keypad2,
Keypad3,
Keypad4,
Keypad5,
Keypad6,
Keypad7,
Keypad8,
Keypad9,
KeypadPeriod,
KeypadDivide,
KeypadMultiply,
KeypadMinus,
KeypadPlus,
KeypadEnter,
KeypadEquals,
LeftCtrl,
LeftShift,
LeftAlt,
LeftGui,
RightCtrl,
RightShift,
RightAlt,
RightGui,
Application,
_0,
_1,
_2,
_3,
Expand All @@ -34,13 +70,6 @@ namespace osc
_7,
_8,
_9,
_0,
UpArrow,
DownArrow,
LeftArrow,
RightArrow,
Minus,
Equal,
A,
B,
C,
Expand All @@ -67,10 +96,32 @@ namespace osc
X,
Y,
Z,

// legacy
MouseLeft,
MouseRight,
F1,
F2,
F3,
F4,
F5,
F6,
F7,
F8,
F9,
F10,
F11,
F12,
F13,
F14,
F15,
F16,
F17,
F18,
F19,
F20,
F21,
F22,
F23,
F24,
AppBack,
AppForward,
Unknown,

NUM_OPTIONS,
Expand Down
Loading

0 comments on commit 98d73e3

Please sign in to comment.