From ece0c2adde6cd8f748573534864adccdcc170c73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Lucas=20Golini?= Date: Tue, 21 Nov 2023 23:47:34 -0300 Subject: [PATCH] Added InputEvent::MouseWheel event. Also added onMouseWheel event for Node. Enabled wheel scroll momentum for macOS on ecode. --- include/eepp/scene/eventdispatcher.hpp | 2 ++ include/eepp/scene/node.hpp | 2 ++ include/eepp/window/inputevent.hpp | 10 ++++++++++ src/eepp/scene/eventdispatcher.cpp | 14 ++++++++++++++ src/eepp/scene/node.cpp | 4 ++++ src/eepp/window/backend/SDL2/inputsdl2.cpp | 16 ++++++++++++++++ src/tools/ecode/ecode.cpp | 1 + src/tools/ecode/macos/macos.hpp | 2 ++ src/tools/ecode/macos/macos.m | 4 ++++ 9 files changed, 55 insertions(+) diff --git a/include/eepp/scene/eventdispatcher.hpp b/include/eepp/scene/eventdispatcher.hpp index d07cc8913..26c7d75f2 100644 --- a/include/eepp/scene/eventdispatcher.hpp +++ b/include/eepp/scene/eventdispatcher.hpp @@ -68,6 +68,8 @@ class EE_API EventDispatcher { void sendMouseDown( Node* toNode, const Vector2i& pos, const Uint32 flags ); + void sendMouseWheel( const Vector2f& offset, bool flipped ); + const Uint32& getPressTrigger() const; const Uint32& getLastPressTrigger() const; diff --git a/include/eepp/scene/node.hpp b/include/eepp/scene/node.hpp index 97c52102a..bbefca5b4 100644 --- a/include/eepp/scene/node.hpp +++ b/include/eepp/scene/node.hpp @@ -495,6 +495,8 @@ class EE_API Node : public Transformable { virtual Uint32 onMouseLeave( const Vector2i& position, const Uint32& flags ); + virtual Uint32 onMouseWheel( const Vector2f& offset, bool flipped ); + virtual Uint32 onCalculateDrag( const Vector2f& position, const Uint32& flags ); void onClose(); diff --git a/include/eepp/window/inputevent.hpp b/include/eepp/window/inputevent.hpp index 11060fb9d..5aa9e776b 100644 --- a/include/eepp/window/inputevent.hpp +++ b/include/eepp/window/inputevent.hpp @@ -76,6 +76,14 @@ class InputEvent { Int16 x, y; /** The X/Y coordinates of the mouse at press time */ }; + struct WheelEvent { + enum Direction { Normal, Flipped }; + Uint32 which; + Float x; + Float y; + Direction direction; + }; + /** Touch finger motion/finger event structure */ struct FingerEvent { Uint32 timestamp; @@ -204,6 +212,7 @@ class InputEvent { FileDropped, TextDropped, TextEditing, + MouseWheel, EventUser, EventCount = EventUser - 1 }; @@ -233,6 +242,7 @@ class InputEvent { TextEditingEvent textediting; QuitEvent quit; UserEvent user; + WheelEvent wheel; SysWMEvent syswm; }; }; diff --git a/src/eepp/scene/eventdispatcher.cpp b/src/eepp/scene/eventdispatcher.cpp index 54a0da449..63dd4859e 100644 --- a/src/eepp/scene/eventdispatcher.cpp +++ b/src/eepp/scene/eventdispatcher.cpp @@ -66,6 +66,11 @@ void EventDispatcher::inputCallback( InputEvent* event ) { sendTextEditing( event->textediting.text, event->textediting.start, event->textediting.length ); break; + case InputEvent::MouseWheel: + sendMouseWheel( { event->wheel.x, event->wheel.y }, + event->wheel.direction == InputEvent::WheelEvent::Normal ? true + : false ); + break; case InputEvent::SysWM: case InputEvent::VideoResize: case InputEvent::VideoExpose: { @@ -253,6 +258,15 @@ void EventDispatcher::sendKeyDown( const Keycode& keyCode, const Scancode& scanc } } +void EventDispatcher::sendMouseWheel( const Vector2f& offset, bool flipped ) { + Node* node = mFocusNode; + while ( NULL != node ) { + if ( node->isEnabled() && node->onMouseWheel( offset, flipped ) ) + break; + node = node->getParent(); + } +} + void EventDispatcher::sendMsg( Node* node, const Uint32& Msg, const Uint32& Flags ) { NodeMessage tMsg( node, Msg, Flags ); node->messagePost( &tMsg ); diff --git a/src/eepp/scene/node.cpp b/src/eepp/scene/node.cpp index cd9cfec1a..23909725a 100644 --- a/src/eepp/scene/node.cpp +++ b/src/eepp/scene/node.cpp @@ -397,6 +397,10 @@ Uint32 Node::onMouseLeave( const Vector2i& Pos, const Uint32& Flags ) { return 1; } +Uint32 Node::onMouseWheel( const Vector2f& offset, bool flipped ) { + return 1; +} + Uint32 Node::onCalculateDrag( const Vector2f&, const Uint32& ) { return 1; } diff --git a/src/eepp/window/backend/SDL2/inputsdl2.cpp b/src/eepp/window/backend/SDL2/inputsdl2.cpp index 56261999a..806cf331e 100644 --- a/src/eepp/window/backend/SDL2/inputsdl2.cpp +++ b/src/eepp/window/backend/SDL2/inputsdl2.cpp @@ -357,6 +357,22 @@ void InputSDL::sendEvent( const SDL_Event& SDLEvent ) { event.Type = InputEvent::MouseButtonUp; event.button.state = 0; + processEvent( &event ); + + event.Type = InputEvent::MouseWheel; + event.wheel.which = SDLEvent.wheel.which; + event.wheel.direction = SDLEvent.wheel.direction == SDL_MOUSEWHEEL_NORMAL + ? InputEvent::WheelEvent::Normal + : InputEvent::WheelEvent::Flipped; + +#if SDL_VERSION_ATLEAST( 2, 0, 18 ) + event.wheel.x = SDLEvent.wheel.preciseX; + event.wheel.y = SDLEvent.wheel.preciseY; +#else + event.wheel.x = SDLEvent.wheel.x; + event.wheel.y = SDLEvent.wheel.y; +#endif + processEvent( &event ); break; } case SDL_FINGERMOTION: { diff --git a/src/tools/ecode/ecode.cpp b/src/tools/ecode/ecode.cpp index b5137056d..beb17de1f 100644 --- a/src/tools/ecode/ecode.cpp +++ b/src/tools/ecode/ecode.cpp @@ -3287,6 +3287,7 @@ void App::init( const LogLevel& logLevel, std::string file, const Float& pidelDe #endif #if EE_PLATFORM == EE_PLATFORM_MACOS macOS_CreateApplicationMenus(); + macOS_EnableScrollMomentum(); mThreadPool->run( [this]() { // Checks if the default shell path contains more paths diff --git a/src/tools/ecode/macos/macos.hpp b/src/tools/ecode/macos/macos.hpp index 2fcf0ed99..f0cdf9206 100644 --- a/src/tools/ecode/macos/macos.hpp +++ b/src/tools/ecode/macos/macos.hpp @@ -7,6 +7,8 @@ extern "C" { void macOS_CreateApplicationMenus(); +void macOS_EnableScrollMomentum(); + #ifdef __cplusplus } #endif diff --git a/src/tools/ecode/macos/macos.m b/src/tools/ecode/macos/macos.m index 55438b73b..98dde2b7a 100644 --- a/src/tools/ecode/macos/macos.m +++ b/src/tools/ecode/macos/macos.m @@ -63,3 +63,7 @@ macOS_CreateApplicationMenus(void) [NSApp setWindowsMenu:windowMenu]; } + +void macOS_EnableScrollMomentum() { + [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"AppleMomentumScrollSupported"]; +}