diff options
author | crupest <crupest@outlook.com> | 2020-03-18 22:37:41 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2020-03-18 22:37:41 +0800 |
commit | 477155d6fccc8eafadb6d7f4c468c9141d7d4e92 (patch) | |
tree | 0fe935c8f699a1c42c8750a39b6ca70d31f941a0 /include/cru/ui | |
parent | 068714c0f2fe7ab003462e5483f9944b0bf2f8e0 (diff) | |
download | cru-477155d6fccc8eafadb6d7f4c468c9141d7d4e92.tar.gz cru-477155d6fccc8eafadb6d7f4c468c9141d7d4e92.tar.bz2 cru-477155d6fccc8eafadb6d7f4c468c9141d7d4e92.zip |
...
Diffstat (limited to 'include/cru/ui')
22 files changed, 110 insertions, 218 deletions
diff --git a/include/cru/ui/base.hpp b/include/cru/ui/base.hpp index 703d61fc..fe53edd4 100644 --- a/include/cru/ui/base.hpp +++ b/include/cru/ui/base.hpp @@ -1,10 +1,15 @@ #pragma once #include "cru/common/base.hpp" -#include "cru/platform/graph_base.hpp" -#include "cru/platform/matrix.hpp" -#include "cru/platform/native/basic_types.hpp" +#include "cru/platform/graph/base.hpp" +#include "cru/platform/native/base.hpp" + +#include <functional> +#include <memory> +#include <optional> +#include <vector> namespace cru::ui { +//-------------------- region: import -------------------- using cru::platform::Color; using cru::platform::Ellipse; using cru::platform::Matrix; @@ -28,6 +33,12 @@ using cru::platform::colors::skyblue; using cru::platform::colors::white; } // namespace colors +//-------------------- region: forward declaration -------------------- +class Window; +class Control; +class ClickDetector; + +//-------------------- region: basic types -------------------- namespace internal { constexpr int align_start = 0; constexpr int align_end = align_start + 1; @@ -41,4 +52,77 @@ enum class Alignment { Center = internal::align_center, Stretch = internal::align_stretch }; + +struct CornerRadius { + constexpr CornerRadius() + : left_top(), right_top(), left_bottom(), right_bottom() {} + constexpr CornerRadius(const Point& value) + : left_top(value), + right_top(value), + left_bottom(value), + right_bottom(value) {} + constexpr CornerRadius(Point left_top, Point right_top, Point left_bottom, + Point right_bottom) + : left_top(left_top), + right_top(right_top), + left_bottom(left_bottom), + right_bottom(right_bottom) {} + + Point left_top; + Point right_top; + Point left_bottom; + Point right_bottom; +}; + +inline bool operator==(const CornerRadius& left, const CornerRadius& right) { + return left.left_top == right.left_top && + left.left_bottom == right.left_bottom && + left.right_top == right.right_top && + left.right_bottom == right.right_bottom; +} + +inline bool operator!=(const CornerRadius& left, const CornerRadius& right) { + return !(left == right); +} + +class CanvasPaintEventArgs { + public: + CanvasPaintEventArgs(platform::graph::IPainter* painter, + const Rect& paint_rect) + : painter_(painter), paint_rect_(paint_rect) {} + CRU_DEFAULT_COPY(CanvasPaintEventArgs) + CRU_DEFAULT_MOVE(CanvasPaintEventArgs) + ~CanvasPaintEventArgs() = default; + + platform::graph::IPainter* GetPainter() const { return painter_; } + Rect GetPaintRect() const { return paint_rect_; } + + private: + platform::graph::IPainter* painter_; + Rect paint_rect_; +}; + +enum class FlexDirection { + Horizontal, + HorizontalReverse, + Vertical, + VertivalReverse +}; + +using FlexMainAlignment = Alignment; +using FlexCrossAlignment = Alignment; + +struct FlexChildLayoutData { + // nullopt stands for looking at my content + std::optional<float> flex_basis = std::nullopt; + float flex_grow = 0; + float flex_shrink = 0; + // nullopt stands for looking at parent's setting + std::optional<FlexCrossAlignment> cross_alignment = std::nullopt; +}; + +struct StackChildLayoutData { + Alignment horizontal = Alignment::Start; + Alignment vertical = Alignment::Start; +}; } // namespace cru::ui diff --git a/include/cru/ui/click_detector.hpp b/include/cru/ui/click_detector.hpp index 9b59c717..6c4761e7 100644 --- a/include/cru/ui/click_detector.hpp +++ b/include/cru/ui/click_detector.hpp @@ -1,8 +1,6 @@ #pragma once #include "control.hpp" -#include <optional> -#include <vector> namespace cru::ui { class ClickEventArgs : Object { diff --git a/include/cru/ui/control.hpp b/include/cru/ui/control.hpp index 1fe49261..95e2cf52 100644 --- a/include/cru/ui/control.hpp +++ b/include/cru/ui/control.hpp @@ -2,23 +2,12 @@ #include "base.hpp" #include "cru/common/event.hpp" +#include "render/base.hpp" #include "ui_event.hpp" -#include <functional> -#include <memory> #include <string_view> -#include <vector> - -namespace cru::platform::native { -struct ICursor; -} namespace cru::ui { -class Window; -namespace render { -class RenderObject; -} // namespace render - class Control : public Object { friend class Window; diff --git a/include/cru/ui/controls/button.hpp b/include/cru/ui/controls/button.hpp index b24a9934..a95b75ce 100644 --- a/include/cru/ui/controls/button.hpp +++ b/include/cru/ui/controls/button.hpp @@ -2,14 +2,8 @@ #include "../content_control.hpp" #include "../click_detector.hpp" -#include "../render/border_render_object.hpp" -#include "cru/platform/native/basic_types.hpp" - -#include <memory> namespace cru::ui::controls { -using render::CornerRadius; - struct ButtonStateStyle { std::shared_ptr<platform::graph::IBrush> border_brush; Thickness border_thickness; @@ -43,7 +37,7 @@ class Button : public ContentControl { Button(Button&& other) = delete; Button& operator=(const Button& other) = delete; Button& operator=(Button&& other) = delete; - ~Button() override = default; + ~Button() override; std::string_view GetControlType() const final { return control_type; } diff --git a/include/cru/ui/controls/container.hpp b/include/cru/ui/controls/container.hpp index efc099f7..d6aa5635 100644 --- a/include/cru/ui/controls/container.hpp +++ b/include/cru/ui/controls/container.hpp @@ -1,10 +1,6 @@ #pragma once #include "../content_control.hpp" -namespace cru::ui::render { -class BorderRenderObject; -} - namespace cru::ui::controls { class Container : public ContentControl { static constexpr std::string_view control_type = "Container"; @@ -19,9 +15,7 @@ class Container : public ContentControl { ~Container() override; public: - std::string_view GetControlType() const final { - return control_type; - } + std::string_view GetControlType() const final { return control_type; } render::RenderObject* GetRenderObject() const override; diff --git a/include/cru/ui/controls/flex_layout.hpp b/include/cru/ui/controls/flex_layout.hpp index 7861534a..52eaec75 100644 --- a/include/cru/ui/controls/flex_layout.hpp +++ b/include/cru/ui/controls/flex_layout.hpp @@ -1,17 +1,7 @@ #pragma once #include "../layout_control.hpp" -#include "../render/flex_layout_render_object.hpp" - -#include <memory> - namespace cru::ui::controls { -// import these basic entities -using render::FlexChildLayoutData; -using render::FlexCrossAlignment; -using render::FlexDirection; -using render::FlexMainAlignment; - class FlexLayout : public LayoutControl { public: static constexpr std::string_view control_type = "FlexLayout"; @@ -26,29 +16,17 @@ class FlexLayout : public LayoutControl { FlexLayout(FlexLayout&& other) = delete; FlexLayout& operator=(const FlexLayout& other) = delete; FlexLayout& operator=(FlexLayout&& other) = delete; - ~FlexLayout() override = default; + ~FlexLayout() override; std::string_view GetControlType() const final { return control_type; } render::RenderObject* GetRenderObject() const override; - FlexMainAlignment GetContentMainAlign() const { - return render_object_->GetContentMainAlign(); - } - - void SetContentMainAlign(FlexMainAlignment value) { - if (value == GetContentMainAlign()) return; - render_object_->SetContentMainAlign(value); - } - - FlexDirection GetFlexDirection() const { - return render_object_->GetFlexDirection(); - } + FlexMainAlignment GetContentMainAlign() const; + void SetContentMainAlign(FlexMainAlignment value); - void SetFlexDirection(FlexDirection direction) { - if (direction == GetFlexDirection()) return; - render_object_->SetFlexDirection(direction); - } + FlexDirection GetFlexDirection() const; + void SetFlexDirection(FlexDirection direction); FlexChildLayoutData GetChildLayoutData(Control* control); void SetChildLayoutData(Control* control, const FlexChildLayoutData& data); diff --git a/include/cru/ui/controls/stack_layout.hpp b/include/cru/ui/controls/stack_layout.hpp index 298de089..93861c19 100644 --- a/include/cru/ui/controls/stack_layout.hpp +++ b/include/cru/ui/controls/stack_layout.hpp @@ -1,12 +1,6 @@ #pragma once #include "../layout_control.hpp" -#include <memory> - -namespace cru::ui::render { -class StackLayoutRenderObject; -} - namespace cru::ui::controls { class StackLayout : public LayoutControl { public: diff --git a/include/cru/ui/controls/text_block.hpp b/include/cru/ui/controls/text_block.hpp index 02e0eb2b..db0fb4e5 100644 --- a/include/cru/ui/controls/text_block.hpp +++ b/include/cru/ui/controls/text_block.hpp @@ -3,14 +3,6 @@ #include "text_common.hpp" -#include <memory> - -namespace cru::ui::render { -class StackLayoutRenderObject; -class TextRenderObject; -class CanvasRenderObject; -} // namespace cru::ui::render - namespace cru::ui::controls { class TextBlock : public NoChildControl, public virtual ITextControl { public: diff --git a/include/cru/ui/controls/text_box.hpp b/include/cru/ui/controls/text_box.hpp index 888a9527..a7e4dfa0 100644 --- a/include/cru/ui/controls/text_box.hpp +++ b/include/cru/ui/controls/text_box.hpp @@ -1,15 +1,6 @@ #pragma once #include "../no_child_control.hpp" -#include <memory> - -namespace cru::ui::render { -class BorderRenderObject; -class StackLayoutRenderObject; -class TextRenderObject; -class CanvasRenderObject; -} // namespace cru::ui::render - namespace cru::ui::controls { class TextBox : public NoChildControl { public: diff --git a/include/cru/ui/controls/text_common.hpp b/include/cru/ui/controls/text_common.hpp index fbef6b06..64a9666c 100644 --- a/include/cru/ui/controls/text_common.hpp +++ b/include/cru/ui/controls/text_common.hpp @@ -3,9 +3,6 @@ #include "cru/ui/ui_event.hpp" -#include <functional> -#include <optional> - namespace cru::platform::graph { struct IBrush; } diff --git a/include/cru/ui/render/base.hpp b/include/cru/ui/render/base.hpp new file mode 100644 index 00000000..f9d936e0 --- /dev/null +++ b/include/cru/ui/render/base.hpp @@ -0,0 +1,12 @@ +#pragma once +#include "../base.hpp" + +namespace cru::ui::render { +class RenderObject; +class BorderRenderObject; +class CanvasRenderObject; +class FlexLayoutRenderObject; +class StackLayoutRenderObject; +class TextRenderObject; +class WindowRenderObject; +} // namespace cru::ui::render diff --git a/include/cru/ui/render/border_render_object.hpp b/include/cru/ui/render/border_render_object.hpp index b9af71c0..259c1530 100644 --- a/include/cru/ui/render/border_render_object.hpp +++ b/include/cru/ui/render/border_render_object.hpp @@ -1,41 +1,7 @@ #pragma once #include "render_object.hpp" -#include <memory> - namespace cru::ui::render { -struct CornerRadius { - constexpr CornerRadius() - : left_top(), right_top(), left_bottom(), right_bottom() {} - constexpr CornerRadius(const Point& value) - : left_top(value), - right_top(value), - left_bottom(value), - right_bottom(value) {} - constexpr CornerRadius(Point left_top, Point right_top, Point left_bottom, - Point right_bottom) - : left_top(left_top), - right_top(right_top), - left_bottom(left_bottom), - right_bottom(right_bottom) {} - - Point left_top; - Point right_top; - Point left_bottom; - Point right_bottom; -}; - -inline bool operator==(const CornerRadius& left, const CornerRadius& right) { - return left.left_top == right.left_top && - left.left_bottom == right.left_bottom && - left.right_top == right.right_top && - left.right_bottom == right.right_bottom; -} - -inline bool operator!=(const CornerRadius& left, const CornerRadius& right) { - return !(left == right); -} - class BorderRenderObject : public RenderObject { public: BorderRenderObject(); diff --git a/include/cru/ui/render/canvas_render_object.hpp b/include/cru/ui/render/canvas_render_object.hpp index e3388014..cb3828b6 100644 --- a/include/cru/ui/render/canvas_render_object.hpp +++ b/include/cru/ui/render/canvas_render_object.hpp @@ -1,26 +1,7 @@ #pragma once #include "render_object.hpp" -#include "cru/common/event.hpp" - namespace cru::ui::render { -class CanvasPaintEventArgs { - public: - CanvasPaintEventArgs(platform::graph::IPainter* painter, - const Rect& paint_rect) - : painter_(painter), paint_rect_(paint_rect) {} - CRU_DEFAULT_COPY(CanvasPaintEventArgs) - CRU_DEFAULT_MOVE(CanvasPaintEventArgs) - ~CanvasPaintEventArgs() = default; - - platform::graph::IPainter* GetPainter() const { return painter_; } - Rect GetPaintRect() const { return paint_rect_; } - - private: - platform::graph::IPainter* painter_; - Rect paint_rect_; -}; - // The measure logic for `CanvasRenderObject` is that you set a desired size by // `SetDesiredSize` (not `SetPreferredSize`) and it will compare desired size // and available size and use the smaller one (by `Min`). diff --git a/include/cru/ui/render/flex_layout_render_object.hpp b/include/cru/ui/render/flex_layout_render_object.hpp index c2bc5fd1..849c1a0d 100644 --- a/include/cru/ui/render/flex_layout_render_object.hpp +++ b/include/cru/ui/render/flex_layout_render_object.hpp @@ -1,44 +1,7 @@ #pragma once #include "layout_render_object.hpp" -#include <optional> - namespace cru::ui::render { -enum class FlexDirection { - Horizontal, - HorizontalReverse, - Vertical, - VertivalReverse -}; - -namespace internal { -constexpr int align_start = 0; -constexpr int align_end = align_start + 1; -constexpr int align_center = align_end + 1; -// constexpr int align_stretch = align_center + 1; -} // namespace internal - -enum class FlexMainAlignment { - Start = internal::align_start, - End = internal::align_end, - Center = internal::align_center -}; -enum class FlexCrossAlignment { - Start = internal::align_start, - End = internal::align_end, - Center = internal::align_center, - // Stretch = internal::align_stretch -}; - -struct FlexChildLayoutData { - // nullopt stands for looking at my content - std::optional<float> flex_basis = std::nullopt; - float flex_grow = 0; - float flex_shrink = 0; - // nullopt stands for looking at parent's setting - std::optional<FlexCrossAlignment> cross_alignment = std::nullopt; -}; - class FlexLayoutRenderObject : public LayoutRenderObject<FlexChildLayoutData> { public: FlexLayoutRenderObject() = default; diff --git a/include/cru/ui/render/layout_render_object.hpp b/include/cru/ui/render/layout_render_object.hpp index db6daba9..0b60a647 100644 --- a/include/cru/ui/render/layout_render_object.hpp +++ b/include/cru/ui/render/layout_render_object.hpp @@ -4,7 +4,6 @@ #include "cru/platform/graph/util/painter.hpp" #include <cassert> -#include <functional> namespace cru::ui::render { template <typename TChildLayoutData> diff --git a/include/cru/ui/render/layout_utility.hpp b/include/cru/ui/render/layout_utility.hpp index 5cf74e71..16a15d87 100644 --- a/include/cru/ui/render/layout_utility.hpp +++ b/include/cru/ui/render/layout_utility.hpp @@ -1,5 +1,5 @@ #pragma once -#include "../base.hpp" +#include "base.hpp" namespace cru::ui::render { Size Min(const Size& left, const Size& right); diff --git a/include/cru/ui/render/render_object.hpp b/include/cru/ui/render/render_object.hpp index 8db1a20f..ca31386a 100644 --- a/include/cru/ui/render/render_object.hpp +++ b/include/cru/ui/render/render_object.hpp @@ -1,15 +1,7 @@ #pragma once -#include "../base.hpp" +#include "base.hpp" #include "cru/common/event.hpp" -#include "cru/platform/graph/base.hpp" - -#include <vector> - -// forward declarations -namespace cru::ui { -class Control; -} namespace cru::ui::render { diff --git a/include/cru/ui/render/stack_layout_render_object.hpp b/include/cru/ui/render/stack_layout_render_object.hpp index 0d33e7e3..c259b98d 100644 --- a/include/cru/ui/render/stack_layout_render_object.hpp +++ b/include/cru/ui/render/stack_layout_render_object.hpp @@ -2,11 +2,6 @@ #include "layout_render_object.hpp" namespace cru::ui::render { -struct StackChildLayoutData { - Alignment horizontal = Alignment::Start; - Alignment vertical = Alignment::Start; -}; - class StackLayoutRenderObject : public LayoutRenderObject<StackChildLayoutData> { public: diff --git a/include/cru/ui/render/text_render_object.hpp b/include/cru/ui/render/text_render_object.hpp index 62313cd3..1c472753 100644 --- a/include/cru/ui/render/text_render_object.hpp +++ b/include/cru/ui/render/text_render_object.hpp @@ -1,7 +1,6 @@ #pragma once #include "render_object.hpp" -#include <memory> #include <string> namespace cru::ui::render { diff --git a/include/cru/ui/render/window_render_object.hpp b/include/cru/ui/render/window_render_object.hpp index ae95b8ee..2f7f1e84 100644 --- a/include/cru/ui/render/window_render_object.hpp +++ b/include/cru/ui/render/window_render_object.hpp @@ -1,12 +1,6 @@ #pragma once #include "render_object.hpp" -#include <memory> - -namespace cru::ui { -class Window; -} - namespace cru::ui::render { class WindowRenderObject : public RenderObject { public: diff --git a/include/cru/ui/ui_manager.hpp b/include/cru/ui/ui_manager.hpp index 8e5c7075..b2137c67 100644 --- a/include/cru/ui/ui_manager.hpp +++ b/include/cru/ui/ui_manager.hpp @@ -3,13 +3,6 @@ #include "controls/button.hpp" -#include <memory> - -namespace cru::platform::graph { -struct IBrush; -struct IFont; -} // namespace cru::platform::graph - namespace cru::ui { struct ThemeResources { std::shared_ptr<platform::graph::IFont> default_font; @@ -31,7 +24,7 @@ class UiManager : public Object { UiManager(UiManager&& other) = delete; UiManager& operator=(const UiManager& other) = delete; UiManager& operator=(UiManager&& other) = delete; - ~UiManager() override = default; + ~UiManager() override; ThemeResources* GetThemeResources() { return &theme_resource_; } diff --git a/include/cru/ui/window.hpp b/include/cru/ui/window.hpp index 9f37c9d5..251712cf 100644 --- a/include/cru/ui/window.hpp +++ b/include/cru/ui/window.hpp @@ -2,21 +2,8 @@ #include "content_control.hpp" #include "cru/common/self_resolvable.hpp" -#include "cru/platform/native/event.hpp" - -#include <memory> -#include <vector> - -namespace cru::platform::native { -struct INativeWindow; -struct INativeWindowResolver; -} // namespace cru::platform::native namespace cru::ui { -namespace render { -class WindowRenderObject; -} - // TODO: Make Window able to be invalid and handle operations in invalidity // situation. class Window final : public ContentControl, public SelfResolvable<Window> { |