aboutsummaryrefslogtreecommitdiff
path: root/src/ui
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2018-11-27 17:53:21 +0800
committercrupest <crupest@outlook.com>2018-11-27 17:53:21 +0800
commit7ccb08ac09a83e81a822712b712dc0473c9b23cf (patch)
treea5757aca9903a5bcddf6b480d57030fd27b2ba1f /src/ui
parent7e00386fc0895f8b08cd64ef737cb6c618955635 (diff)
downloadcru-7ccb08ac09a83e81a822712b712dc0473c9b23cf.tar.gz
cru-7ccb08ac09a83e81a822712b712dc0473c9b23cf.tar.bz2
cru-7ccb08ac09a83e81a822712b712dc0473c9b23cf.zip
Add mouse wheel support.
Diffstat (limited to 'src/ui')
-rw-r--r--src/ui/control.cpp17
-rw-r--r--src/ui/control.hpp7
-rw-r--r--src/ui/controls/scroll_control.cpp23
-rw-r--r--src/ui/controls/scroll_control.hpp4
-rw-r--r--src/ui/events/ui_event.hpp25
-rw-r--r--src/ui/window.cpp22
-rw-r--r--src/ui/window.hpp3
7 files changed, 99 insertions, 2 deletions
diff --git a/src/ui/control.cpp b/src/ui/control.cpp
index 32909c75..2a81427a 100644
--- a/src/ui/control.cpp
+++ b/src/ui/control.cpp
@@ -709,6 +709,16 @@ namespace cru::ui
}
+ void Control::OnMouseWheel(events::MouseWheelEventArgs& args)
+ {
+
+ }
+
+ void Control::OnMouseWheelCore(events::MouseWheelEventArgs& args)
+ {
+
+ }
+
void Control::RaiseMouseEnterEvent(MouseEventArgs& args)
{
OnMouseEnterCore(args);
@@ -751,6 +761,13 @@ namespace cru::ui
mouse_click_event.Raise(args);
}
+ void Control::RaiseMouseWheelEvent(MouseWheelEventArgs& args)
+ {
+ OnMouseWheelCore(args);
+ OnMouseWheel(args);
+ mouse_wheel_event.Raise(args);
+ }
+
void Control::OnMouseClickBegin(MouseButton button)
{
diff --git a/src/ui/control.hpp b/src/ui/control.hpp
index 5f5285b6..d6ad9f02 100644
--- a/src/ui/control.hpp
+++ b/src/ui/control.hpp
@@ -261,6 +261,8 @@ namespace cru::ui
//Raised when a mouse button is pressed in the control and released in the control with mouse not leaving it between two operations.
events::MouseButtonEvent mouse_click_event;
+ events::MouseWheelEvent mouse_wheel_event;
+
events::KeyEvent key_down_event;
events::KeyEvent key_up_event;
events::CharEvent char_event;
@@ -334,6 +336,9 @@ namespace cru::ui
virtual void OnMouseUpCore(events::MouseButtonEventArgs& args);
virtual void OnMouseClickCore(events::MouseButtonEventArgs& args);
+ virtual void OnMouseWheel(events::MouseWheelEventArgs& args);
+ virtual void OnMouseWheelCore(events::MouseWheelEventArgs& args);
+
void RaiseMouseEnterEvent(events::MouseEventArgs& args);
void RaiseMouseLeaveEvent(events::MouseEventArgs& args);
void RaiseMouseMoveEvent(events::MouseEventArgs& args);
@@ -341,6 +346,8 @@ namespace cru::ui
void RaiseMouseUpEvent(events::MouseButtonEventArgs& args);
void RaiseMouseClickEvent(events::MouseButtonEventArgs& args);
+ void RaiseMouseWheelEvent(events::MouseWheelEventArgs& args);
+
virtual void OnMouseClickBegin(MouseButton button);
virtual void OnMouseClickEnd(MouseButton button);
diff --git a/src/ui/controls/scroll_control.cpp b/src/ui/controls/scroll_control.cpp
index 103a5c20..aa5403d4 100644
--- a/src/ui/controls/scroll_control.cpp
+++ b/src/ui/controls/scroll_control.cpp
@@ -308,6 +308,29 @@ namespace cru::ui::controls
}
}
+ void ScrollControl::OnMouseWheelCore(events::MouseWheelEventArgs& args)
+ {
+ Control::OnMouseWheelCore(args);
+
+ constexpr const auto view_delta = 30.0f;
+
+ if (args.GetDelta() == 0.0f)
+ return;
+
+ const auto content_rect = GetRect(RectRange::Content);
+ if (IsVerticalScrollEnabled() && GetScrollOffsetY() != (args.GetDelta() > 0.0f ? 0.0f : AtLeast0(GetViewHeight() - content_rect.height)))
+ {
+ SetScrollOffset(std::nullopt, GetScrollOffsetY() - args.GetDelta() / WHEEL_DELTA * view_delta);
+ return;
+ }
+
+ if (IsHorizontalScrollEnabled() && GetScrollOffsetX() != (args.GetDelta() > 0.0f ? 0.0f : AtLeast0(GetViewWidth() - content_rect.width)))
+ {
+ SetScrollOffset(GetScrollOffsetX() - args.GetDelta() / WHEEL_DELTA * view_delta, std::nullopt);
+ return;
+ }
+ }
+
void ScrollControl::CoerceAndSetOffsets(const float offset_x, const float offset_y, const bool update_children)
{
const auto old_offset_x = offset_x_;
diff --git a/src/ui/controls/scroll_control.hpp b/src/ui/controls/scroll_control.hpp
index 76762f21..0541a010 100644
--- a/src/ui/controls/scroll_control.hpp
+++ b/src/ui/controls/scroll_control.hpp
@@ -16,7 +16,7 @@ namespace cru::ui::controls
// Done: Draw(no need)
// Done: API
// Done: ScrollBar
- // TODO: MouseEvent
+ // Done: MouseEvent
class ScrollControl : public Control
{
private:
@@ -128,6 +128,8 @@ namespace cru::ui::controls
void OnMouseMoveCore(events::MouseEventArgs& args) override final;
void OnMouseUpCore(events::MouseButtonEventArgs& args) override final;
+ void OnMouseWheelCore(events::MouseWheelEventArgs& args) override;
+
private:
void CoerceAndSetOffsets(float offset_x, float offset_y, bool update_children = true);
void UpdateScrollBarVisibility();
diff --git a/src/ui/events/ui_event.hpp b/src/ui/events/ui_event.hpp
index cc651832..321e7135 100644
--- a/src/ui/events/ui_event.hpp
+++ b/src/ui/events/ui_event.hpp
@@ -88,6 +88,30 @@ namespace cru::ui::events
};
+ class MouseWheelEventArgs : public MouseEventArgs
+ {
+ public:
+ MouseWheelEventArgs(Object* sender, Object* original_sender, const Point& point, const float delta)
+ : MouseEventArgs(sender, original_sender, point), delta_(delta)
+ {
+
+ }
+ MouseWheelEventArgs(const MouseWheelEventArgs& other) = default;
+ MouseWheelEventArgs(MouseWheelEventArgs&& other) = default;
+ MouseWheelEventArgs& operator=(const MouseWheelEventArgs& other) = default;
+ MouseWheelEventArgs& operator=(MouseWheelEventArgs&& other) = default;
+ ~MouseWheelEventArgs() override = default;
+
+ float GetDelta() const
+ {
+ return delta_;
+ }
+
+ private:
+ float delta_;
+ };
+
+
class DrawEventArgs : public UiEventArgs
{
public:
@@ -307,6 +331,7 @@ namespace cru::ui::events
using UiEvent = Event<UiEventArgs>;
using MouseEvent = Event<MouseEventArgs>;
using MouseButtonEvent = Event<MouseButtonEventArgs>;
+ using MouseWheelEvent = Event<MouseWheelEventArgs>;
using DrawEvent = Event<DrawEventArgs>;
using PositionChangedEvent = Event<PositionChangedEventArgs>;
using SizeChangedEvent = Event<SizeChangedEventArgs>;
diff --git a/src/ui/window.cpp b/src/ui/window.cpp
index ceabddef..9352b747 100644
--- a/src/ui/window.cpp
+++ b/src/ui/window.cpp
@@ -412,6 +412,14 @@ namespace cru::ui
result = 0;
return true;
}
+ case WM_MOUSEWHEEL:
+ POINT point;
+ point.x = GET_X_LPARAM(l_param);
+ point.y = GET_Y_LPARAM(l_param);
+ ScreenToClient(hwnd, &point);
+ OnMouseWheelInternal(GET_WHEEL_DELTA_WPARAM(w_param), point);
+ result = 0;
+ return true;
case WM_KEYDOWN:
OnKeyDownInternal(static_cast<int>(w_param));
result = 0;
@@ -722,6 +730,20 @@ namespace cru::ui
DispatchEvent(control, &Control::RaiseMouseUpEvent, nullptr, dip_point, button);
}
+ void Window::OnMouseWheelInternal(short delta, POINT point)
+ {
+ const auto dip_point = PiToDip(point);
+
+ Control* control;
+
+ if (mouse_capture_control_)
+ control = mouse_capture_control_;
+ else
+ control = HitTest(dip_point);
+
+ DispatchEvent(control, &Control::RaiseMouseWheelEvent, nullptr, dip_point, static_cast<float>(delta));
+ }
+
void Window::OnKeyDownInternal(int virtual_code)
{
DispatchEvent(focus_control_, &Control::RaiseKeyDownEvent, nullptr, virtual_code);
diff --git a/src/ui/window.hpp b/src/ui/window.hpp
index 7c82bf89..e82aa585 100644
--- a/src/ui/window.hpp
+++ b/src/ui/window.hpp
@@ -270,7 +270,8 @@ namespace cru::ui
void OnMouseLeaveInternal();
void OnMouseDownInternal(MouseButton button, POINT point);
void OnMouseUpInternal(MouseButton button, POINT point);
-
+
+ void OnMouseWheelInternal(short delta, POINT point);
void OnKeyDownInternal(int virtual_code);
void OnKeyUpInternal(int virtual_code);
void OnCharInternal(wchar_t c);