From abc776a9ea02a564dfe44141e51e4cbc87a7a3a1 Mon Sep 17 00:00:00 2001 From: crupest Date: Sat, 24 Nov 2018 22:39:50 +0800 Subject: Develop layout for ScrollView --- src/ui/controls/scroll_control.cpp | 146 +++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 src/ui/controls/scroll_control.cpp (limited to 'src/ui/controls/scroll_control.cpp') diff --git a/src/ui/controls/scroll_control.cpp b/src/ui/controls/scroll_control.cpp new file mode 100644 index 00000000..00969ab7 --- /dev/null +++ b/src/ui/controls/scroll_control.cpp @@ -0,0 +1,146 @@ +#include "scroll_control.hpp" + +#include + +#include "cru_debug.hpp" +#include "format.hpp" + +namespace cru::ui::controls +{ + ScrollControl::ScrollControl(const bool container) : Control(container) + { + SetClipToPadding(true); + } + + ScrollControl::~ScrollControl() + { + + } + + void ScrollControl::SetHorizontalScrollEnabled(const bool enable) + { + horizontal_scroll_enabled_ = enable; + InvalidateLayout(); + InvalidateDraw(); + } + + void ScrollControl::SetVerticalScrollEnabled(const bool enable) + { + vertical_scroll_enabled_ = enable; + InvalidateLayout(); + InvalidateDraw(); + } + + Control* ScrollControl::HitTest(const Point& point) + { + + } + + void ScrollControl::SetViewWidth(const float length) + { + view_width_ = length; + } + + void ScrollControl::SetViewHeight(const float length) + { + view_height_ = length; + } + + Size ScrollControl::OnMeasureContent(const Size& available_size) + { + const auto layout_params = GetLayoutParams(); + + auto available_size_for_children = available_size; + if (IsHorizontalScrollEnabled()) + { + if (layout_params->width.mode == MeasureMode::Content) + debug::DebugMessage(L"ScrollView: Width measure mode is Content and horizontal scroll is enabled. So Stretch is used instead."); + + for (auto child : GetChildren()) + { + const auto child_layout_params = child->GetLayoutParams(); + if (child_layout_params->width.mode == MeasureMode::Stretch) + throw std::runtime_error(Format("ScrollView: Horizontal scroll is enabled but a child {} 's width measure mode is Stretch which may cause infinite length.", ToUtf8String(child->GetControlType()))); + } + + available_size_for_children.width = std::numeric_limits::max(); + } + + if (IsVerticalScrollEnabled()) + { + if (layout_params->height.mode == MeasureMode::Content) + debug::DebugMessage(L"ScrollView: Height measure mode is Content and vertical scroll is enabled. So Stretch is used instead."); + + for (auto child : GetChildren()) + { + const auto child_layout_params = child->GetLayoutParams(); + if (child_layout_params->height.mode == MeasureMode::Stretch) + throw std::runtime_error(Format("ScrollView: Vertical scroll is enabled but a child {} 's height measure mode is Stretch which may cause infinite length.", ToUtf8String(child->GetControlType()))); + } + + available_size_for_children.height = std::numeric_limits::max(); + } + + auto max_child_size = Size::Zero(); + for (auto control: GetChildren()) + { + control->Measure(available_size_for_children); + const auto&& size = control->GetDesiredSize(); + if (max_child_size.width < size.width) + max_child_size.width = size.width; + if (max_child_size.height < size.height) + max_child_size.height = size.height; + } + + // coerce size fro stretch. + for (auto control: GetChildren()) + { + auto size = control->GetDesiredSize(); + const auto child_layout_params = control->GetLayoutParams(); + if (child_layout_params->width.mode == MeasureMode::Stretch) + size.width = max_child_size.width; + if (child_layout_params->height.mode == MeasureMode::Stretch) + size.height = max_child_size.height; + control->SetDesiredSize(size); + } + + auto result = max_child_size; + if (IsHorizontalScrollEnabled()) + { + SetViewWidth(max_child_size.width); + result.width = available_size.width; + } + if (IsVerticalScrollEnabled()) + { + SetViewHeight(max_child_size.height); + result.height = available_size.height; + } + + return result; + } + + void ScrollControl::OnLayoutContent(const Rect& rect) + { + auto layout_rect = rect; + + if (IsHorizontalScrollEnabled()) + layout_rect.width = GetViewWidth(); + if (IsVerticalScrollEnabled()) + layout_rect.height = GetViewHeight(); + + for (auto control: GetChildren()) + { + const auto size = control->GetDesiredSize(); + // Ignore alignment, always center aligned. + auto&& calculate_anchor = [](const float anchor, const float layout_length, const float control_length) -> float + { + return anchor + (layout_length - control_length) / 2; + }; + + control->Layout(Rect(Point( + calculate_anchor(rect.left, layout_rect.width, size.width), + calculate_anchor(rect.top, layout_rect.height, size.height) + ), size)); + } + } +} -- cgit v1.2.3 From 5391e781e4682731721c841462904809bc618870 Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 25 Nov 2018 17:50:12 +0800 Subject: Change clip to padding into clip content. --- src/ui/control.cpp | 92 +++++++++++++++++++++++++------------- src/ui/control.hpp | 24 ++++++---- src/ui/controls/scroll_control.cpp | 2 +- src/ui/controls/text_control.cpp | 2 +- 4 files changed, 80 insertions(+), 40 deletions(-) (limited to 'src/ui/controls/scroll_control.cpp') diff --git a/src/ui/control.cpp b/src/ui/control.cpp index 22b7164f..66f22c58 100644 --- a/src/ui/control.cpp +++ b/src/ui/control.cpp @@ -191,20 +191,21 @@ namespace cru::ui bool Control::IsPointInside(const Point & point) { - if (border_geometry_ != nullptr) + const auto border_geometry = geometry_info_.border_geometry; + if (border_geometry != nullptr) { if (IsBordered()) { BOOL contains; - border_geometry_->FillContainsPoint(Convert(point), D2D1::Matrix3x2F::Identity(), &contains); + border_geometry->FillContainsPoint(Convert(point), D2D1::Matrix3x2F::Identity(), &contains); if (!contains) - border_geometry_->StrokeContainsPoint(Convert(point), GetBorderProperty().GetStrokeWidth(), nullptr, D2D1::Matrix3x2F::Identity(), &contains); + border_geometry->StrokeContainsPoint(Convert(point), GetBorderProperty().GetStrokeWidth(), nullptr, D2D1::Matrix3x2F::Identity(), &contains); return contains != 0; } else { BOOL contains; - border_geometry_->FillContainsPoint(Convert(point), D2D1::Matrix3x2F::Identity(), &contains); + border_geometry->FillContainsPoint(Convert(point), D2D1::Matrix3x2F::Identity(), &contains); return contains != 0; } } @@ -215,8 +216,18 @@ namespace cru::ui { const auto point_inside = IsPointInside(point); - if (!point_inside && IsClipToPadding()) - return nullptr; // if clip then don't test children. + if (IsClipContent()) + { + if (!point_inside) + return nullptr; + if (geometry_info_.content_geometry != nullptr) + { + BOOL contains; + ThrowIfFailed(geometry_info_.content_geometry->FillContainsPoint(Convert(point), D2D1::Matrix3x2F::Identity(), &contains)); + if (contains == 0) + return this; + } + } const auto& children = GetChildren(); @@ -229,18 +240,15 @@ namespace cru::ui return child_hit_test_result; } - if (!point_inside) - return nullptr; - - return this; + return point_inside ? this : nullptr; } - void Control::SetClipToPadding(const bool clip) + void Control::SetClipContent(const bool clip) { - if (clip_to_padding_ == clip) + if (clip_content_ == clip) return; - clip_to_padding_ = clip; + clip_content_ = clip; InvalidateDraw(); } @@ -254,9 +262,9 @@ namespace cru::ui OnDrawDecoration(device_context); - const auto set_layer = in_border_geometry_ != nullptr && IsClipToPadding(); + const auto set_layer = geometry_info_.content_geometry != nullptr && IsClipContent(); if (set_layer) - device_context->PushLayer(D2D1::LayerParameters(D2D1::InfiniteRect(), in_border_geometry_.Get()), nullptr); + device_context->PushLayer(D2D1::LayerParameters(D2D1::InfiniteRect(), geometry_info_.content_geometry.Get()), nullptr); OnDrawCore(device_context); @@ -377,7 +385,7 @@ namespace cru::ui void Control::UpdateBorder() { - RegenerateBorderGeometry(); + RegenerateGeometries(); InvalidateLayout(); InvalidateDraw(); } @@ -447,9 +455,9 @@ namespace cru::ui } #endif - if (is_bordered_ && border_geometry_ != nullptr) + if (is_bordered_ && geometry_info_.border_geometry != nullptr) device_context->DrawGeometry( - border_geometry_.Get(), + geometry_info_.border_geometry.Get(), GetBorderProperty().GetBrush().Get(), GetBorderProperty().GetStrokeWidth(), GetBorderProperty().GetStrokeStyle().Get() @@ -458,9 +466,10 @@ namespace cru::ui void Control::OnDrawCore(ID2D1DeviceContext* device_context) { + const auto ground_geometry = geometry_info_.padding_content_geometry; //draw background. - if (in_border_geometry_ != nullptr && background_brush_ != nullptr) - device_context->FillGeometry(in_border_geometry_.Get(), background_brush_.Get()); + if (ground_geometry != nullptr && background_brush_ != nullptr) + device_context->FillGeometry(ground_geometry.Get(), background_brush_.Get()); const auto padding_rect = GetRect(RectRange::Padding); graph::WithTransform(device_context, D2D1::Matrix3x2F::Translation(padding_rect.left, padding_rect.top), [this](ID2D1DeviceContext* device_context) @@ -482,8 +491,8 @@ namespace cru::ui //draw foreground. - if (in_border_geometry_ != nullptr && foreground_brush_ != nullptr) - device_context->FillGeometry(in_border_geometry_.Get(), foreground_brush_.Get()); + if (ground_geometry != nullptr && foreground_brush_ != nullptr) + device_context->FillGeometry(ground_geometry.Get(), foreground_brush_.Get()); graph::WithTransform(device_context, D2D1::Matrix3x2F::Translation(padding_rect.left, padding_rect.top), [this](ID2D1DeviceContext* device_context) { @@ -545,7 +554,7 @@ namespace cru::ui void Control::OnSizeChangedCore(SizeChangedEventArgs & args) { - RegenerateBorderGeometry(); + RegenerateGeometries(); #ifdef CRU_DEBUG_LAYOUT margin_geometry_ = CalculateSquareRingGeometry(GetRect(RectRange::Margin), GetRect(RectRange::FullBorder)); padding_geometry_ = CalculateSquareRingGeometry(GetRect(RectRange::Padding), GetRect(RectRange::Content)); @@ -566,7 +575,7 @@ namespace cru::ui size_changed_event.Raise(args); } - void Control::RegenerateBorderGeometry() + void Control::RegenerateGeometries() { if (IsBordered()) { @@ -579,10 +588,10 @@ namespace cru::ui ThrowIfFailed( graph::GraphManager::GetInstance()->GetD2D1Factory()->CreateRoundedRectangleGeometry(bound_rounded_rect, &geometry) ); - border_geometry_ = std::move(geometry); + geometry_info_.border_geometry = std::move(geometry); - const auto in_border_rect = GetRect(RectRange::Padding); - const auto in_border_rounded_rect = D2D1::RoundedRect(Convert(in_border_rect), + const auto padding_rect = GetRect(RectRange::Padding); + const auto in_border_rounded_rect = D2D1::RoundedRect(Convert(padding_rect), GetBorderProperty().GetRadiusX() - GetBorderProperty().GetStrokeWidth() / 2.0f, GetBorderProperty().GetRadiusY() - GetBorderProperty().GetStrokeWidth() / 2.0f); @@ -590,7 +599,24 @@ namespace cru::ui ThrowIfFailed( graph::GraphManager::GetInstance()->GetD2D1Factory()->CreateRoundedRectangleGeometry(in_border_rounded_rect, &geometry2) ); - in_border_geometry_ = std::move(geometry2); + geometry_info_.padding_content_geometry = geometry2; + + + Microsoft::WRL::ComPtr geometry3; + ThrowIfFailed( + graph::GraphManager::GetInstance()->GetD2D1Factory()->CreateRectangleGeometry(Convert(GetRect(RectRange::Content)), &geometry3) + ); + Microsoft::WRL::ComPtr geometry4; + ThrowIfFailed( + graph::GraphManager::GetInstance()->GetD2D1Factory()->CreatePathGeometry(&geometry4) + ); + Microsoft::WRL::ComPtr sink; + geometry4->Open(&sink); + ThrowIfFailed( + geometry3->CombineWithGeometry(geometry2.Get(), D2D1_COMBINE_MODE_INTERSECT, D2D1::Matrix3x2F::Identity(), sink.Get()) + ); + sink->Close(); + geometry_info_.content_geometry = std::move(geometry4); } else { @@ -599,8 +625,14 @@ namespace cru::ui ThrowIfFailed( graph::GraphManager::GetInstance()->GetD2D1Factory()->CreateRectangleGeometry(Convert(bound_rect), &geometry) ); - border_geometry_ = geometry; - in_border_geometry_ = std::move(geometry); + geometry_info_.border_geometry = geometry; + geometry_info_.padding_content_geometry = std::move(geometry); + + Microsoft::WRL::ComPtr geometry2; + ThrowIfFailed( + graph::GraphManager::GetInstance()->GetD2D1Factory()->CreateRectangleGeometry(Convert(GetRect(RectRange::Content)), &geometry2) + ); + geometry_info_.content_geometry = std::move(geometry2); } } diff --git a/src/ui/control.hpp b/src/ui/control.hpp index 44c98728..6c5b0ea5 100644 --- a/src/ui/control.hpp +++ b/src/ui/control.hpp @@ -26,11 +26,21 @@ namespace cru::ui Point lefttop_position_absolute; }; + class Control : public Object { friend class Window; friend class LayoutManager; + private: + struct GeometryInfo + { + Microsoft::WRL::ComPtr border_geometry = nullptr; + Microsoft::WRL::ComPtr padding_content_geometry = nullptr; + Microsoft::WRL::ComPtr content_geometry = nullptr; + }; + + protected: struct WindowConstructorTag {}; //Used for constructor for class Window. @@ -126,12 +136,12 @@ namespace cru::ui //*************** region: graphic *************** - bool IsClipToPadding() const + bool IsClipContent() const { - return clip_to_padding_; + return clip_content_; } - void SetClipToPadding(bool clip); + void SetClipContent(bool clip); //Draw this control and its child controls. void Draw(ID2D1DeviceContext* device_context); @@ -299,7 +309,7 @@ namespace cru::ui void RaisePositionChangedEvent(events::PositionChangedEventArgs& args); void RaiseSizeChangedEvent(events::SizeChangedEventArgs& args); - void RegenerateBorderGeometry(); + void RegenerateGeometries(); //*************** region: mouse event *************** virtual void OnMouseEnter(events::MouseEventArgs& args); @@ -403,11 +413,9 @@ namespace cru::ui bool is_bordered_ = false; BorderProperty border_property_; - bool clip_to_padding_ = false; + GeometryInfo geometry_info_{}; - Microsoft::WRL::ComPtr border_geometry_ = nullptr; - // used for foreground and background brush and clip. - Microsoft::WRL::ComPtr in_border_geometry_ = nullptr; + bool clip_content_ = false; Microsoft::WRL::ComPtr foreground_brush_ = nullptr; Microsoft::WRL::ComPtr background_brush_ = nullptr; diff --git a/src/ui/controls/scroll_control.cpp b/src/ui/controls/scroll_control.cpp index 00969ab7..6b6d899c 100644 --- a/src/ui/controls/scroll_control.cpp +++ b/src/ui/controls/scroll_control.cpp @@ -9,7 +9,7 @@ namespace cru::ui::controls { ScrollControl::ScrollControl(const bool container) : Control(container) { - SetClipToPadding(true); + SetClipContent(true); } ScrollControl::~ScrollControl() diff --git a/src/ui/controls/text_control.cpp b/src/ui/controls/text_control.cpp index c37441b3..d7d6b810 100644 --- a/src/ui/controls/text_control.cpp +++ b/src/ui/controls/text_control.cpp @@ -20,7 +20,7 @@ namespace cru::ui::controls selection_brush_ = UiManager::GetInstance()->GetPredefineResources()->text_control_selection_brush; - SetClipToPadding(true); + SetClipContent(true); } -- cgit v1.2.3 From 22322daf6f51da53740ff95ef2eaceed9a6efcae Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 25 Nov 2018 22:57:07 +0800 Subject: Develop basic functions of ScrollControl. --- CruUI.vcxproj | 1 + CruUI.vcxproj.filters | 3 + src/base.hpp | 30 ----- src/main.cpp | 13 +- src/math_util.hpp | 66 ++++++++++ src/ui/control.cpp | 13 +- src/ui/control.hpp | 12 +- src/ui/controls/linear_layout.cpp | 12 +- src/ui/controls/scroll_control.cpp | 249 +++++++++++++++++++++++++++++++++++-- src/ui/controls/scroll_control.hpp | 87 +++++++++++-- src/ui/ui_base.hpp | 10 ++ src/ui/ui_manager.cpp | 6 +- src/ui/ui_manager.hpp | 4 + 13 files changed, 441 insertions(+), 65 deletions(-) create mode 100644 src/math_util.hpp (limited to 'src/ui/controls/scroll_control.cpp') diff --git a/CruUI.vcxproj b/CruUI.vcxproj index a88f7bf2..c397f1d0 100644 --- a/CruUI.vcxproj +++ b/CruUI.vcxproj @@ -138,6 +138,7 @@ + diff --git a/CruUI.vcxproj.filters b/CruUI.vcxproj.filters index 1a4d3287..7852404c 100644 --- a/CruUI.vcxproj.filters +++ b/CruUI.vcxproj.filters @@ -188,6 +188,9 @@ Header Files + + Header Files + diff --git a/src/base.hpp b/src/base.hpp index 5d8cb9ce..fdd736a0 100644 --- a/src/base.hpp +++ b/src/base.hpp @@ -8,9 +8,6 @@ #include #include #include -#include -// ReSharper disable once CppUnusedIncludeDirective -#include namespace cru { @@ -60,31 +57,4 @@ namespace cru if (!condition) throw std::invalid_argument(error_message.data()); } - - template >> - float Coerce(const T n, const std::optional min, const std::optional max) - { - if (min.has_value() && n < min.value()) - return min.value(); - if (max.has_value() && n > max.value()) - return max.value(); - return n; - } - - template >> - float Coerce(const T n, const std::nullopt_t, const std::optional max) - { - if (max.has_value() && n > max.value()) - return max.value(); - return n; - } - - template >> - float Coerce(const T n, const std::optional min, const std::nullopt_t) - { - if (min.has_value() && n < min.value()) - return min.value(); - return n; - } - } diff --git a/src/main.cpp b/src/main.cpp index 376c03b8..5cc98a72 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,6 +8,7 @@ #include "ui/controls/list_item.hpp" #include "ui/controls/popup_menu.hpp" #include "ui/controls/frame_layout.hpp" +#include "ui/controls/scroll_control.hpp" #include "graph/graph.hpp" using cru::String; @@ -27,6 +28,7 @@ using cru::ui::controls::Button; using cru::ui::controls::TextBox; using cru::ui::controls::ListItem; using cru::ui::controls::FrameLayout; +using cru::ui::controls::ScrollControl; int APIENTRY wWinMain( HINSTANCE hInstance, @@ -183,9 +185,16 @@ int APIENTRY wWinMain( } { - const auto text_block = CreateWithLayout(LayoutSideParams::Stretch(), LayoutSideParams::Stretch(), L"This is a very very very very very long sentence!!!"); + const auto scroll_view = CreateWithLayout(LayoutSideParams::Stretch(), LayoutSideParams::Stretch()); + + scroll_view->SetVerticalScrollBarVisibility(ScrollControl::ScrollBarVisibility::Always); + + const auto text_block = TextBlock::Create( + L"Love myself I do. Not everything, but I love the good as well as the bad. I love my crazy lifestyle, and I love my hard discipline. I love my freedom of speech and the way my eyes get dark when I'm tired. I love that I have learned to trust people with my heart, even if it will get broken. I am proud of everything that I am and will become."); text_block->SetSelectable(true); - layout->AddChild(text_block); + + scroll_view->AddChild(text_block); + layout->AddChild(scroll_view); } layout->AddChild(CreateWithLayout(LayoutSideParams::Content(Alignment::Start), LayoutSideParams::Content(), L"This is a little short sentence!!!")); diff --git a/src/math_util.hpp b/src/math_util.hpp new file mode 100644 index 00000000..7b286346 --- /dev/null +++ b/src/math_util.hpp @@ -0,0 +1,66 @@ +#pragma once + +// ReSharper disable once CppUnusedIncludeDirective +#include +#include + +namespace cru +{ + template >> + float Coerce(const T n, const std::optional min, const std::optional max) + { + if (min.has_value() && n < min.value()) + return min.value(); + if (max.has_value() && n > max.value()) + return max.value(); + return n; + } + + template >> + float Coerce(const T n, const T min, const T max) + { + if (n < min) + return min; + if (n > max) + return max; + return n; + } + + template >> + float Coerce(const T n, const std::nullopt_t, const std::optional max) + { + if (max.has_value() && n > max.value()) + return max.value(); + return n; + } + + template >> + float Coerce(const T n, const std::optional min, const std::nullopt_t) + { + if (min.has_value() && n < min.value()) + return min.value(); + return n; + } + + template >> + float Coerce(const T n, const std::nullopt_t, const T max) + { + if (n > max) + return max; + return n; + } + + template >> + float Coerce(const T n, const T min, const std::nullopt_t) + { + if (n < min) + return min; + return n; + } + + template >> + T AtLeast0(const T value) + { + return value < static_cast(0) ? static_cast(0) : value; + } +} diff --git a/src/ui/control.cpp b/src/ui/control.cpp index 66f22c58..32909c75 100644 --- a/src/ui/control.cpp +++ b/src/ui/control.cpp @@ -8,6 +8,7 @@ #include "exception.hpp" #include "cru_debug.hpp" #include "convert_util.hpp" +#include "math_util.hpp" #ifdef CRU_DEBUG_LAYOUT #include "ui_manager.hpp" @@ -316,6 +317,7 @@ namespace cru::ui { SetPositionRelative(rect.GetLeftTop()); SetSize(rect.GetSize()); + AfterLayoutSelf(); OnLayoutCore(Rect(Point::Zero(), rect.GetSize())); } @@ -385,7 +387,7 @@ namespace cru::ui void Control::UpdateBorder() { - RegenerateGeometries(); + RegenerateGeometryInfo(); InvalidateLayout(); InvalidateDraw(); } @@ -554,7 +556,7 @@ namespace cru::ui void Control::OnSizeChangedCore(SizeChangedEventArgs & args) { - RegenerateGeometries(); + RegenerateGeometryInfo(); #ifdef CRU_DEBUG_LAYOUT margin_geometry_ = CalculateSquareRingGeometry(GetRect(RectRange::Margin), GetRect(RectRange::FullBorder)); padding_geometry_ = CalculateSquareRingGeometry(GetRect(RectRange::Padding), GetRect(RectRange::Content)); @@ -575,7 +577,7 @@ namespace cru::ui size_changed_event.Raise(args); } - void Control::RegenerateGeometries() + void Control::RegenerateGeometryInfo() { if (IsBordered()) { @@ -1044,6 +1046,11 @@ namespace cru::ui } } + void Control::AfterLayoutSelf() + { + + } + void Control::CheckAndNotifyPositionChanged() { if (this->old_position_ != this->position_) diff --git a/src/ui/control.hpp b/src/ui/control.hpp index 6c5b0ea5..1ce4afe3 100644 --- a/src/ui/control.hpp +++ b/src/ui/control.hpp @@ -32,7 +32,7 @@ namespace cru::ui friend class Window; friend class LayoutManager; - private: + protected: struct GeometryInfo { Microsoft::WRL::ComPtr border_geometry = nullptr; @@ -309,7 +309,12 @@ namespace cru::ui void RaisePositionChangedEvent(events::PositionChangedEventArgs& args); void RaiseSizeChangedEvent(events::SizeChangedEventArgs& args); - void RegenerateGeometries(); + void RegenerateGeometryInfo(); + + const GeometryInfo& GetGeometryInfo() const + { + return geometry_info_; + } //*************** region: mouse event *************** virtual void OnMouseEnter(events::MouseEventArgs& args); @@ -366,6 +371,9 @@ namespace cru::ui virtual Size OnMeasureContent(const Size& available_size); virtual void OnLayoutContent(const Rect& rect); + // Called by Layout after set position and size. + virtual void AfterLayoutSelf(); + private: // Only for layout manager to use. // Check if the old position is updated to current position. diff --git a/src/ui/controls/linear_layout.cpp b/src/ui/controls/linear_layout.cpp index 3789b305..8fb91513 100644 --- a/src/ui/controls/linear_layout.cpp +++ b/src/ui/controls/linear_layout.cpp @@ -2,6 +2,8 @@ #include +#include "math_util.hpp" + namespace cru::ui::controls { LinearLayout::LinearLayout(const Orientation orientation) @@ -10,16 +12,6 @@ namespace cru::ui::controls } - inline float AtLeast0(const float value) - { - return value < 0 ? 0 : value; - } - - inline Size AtLeast0(const Size& size) - { - return Size(AtLeast0(size.width), AtLeast0(size.height)); - } - StringView LinearLayout::GetControlType() const { return control_type; diff --git a/src/ui/controls/scroll_control.cpp b/src/ui/controls/scroll_control.cpp index 6b6d899c..103a5c20 100644 --- a/src/ui/controls/scroll_control.cpp +++ b/src/ui/controls/scroll_control.cpp @@ -4,9 +4,16 @@ #include "cru_debug.hpp" #include "format.hpp" +#include "ui/convert_util.hpp" +#include "exception.hpp" +#include "math_util.hpp" +#include "ui/ui_manager.hpp" +#include "ui/window.hpp" namespace cru::ui::controls { + constexpr auto scroll_bar_width = 15.0f; + ScrollControl::ScrollControl(const bool container) : Control(container) { SetClipContent(true); @@ -17,6 +24,11 @@ namespace cru::ui::controls } + StringView ScrollControl::GetControlType() const + { + return control_type; + } + void ScrollControl::SetHorizontalScrollEnabled(const bool enable) { horizontal_scroll_enabled_ = enable; @@ -31,9 +43,50 @@ namespace cru::ui::controls InvalidateDraw(); } - Control* ScrollControl::HitTest(const Point& point) + void ScrollControl::SetHorizontalScrollBarVisibility(const ScrollBarVisibility visibility) { + if (visibility != horizontal_scroll_bar_visibility_) + { + horizontal_scroll_bar_visibility_ = visibility; + switch (visibility) + { + case ScrollBarVisibility::Always: + is_horizontal_scroll_bar_visible_ = true; + break; + case ScrollBarVisibility::None: + is_horizontal_scroll_bar_visible_ = false; + break; + case ScrollBarVisibility::Auto: + UpdateScrollBarVisibility(); + } + InvalidateDraw(); + } + } + void ScrollControl::SetVerticalScrollBarVisibility(const ScrollBarVisibility visibility) + { + if (visibility != vertical_scroll_bar_visibility_) + { + vertical_scroll_bar_visibility_ = visibility; + switch (visibility) + { + case ScrollBarVisibility::Always: + is_vertical_scroll_bar_visible_ = true; + break; + case ScrollBarVisibility::None: + is_vertical_scroll_bar_visible_ = false; + break; + case ScrollBarVisibility::Auto: + UpdateScrollBarVisibility(); + } + InvalidateDraw(); + } + + } + + void ScrollControl::SetScrollOffset(std::optional x, std::optional y) + { + CoerceAndSetOffsets(x.value_or(GetScrollOffsetX()), y.value_or(GetScrollOffsetY())); } void ScrollControl::SetViewWidth(const float length) @@ -54,13 +107,13 @@ namespace cru::ui::controls if (IsHorizontalScrollEnabled()) { if (layout_params->width.mode == MeasureMode::Content) - debug::DebugMessage(L"ScrollView: Width measure mode is Content and horizontal scroll is enabled. So Stretch is used instead."); + debug::DebugMessage(L"ScrollControl: Width measure mode is Content and horizontal scroll is enabled. So Stretch is used instead."); for (auto child : GetChildren()) { const auto child_layout_params = child->GetLayoutParams(); if (child_layout_params->width.mode == MeasureMode::Stretch) - throw std::runtime_error(Format("ScrollView: Horizontal scroll is enabled but a child {} 's width measure mode is Stretch which may cause infinite length.", ToUtf8String(child->GetControlType()))); + throw std::runtime_error(Format("ScrollControl: Horizontal scroll is enabled but a child {} 's width measure mode is Stretch which may cause infinite length.", ToUtf8String(child->GetControlType()))); } available_size_for_children.width = std::numeric_limits::max(); @@ -69,13 +122,13 @@ namespace cru::ui::controls if (IsVerticalScrollEnabled()) { if (layout_params->height.mode == MeasureMode::Content) - debug::DebugMessage(L"ScrollView: Height measure mode is Content and vertical scroll is enabled. So Stretch is used instead."); + debug::DebugMessage(L"ScrollControl: Height measure mode is Content and vertical scroll is enabled. So Stretch is used instead."); for (auto child : GetChildren()) { const auto child_layout_params = child->GetLayoutParams(); if (child_layout_params->height.mode == MeasureMode::Stretch) - throw std::runtime_error(Format("ScrollView: Vertical scroll is enabled but a child {} 's height measure mode is Stretch which may cause infinite length.", ToUtf8String(child->GetControlType()))); + throw std::runtime_error(Format("ScrollControl: Vertical scroll is enabled but a child {} 's height measure mode is Stretch which may cause infinite length.", ToUtf8String(child->GetControlType()))); } available_size_for_children.height = std::numeric_limits::max(); @@ -132,15 +185,193 @@ namespace cru::ui::controls { const auto size = control->GetDesiredSize(); // Ignore alignment, always center aligned. - auto&& calculate_anchor = [](const float anchor, const float layout_length, const float control_length) -> float + auto&& calculate_anchor = [](const float anchor, const float layout_length, const float control_length, const float offset) -> float { - return anchor + (layout_length - control_length) / 2; + return anchor + (layout_length - control_length) / 2 - offset; }; control->Layout(Rect(Point( - calculate_anchor(rect.left, layout_rect.width, size.width), - calculate_anchor(rect.top, layout_rect.height, size.height) + calculate_anchor(rect.left, layout_rect.width, size.width, offset_x_), + calculate_anchor(rect.top, layout_rect.height, size.height, offset_y_) ), size)); } } + + void ScrollControl::AfterLayoutSelf() + { + UpdateScrollBarBorderInfo(); + CoerceAndSetOffsets(offset_x_, offset_y_, false); + 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::CoerceAndSetOffsets(const float offset_x, const float offset_y, const bool update_children) + { + const auto old_offset_x = offset_x_; + const auto old_offset_y = offset_y_; + + const auto content_rect = GetRect(RectRange::Content); + offset_x_ = Coerce(offset_x, 0.0f, AtLeast0(view_width_ - content_rect.width)); + offset_y_ = Coerce(offset_y, 0.0f, AtLeast0(view_height_ - content_rect.height)); + UpdateScrollBarBarInfo(); + + if (update_children) + { + for (auto child : GetChildren()) + { + const auto old_position = child->GetPositionRelative(); + child->SetPositionRelative(Point( + old_position.x + old_offset_x - offset_x_, + old_position.y + old_offset_y - offset_y_ + )); + } + } + InvalidateDraw(); + } + + void ScrollControl::UpdateScrollBarVisibility() + { + const auto content_rect = GetRect(RectRange::Content); + if (GetHorizontalScrollBarVisibility() == ScrollBarVisibility::Auto) + is_horizontal_scroll_bar_visible_ = view_width_ > content_rect.width; + if (GetVerticalScrollBarVisibility() == ScrollBarVisibility::Auto) + is_vertical_scroll_bar_visible_ = view_height_ > content_rect.height; + } + + void ScrollControl::UpdateScrollBarBorderInfo() + { + const auto content_rect = GetRect(RectRange::Content); + horizontal_bar_info_.border = Rect(content_rect.left, content_rect.GetBottom() - scroll_bar_width, content_rect.width, scroll_bar_width); + vertical_bar_info_.border = Rect(content_rect.GetRight() - scroll_bar_width , content_rect.top, scroll_bar_width, content_rect.height); + } + + void ScrollControl::UpdateScrollBarBarInfo() + { + const auto content_rect = GetRect(RectRange::Content); + { + const auto& border = horizontal_bar_info_.border; + if (view_width_ <= content_rect.width) + horizontal_bar_info_.bar = border; + else + { + const auto bar_length = border.width * content_rect.width / view_width_; + const auto offset = border.width * offset_x_ / view_width_; + horizontal_bar_info_.bar = Rect(border.left + offset, border.top, bar_length, border.height); + } + } + { + const auto& border = vertical_bar_info_.border; + if (view_height_ <= content_rect.height) + vertical_bar_info_.bar = border; + else + { + const auto bar_length = border.height * content_rect.height / view_height_; + const auto offset = border.height * offset_y_ / view_height_; + vertical_bar_info_.bar = Rect(border.left, border.top + offset, border.width, bar_length); + } + } + } } diff --git a/src/ui/controls/scroll_control.hpp b/src/ui/controls/scroll_control.hpp index c39b18f4..faf192ad 100644 --- a/src/ui/controls/scroll_control.hpp +++ b/src/ui/controls/scroll_control.hpp @@ -1,17 +1,34 @@ #pragma once +#include +#include + #include "ui/control.hpp" namespace cru::ui::controls { // Done: OnMeasureContent // Done: OnLayoutContent - // TODO: HitTest - // TODO: Draw - // TODO: ScrollBar + // Done: HitTest(no need) + // Done: Draw(no need) + // Done: API + // Done: ScrollBar // TODO: MouseEvent class ScrollControl : public Control { + private: + struct ScrollBarInfo + { + Rect border = Rect(); + Rect bar = Rect(); + }; + + enum class Orientation + { + Horizontal, + Vertical + }; + public: enum class ScrollBarVisibility { @@ -20,6 +37,16 @@ namespace cru::ui::controls Always }; + static ScrollControl* Create(const std::initializer_list& children = std::initializer_list{}) + { + const auto control = new ScrollControl(true); + for (auto child : children) + control->AddChild(child); + return control; + } + + static constexpr auto control_type = L"ScrollControl"; + protected: explicit ScrollControl(bool container); public: @@ -29,6 +56,7 @@ namespace cru::ui::controls ScrollControl& operator=(ScrollControl&& other) = delete; ~ScrollControl() override; + StringView GetControlType() const override final; bool IsHorizontalScrollEnabled() const { @@ -45,14 +73,20 @@ namespace cru::ui::controls void SetVerticalScrollEnabled(bool enable); - ScrollBarVisibility GetHorizontalScrollBarVisibility() const; + ScrollBarVisibility GetHorizontalScrollBarVisibility() const + { + return horizontal_scroll_bar_visibility_; + } + void SetHorizontalScrollBarVisibility(ScrollBarVisibility visibility); - ScrollBarVisibility GetVerticalScrollBarVisibility() const; - void SetVerticalScrollBarVisibility(ScrollBarVisibility visibility); - Control* HitTest(const Point& point) override final; + ScrollBarVisibility GetVerticalScrollBarVisibility() const + { + return vertical_scroll_bar_visibility_; + } + + void SetVerticalScrollBarVisibility(ScrollBarVisibility visibility); - protected: float GetViewWidth() const { return view_width_; @@ -63,12 +97,40 @@ namespace cru::ui::controls return view_height_; } + float GetScrollOffsetX() const + { + return offset_x_; + } + + float GetScrollOffsetY() const + { + return offset_y_; + } + + // nullopt for not set. value is auto-coerced. + void SetScrollOffset(std::optional x, std::optional y); + + protected: void SetViewWidth(float length); void SetViewHeight(float length); Size OnMeasureContent(const Size& available_size) override final; void OnLayoutContent(const Rect& rect) override final; + void AfterLayoutSelf() override; + + void OnDrawForeground(ID2D1DeviceContext* device_context) override; + + void OnMouseDownCore(events::MouseButtonEventArgs& args) override final; + void OnMouseMoveCore(events::MouseEventArgs& args) override final; + void OnMouseUpCore(events::MouseButtonEventArgs& args) override final; + + private: + void CoerceAndSetOffsets(float offset_x, float offset_y, bool update_children = true); + void UpdateScrollBarVisibility(); + void UpdateScrollBarBorderInfo(); + void UpdateScrollBarBarInfo(); + private: bool horizontal_scroll_enabled_ = true; bool vertical_scroll_enabled_ = true; @@ -76,10 +138,19 @@ namespace cru::ui::controls ScrollBarVisibility horizontal_scroll_bar_visibility_ = ScrollBarVisibility::Auto; ScrollBarVisibility vertical_scroll_bar_visibility_ = ScrollBarVisibility::Auto; + bool is_horizontal_scroll_bar_visible_ = false; + bool is_vertical_scroll_bar_visible_ = false; + float offset_x_ = 0.0f; float offset_y_ = 0.0f; float view_width_ = 0.0f; float view_height_ = 0.0f; + + ScrollBarInfo horizontal_bar_info_; + ScrollBarInfo vertical_bar_info_; + + std::optional is_pressing_scroll_bar_ = std::nullopt; + float pressing_delta_ = 0.0f; }; } diff --git a/src/ui/ui_base.hpp b/src/ui/ui_base.hpp index d9c9d0b2..aaba343e 100644 --- a/src/ui/ui_base.hpp +++ b/src/ui/ui_base.hpp @@ -150,6 +150,16 @@ namespace cru::ui return Point(left + width, top + height); } + constexpr Point GetLeftBottom() const + { + return Point(left, top + height); + } + + constexpr Point GetRightTop() const + { + return Point(left + width, top); + } + constexpr Size GetSize() const { return Size(width, height); diff --git a/src/ui/ui_manager.cpp b/src/ui/ui_manager.cpp index 36fb2fb0..689a04a2 100644 --- a/src/ui/ui_manager.cpp +++ b/src/ui/ui_manager.cpp @@ -75,7 +75,11 @@ namespace cru::ui list_item_hover_border_brush {CreateSolidBrush(graph_manager, D2D1::ColorF(D2D1::ColorF::SkyBlue))}, list_item_hover_fill_brush {CreateSolidBrush(graph_manager, D2D1::ColorF(D2D1::ColorF::SkyBlue, 0.3f))}, list_item_select_border_brush {CreateSolidBrush(graph_manager, D2D1::ColorF(D2D1::ColorF::MediumBlue))}, - list_item_select_fill_brush {CreateSolidBrush(graph_manager, D2D1::ColorF(D2D1::ColorF::SkyBlue, 0.3f))} + list_item_select_fill_brush {CreateSolidBrush(graph_manager, D2D1::ColorF(D2D1::ColorF::SkyBlue, 0.3f))}, + + scroll_bar_background_brush {CreateSolidBrush(graph_manager, D2D1::ColorF(D2D1::ColorF::Gainsboro, 0.3f))}, + scroll_bar_border_brush {CreateSolidBrush(graph_manager, D2D1::ColorF(D2D1::ColorF::DimGray))}, + scroll_bar_brush {CreateSolidBrush(graph_manager, D2D1::ColorF(D2D1::ColorF::DimGray))} #ifdef CRU_DEBUG_LAYOUT , diff --git a/src/ui/ui_manager.hpp b/src/ui/ui_manager.hpp index 6b368e12..9ad68eff 100644 --- a/src/ui/ui_manager.hpp +++ b/src/ui/ui_manager.hpp @@ -61,6 +61,10 @@ namespace cru::ui Microsoft::WRL::ComPtr list_item_select_border_brush; Microsoft::WRL::ComPtr list_item_select_fill_brush; + //region ScrollControl + Microsoft::WRL::ComPtr scroll_bar_background_brush; + Microsoft::WRL::ComPtr scroll_bar_border_brush; + Microsoft::WRL::ComPtr scroll_bar_brush; #ifdef CRU_DEBUG_LAYOUT //region debug -- cgit v1.2.3 From 7ccb08ac09a83e81a822712b712dc0473c9b23cf Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 27 Nov 2018 17:53:21 +0800 Subject: Add mouse wheel support. --- src/ui/control.cpp | 17 +++++++++++++++++ src/ui/control.hpp | 7 +++++++ src/ui/controls/scroll_control.cpp | 23 +++++++++++++++++++++++ src/ui/controls/scroll_control.hpp | 4 +++- src/ui/events/ui_event.hpp | 25 +++++++++++++++++++++++++ src/ui/window.cpp | 22 ++++++++++++++++++++++ src/ui/window.hpp | 3 ++- 7 files changed, 99 insertions(+), 2 deletions(-) (limited to 'src/ui/controls/scroll_control.cpp') 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; using MouseEvent = Event; using MouseButtonEvent = Event; + using MouseWheelEvent = Event; using DrawEvent = Event; using PositionChangedEvent = Event; using SizeChangedEvent = Event; 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(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(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); -- cgit v1.2.3