From f78458173c1baf567cc96880571b380e95a1039a Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 28 Nov 2018 19:18:00 +0800 Subject: Refactor event system. --- src/ui/controls/scroll_control.cpp | 245 ++++++++++++++++++------------------- 1 file changed, 118 insertions(+), 127 deletions(-) (limited to 'src/ui/controls/scroll_control.cpp') diff --git a/src/ui/controls/scroll_control.cpp b/src/ui/controls/scroll_control.cpp index aa5403d4..07715892 100644 --- a/src/ui/controls/scroll_control.cpp +++ b/src/ui/controls/scroll_control.cpp @@ -17,6 +17,124 @@ namespace cru::ui::controls ScrollControl::ScrollControl(const bool container) : Control(container) { SetClipContent(true); + + draw_foreground_event.AddHandler([this](events::DrawEventArgs& args) + { + const auto device_context = args.GetDeviceContext(); + const auto predefined = UiManager::GetInstance()->GetPredefineResources(); + + if (is_horizontal_scroll_bar_visible_) + { + device_context->FillRectangle( + Convert(horizontal_bar_info_.border), + predefined->scroll_bar_background_brush.Get() + ); + + device_context->FillRectangle( + Convert(horizontal_bar_info_.bar), + predefined->scroll_bar_brush.Get() + ); + + device_context->DrawLine( + Convert(horizontal_bar_info_.border.GetLeftTop()), + Convert(horizontal_bar_info_.border.GetRightTop()), + predefined->scroll_bar_border_brush.Get() + ); + } + + if (is_vertical_scroll_bar_visible_) + { + device_context->FillRectangle( + Convert(vertical_bar_info_.border), + predefined->scroll_bar_background_brush.Get() + ); + + device_context->FillRectangle( + Convert(vertical_bar_info_.bar), + predefined->scroll_bar_brush.Get() + ); + + device_context->DrawLine( + Convert(vertical_bar_info_.border.GetLeftTop()), + Convert(vertical_bar_info_.border.GetLeftBottom()), + predefined->scroll_bar_border_brush.Get() + ); + } + }); + + mouse_down_event.bubble.AddHandler([this](events::MouseButtonEventArgs& args) + { + if (args.GetMouseButton() == MouseButton::Left) + { + const auto point = args.GetPoint(this); + if (is_vertical_scroll_bar_visible_ && vertical_bar_info_.bar.IsPointInside(point)) + { + GetWindow()->CaptureMouseFor(this); + is_pressing_scroll_bar_ = Orientation::Vertical; + pressing_delta_ = point.y - vertical_bar_info_.bar.top; + return; + } + + if (is_horizontal_scroll_bar_visible_ && horizontal_bar_info_.bar.IsPointInside(point)) + { + GetWindow()->CaptureMouseFor(this); + pressing_delta_ = point.x - horizontal_bar_info_.bar.left; + is_pressing_scroll_bar_ = Orientation::Horizontal; + return; + } + } + }); + + mouse_move_event.bubble.AddHandler([this](events::MouseEventArgs& args) + { + const auto mouse_point = args.GetPoint(this); + + if (is_pressing_scroll_bar_ == Orientation::Horizontal) + { + const auto new_head_position = mouse_point.x - pressing_delta_; + const auto new_offset = new_head_position / horizontal_bar_info_.border.width * view_width_; + SetScrollOffset(new_offset, std::nullopt); + return; + } + + if (is_pressing_scroll_bar_ == Orientation::Vertical) + { + const auto new_head_position = mouse_point.y - pressing_delta_; + const auto new_offset = new_head_position / vertical_bar_info_.border.height * view_height_; + SetScrollOffset(std::nullopt, new_offset); + return; + } + }); + + mouse_up_event.bubble.AddHandler([this](events::MouseButtonEventArgs& args) + { + if (args.GetMouseButton() == MouseButton::Left && is_pressing_scroll_bar_.has_value()) + { + GetWindow()->ReleaseCurrentMouseCapture(); + is_pressing_scroll_bar_ = std::nullopt; + } + }); + + mouse_wheel_event.bubble.AddHandler([this](events::MouseWheelEventArgs& 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; + } + }); } ScrollControl::~ScrollControl() @@ -204,133 +322,6 @@ namespace cru::ui::controls UpdateScrollBarVisibility(); } - void ScrollControl::OnDrawForeground(ID2D1DeviceContext* device_context) - { - Control::OnDrawForeground(device_context); - - const auto predefined = UiManager::GetInstance()->GetPredefineResources(); - - if (is_horizontal_scroll_bar_visible_) - { - device_context->FillRectangle( - Convert(horizontal_bar_info_.border), - predefined->scroll_bar_background_brush.Get() - ); - - device_context->FillRectangle( - Convert(horizontal_bar_info_.bar), - predefined->scroll_bar_brush.Get() - ); - - device_context->DrawLine( - Convert(horizontal_bar_info_.border.GetLeftTop()), - Convert(horizontal_bar_info_.border.GetRightTop()), - predefined->scroll_bar_border_brush.Get() - ); - } - - if (is_vertical_scroll_bar_visible_) - { - device_context->FillRectangle( - Convert(vertical_bar_info_.border), - predefined->scroll_bar_background_brush.Get() - ); - - device_context->FillRectangle( - Convert(vertical_bar_info_.bar), - predefined->scroll_bar_brush.Get() - ); - - device_context->DrawLine( - Convert(vertical_bar_info_.border.GetLeftTop()), - Convert(vertical_bar_info_.border.GetLeftBottom()), - predefined->scroll_bar_border_brush.Get() - ); - } - } - - void ScrollControl::OnMouseDownCore(events::MouseButtonEventArgs& args) - { - Control::OnMouseDownCore(args); - - if (args.GetMouseButton() == MouseButton::Left) - { - const auto point = args.GetPoint(this); - if (is_vertical_scroll_bar_visible_ && vertical_bar_info_.bar.IsPointInside(point)) - { - GetWindow()->CaptureMouseFor(this); - is_pressing_scroll_bar_ = Orientation::Vertical; - pressing_delta_ = point.y - vertical_bar_info_.bar.top; - return; - } - - if (is_horizontal_scroll_bar_visible_ && horizontal_bar_info_.bar.IsPointInside(point)) - { - GetWindow()->CaptureMouseFor(this); - pressing_delta_ = point.x - horizontal_bar_info_.bar.left; - is_pressing_scroll_bar_ = Orientation::Horizontal; - return; - } - } - } - - void ScrollControl::OnMouseMoveCore(events::MouseEventArgs& args) - { - Control::OnMouseMoveCore(args); - - const auto mouse_point = args.GetPoint(this); - - if (is_pressing_scroll_bar_ == Orientation::Horizontal) - { - const auto new_head_position = mouse_point.x - pressing_delta_; - const auto new_offset = new_head_position / horizontal_bar_info_.border.width * view_width_; - SetScrollOffset(new_offset, std::nullopt); - return; - } - - if (is_pressing_scroll_bar_ == Orientation::Vertical) - { - const auto new_head_position = mouse_point.y - pressing_delta_; - const auto new_offset = new_head_position / vertical_bar_info_.border.height * view_height_; - SetScrollOffset(std::nullopt, new_offset); - return; - } - } - - void ScrollControl::OnMouseUpCore(events::MouseButtonEventArgs& args) - { - Control::OnMouseUpCore(args); - - if (args.GetMouseButton() == MouseButton::Left && is_pressing_scroll_bar_.has_value()) - { - GetWindow()->ReleaseCurrentMouseCapture(); - is_pressing_scroll_bar_ = std::nullopt; - } - } - - 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_; -- cgit v1.2.3 From b4571d0ddad966efcc6e305f5d7e79cd593a2b25 Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 28 Nov 2018 19:25:35 +0800 Subject: Change some routed events handlers in subcontrols. --- src/ui/controls/list_item.cpp | 6 +++--- src/ui/controls/scroll_control.cpp | 13 ++++++++++--- src/ui/controls/text_box.cpp | 4 ++-- src/ui/controls/text_control.cpp | 2 +- 4 files changed, 16 insertions(+), 9 deletions(-) (limited to 'src/ui/controls/scroll_control.cpp') diff --git a/src/ui/controls/list_item.cpp b/src/ui/controls/list_item.cpp index 92f8750f..8234da30 100644 --- a/src/ui/controls/list_item.cpp +++ b/src/ui/controls/list_item.cpp @@ -24,7 +24,7 @@ namespace cru::ui::controls device_context->DrawRectangle(Convert(rect.Shrink(Thickness(0.5))), brushes_[state_].border_brush.Get(), 1); }); - mouse_enter_event.bubble.AddHandler([this](events::MouseEventArgs& args) + mouse_enter_event.direct.AddHandler([this](events::MouseEventArgs& args) { if (GetState() == State::Select) return; @@ -35,7 +35,7 @@ namespace cru::ui::controls SetState(State::Hover); }); - mouse_leave_event.bubble.AddHandler([this](events::MouseEventArgs& args) + mouse_leave_event.direct.AddHandler([this](events::MouseEventArgs& args) { if (GetState() == State::Select) return; @@ -43,7 +43,7 @@ namespace cru::ui::controls SetState(State::Normal); }); - mouse_click_event.bubble.AddHandler([this](events::MouseButtonEventArgs& args) + mouse_click_event.direct.AddHandler([this](events::MouseButtonEventArgs& args) { if (args.GetMouseButton() == MouseButton::Left) SetState(State::Select); diff --git a/src/ui/controls/scroll_control.cpp b/src/ui/controls/scroll_control.cpp index 07715892..9681924d 100644 --- a/src/ui/controls/scroll_control.cpp +++ b/src/ui/controls/scroll_control.cpp @@ -62,7 +62,7 @@ namespace cru::ui::controls } }); - mouse_down_event.bubble.AddHandler([this](events::MouseButtonEventArgs& args) + mouse_down_event.tunnel.AddHandler([this](events::MouseButtonEventArgs& args) { if (args.GetMouseButton() == MouseButton::Left) { @@ -72,6 +72,7 @@ namespace cru::ui::controls GetWindow()->CaptureMouseFor(this); is_pressing_scroll_bar_ = Orientation::Vertical; pressing_delta_ = point.y - vertical_bar_info_.bar.top; + args.SetHandled(); return; } @@ -80,12 +81,13 @@ namespace cru::ui::controls GetWindow()->CaptureMouseFor(this); pressing_delta_ = point.x - horizontal_bar_info_.bar.left; is_pressing_scroll_bar_ = Orientation::Horizontal; + args.SetHandled(); return; } } }); - mouse_move_event.bubble.AddHandler([this](events::MouseEventArgs& args) + mouse_move_event.tunnel.AddHandler([this](events::MouseEventArgs& args) { const auto mouse_point = args.GetPoint(this); @@ -94,6 +96,7 @@ namespace cru::ui::controls const auto new_head_position = mouse_point.x - pressing_delta_; const auto new_offset = new_head_position / horizontal_bar_info_.border.width * view_width_; SetScrollOffset(new_offset, std::nullopt); + args.SetHandled(); return; } @@ -102,16 +105,18 @@ namespace cru::ui::controls const auto new_head_position = mouse_point.y - pressing_delta_; const auto new_offset = new_head_position / vertical_bar_info_.border.height * view_height_; SetScrollOffset(std::nullopt, new_offset); + args.SetHandled(); return; } }); - mouse_up_event.bubble.AddHandler([this](events::MouseButtonEventArgs& args) + mouse_up_event.tunnel.AddHandler([this](events::MouseButtonEventArgs& args) { if (args.GetMouseButton() == MouseButton::Left && is_pressing_scroll_bar_.has_value()) { GetWindow()->ReleaseCurrentMouseCapture(); is_pressing_scroll_bar_ = std::nullopt; + args.SetHandled(); } }); @@ -126,12 +131,14 @@ namespace cru::ui::controls if (IsVerticalScrollEnabled() && GetScrollOffsetY() != (args.GetDelta() > 0.0f ? 0.0f : AtLeast0(GetViewHeight() - content_rect.height))) { SetScrollOffset(std::nullopt, GetScrollOffsetY() - args.GetDelta() / WHEEL_DELTA * view_delta); + args.SetHandled(); 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); + args.SetHandled(); return; } }); diff --git a/src/ui/controls/text_box.cpp b/src/ui/controls/text_box.cpp index 28e1053d..893d6e8d 100644 --- a/src/ui/controls/text_box.cpp +++ b/src/ui/controls/text_box.cpp @@ -34,7 +34,7 @@ namespace cru::ui::controls } }); - get_focus_event.bubble.AddHandler([this](events::FocusChangeEventArgs& args) + get_focus_event.direct.AddHandler([this](events::FocusChangeEventArgs& args) { assert(!caret_timer_.has_value()); is_caret_show_ = true; @@ -45,7 +45,7 @@ namespace cru::ui::controls }); }); - lose_focus_event.bubble.AddHandler([this](events::FocusChangeEventArgs& args) + lose_focus_event.direct.AddHandler([this](events::FocusChangeEventArgs& args) { assert(caret_timer_.has_value()); caret_timer_->Cancel(); diff --git a/src/ui/controls/text_control.cpp b/src/ui/controls/text_control.cpp index 71a167e2..f32c068f 100644 --- a/src/ui/controls/text_control.cpp +++ b/src/ui/controls/text_control.cpp @@ -110,7 +110,7 @@ namespace cru::ui::controls } }); - lose_focus_event.bubble.AddHandler([this](events::FocusChangeEventArgs& args) + lose_focus_event.direct.AddHandler([this](events::FocusChangeEventArgs& args) { if (is_selecting_) { -- cgit v1.2.3