diff options
-rw-r--r-- | include/cru/platform/gui/Base.hpp | 7 | ||||
-rw-r--r-- | include/cru/platform/gui/Window.hpp | 1 | ||||
-rw-r--r-- | include/cru/ui/events/UiEvent.hpp | 10 | ||||
-rw-r--r-- | include/cru/ui/host/WindowHost.hpp | 2 | ||||
-rw-r--r-- | include/cru/win/gui/Window.hpp | 11 | ||||
-rw-r--r-- | src/ui/host/WindowHost.cpp | 15 | ||||
-rw-r--r-- | src/ui/render/ScrollRenderObject.cpp | 5 | ||||
-rw-r--r-- | src/win/gui/Window.cpp | 5 |
8 files changed, 47 insertions, 9 deletions
diff --git a/include/cru/platform/gui/Base.hpp b/include/cru/platform/gui/Base.hpp index 7a9d1889..fd9d265c 100644 --- a/include/cru/platform/gui/Base.hpp +++ b/include/cru/platform/gui/Base.hpp @@ -31,6 +31,13 @@ struct NativeMouseButtonEventArgs { KeyModifier modifier; }; +struct NativeMouseWheelEventArgs { + // Positive means down. Negative means up. + float delta; + Point point; + KeyModifier modifier; +}; + struct NativeKeyEventArgs { KeyCode key; KeyModifier modifier; diff --git a/include/cru/platform/gui/Window.hpp b/include/cru/platform/gui/Window.hpp index 26d1a476..b2129322 100644 --- a/include/cru/platform/gui/Window.hpp +++ b/include/cru/platform/gui/Window.hpp @@ -49,6 +49,7 @@ struct INativeWindow : virtual INativeResource { virtual IEvent<Point>* MouseMoveEvent() = 0; virtual IEvent<NativeMouseButtonEventArgs>* MouseDownEvent() = 0; virtual IEvent<NativeMouseButtonEventArgs>* MouseUpEvent() = 0; + virtual IEvent<NativeMouseWheelEventArgs>* MouseWheelEvent() = 0; virtual IEvent<NativeKeyEventArgs>* KeyDownEvent() = 0; virtual IEvent<NativeKeyEventArgs>* KeyUpEvent() = 0; diff --git a/include/cru/ui/events/UiEvent.hpp b/include/cru/ui/events/UiEvent.hpp index 22ad0150..33bc0a60 100644 --- a/include/cru/ui/events/UiEvent.hpp +++ b/include/cru/ui/events/UiEvent.hpp @@ -122,18 +122,24 @@ class MouseButtonEventArgs : public MouseEventArgs { class MouseWheelEventArgs : public MouseEventArgs { public: MouseWheelEventArgs(Object* sender, Object* original_sender, - const Point& point, const float delta) - : MouseEventArgs(sender, original_sender, point), delta_(delta) {} + const Point& point, const float delta, + platform::gui::KeyModifier key_modifier) + : MouseEventArgs(sender, original_sender, point), + delta_(delta), + key_modifier_(key_modifier) {} MouseWheelEventArgs(const MouseWheelEventArgs& other) = default; MouseWheelEventArgs(MouseWheelEventArgs&& other) = default; MouseWheelEventArgs& operator=(const MouseWheelEventArgs& other) = default; MouseWheelEventArgs& operator=(MouseWheelEventArgs&& other) = default; ~MouseWheelEventArgs() override = default; + // Positive means down; Negative means up. float GetDelta() const { return delta_; } + platform::gui::KeyModifier GetKeyModifier() const { return key_modifier_; } private: float delta_; + platform::gui::KeyModifier key_modifier_; }; class PaintEventArgs : public UiEventArgs { diff --git a/include/cru/ui/host/WindowHost.hpp b/include/cru/ui/host/WindowHost.hpp index cd9093bc..258b0c4c 100644 --- a/include/cru/ui/host/WindowHost.hpp +++ b/include/cru/ui/host/WindowHost.hpp @@ -139,6 +139,8 @@ class WindowHost : public Object { const platform::gui::NativeMouseButtonEventArgs& args); void OnNativeMouseUp(platform::gui::INativeWindow* window, const platform::gui::NativeMouseButtonEventArgs& args); + void OnNativeMouseWheel(platform::gui::INativeWindow* window, + const platform::gui::NativeMouseWheelEventArgs& args); void OnNativeKeyDown(platform::gui::INativeWindow* window, const platform::gui::NativeKeyEventArgs& args); diff --git a/include/cru/win/gui/Window.hpp b/include/cru/win/gui/Window.hpp index 3ba9ef68..97a74fa7 100644 --- a/include/cru/win/gui/Window.hpp +++ b/include/cru/win/gui/Window.hpp @@ -59,14 +59,16 @@ class WinNativeWindow : public WinNativeResource, public virtual INativeWindow { return &mouse_enter_leave_event_; } IEvent<Point>* MouseMoveEvent() override { return &mouse_move_event_; } - IEvent<platform::gui::NativeMouseButtonEventArgs>* MouseDownEvent() - override { + IEvent<platform::gui::NativeMouseButtonEventArgs>* MouseDownEvent() override { return &mouse_down_event_; } - IEvent<platform::gui::NativeMouseButtonEventArgs>* MouseUpEvent() - override { + IEvent<platform::gui::NativeMouseButtonEventArgs>* MouseUpEvent() override { return &mouse_up_event_; } + IEvent<NativeMouseWheelEventArgs>* MouseWheelEvent() override { + return &mouse_wheel_event_; + } + IEvent<platform::gui::NativeKeyEventArgs>* KeyDownEvent() override { return &key_down_event_; } @@ -170,6 +172,7 @@ class WinNativeWindow : public WinNativeResource, public virtual INativeWindow { Event<Point> mouse_move_event_; Event<platform::gui::NativeMouseButtonEventArgs> mouse_down_event_; Event<platform::gui::NativeMouseButtonEventArgs> mouse_up_event_; + Event<platform::gui::NativeMouseWheelEventArgs> mouse_wheel_event_; Event<platform::gui::NativeKeyEventArgs> key_down_event_; Event<platform::gui::NativeKeyEventArgs> key_up_event_; diff --git a/src/ui/host/WindowHost.cpp b/src/ui/host/WindowHost.cpp index eac2ef41..e24a883f 100644 --- a/src/ui/host/WindowHost.cpp +++ b/src/ui/host/WindowHost.cpp @@ -36,6 +36,7 @@ CRU_DEFINE_EVENT_NAME(MouseLeave) CRU_DEFINE_EVENT_NAME(MouseMove) CRU_DEFINE_EVENT_NAME(MouseDown) CRU_DEFINE_EVENT_NAME(MouseUp) +CRU_DEFINE_EVENT_NAME(MouseWheel) CRU_DEFINE_EVENT_NAME(KeyDown) CRU_DEFINE_EVENT_NAME(KeyUp) @@ -145,6 +146,8 @@ gsl::not_null<platform::gui::INativeWindow*> WindowHost::CreateNativeWindow( &WindowHost::OnNativeMouseDown, event_revoker_guards_); BindNativeEvent(this, native_window, native_window->MouseUpEvent(), &WindowHost::OnNativeMouseUp, event_revoker_guards_); + BindNativeEvent(this, native_window, native_window->MouseWheelEvent(), + &WindowHost::OnNativeMouseWheel, event_revoker_guards_); BindNativeEvent(this, native_window, native_window->KeyDownEvent(), &WindowHost::OnNativeKeyDown, event_revoker_guards_); BindNativeEvent(this, native_window, native_window->KeyUpEvent(), @@ -383,6 +386,18 @@ void WindowHost::OnNativeMouseUp( nullptr, args.point, args.button, args.modifier); } +void WindowHost::OnNativeMouseWheel( + platform::gui::INativeWindow* window, + const platform::gui::NativeMouseWheelEventArgs& args) { + CRU_UNUSED(window) + + controls::Control* control = + mouse_captured_control_ ? mouse_captured_control_ : HitTest(args.point); + DispatchEvent(event_names::MouseWheel, control, + &controls::Control::MouseWheelEvent, nullptr, args.point, + args.delta, args.modifier); +} + void WindowHost::OnNativeKeyDown( INativeWindow* window, const platform::gui::NativeKeyEventArgs& args) { CRU_UNUSED(window) diff --git a/src/ui/render/ScrollRenderObject.cpp b/src/ui/render/ScrollRenderObject.cpp index d0cdfdf6..93472cdc 100644 --- a/src/ui/render/ScrollRenderObject.cpp +++ b/src/ui/render/ScrollRenderObject.cpp @@ -212,7 +212,10 @@ void ScrollRenderObject::InstallMouseWheelHandler(controls::Control* control) { if (control != nullptr) { guard_ += control->MouseWheelEvent()->Bubble()->PrependShortCircuitHandler( [this](event::MouseWheelEventArgs& args) { - const auto delta = args.GetDelta(); + auto delta = args.GetDelta(); + + delta *= 24; + if (delta > 0) { if (VerticalCanScrollDown()) { ApplyScroll( diff --git a/src/win/gui/Window.cpp b/src/win/gui/Window.cpp index efd3bfcc..4c75a319 100644 --- a/src/win/gui/Window.cpp +++ b/src/win/gui/Window.cpp @@ -438,8 +438,9 @@ void WinNativeWindow::OnMouseUpInternal(platform::gui::MouseButton button, } void WinNativeWindow::OnMouseWheelInternal(short delta, POINT point) { - CRU_UNUSED(delta) - CRU_UNUSED(point) + const auto dip_point = PixelToDip(point); + const float d = -((float)delta / 120.f); + mouse_wheel_event_.Raise({d, dip_point, RetrieveKeyMofifier()}); } void WinNativeWindow::OnKeyDownInternal(int virtual_code) { |