From d86a71f79afe0e4dac768f61d6bff690567aca5b Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 24 May 2020 01:40:02 +0800 Subject: ... --- include/cru/common/PreConfig.hpp | 8 + include/cru/common/SelfResolvable.hpp | 51 +++ include/cru/common/base.hpp | 2 +- include/cru/common/bitmask.hpp | 2 +- include/cru/common/event.hpp | 4 +- include/cru/common/logger.hpp | 2 +- include/cru/common/pre_config.hpp | 8 - include/cru/common/self_resolvable.hpp | 51 --- include/cru/platform/GraphBase.hpp | 419 +++++++++++++++++++++ include/cru/platform/HeapDebug.hpp | 7 + include/cru/platform/check.hpp | 4 +- include/cru/platform/exception.hpp | 2 +- include/cru/platform/graph/TextLayout.hpp | 23 ++ include/cru/platform/graph/base.hpp | 6 +- include/cru/platform/graph/brush.hpp | 2 +- include/cru/platform/graph/factory.hpp | 10 +- include/cru/platform/graph/font.hpp | 2 +- include/cru/platform/graph/geometry.hpp | 2 +- include/cru/platform/graph/painter.hpp | 2 +- include/cru/platform/graph/resource.hpp | 2 +- include/cru/platform/graph/text_layout.hpp | 23 -- include/cru/platform/graph/util/painter.hpp | 2 +- include/cru/platform/graph_base.hpp | 419 --------------------- include/cru/platform/heap_debug.hpp | 7 - include/cru/platform/matrix.hpp | 2 +- include/cru/platform/native/InputMethod.hpp | 77 ++++ include/cru/platform/native/UiApplication.hpp | 58 +++ include/cru/platform/native/base.hpp | 8 +- include/cru/platform/native/cursor.hpp | 4 +- include/cru/platform/native/input_method.hpp | 77 ---- include/cru/platform/native/keyboard.hpp | 2 +- include/cru/platform/native/ui_application.hpp | 58 --- include/cru/platform/native/window.hpp | 6 +- include/cru/platform/resource.hpp | 2 +- include/cru/ui/ClickDetector.hpp | 86 +++++ include/cru/ui/ContentControl.hpp | 29 ++ include/cru/ui/LayoutControl.hpp | 31 ++ include/cru/ui/NoChildControl.hpp | 24 ++ include/cru/ui/UiEvent.hpp | 229 +++++++++++ include/cru/ui/UiHost.hpp | 170 +++++++++ include/cru/ui/UiManager.hpp | 35 ++ include/cru/ui/base.hpp | 6 +- include/cru/ui/click_detector.hpp | 86 ----- include/cru/ui/content_control.hpp | 29 -- include/cru/ui/control.hpp | 8 +- include/cru/ui/controls/FlexLayout.hpp | 41 ++ include/cru/ui/controls/StackLayout.hpp | 31 ++ include/cru/ui/controls/TextBlock.hpp | 38 ++ include/cru/ui/controls/TextBox.hpp | 49 +++ include/cru/ui/controls/base.hpp | 2 +- include/cru/ui/controls/button.hpp | 6 +- include/cru/ui/controls/container.hpp | 2 +- include/cru/ui/controls/flex_layout.hpp | 41 -- include/cru/ui/controls/stack_layout.hpp | 31 -- include/cru/ui/controls/text_block.hpp | 38 -- include/cru/ui/controls/text_box.hpp | 49 --- include/cru/ui/layout_control.hpp | 31 -- include/cru/ui/no_child_control.hpp | 24 -- include/cru/ui/render/BorderRenderObject.hpp | 102 +++++ include/cru/ui/render/CanvasRenderObject.hpp | 39 ++ include/cru/ui/render/FlexLayoutRenderObject.hpp | 42 +++ include/cru/ui/render/LayoutRenderObject.hpp | 88 +++++ include/cru/ui/render/LayoutUtility.hpp | 7 + include/cru/ui/render/RenderObject.hpp | 118 ++++++ include/cru/ui/render/ScrollRenderObject.hpp | 30 ++ include/cru/ui/render/StackLayoutRenderObject.hpp | 17 + include/cru/ui/render/TextRenderObject.hpp | 85 +++++ include/cru/ui/render/WindowRenderObject.hpp | 30 ++ include/cru/ui/render/base.hpp | 2 +- include/cru/ui/render/border_render_object.hpp | 102 ----- include/cru/ui/render/canvas_render_object.hpp | 39 -- .../cru/ui/render/flex_layout_render_object.hpp | 42 --- include/cru/ui/render/layout_render_object.hpp | 88 ----- include/cru/ui/render/layout_utility.hpp | 7 - include/cru/ui/render/render_object.hpp | 118 ------ include/cru/ui/render/scroll_render_object.hpp | 30 -- .../cru/ui/render/stack_layout_render_object.hpp | 17 - include/cru/ui/render/text_render_object.hpp | 85 ----- include/cru/ui/render/window_render_object.hpp | 30 -- include/cru/ui/ui_event.hpp | 229 ----------- include/cru/ui/ui_host.hpp | 170 --------- include/cru/ui/ui_manager.hpp | 35 -- include/cru/ui/window.hpp | 2 +- include/cru/win/WinPreConfig.hpp | 16 + include/cru/win/exception.hpp | 4 +- include/cru/win/graph/direct/ComResource.hpp | 11 + include/cru/win/graph/direct/ConvertUtil.hpp | 107 ++++++ include/cru/win/graph/direct/TextLayout.hpp | 54 +++ include/cru/win/graph/direct/brush.hpp | 6 +- include/cru/win/graph/direct/com_resource.hpp | 11 - include/cru/win/graph/direct/convert_util.hpp | 107 ------ include/cru/win/graph/direct/exception.hpp | 2 +- include/cru/win/graph/direct/factory.hpp | 4 +- include/cru/win/graph/direct/font.hpp | 6 +- include/cru/win/graph/direct/geometry.hpp | 6 +- include/cru/win/graph/direct/painter.hpp | 6 +- include/cru/win/graph/direct/resource.hpp | 4 +- include/cru/win/graph/direct/text_layout.hpp | 54 --- include/cru/win/native/GodWindow.hpp | 27 ++ include/cru/win/native/InputMethod.hpp | 98 +++++ include/cru/win/native/UiApplication.hpp | 76 ++++ include/cru/win/native/WindowClass.hpp | 24 ++ .../win/native/WindowNativeMessageEventArgs.hpp | 40 ++ include/cru/win/native/WindowRenderTarget.hpp | 47 +++ include/cru/win/native/base.hpp | 4 +- include/cru/win/native/cursor.hpp | 4 +- include/cru/win/native/exception.hpp | 2 +- include/cru/win/native/god_window.hpp | 27 -- include/cru/win/native/input_method.hpp | 98 ----- include/cru/win/native/keyboard.hpp | 4 +- include/cru/win/native/resource.hpp | 4 +- include/cru/win/native/ui_application.hpp | 76 ---- include/cru/win/native/window.hpp | 6 +- include/cru/win/native/window_class.hpp | 24 -- .../native/window_native_message_event_args.hpp | 40 -- include/cru/win/native/window_render_target.hpp | 47 --- include/cru/win/string.hpp | 4 +- include/cru/win/win_pre_config.hpp | 16 - 118 files changed, 2545 insertions(+), 2545 deletions(-) create mode 100644 include/cru/common/PreConfig.hpp create mode 100644 include/cru/common/SelfResolvable.hpp delete mode 100644 include/cru/common/pre_config.hpp delete mode 100644 include/cru/common/self_resolvable.hpp create mode 100644 include/cru/platform/GraphBase.hpp create mode 100644 include/cru/platform/HeapDebug.hpp create mode 100644 include/cru/platform/graph/TextLayout.hpp delete mode 100644 include/cru/platform/graph/text_layout.hpp delete mode 100644 include/cru/platform/graph_base.hpp delete mode 100644 include/cru/platform/heap_debug.hpp create mode 100644 include/cru/platform/native/InputMethod.hpp create mode 100644 include/cru/platform/native/UiApplication.hpp delete mode 100644 include/cru/platform/native/input_method.hpp delete mode 100644 include/cru/platform/native/ui_application.hpp create mode 100644 include/cru/ui/ClickDetector.hpp create mode 100644 include/cru/ui/ContentControl.hpp create mode 100644 include/cru/ui/LayoutControl.hpp create mode 100644 include/cru/ui/NoChildControl.hpp create mode 100644 include/cru/ui/UiEvent.hpp create mode 100644 include/cru/ui/UiHost.hpp create mode 100644 include/cru/ui/UiManager.hpp delete mode 100644 include/cru/ui/click_detector.hpp delete mode 100644 include/cru/ui/content_control.hpp create mode 100644 include/cru/ui/controls/FlexLayout.hpp create mode 100644 include/cru/ui/controls/StackLayout.hpp create mode 100644 include/cru/ui/controls/TextBlock.hpp create mode 100644 include/cru/ui/controls/TextBox.hpp delete mode 100644 include/cru/ui/controls/flex_layout.hpp delete mode 100644 include/cru/ui/controls/stack_layout.hpp delete mode 100644 include/cru/ui/controls/text_block.hpp delete mode 100644 include/cru/ui/controls/text_box.hpp delete mode 100644 include/cru/ui/layout_control.hpp delete mode 100644 include/cru/ui/no_child_control.hpp create mode 100644 include/cru/ui/render/BorderRenderObject.hpp create mode 100644 include/cru/ui/render/CanvasRenderObject.hpp create mode 100644 include/cru/ui/render/FlexLayoutRenderObject.hpp create mode 100644 include/cru/ui/render/LayoutRenderObject.hpp create mode 100644 include/cru/ui/render/LayoutUtility.hpp create mode 100644 include/cru/ui/render/RenderObject.hpp create mode 100644 include/cru/ui/render/ScrollRenderObject.hpp create mode 100644 include/cru/ui/render/StackLayoutRenderObject.hpp create mode 100644 include/cru/ui/render/TextRenderObject.hpp create mode 100644 include/cru/ui/render/WindowRenderObject.hpp delete mode 100644 include/cru/ui/render/border_render_object.hpp delete mode 100644 include/cru/ui/render/canvas_render_object.hpp delete mode 100644 include/cru/ui/render/flex_layout_render_object.hpp delete mode 100644 include/cru/ui/render/layout_render_object.hpp delete mode 100644 include/cru/ui/render/layout_utility.hpp delete mode 100644 include/cru/ui/render/render_object.hpp delete mode 100644 include/cru/ui/render/scroll_render_object.hpp delete mode 100644 include/cru/ui/render/stack_layout_render_object.hpp delete mode 100644 include/cru/ui/render/text_render_object.hpp delete mode 100644 include/cru/ui/render/window_render_object.hpp delete mode 100644 include/cru/ui/ui_event.hpp delete mode 100644 include/cru/ui/ui_host.hpp delete mode 100644 include/cru/ui/ui_manager.hpp create mode 100644 include/cru/win/WinPreConfig.hpp create mode 100644 include/cru/win/graph/direct/ComResource.hpp create mode 100644 include/cru/win/graph/direct/ConvertUtil.hpp create mode 100644 include/cru/win/graph/direct/TextLayout.hpp delete mode 100644 include/cru/win/graph/direct/com_resource.hpp delete mode 100644 include/cru/win/graph/direct/convert_util.hpp delete mode 100644 include/cru/win/graph/direct/text_layout.hpp create mode 100644 include/cru/win/native/GodWindow.hpp create mode 100644 include/cru/win/native/InputMethod.hpp create mode 100644 include/cru/win/native/UiApplication.hpp create mode 100644 include/cru/win/native/WindowClass.hpp create mode 100644 include/cru/win/native/WindowNativeMessageEventArgs.hpp create mode 100644 include/cru/win/native/WindowRenderTarget.hpp delete mode 100644 include/cru/win/native/god_window.hpp delete mode 100644 include/cru/win/native/input_method.hpp delete mode 100644 include/cru/win/native/ui_application.hpp delete mode 100644 include/cru/win/native/window_class.hpp delete mode 100644 include/cru/win/native/window_native_message_event_args.hpp delete mode 100644 include/cru/win/native/window_render_target.hpp delete mode 100644 include/cru/win/win_pre_config.hpp (limited to 'include') diff --git a/include/cru/common/PreConfig.hpp b/include/cru/common/PreConfig.hpp new file mode 100644 index 00000000..4bccef1d --- /dev/null +++ b/include/cru/common/PreConfig.hpp @@ -0,0 +1,8 @@ +#pragma once + +#ifdef _MSC_VER +// disable the unnecessary warning about multi-inheritance +#pragma warning(disable : 4250) +#endif + +#define _CRT_SECURE_NO_WARNINGS diff --git a/include/cru/common/SelfResolvable.hpp b/include/cru/common/SelfResolvable.hpp new file mode 100644 index 00000000..94f3ae87 --- /dev/null +++ b/include/cru/common/SelfResolvable.hpp @@ -0,0 +1,51 @@ +#pragma once +#include "PreConfig.hpp" + +#include +#include + +namespace cru { +template +class SelfResolvable; + +template +class ObjectResolver { + friend SelfResolvable; + + private: + ObjectResolver(const std::shared_ptr& resolver) : resolver_(resolver) {} + + public: + ObjectResolver(const ObjectResolver&) = default; + ObjectResolver& operator=(const ObjectResolver&) = default; + ObjectResolver(ObjectResolver&&) = default; + ObjectResolver& operator=(ObjectResolver&&) = default; + ~ObjectResolver() = default; + + T* Resolve() const { + // resolver_ is null only when this has been moved. + // You shouldn't resolve a moved resolver. So assert it. + Expects(resolver_); + return *resolver_; + } + + private: + std::shared_ptr resolver_; +}; + +template +class SelfResolvable { + public: + SelfResolvable() : resolver_(new T*(static_cast(this))) {} + SelfResolvable(const SelfResolvable&) = delete; + SelfResolvable& operator=(const SelfResolvable&) = delete; + SelfResolvable(SelfResolvable&&) = delete; + SelfResolvable& operator=(SelfResolvable&&) = delete; + virtual ~SelfResolvable() { (*resolver_) = nullptr; } + + ObjectResolver CreateResolver() { return ObjectResolver(resolver_); } + + private: + std::shared_ptr resolver_; +}; +} // namespace cru diff --git a/include/cru/common/base.hpp b/include/cru/common/base.hpp index 20dd811d..ff7ab31e 100644 --- a/include/cru/common/base.hpp +++ b/include/cru/common/base.hpp @@ -1,5 +1,5 @@ #pragma once -#include "pre_config.hpp" +#include "PreConfig.hpp" #include diff --git a/include/cru/common/bitmask.hpp b/include/cru/common/bitmask.hpp index 95edee13..ddfdc86b 100644 --- a/include/cru/common/bitmask.hpp +++ b/include/cru/common/bitmask.hpp @@ -1,5 +1,5 @@ #pragma once -#include "base.hpp" +#include "Base.hpp" namespace cru { template diff --git a/include/cru/common/event.hpp b/include/cru/common/event.hpp index 8389b38e..377ca7f3 100644 --- a/include/cru/common/event.hpp +++ b/include/cru/common/event.hpp @@ -1,7 +1,7 @@ #pragma once -#include "base.hpp" +#include "Base.hpp" -#include "self_resolvable.hpp" +#include "SelfResolvable.hpp" #include #include diff --git a/include/cru/common/logger.hpp b/include/cru/common/logger.hpp index ab3f2250..bd16678b 100644 --- a/include/cru/common/logger.hpp +++ b/include/cru/common/logger.hpp @@ -1,5 +1,5 @@ #pragma once -#include "cru/common/base.hpp" +#include "cru/common/Base.hpp" #include #include diff --git a/include/cru/common/pre_config.hpp b/include/cru/common/pre_config.hpp deleted file mode 100644 index 4bccef1d..00000000 --- a/include/cru/common/pre_config.hpp +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once - -#ifdef _MSC_VER -// disable the unnecessary warning about multi-inheritance -#pragma warning(disable : 4250) -#endif - -#define _CRT_SECURE_NO_WARNINGS diff --git a/include/cru/common/self_resolvable.hpp b/include/cru/common/self_resolvable.hpp deleted file mode 100644 index 19c06a1b..00000000 --- a/include/cru/common/self_resolvable.hpp +++ /dev/null @@ -1,51 +0,0 @@ -#pragma once -#include "pre_config.hpp" - -#include -#include - -namespace cru { -template -class SelfResolvable; - -template -class ObjectResolver { - friend SelfResolvable; - - private: - ObjectResolver(const std::shared_ptr& resolver) : resolver_(resolver) {} - - public: - ObjectResolver(const ObjectResolver&) = default; - ObjectResolver& operator=(const ObjectResolver&) = default; - ObjectResolver(ObjectResolver&&) = default; - ObjectResolver& operator=(ObjectResolver&&) = default; - ~ObjectResolver() = default; - - T* Resolve() const { - // resolver_ is null only when this has been moved. - // You shouldn't resolve a moved resolver. So assert it. - Expects(resolver_); - return *resolver_; - } - - private: - std::shared_ptr resolver_; -}; - -template -class SelfResolvable { - public: - SelfResolvable() : resolver_(new T*(static_cast(this))) {} - SelfResolvable(const SelfResolvable&) = delete; - SelfResolvable& operator=(const SelfResolvable&) = delete; - SelfResolvable(SelfResolvable&&) = delete; - SelfResolvable& operator=(SelfResolvable&&) = delete; - virtual ~SelfResolvable() { (*resolver_) = nullptr; } - - ObjectResolver CreateResolver() { return ObjectResolver(resolver_); } - - private: - std::shared_ptr resolver_; -}; -} // namespace cru diff --git a/include/cru/platform/GraphBase.hpp b/include/cru/platform/GraphBase.hpp new file mode 100644 index 00000000..af61eba3 --- /dev/null +++ b/include/cru/platform/GraphBase.hpp @@ -0,0 +1,419 @@ +#pragma once +#include "cru/common/Base.hpp" + +#include +#include +#include + +namespace cru::platform { +struct Point final { + constexpr Point() = default; + constexpr Point(const float x, const float y) : x(x), y(y) {} + + float x = 0; + float y = 0; +}; + +constexpr Point operator+(const Point& left, const Point& right) { + return Point(left.x + right.x, left.y + right.y); +} + +constexpr Point operator-(const Point& left, const Point& right) { + return Point(left.x - right.x, left.y - right.y); +} + +constexpr bool operator==(const Point& left, const Point& right) { + return left.x == right.x && left.y == right.y; +} + +constexpr bool operator!=(const Point& left, const Point& right) { + return !(left == right); +} + +struct Size final { + constexpr Size() = default; + constexpr Size(const float width, const float height) + : width(width), height(height) {} + + float width = 0; + float height = 0; +}; + +constexpr Size operator+(const Size& left, const Size& right) { + return Size(left.width + right.width, left.height + right.height); +} + +constexpr Size operator-(const Size& left, const Size& right) { + return Size(left.width - right.width, left.height - right.height); +} + +constexpr bool operator==(const Size& left, const Size& right) { + return left.width == right.width && left.height == right.height; +} + +constexpr bool operator!=(const Size& left, const Size& right) { + return !(left == right); +} + +struct Thickness final { + constexpr Thickness() : Thickness(0) {} + + constexpr explicit Thickness(const float width) + : left(width), top(width), right(width), bottom(width) {} + + constexpr explicit Thickness(const float horizontal, const float vertical) + : left(horizontal), top(vertical), right(horizontal), bottom(vertical) {} + + constexpr Thickness(const float left, const float top, const float right, + const float bottom) + : left(left), top(top), right(right), bottom(bottom) {} + + constexpr float GetHorizontalTotal() const { return left + right; } + + constexpr float GetVerticalTotal() const { return top + bottom; } + + void SetLeftRight(const float value) { left = right = value; } + + void SetTopBottom(const float value) { top = bottom = value; } + + void SetAll(const float value) { left = top = right = bottom = value; } + + constexpr float Validate() const { + return left >= 0.0 && top >= 0.0 && right >= 0.0 && bottom >= 0.0; + } + + float left; + float top; + float right; + float bottom; +}; + +constexpr bool operator==(const Thickness& left, const Thickness& right) { + return left.left == right.left && left.top == right.top && + left.right == right.right && left.bottom == right.bottom; +} + +constexpr bool operator!=(const Thickness& left, const Thickness& right) { + return !(left == right); +} + +struct Rect final { + constexpr Rect() = default; + constexpr Rect(const float left, const float top, const float width, + const float height) + : left(left), top(top), width(width), height(height) {} + constexpr Rect(const Point& lefttop, const Size& size) + : left(lefttop.x), + top(lefttop.y), + width(size.width), + height(size.height) {} + + constexpr static Rect FromVertices(const float left, const float top, + const float right, const float bottom) { + return Rect(left, top, right - left, bottom - top); + } + + constexpr static Rect FromCenter(const Point& center, const float width, + const float height) { + return Rect(center.x - width / 2.0f, center.y - height / 2.0f, width, + height); + } + + constexpr float GetRight() const { return left + width; } + + constexpr float GetBottom() const { return top + height; } + + constexpr Point GetLeftTop() const { return Point(left, top); } + + constexpr Point GetRightBottom() const { + 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 Point GetCenter() const { + return Point(left + width / 2.0f, top + height / 2.0f); + } + + constexpr Size GetSize() const { return Size(width, height); } + + constexpr Rect Shrink(const Thickness& thickness) const { + return Rect(left + thickness.left, top + thickness.top, + width - thickness.GetHorizontalTotal(), + height - thickness.GetVerticalTotal()); + } + + constexpr bool IsPointInside(const Point& point) const { + return point.x >= left && point.x < GetRight() && point.y >= top && + point.y < GetBottom(); + } + + float left = 0.0f; + float top = 0.0f; + float width = 0.0f; + float height = 0.0f; +}; + +constexpr bool operator==(const Rect& left, const Rect& right) { + return left.left == right.left && left.top == right.top && + left.width == right.width && left.height == right.height; +} + +constexpr bool operator!=(const Rect& left, const Rect& right) { + return !(left == right); +} + +struct RoundedRect final { + constexpr RoundedRect() = default; + constexpr RoundedRect(const Rect& rect, const float radius_x, + const float radius_y) + : rect(rect), radius_x(radius_x), radius_y(radius_y) {} + + Rect rect{}; + float radius_x = 0.0f; + float radius_y = 0.0f; +}; + +constexpr bool operator==(const RoundedRect& left, const RoundedRect& right) { + return left.rect == right.rect && left.radius_x == right.radius_x && + left.radius_y == right.radius_y; +} + +constexpr bool operator!=(const RoundedRect& left, const RoundedRect& right) { + return !(left == right); +} + +struct Ellipse final { + constexpr Ellipse() = default; + constexpr Ellipse(const Point& center, const float radius_x, + const float radius_y) + : center(center), radius_x(radius_x), radius_y(radius_y) {} + + constexpr static Ellipse FromRect(const Rect& rect) { + return Ellipse(rect.GetCenter(), rect.width / 2.0f, rect.height / 2.0f); + } + + constexpr Rect GetBoundRect() const { + return Rect::FromCenter(center, radius_x * 2.0f, radius_y * 2.0f); + } + + Point center{}; + float radius_x = 0.0f; + float radius_y = 0.0f; +}; + +constexpr bool operator==(const Ellipse& left, const Ellipse& right) { + return left.center == right.center && left.radius_x == right.radius_x && + left.radius_y == right.radius_y; +} + +constexpr bool operator!=(const Ellipse& left, const Ellipse& right) { + return !(left == right); +} + +struct TextRange final { + constexpr static TextRange FromTwoSides(gsl::index start, gsl::index end) { + return TextRange(start, end - start); + } + + constexpr static TextRange FromTwoSides(gsl::index start, gsl::index end, + gsl::index offset) { + return TextRange(start + offset, end - start); + } + + constexpr TextRange() = default; + constexpr TextRange(const gsl::index position, const gsl::index count = 0) + : position(position), count(count) {} + + gsl::index GetEnd() const { return position + count; } + + TextRange Normalize() const { + auto result = *this; + if (result.count < 0) { + result.position += result.count; + result.count = -result.count; + } + return result; + } + + gsl::index position = 0; + gsl::index count = 0; +}; + +struct Color { + constexpr Color() : Color(0, 0, 0, 255) {} + constexpr Color(std::uint8_t red, std::uint8_t green, std::uint8_t blue, + std::uint8_t alpha = 255) + : red(red), green(green), blue(blue), alpha(alpha) {} + + constexpr static Color FromHex(std::uint32_t hex) { + const std::uint32_t mask = 0b11111111; + return Color((hex >> 16) & mask, (hex >> 8) & mask, hex & mask, 255); + } + + constexpr static Color FromHexAlpha(std::uint32_t hex) { + const std::uint32_t mask = 0b11111111; + return Color((hex >> 16) & mask, (hex >> 8) & mask, hex & mask, + (hex >> 24) & mask); + } + + std::uint8_t red; + std::uint8_t green; + std::uint8_t blue; + std::uint8_t alpha; +}; + +namespace colors { +constexpr Color transparent = Color::FromHexAlpha(0x00000000); +constexpr Color black = Color::FromHex(0x000000); +constexpr Color silver = Color::FromHex(0xc0c0c0); +constexpr Color gray = Color::FromHex(0x808080); +constexpr Color white = Color::FromHex(0xffffff); +constexpr Color maroon = Color::FromHex(0x800000); +constexpr Color red = Color::FromHex(0xff0000); +constexpr Color purple = Color::FromHex(0x800080); +constexpr Color fuchsia = Color::FromHex(0xff00ff); +constexpr Color green = Color::FromHex(0x008000); +constexpr Color lime = Color::FromHex(0x00ff00); +constexpr Color olive = Color::FromHex(0x808000); +constexpr Color yellow = Color::FromHex(0xffff00); +constexpr Color navy = Color::FromHex(0x000080); +constexpr Color blue = Color::FromHex(0x0000ff); +constexpr Color teal = Color::FromHex(0x008080); +constexpr Color aqua = Color::FromHex(0x00ffff); +constexpr Color orange = Color::FromHex(0xffa500); +constexpr Color aliceblue = Color::FromHex(0xf0f8ff); +constexpr Color antiquewhite = Color::FromHex(0xfaebd7); +constexpr Color aquamarine = Color::FromHex(0x7fffd4); +constexpr Color azure = Color::FromHex(0xf0ffff); +constexpr Color beige = Color::FromHex(0xf5f5dc); +constexpr Color bisque = Color::FromHex(0xffe4c4); +constexpr Color blanchedalmond = Color::FromHex(0xffebcd); +constexpr Color blueviolet = Color::FromHex(0x8a2be2); +constexpr Color brown = Color::FromHex(0xa52a2a); +constexpr Color burlywood = Color::FromHex(0xdeb887); +constexpr Color cadetblue = Color::FromHex(0x5f9ea0); +constexpr Color chartreuse = Color::FromHex(0x7fff00); +constexpr Color chocolate = Color::FromHex(0xd2691e); +constexpr Color coral = Color::FromHex(0xff7f50); +constexpr Color cornflowerblue = Color::FromHex(0x6495ed); +constexpr Color cornsilk = Color::FromHex(0xfff8dc); +constexpr Color crimson = Color::FromHex(0xdc143c); +constexpr Color cyan = aqua; +constexpr Color darkblue = Color::FromHex(0x00008b); +constexpr Color darkcyan = Color::FromHex(0x008b8b); +constexpr Color darkgoldenrod = Color::FromHex(0xb8860b); +constexpr Color darkgray = Color::FromHex(0xa9a9a9); +constexpr Color darkgreen = Color::FromHex(0x006400); +constexpr Color darkgrey = Color::FromHex(0xa9a9a9); +constexpr Color darkkhaki = Color::FromHex(0xbdb76b); +constexpr Color darkmagenta = Color::FromHex(0x8b008b); +constexpr Color darkolivegreen = Color::FromHex(0x556b2f); +constexpr Color darkorange = Color::FromHex(0xff8c00); +constexpr Color darkorchid = Color::FromHex(0x9932cc); +constexpr Color darkred = Color::FromHex(0x8b0000); +constexpr Color darksalmon = Color::FromHex(0xe9967a); +constexpr Color darkseagreen = Color::FromHex(0x8fbc8f); +constexpr Color darkslateblue = Color::FromHex(0x483d8b); +constexpr Color darkslategray = Color::FromHex(0x2f4f4f); +constexpr Color darkslategrey = Color::FromHex(0x2f4f4f); +constexpr Color darkturquoise = Color::FromHex(0x00ced1); +constexpr Color darkviolet = Color::FromHex(0x9400d3); +constexpr Color deeppink = Color::FromHex(0xff1493); +constexpr Color deepskyblue = Color::FromHex(0x00bfff); +constexpr Color dimgray = Color::FromHex(0x696969); +constexpr Color dimgrey = Color::FromHex(0x696969); +constexpr Color dodgerblue = Color::FromHex(0x1e90ff); +constexpr Color firebrick = Color::FromHex(0xb22222); +constexpr Color floralwhite = Color::FromHex(0xfffaf0); +constexpr Color forestgreen = Color::FromHex(0x228b22); +constexpr Color gainsboro = Color::FromHex(0xdcdcdc); +constexpr Color ghostwhite = Color::FromHex(0xf8f8ff); +constexpr Color gold = Color::FromHex(0xffd700); +constexpr Color goldenrod = Color::FromHex(0xdaa520); +constexpr Color greenyellow = Color::FromHex(0xadff2f); +constexpr Color grey = Color::FromHex(0x808080); +constexpr Color honeydew = Color::FromHex(0xf0fff0); +constexpr Color hotpink = Color::FromHex(0xff69b4); +constexpr Color indianred = Color::FromHex(0xcd5c5c); +constexpr Color indigo = Color::FromHex(0x4b0082); +constexpr Color ivory = Color::FromHex(0xfffff0); +constexpr Color khaki = Color::FromHex(0xf0e68c); +constexpr Color lavender = Color::FromHex(0xe6e6fa); +constexpr Color lavenderblush = Color::FromHex(0xfff0f5); +constexpr Color lawngreen = Color::FromHex(0x7cfc00); +constexpr Color lemonchiffon = Color::FromHex(0xfffacd); +constexpr Color lightblue = Color::FromHex(0xadd8e6); +constexpr Color lightcoral = Color::FromHex(0xf08080); +constexpr Color lightcyan = Color::FromHex(0xe0ffff); +constexpr Color lightgoldenrodyellow = Color::FromHex(0xfafad2); +constexpr Color lightgray = Color::FromHex(0xd3d3d3); +constexpr Color lightgreen = Color::FromHex(0x90ee90); +constexpr Color lightgrey = Color::FromHex(0xd3d3d3); +constexpr Color lightpink = Color::FromHex(0xffb6c1); +constexpr Color lightsalmon = Color::FromHex(0xffa07a); +constexpr Color lightseagreen = Color::FromHex(0x20b2aa); +constexpr Color lightskyblue = Color::FromHex(0x87cefa); +constexpr Color lightslategray = Color::FromHex(0x778899); +constexpr Color lightslategrey = Color::FromHex(0x778899); +constexpr Color lightsteelblue = Color::FromHex(0xb0c4de); +constexpr Color lightyellow = Color::FromHex(0xffffe0); +constexpr Color limegreen = Color::FromHex(0x32cd32); +constexpr Color linen = Color::FromHex(0xfaf0e6); +constexpr Color magenta = fuchsia; +constexpr Color mediumaquamarine = Color::FromHex(0x66cdaa); +constexpr Color mediumblue = Color::FromHex(0x0000cd); +constexpr Color mediumorchid = Color::FromHex(0xba55d3); +constexpr Color mediumpurple = Color::FromHex(0x9370db); +constexpr Color mediumseagreen = Color::FromHex(0x3cb371); +constexpr Color mediumslateblue = Color::FromHex(0x7b68ee); +constexpr Color mediumspringgreen = Color::FromHex(0x00fa9a); +constexpr Color mediumturquoise = Color::FromHex(0x48d1cc); +constexpr Color mediumvioletred = Color::FromHex(0xc71585); +constexpr Color midnightblue = Color::FromHex(0x191970); +constexpr Color mintcream = Color::FromHex(0xf5fffa); +constexpr Color mistyrose = Color::FromHex(0xffe4e1); +constexpr Color moccasin = Color::FromHex(0xffe4b5); +constexpr Color navajowhite = Color::FromHex(0xffdead); +constexpr Color oldlace = Color::FromHex(0xfdf5e6); +constexpr Color olivedrab = Color::FromHex(0x6b8e23); +constexpr Color orangered = Color::FromHex(0xff4500); +constexpr Color orchid = Color::FromHex(0xda70d6); +constexpr Color palegoldenrod = Color::FromHex(0xeee8aa); +constexpr Color palegreen = Color::FromHex(0x98fb98); +constexpr Color paleturquoise = Color::FromHex(0xafeeee); +constexpr Color palevioletred = Color::FromHex(0xdb7093); +constexpr Color papayawhip = Color::FromHex(0xffefd5); +constexpr Color peachpuff = Color::FromHex(0xffdab9); +constexpr Color peru = Color::FromHex(0xcd853f); +constexpr Color pink = Color::FromHex(0xffc0cb); +constexpr Color plum = Color::FromHex(0xdda0dd); +constexpr Color powderblue = Color::FromHex(0xb0e0e6); +constexpr Color rosybrown = Color::FromHex(0xbc8f8f); +constexpr Color royalblue = Color::FromHex(0x4169e1); +constexpr Color saddlebrown = Color::FromHex(0x8b4513); +constexpr Color salmon = Color::FromHex(0xfa8072); +constexpr Color sandybrown = Color::FromHex(0xf4a460); +constexpr Color seagreen = Color::FromHex(0x2e8b57); +constexpr Color seashell = Color::FromHex(0xfff5ee); +constexpr Color sienna = Color::FromHex(0xa0522d); +constexpr Color skyblue = Color::FromHex(0x87ceeb); +constexpr Color slateblue = Color::FromHex(0x6a5acd); +constexpr Color slategray = Color::FromHex(0x708090); +constexpr Color slategrey = Color::FromHex(0x708090); +constexpr Color snow = Color::FromHex(0xfffafa); +constexpr Color springgreen = Color::FromHex(0x00ff7f); +constexpr Color steelblue = Color::FromHex(0x4682b4); +constexpr Color tan = Color::FromHex(0xd2b48c); +constexpr Color thistle = Color::FromHex(0xd8bfd8); +constexpr Color tomato = Color::FromHex(0xff6347); +constexpr Color turquoise = Color::FromHex(0x40e0d0); +constexpr Color violet = Color::FromHex(0xee82ee); +constexpr Color wheat = Color::FromHex(0xf5deb3); +constexpr Color whitesmoke = Color::FromHex(0xf5f5f5); +constexpr Color yellowgreen = Color::FromHex(0x9acd32); +constexpr Color rebeccapurple = Color::FromHex(0x663399); +} // namespace colors +} // namespace cru::platform diff --git a/include/cru/platform/HeapDebug.hpp b/include/cru/platform/HeapDebug.hpp new file mode 100644 index 00000000..10ebfd2c --- /dev/null +++ b/include/cru/platform/HeapDebug.hpp @@ -0,0 +1,7 @@ +#pragma once +#include "cru/common/PreConfig.hpp" + +namespace cru::platform { +// Setup the heap debug function. Currently I only use this on Windows... +void SetupHeapDebug(); +} // namespace cru::platform diff --git a/include/cru/platform/check.hpp b/include/cru/platform/check.hpp index 6e353afb..f4bbcfe8 100644 --- a/include/cru/platform/check.hpp +++ b/include/cru/platform/check.hpp @@ -1,6 +1,6 @@ #pragma once -#include "exception.hpp" -#include "resource.hpp" +#include "Exception.hpp" +#include "Resource.hpp" #include #include diff --git a/include/cru/platform/exception.hpp b/include/cru/platform/exception.hpp index afe11c03..8b958a1d 100644 --- a/include/cru/platform/exception.hpp +++ b/include/cru/platform/exception.hpp @@ -1,5 +1,5 @@ #pragma once -#include "cru/common/base.hpp" +#include "cru/common/Base.hpp" #include diff --git a/include/cru/platform/graph/TextLayout.hpp b/include/cru/platform/graph/TextLayout.hpp new file mode 100644 index 00000000..4086ac56 --- /dev/null +++ b/include/cru/platform/graph/TextLayout.hpp @@ -0,0 +1,23 @@ +#pragma once +#include "Resource.hpp" + +#include +#include + +namespace cru::platform::graph { +struct ITextLayout : virtual IGraphResource { + virtual std::string GetText() = 0; + virtual void SetText(std::string new_text) = 0; + + virtual std::shared_ptr GetFont() = 0; + virtual void SetFont(std::shared_ptr font) = 0; + + virtual void SetMaxWidth(float max_width) = 0; + virtual void SetMaxHeight(float max_height) = 0; + + virtual Rect GetTextBounds() = 0; + virtual std::vector TextRangeRect(const TextRange& text_range) = 0; + virtual Point TextSinglePoint(gsl::index position, bool trailing) = 0; + virtual TextHitTestResult HitTest(const Point& point) = 0; +}; +} // namespace cru::platform::graph diff --git a/include/cru/platform/graph/base.hpp b/include/cru/platform/graph/base.hpp index 002c2f51..61cfc5ef 100644 --- a/include/cru/platform/graph/base.hpp +++ b/include/cru/platform/graph/base.hpp @@ -1,7 +1,7 @@ #pragma once -#include "../graph_base.hpp" -#include "../matrix.hpp" -#include "../resource.hpp" +#include "../GraphBase.hpp" +#include "../Matrix.hpp" +#include "../Resource.hpp" #include diff --git a/include/cru/platform/graph/brush.hpp b/include/cru/platform/graph/brush.hpp index af7a1dec..e67384de 100644 --- a/include/cru/platform/graph/brush.hpp +++ b/include/cru/platform/graph/brush.hpp @@ -1,5 +1,5 @@ #pragma once -#include "resource.hpp" +#include "Resource.hpp" namespace cru::platform::graph { struct IBrush : virtual IGraphResource {}; diff --git a/include/cru/platform/graph/factory.hpp b/include/cru/platform/graph/factory.hpp index 0ed45161..0a425d15 100644 --- a/include/cru/platform/graph/factory.hpp +++ b/include/cru/platform/graph/factory.hpp @@ -1,10 +1,10 @@ #pragma once -#include "resource.hpp" +#include "Resource.hpp" -#include "brush.hpp" -#include "font.hpp" -#include "geometry.hpp" -#include "text_layout.hpp" +#include "Brush.hpp" +#include "Font.hpp" +#include "Geometry.hpp" +#include "TextLayout.hpp" #include #include diff --git a/include/cru/platform/graph/font.hpp b/include/cru/platform/graph/font.hpp index d0aa2d28..182cc15b 100644 --- a/include/cru/platform/graph/font.hpp +++ b/include/cru/platform/graph/font.hpp @@ -1,5 +1,5 @@ #pragma once -#include "resource.hpp" +#include "Resource.hpp" namespace cru::platform::graph { struct IFont : virtual IGraphResource { diff --git a/include/cru/platform/graph/geometry.hpp b/include/cru/platform/graph/geometry.hpp index 85ffd3f6..354efd97 100644 --- a/include/cru/platform/graph/geometry.hpp +++ b/include/cru/platform/graph/geometry.hpp @@ -1,5 +1,5 @@ #pragma once -#include "resource.hpp" +#include "Resource.hpp" namespace cru::platform::graph { struct IGeometry : virtual IGraphResource { diff --git a/include/cru/platform/graph/painter.hpp b/include/cru/platform/graph/painter.hpp index b6eb5452..27ae420b 100644 --- a/include/cru/platform/graph/painter.hpp +++ b/include/cru/platform/graph/painter.hpp @@ -1,5 +1,5 @@ #pragma once -#include "resource.hpp" +#include "Resource.hpp" namespace cru::platform::graph { diff --git a/include/cru/platform/graph/resource.hpp b/include/cru/platform/graph/resource.hpp index 255865eb..8859360c 100644 --- a/include/cru/platform/graph/resource.hpp +++ b/include/cru/platform/graph/resource.hpp @@ -1,5 +1,5 @@ #pragma once -#include "base.hpp" +#include "Base.hpp" namespace cru::platform::graph { struct IGraphFactory; diff --git a/include/cru/platform/graph/text_layout.hpp b/include/cru/platform/graph/text_layout.hpp deleted file mode 100644 index d91834c0..00000000 --- a/include/cru/platform/graph/text_layout.hpp +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once -#include "resource.hpp" - -#include -#include - -namespace cru::platform::graph { -struct ITextLayout : virtual IGraphResource { - virtual std::string GetText() = 0; - virtual void SetText(std::string new_text) = 0; - - virtual std::shared_ptr GetFont() = 0; - virtual void SetFont(std::shared_ptr font) = 0; - - virtual void SetMaxWidth(float max_width) = 0; - virtual void SetMaxHeight(float max_height) = 0; - - virtual Rect GetTextBounds() = 0; - virtual std::vector TextRangeRect(const TextRange& text_range) = 0; - virtual Point TextSinglePoint(gsl::index position, bool trailing) = 0; - virtual TextHitTestResult HitTest(const Point& point) = 0; -}; -} // namespace cru::platform::graph diff --git a/include/cru/platform/graph/util/painter.hpp b/include/cru/platform/graph/util/painter.hpp index 72d96bc1..f9aec027 100644 --- a/include/cru/platform/graph/util/painter.hpp +++ b/include/cru/platform/graph/util/painter.hpp @@ -1,5 +1,5 @@ #pragma once -#include "../painter.hpp" +#include "../Painter.hpp" #include #include diff --git a/include/cru/platform/graph_base.hpp b/include/cru/platform/graph_base.hpp deleted file mode 100644 index c880c7a2..00000000 --- a/include/cru/platform/graph_base.hpp +++ /dev/null @@ -1,419 +0,0 @@ -#pragma once -#include "cru/common/base.hpp" - -#include -#include -#include - -namespace cru::platform { -struct Point final { - constexpr Point() = default; - constexpr Point(const float x, const float y) : x(x), y(y) {} - - float x = 0; - float y = 0; -}; - -constexpr Point operator+(const Point& left, const Point& right) { - return Point(left.x + right.x, left.y + right.y); -} - -constexpr Point operator-(const Point& left, const Point& right) { - return Point(left.x - right.x, left.y - right.y); -} - -constexpr bool operator==(const Point& left, const Point& right) { - return left.x == right.x && left.y == right.y; -} - -constexpr bool operator!=(const Point& left, const Point& right) { - return !(left == right); -} - -struct Size final { - constexpr Size() = default; - constexpr Size(const float width, const float height) - : width(width), height(height) {} - - float width = 0; - float height = 0; -}; - -constexpr Size operator+(const Size& left, const Size& right) { - return Size(left.width + right.width, left.height + right.height); -} - -constexpr Size operator-(const Size& left, const Size& right) { - return Size(left.width - right.width, left.height - right.height); -} - -constexpr bool operator==(const Size& left, const Size& right) { - return left.width == right.width && left.height == right.height; -} - -constexpr bool operator!=(const Size& left, const Size& right) { - return !(left == right); -} - -struct Thickness final { - constexpr Thickness() : Thickness(0) {} - - constexpr explicit Thickness(const float width) - : left(width), top(width), right(width), bottom(width) {} - - constexpr explicit Thickness(const float horizontal, const float vertical) - : left(horizontal), top(vertical), right(horizontal), bottom(vertical) {} - - constexpr Thickness(const float left, const float top, const float right, - const float bottom) - : left(left), top(top), right(right), bottom(bottom) {} - - constexpr float GetHorizontalTotal() const { return left + right; } - - constexpr float GetVerticalTotal() const { return top + bottom; } - - void SetLeftRight(const float value) { left = right = value; } - - void SetTopBottom(const float value) { top = bottom = value; } - - void SetAll(const float value) { left = top = right = bottom = value; } - - constexpr float Validate() const { - return left >= 0.0 && top >= 0.0 && right >= 0.0 && bottom >= 0.0; - } - - float left; - float top; - float right; - float bottom; -}; - -constexpr bool operator==(const Thickness& left, const Thickness& right) { - return left.left == right.left && left.top == right.top && - left.right == right.right && left.bottom == right.bottom; -} - -constexpr bool operator!=(const Thickness& left, const Thickness& right) { - return !(left == right); -} - -struct Rect final { - constexpr Rect() = default; - constexpr Rect(const float left, const float top, const float width, - const float height) - : left(left), top(top), width(width), height(height) {} - constexpr Rect(const Point& lefttop, const Size& size) - : left(lefttop.x), - top(lefttop.y), - width(size.width), - height(size.height) {} - - constexpr static Rect FromVertices(const float left, const float top, - const float right, const float bottom) { - return Rect(left, top, right - left, bottom - top); - } - - constexpr static Rect FromCenter(const Point& center, const float width, - const float height) { - return Rect(center.x - width / 2.0f, center.y - height / 2.0f, width, - height); - } - - constexpr float GetRight() const { return left + width; } - - constexpr float GetBottom() const { return top + height; } - - constexpr Point GetLeftTop() const { return Point(left, top); } - - constexpr Point GetRightBottom() const { - 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 Point GetCenter() const { - return Point(left + width / 2.0f, top + height / 2.0f); - } - - constexpr Size GetSize() const { return Size(width, height); } - - constexpr Rect Shrink(const Thickness& thickness) const { - return Rect(left + thickness.left, top + thickness.top, - width - thickness.GetHorizontalTotal(), - height - thickness.GetVerticalTotal()); - } - - constexpr bool IsPointInside(const Point& point) const { - return point.x >= left && point.x < GetRight() && point.y >= top && - point.y < GetBottom(); - } - - float left = 0.0f; - float top = 0.0f; - float width = 0.0f; - float height = 0.0f; -}; - -constexpr bool operator==(const Rect& left, const Rect& right) { - return left.left == right.left && left.top == right.top && - left.width == right.width && left.height == right.height; -} - -constexpr bool operator!=(const Rect& left, const Rect& right) { - return !(left == right); -} - -struct RoundedRect final { - constexpr RoundedRect() = default; - constexpr RoundedRect(const Rect& rect, const float radius_x, - const float radius_y) - : rect(rect), radius_x(radius_x), radius_y(radius_y) {} - - Rect rect{}; - float radius_x = 0.0f; - float radius_y = 0.0f; -}; - -constexpr bool operator==(const RoundedRect& left, const RoundedRect& right) { - return left.rect == right.rect && left.radius_x == right.radius_x && - left.radius_y == right.radius_y; -} - -constexpr bool operator!=(const RoundedRect& left, const RoundedRect& right) { - return !(left == right); -} - -struct Ellipse final { - constexpr Ellipse() = default; - constexpr Ellipse(const Point& center, const float radius_x, - const float radius_y) - : center(center), radius_x(radius_x), radius_y(radius_y) {} - - constexpr static Ellipse FromRect(const Rect& rect) { - return Ellipse(rect.GetCenter(), rect.width / 2.0f, rect.height / 2.0f); - } - - constexpr Rect GetBoundRect() const { - return Rect::FromCenter(center, radius_x * 2.0f, radius_y * 2.0f); - } - - Point center{}; - float radius_x = 0.0f; - float radius_y = 0.0f; -}; - -constexpr bool operator==(const Ellipse& left, const Ellipse& right) { - return left.center == right.center && left.radius_x == right.radius_x && - left.radius_y == right.radius_y; -} - -constexpr bool operator!=(const Ellipse& left, const Ellipse& right) { - return !(left == right); -} - -struct TextRange final { - constexpr static TextRange FromTwoSides(gsl::index start, gsl::index end) { - return TextRange(start, end - start); - } - - constexpr static TextRange FromTwoSides(gsl::index start, gsl::index end, - gsl::index offset) { - return TextRange(start + offset, end - start); - } - - constexpr TextRange() = default; - constexpr TextRange(const gsl::index position, const gsl::index count = 0) - : position(position), count(count) {} - - gsl::index GetEnd() const { return position + count; } - - TextRange Normalize() const { - auto result = *this; - if (result.count < 0) { - result.position += result.count; - result.count = -result.count; - } - return result; - } - - gsl::index position = 0; - gsl::index count = 0; -}; - -struct Color { - constexpr Color() : Color(0, 0, 0, 255) {} - constexpr Color(std::uint8_t red, std::uint8_t green, std::uint8_t blue, - std::uint8_t alpha = 255) - : red(red), green(green), blue(blue), alpha(alpha) {} - - constexpr static Color FromHex(std::uint32_t hex) { - const std::uint32_t mask = 0b11111111; - return Color((hex >> 16) & mask, (hex >> 8) & mask, hex & mask, 255); - } - - constexpr static Color FromHexAlpha(std::uint32_t hex) { - const std::uint32_t mask = 0b11111111; - return Color((hex >> 16) & mask, (hex >> 8) & mask, hex & mask, - (hex >> 24) & mask); - } - - std::uint8_t red; - std::uint8_t green; - std::uint8_t blue; - std::uint8_t alpha; -}; - -namespace colors { -constexpr Color transparent = Color::FromHexAlpha(0x00000000); -constexpr Color black = Color::FromHex(0x000000); -constexpr Color silver = Color::FromHex(0xc0c0c0); -constexpr Color gray = Color::FromHex(0x808080); -constexpr Color white = Color::FromHex(0xffffff); -constexpr Color maroon = Color::FromHex(0x800000); -constexpr Color red = Color::FromHex(0xff0000); -constexpr Color purple = Color::FromHex(0x800080); -constexpr Color fuchsia = Color::FromHex(0xff00ff); -constexpr Color green = Color::FromHex(0x008000); -constexpr Color lime = Color::FromHex(0x00ff00); -constexpr Color olive = Color::FromHex(0x808000); -constexpr Color yellow = Color::FromHex(0xffff00); -constexpr Color navy = Color::FromHex(0x000080); -constexpr Color blue = Color::FromHex(0x0000ff); -constexpr Color teal = Color::FromHex(0x008080); -constexpr Color aqua = Color::FromHex(0x00ffff); -constexpr Color orange = Color::FromHex(0xffa500); -constexpr Color aliceblue = Color::FromHex(0xf0f8ff); -constexpr Color antiquewhite = Color::FromHex(0xfaebd7); -constexpr Color aquamarine = Color::FromHex(0x7fffd4); -constexpr Color azure = Color::FromHex(0xf0ffff); -constexpr Color beige = Color::FromHex(0xf5f5dc); -constexpr Color bisque = Color::FromHex(0xffe4c4); -constexpr Color blanchedalmond = Color::FromHex(0xffebcd); -constexpr Color blueviolet = Color::FromHex(0x8a2be2); -constexpr Color brown = Color::FromHex(0xa52a2a); -constexpr Color burlywood = Color::FromHex(0xdeb887); -constexpr Color cadetblue = Color::FromHex(0x5f9ea0); -constexpr Color chartreuse = Color::FromHex(0x7fff00); -constexpr Color chocolate = Color::FromHex(0xd2691e); -constexpr Color coral = Color::FromHex(0xff7f50); -constexpr Color cornflowerblue = Color::FromHex(0x6495ed); -constexpr Color cornsilk = Color::FromHex(0xfff8dc); -constexpr Color crimson = Color::FromHex(0xdc143c); -constexpr Color cyan = aqua; -constexpr Color darkblue = Color::FromHex(0x00008b); -constexpr Color darkcyan = Color::FromHex(0x008b8b); -constexpr Color darkgoldenrod = Color::FromHex(0xb8860b); -constexpr Color darkgray = Color::FromHex(0xa9a9a9); -constexpr Color darkgreen = Color::FromHex(0x006400); -constexpr Color darkgrey = Color::FromHex(0xa9a9a9); -constexpr Color darkkhaki = Color::FromHex(0xbdb76b); -constexpr Color darkmagenta = Color::FromHex(0x8b008b); -constexpr Color darkolivegreen = Color::FromHex(0x556b2f); -constexpr Color darkorange = Color::FromHex(0xff8c00); -constexpr Color darkorchid = Color::FromHex(0x9932cc); -constexpr Color darkred = Color::FromHex(0x8b0000); -constexpr Color darksalmon = Color::FromHex(0xe9967a); -constexpr Color darkseagreen = Color::FromHex(0x8fbc8f); -constexpr Color darkslateblue = Color::FromHex(0x483d8b); -constexpr Color darkslategray = Color::FromHex(0x2f4f4f); -constexpr Color darkslategrey = Color::FromHex(0x2f4f4f); -constexpr Color darkturquoise = Color::FromHex(0x00ced1); -constexpr Color darkviolet = Color::FromHex(0x9400d3); -constexpr Color deeppink = Color::FromHex(0xff1493); -constexpr Color deepskyblue = Color::FromHex(0x00bfff); -constexpr Color dimgray = Color::FromHex(0x696969); -constexpr Color dimgrey = Color::FromHex(0x696969); -constexpr Color dodgerblue = Color::FromHex(0x1e90ff); -constexpr Color firebrick = Color::FromHex(0xb22222); -constexpr Color floralwhite = Color::FromHex(0xfffaf0); -constexpr Color forestgreen = Color::FromHex(0x228b22); -constexpr Color gainsboro = Color::FromHex(0xdcdcdc); -constexpr Color ghostwhite = Color::FromHex(0xf8f8ff); -constexpr Color gold = Color::FromHex(0xffd700); -constexpr Color goldenrod = Color::FromHex(0xdaa520); -constexpr Color greenyellow = Color::FromHex(0xadff2f); -constexpr Color grey = Color::FromHex(0x808080); -constexpr Color honeydew = Color::FromHex(0xf0fff0); -constexpr Color hotpink = Color::FromHex(0xff69b4); -constexpr Color indianred = Color::FromHex(0xcd5c5c); -constexpr Color indigo = Color::FromHex(0x4b0082); -constexpr Color ivory = Color::FromHex(0xfffff0); -constexpr Color khaki = Color::FromHex(0xf0e68c); -constexpr Color lavender = Color::FromHex(0xe6e6fa); -constexpr Color lavenderblush = Color::FromHex(0xfff0f5); -constexpr Color lawngreen = Color::FromHex(0x7cfc00); -constexpr Color lemonchiffon = Color::FromHex(0xfffacd); -constexpr Color lightblue = Color::FromHex(0xadd8e6); -constexpr Color lightcoral = Color::FromHex(0xf08080); -constexpr Color lightcyan = Color::FromHex(0xe0ffff); -constexpr Color lightgoldenrodyellow = Color::FromHex(0xfafad2); -constexpr Color lightgray = Color::FromHex(0xd3d3d3); -constexpr Color lightgreen = Color::FromHex(0x90ee90); -constexpr Color lightgrey = Color::FromHex(0xd3d3d3); -constexpr Color lightpink = Color::FromHex(0xffb6c1); -constexpr Color lightsalmon = Color::FromHex(0xffa07a); -constexpr Color lightseagreen = Color::FromHex(0x20b2aa); -constexpr Color lightskyblue = Color::FromHex(0x87cefa); -constexpr Color lightslategray = Color::FromHex(0x778899); -constexpr Color lightslategrey = Color::FromHex(0x778899); -constexpr Color lightsteelblue = Color::FromHex(0xb0c4de); -constexpr Color lightyellow = Color::FromHex(0xffffe0); -constexpr Color limegreen = Color::FromHex(0x32cd32); -constexpr Color linen = Color::FromHex(0xfaf0e6); -constexpr Color magenta = fuchsia; -constexpr Color mediumaquamarine = Color::FromHex(0x66cdaa); -constexpr Color mediumblue = Color::FromHex(0x0000cd); -constexpr Color mediumorchid = Color::FromHex(0xba55d3); -constexpr Color mediumpurple = Color::FromHex(0x9370db); -constexpr Color mediumseagreen = Color::FromHex(0x3cb371); -constexpr Color mediumslateblue = Color::FromHex(0x7b68ee); -constexpr Color mediumspringgreen = Color::FromHex(0x00fa9a); -constexpr Color mediumturquoise = Color::FromHex(0x48d1cc); -constexpr Color mediumvioletred = Color::FromHex(0xc71585); -constexpr Color midnightblue = Color::FromHex(0x191970); -constexpr Color mintcream = Color::FromHex(0xf5fffa); -constexpr Color mistyrose = Color::FromHex(0xffe4e1); -constexpr Color moccasin = Color::FromHex(0xffe4b5); -constexpr Color navajowhite = Color::FromHex(0xffdead); -constexpr Color oldlace = Color::FromHex(0xfdf5e6); -constexpr Color olivedrab = Color::FromHex(0x6b8e23); -constexpr Color orangered = Color::FromHex(0xff4500); -constexpr Color orchid = Color::FromHex(0xda70d6); -constexpr Color palegoldenrod = Color::FromHex(0xeee8aa); -constexpr Color palegreen = Color::FromHex(0x98fb98); -constexpr Color paleturquoise = Color::FromHex(0xafeeee); -constexpr Color palevioletred = Color::FromHex(0xdb7093); -constexpr Color papayawhip = Color::FromHex(0xffefd5); -constexpr Color peachpuff = Color::FromHex(0xffdab9); -constexpr Color peru = Color::FromHex(0xcd853f); -constexpr Color pink = Color::FromHex(0xffc0cb); -constexpr Color plum = Color::FromHex(0xdda0dd); -constexpr Color powderblue = Color::FromHex(0xb0e0e6); -constexpr Color rosybrown = Color::FromHex(0xbc8f8f); -constexpr Color royalblue = Color::FromHex(0x4169e1); -constexpr Color saddlebrown = Color::FromHex(0x8b4513); -constexpr Color salmon = Color::FromHex(0xfa8072); -constexpr Color sandybrown = Color::FromHex(0xf4a460); -constexpr Color seagreen = Color::FromHex(0x2e8b57); -constexpr Color seashell = Color::FromHex(0xfff5ee); -constexpr Color sienna = Color::FromHex(0xa0522d); -constexpr Color skyblue = Color::FromHex(0x87ceeb); -constexpr Color slateblue = Color::FromHex(0x6a5acd); -constexpr Color slategray = Color::FromHex(0x708090); -constexpr Color slategrey = Color::FromHex(0x708090); -constexpr Color snow = Color::FromHex(0xfffafa); -constexpr Color springgreen = Color::FromHex(0x00ff7f); -constexpr Color steelblue = Color::FromHex(0x4682b4); -constexpr Color tan = Color::FromHex(0xd2b48c); -constexpr Color thistle = Color::FromHex(0xd8bfd8); -constexpr Color tomato = Color::FromHex(0xff6347); -constexpr Color turquoise = Color::FromHex(0x40e0d0); -constexpr Color violet = Color::FromHex(0xee82ee); -constexpr Color wheat = Color::FromHex(0xf5deb3); -constexpr Color whitesmoke = Color::FromHex(0xf5f5f5); -constexpr Color yellowgreen = Color::FromHex(0x9acd32); -constexpr Color rebeccapurple = Color::FromHex(0x663399); -} // namespace colors -} // namespace cru::platform diff --git a/include/cru/platform/heap_debug.hpp b/include/cru/platform/heap_debug.hpp deleted file mode 100644 index 9e3ae368..00000000 --- a/include/cru/platform/heap_debug.hpp +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once -#include "cru/common/pre_config.hpp" - -namespace cru::platform { -// Setup the heap debug function. Currently I only use this on Windows... -void SetupHeapDebug(); -} // namespace cru::platform diff --git a/include/cru/platform/matrix.hpp b/include/cru/platform/matrix.hpp index 030e1378..cea5198b 100644 --- a/include/cru/platform/matrix.hpp +++ b/include/cru/platform/matrix.hpp @@ -1,5 +1,5 @@ #pragma once -#include "graph_base.hpp" +#include "GraphBase.hpp" #include diff --git a/include/cru/platform/native/InputMethod.hpp b/include/cru/platform/native/InputMethod.hpp new file mode 100644 index 00000000..1ede15b2 --- /dev/null +++ b/include/cru/platform/native/InputMethod.hpp @@ -0,0 +1,77 @@ +#pragma once +#include "../Resource.hpp" +#include "Base.hpp" + +#include "cru/common/Event.hpp" + +#include +#include +#include + +namespace cru::platform::native { +struct CompositionClause { + int start; + int end; + bool target; +}; + +using CompositionClauses = std::vector; + +struct CompositionText { + std::string text; + CompositionClauses clauses; + TextRange selection; +}; + +inline std::ostream& operator<<(std::ostream& stream, + const CompositionText& composition_text) { + stream << "text: " << composition_text.text << "\n" + << "clauses:\n"; + for (int i = 0; i < static_cast(composition_text.clauses.size()); i++) { + const auto& clause = composition_text.clauses[i]; + stream << "\t" << i << ". start:" << clause.start << " end:" << clause.end; + if (clause.target) { + stream << " target"; + } + stream << "\n"; + } + stream << "selection: position:" << composition_text.selection.position + << " count:" << composition_text.selection.count; + return stream; +} + +struct IInputMethodContext : virtual INativeResource { + // Return true if you should draw composition text manually. Return false if + // system will take care of that for you. + virtual bool ShouldManuallyDrawCompositionText() = 0; + + virtual void EnableIME() = 0; + + virtual void DisableIME() = 0; + + virtual void CompleteComposition() = 0; + + virtual void CancelComposition() = 0; + + virtual CompositionText GetCompositionText() = 0; + + // Set the candidate window lefttop. Use this method to prepare typing. + virtual void SetCandidateWindowPosition(const Point& point) = 0; + + // Triggered when user starts composition. + virtual IEvent* CompositionStartEvent() = 0; + + // Triggered when user stops composition. + virtual IEvent* CompositionEndEvent() = 0; + + // Triggered every time composition text changes. + virtual IEvent* CompositionEvent() = 0; + + virtual IEvent* TextEvent() = 0; +}; + +struct IInputMethodManager : virtual INativeResource { + virtual std::unique_ptr GetContext( + INativeWindow* window) = 0; +}; +} // namespace cru::platform::native diff --git a/include/cru/platform/native/UiApplication.hpp b/include/cru/platform/native/UiApplication.hpp new file mode 100644 index 00000000..1aa4df57 --- /dev/null +++ b/include/cru/platform/native/UiApplication.hpp @@ -0,0 +1,58 @@ +#pragma once +#include "../Resource.hpp" +#include "Base.hpp" + +#include +#include +#include +#include + +namespace cru::platform::native { +// The entry point of a ui application. +struct IUiApplication : public virtual INativeResource { + public: + static IUiApplication* GetInstance() { return instance; } + + private: + static IUiApplication* instance; + + protected: + IUiApplication(); + + public: + ~IUiApplication() override; + + // Block current thread and run the message loop. Return the exit code when + // message loop gets a quit message (possibly posted by method RequestQuit). + virtual int Run() = 0; + + // Post a quit message with given quit code. + virtual void RequestQuit(int quit_code) = 0; + + virtual void AddOnQuitHandler(std::function handler) = 0; + + virtual void InvokeLater(std::function action) = 0; + // Timer id should always be positive and never the same. So it's ok to use + // negative value to represent no timer. + virtual long long SetTimeout(std::chrono::milliseconds milliseconds, + std::function action) = 0; + virtual long long SetInterval(std::chrono::milliseconds milliseconds, + std::function action) = 0; + // Implementation should guarantee calls on timer id already canceled have no + // effects and do not crash. Also canceling negative id should always result + // in no-op. + virtual void CancelTimer(long long id) = 0; + + virtual std::vector GetAllWindow() = 0; + virtual std::shared_ptr CreateWindow( + INativeWindow* parent) = 0; + + virtual cru::platform::graph::IGraphFactory* GetGraphFactory() = 0; + + virtual ICursorManager* GetCursorManager() = 0; + virtual IInputMethodManager* GetInputMethodManager() = 0; +}; + +// Bootstrap from this. +std::unique_ptr CreateUiApplication(); +} // namespace cru::platform::native diff --git a/include/cru/platform/native/base.hpp b/include/cru/platform/native/base.hpp index e0ecbda7..bba7b960 100644 --- a/include/cru/platform/native/base.hpp +++ b/include/cru/platform/native/base.hpp @@ -1,8 +1,8 @@ #pragma once -#include "cru/common/base.hpp" -#include "cru/common/bitmask.hpp" -#include "cru/platform/graph/base.hpp" -#include "keyboard.hpp" +#include "cru/common/Base.hpp" +#include "cru/common/Bitmask.hpp" +#include "cru/platform/graph/Base.hpp" +#include "Keyboard.hpp" namespace cru::platform::native { struct ICursor; diff --git a/include/cru/platform/native/cursor.hpp b/include/cru/platform/native/cursor.hpp index eae51ffe..6c8f8068 100644 --- a/include/cru/platform/native/cursor.hpp +++ b/include/cru/platform/native/cursor.hpp @@ -1,6 +1,6 @@ #pragma once -#include "../resource.hpp" -#include "base.hpp" +#include "../Resource.hpp" +#include "Base.hpp" #include diff --git a/include/cru/platform/native/input_method.hpp b/include/cru/platform/native/input_method.hpp deleted file mode 100644 index bcf030b4..00000000 --- a/include/cru/platform/native/input_method.hpp +++ /dev/null @@ -1,77 +0,0 @@ -#pragma once -#include "../resource.hpp" -#include "base.hpp" - -#include "cru/common/event.hpp" - -#include -#include -#include - -namespace cru::platform::native { -struct CompositionClause { - int start; - int end; - bool target; -}; - -using CompositionClauses = std::vector; - -struct CompositionText { - std::string text; - CompositionClauses clauses; - TextRange selection; -}; - -inline std::ostream& operator<<(std::ostream& stream, - const CompositionText& composition_text) { - stream << "text: " << composition_text.text << "\n" - << "clauses:\n"; - for (int i = 0; i < static_cast(composition_text.clauses.size()); i++) { - const auto& clause = composition_text.clauses[i]; - stream << "\t" << i << ". start:" << clause.start << " end:" << clause.end; - if (clause.target) { - stream << " target"; - } - stream << "\n"; - } - stream << "selection: position:" << composition_text.selection.position - << " count:" << composition_text.selection.count; - return stream; -} - -struct IInputMethodContext : virtual INativeResource { - // Return true if you should draw composition text manually. Return false if - // system will take care of that for you. - virtual bool ShouldManuallyDrawCompositionText() = 0; - - virtual void EnableIME() = 0; - - virtual void DisableIME() = 0; - - virtual void CompleteComposition() = 0; - - virtual void CancelComposition() = 0; - - virtual CompositionText GetCompositionText() = 0; - - // Set the candidate window lefttop. Use this method to prepare typing. - virtual void SetCandidateWindowPosition(const Point& point) = 0; - - // Triggered when user starts composition. - virtual IEvent* CompositionStartEvent() = 0; - - // Triggered when user stops composition. - virtual IEvent* CompositionEndEvent() = 0; - - // Triggered every time composition text changes. - virtual IEvent* CompositionEvent() = 0; - - virtual IEvent* TextEvent() = 0; -}; - -struct IInputMethodManager : virtual INativeResource { - virtual std::unique_ptr GetContext( - INativeWindow* window) = 0; -}; -} // namespace cru::platform::native diff --git a/include/cru/platform/native/keyboard.hpp b/include/cru/platform/native/keyboard.hpp index 8b5e6162..26a1409d 100644 --- a/include/cru/platform/native/keyboard.hpp +++ b/include/cru/platform/native/keyboard.hpp @@ -1,5 +1,5 @@ #pragma once -#include "cru/common/bitmask.hpp" +#include "cru/common/Bitmask.hpp" namespace cru::platform::native { // Because of the complexity of keyboard layout, I only add code in US keyboard diff --git a/include/cru/platform/native/ui_application.hpp b/include/cru/platform/native/ui_application.hpp deleted file mode 100644 index afcc7117..00000000 --- a/include/cru/platform/native/ui_application.hpp +++ /dev/null @@ -1,58 +0,0 @@ -#pragma once -#include "../resource.hpp" -#include "base.hpp" - -#include -#include -#include -#include - -namespace cru::platform::native { -// The entry point of a ui application. -struct IUiApplication : public virtual INativeResource { - public: - static IUiApplication* GetInstance() { return instance; } - - private: - static IUiApplication* instance; - - protected: - IUiApplication(); - - public: - ~IUiApplication() override; - - // Block current thread and run the message loop. Return the exit code when - // message loop gets a quit message (possibly posted by method RequestQuit). - virtual int Run() = 0; - - // Post a quit message with given quit code. - virtual void RequestQuit(int quit_code) = 0; - - virtual void AddOnQuitHandler(std::function handler) = 0; - - virtual void InvokeLater(std::function action) = 0; - // Timer id should always be positive and never the same. So it's ok to use - // negative value to represent no timer. - virtual long long SetTimeout(std::chrono::milliseconds milliseconds, - std::function action) = 0; - virtual long long SetInterval(std::chrono::milliseconds milliseconds, - std::function action) = 0; - // Implementation should guarantee calls on timer id already canceled have no - // effects and do not crash. Also canceling negative id should always result - // in no-op. - virtual void CancelTimer(long long id) = 0; - - virtual std::vector GetAllWindow() = 0; - virtual std::shared_ptr CreateWindow( - INativeWindow* parent) = 0; - - virtual cru::platform::graph::IGraphFactory* GetGraphFactory() = 0; - - virtual ICursorManager* GetCursorManager() = 0; - virtual IInputMethodManager* GetInputMethodManager() = 0; -}; - -// Bootstrap from this. -std::unique_ptr CreateUiApplication(); -} // namespace cru::platform::native diff --git a/include/cru/platform/native/window.hpp b/include/cru/platform/native/window.hpp index 57363a3b..1fcac1fc 100644 --- a/include/cru/platform/native/window.hpp +++ b/include/cru/platform/native/window.hpp @@ -1,7 +1,7 @@ #pragma once -#include "../resource.hpp" -#include "base.hpp" -#include "cru/common/event.hpp" +#include "../Resource.hpp" +#include "Base.hpp" +#include "cru/common/Event.hpp" #include diff --git a/include/cru/platform/resource.hpp b/include/cru/platform/resource.hpp index 6b315527..72cfaf52 100644 --- a/include/cru/platform/resource.hpp +++ b/include/cru/platform/resource.hpp @@ -1,5 +1,5 @@ #pragma once -#include "cru/common/base.hpp" +#include "cru/common/Base.hpp" #include diff --git a/include/cru/ui/ClickDetector.hpp b/include/cru/ui/ClickDetector.hpp new file mode 100644 index 00000000..f5fd3d8f --- /dev/null +++ b/include/cru/ui/ClickDetector.hpp @@ -0,0 +1,86 @@ +#pragma once +#include "Control.hpp" + + +namespace cru::ui { +class ClickEventArgs : Object { + public: + ClickEventArgs(Control* sender, const Point& down_point, + const Point& up_point, MouseButton button) + : sender_(sender), + down_point_(down_point), + up_point_(up_point), + button_(button) {} + + CRU_DEFAULT_COPY(ClickEventArgs) + CRU_DEFAULT_MOVE(ClickEventArgs) + + ~ClickEventArgs() override = default; + + Control* GetSender() const { return sender_; } + Point GetDownPoint() const { return down_point_; } + Point GetUpPoint() const { return up_point_; } + MouseButton GetButton() const { return button_; } + + private: + Control* sender_; + Point down_point_; + Point up_point_; + MouseButton button_; +}; + +enum class ClickState { + None, // Mouse is outside the control. + Hover, // Mouse hovers on the control but not pressed + Press, // Mouse is pressed and if released click is done. + PressInactive // Mouse is pressed but if released click is canceled. +}; + +class ClickDetector : public Object { + public: + explicit ClickDetector(Control* control); + + CRU_DELETE_COPY(ClickDetector) + CRU_DELETE_MOVE(ClickDetector) + + ~ClickDetector() override = default; + + Control* GetControl() const { return control_; } + + ClickState GetState() const { return state_; } + + // Default is enable. + bool IsEnabled() const { return enable_; } + // If disable when user is pressing, the pressing is deactivated. + void SetEnabled(bool enable); + + // Default is left and right. + MouseButton GetTriggerButton() const { return trigger_button_; } + // If unset the trigger button when user is pressing, the pressing is + // deactivated. + void SetTriggerButton(MouseButton trigger_button); + + IEvent* ClickEvent() { return &event_; } + + IEvent* StateChangeEvent() { return &state_change_event_; } + + private: + void SetState(ClickState state); + + private: + Control* control_; + + ClickState state_; + + bool enable_ = true; + MouseButton trigger_button_ = mouse_buttons::left | mouse_buttons::right; + + Event event_; + Event state_change_event_; + + std::vector event_rovoker_guards_; + + Point down_point_; + MouseButton button_; +}; +} // namespace cru::ui diff --git a/include/cru/ui/ContentControl.hpp b/include/cru/ui/ContentControl.hpp new file mode 100644 index 00000000..19f13a1d --- /dev/null +++ b/include/cru/ui/ContentControl.hpp @@ -0,0 +1,29 @@ +#pragma once +#include "Control.hpp" + +namespace cru::ui { +class ContentControl : public Control { + protected: + ContentControl(); + + public: + ContentControl(const ContentControl& other) = delete; + ContentControl(ContentControl&& other) = delete; + ContentControl& operator=(const ContentControl& other) = delete; + ContentControl& operator=(ContentControl&& other) = delete; + ~ContentControl() override; + + const std::vector& GetChildren() const override final { + return child_vector_; + } + Control* GetChild() const { return child_; } + void SetChild(Control* child); + + protected: + virtual void OnChildChanged(Control* old_child, Control* new_child); + + private: + std::vector child_vector_; + Control*& child_; +}; +} // namespace cru::ui diff --git a/include/cru/ui/LayoutControl.hpp b/include/cru/ui/LayoutControl.hpp new file mode 100644 index 00000000..7997b37e --- /dev/null +++ b/include/cru/ui/LayoutControl.hpp @@ -0,0 +1,31 @@ +#pragma once +#include "Control.hpp" + +namespace cru::ui { +class LayoutControl : public Control { + protected: + LayoutControl() = default; + + public: + LayoutControl(const LayoutControl& other) = delete; + LayoutControl(LayoutControl&& other) = delete; + LayoutControl& operator=(const LayoutControl& other) = delete; + LayoutControl& operator=(LayoutControl&& other) = delete; + ~LayoutControl() override; + + const std::vector& GetChildren() const override final { + return children_; + } + + void AddChild(Control* control, Index position); + + void RemoveChild(Index position); + + protected: + virtual void OnAddChild(Control* child, Index position); + virtual void OnRemoveChild(Control* child, Index position); + + private: + std::vector children_; +}; +} // namespace cru::ui diff --git a/include/cru/ui/NoChildControl.hpp b/include/cru/ui/NoChildControl.hpp new file mode 100644 index 00000000..1a31ae7e --- /dev/null +++ b/include/cru/ui/NoChildControl.hpp @@ -0,0 +1,24 @@ +#pragma once +#include "Control.hpp" + +namespace cru::ui { +class NoChildControl : public Control { + private: + static const std::vector empty_control_vector; + + protected: + NoChildControl() = default; + + public: + NoChildControl(const NoChildControl& other) = delete; + NoChildControl(NoChildControl&& other) = delete; + NoChildControl& operator=(const NoChildControl& other) = delete; + NoChildControl& operator=(NoChildControl&& other) = delete; + ~NoChildControl() override = default; + + protected: + const std::vector& GetChildren() const override final { + return empty_control_vector; + } +}; +} // namespace cru::ui diff --git a/include/cru/ui/UiEvent.hpp b/include/cru/ui/UiEvent.hpp new file mode 100644 index 00000000..29292d75 --- /dev/null +++ b/include/cru/ui/UiEvent.hpp @@ -0,0 +1,229 @@ +#pragma once +#include "Base.hpp" + +#include "cru/common/Event.hpp" +#include "cru/platform/native/Keyboard.hpp" + +#include +#include +#include +#include + +namespace cru::platform::graph { +struct IPainter; +} + +namespace cru::ui { +class Control; +} + +namespace cru::ui::event { +class UiEventArgs : public Object { + public: + UiEventArgs(Object* sender, Object* original_sender) + : sender_(sender), original_sender_(original_sender), handled_(false) {} + + UiEventArgs(const UiEventArgs& other) = default; + UiEventArgs(UiEventArgs&& other) = default; + UiEventArgs& operator=(const UiEventArgs& other) = default; + UiEventArgs& operator=(UiEventArgs&& other) = default; + ~UiEventArgs() override = default; + + Object* GetSender() const { return sender_; } + + Object* GetOriginalSender() const { return original_sender_; } + + bool IsHandled() const { return handled_; } + void SetHandled(const bool handled = true) { handled_ = handled; } + + private: + Object* sender_; + Object* original_sender_; + bool handled_; +}; + +// TEventArgs must not be a reference type. This class help add reference. +// EventArgs must be reference because the IsHandled property must be settable. +template +class RoutedEvent { + public: + static_assert(std::is_base_of_v, + "TEventArgs must be subclass of UiEventArgs."); + static_assert(!std::is_reference_v, + "TEventArgs must not be reference."); + + using EventArgs = TEventArgs; + + RoutedEvent() = default; + RoutedEvent(const RoutedEvent& other) = delete; + RoutedEvent(RoutedEvent&& other) = delete; + RoutedEvent& operator=(const RoutedEvent& other) = delete; + RoutedEvent& operator=(RoutedEvent&& other) = delete; + ~RoutedEvent() = default; + + IEvent* Direct() { return &direct_; } + + IEvent* Bubble() { return &bubble_; } + + IEvent* Tunnel() { return &tunnel_; } + + private: + Event direct_; + Event bubble_; + Event tunnel_; +}; + +class MouseEventArgs : public UiEventArgs { + public: + MouseEventArgs(Object* sender, Object* original_sender, + const std::optional& point = std::nullopt) + : UiEventArgs(sender, original_sender), point_(point) {} + MouseEventArgs(const MouseEventArgs& other) = default; + MouseEventArgs(MouseEventArgs&& other) = default; + MouseEventArgs& operator=(const MouseEventArgs& other) = default; + MouseEventArgs& operator=(MouseEventArgs&& other) = default; + ~MouseEventArgs() override = default; + + // This point is relative to window client lefttop. + Point GetPoint() const { return point_.value_or(Point{}); } + + private: + std::optional point_; +}; + +class MouseButtonEventArgs : public MouseEventArgs { + public: + MouseButtonEventArgs(Object* sender, Object* original_sender, + const Point& point, const MouseButton button, + platform::native::KeyModifier key_modifier) + : MouseEventArgs(sender, original_sender, point), + button_(button), + key_modifier_(key_modifier) {} + MouseButtonEventArgs(Object* sender, Object* original_sender, + const MouseButton button, + platform::native::KeyModifier key_modifier) + : MouseEventArgs(sender, original_sender), + button_(button), + key_modifier_(key_modifier) {} + MouseButtonEventArgs(const MouseButtonEventArgs& other) = default; + MouseButtonEventArgs(MouseButtonEventArgs&& other) = default; + MouseButtonEventArgs& operator=(const MouseButtonEventArgs& other) = default; + MouseButtonEventArgs& operator=(MouseButtonEventArgs&& other) = default; + ~MouseButtonEventArgs() override = default; + + MouseButton GetButton() const { return button_; } + platform::native::KeyModifier GetKeyModifier() const { return key_modifier_; } + + private: + MouseButton button_; + platform::native::KeyModifier key_modifier_; +}; + +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 PaintEventArgs : public UiEventArgs { + public: + PaintEventArgs(Object* sender, Object* original_sender, + platform::graph::IPainter* painter) + : UiEventArgs(sender, original_sender), painter_(painter) {} + PaintEventArgs(const PaintEventArgs& other) = default; + PaintEventArgs(PaintEventArgs&& other) = default; + PaintEventArgs& operator=(const PaintEventArgs& other) = default; + PaintEventArgs& operator=(PaintEventArgs&& other) = default; + ~PaintEventArgs() = default; + + platform::graph::IPainter* GetPainter() const { return painter_; } + + private: + platform::graph::IPainter* painter_; +}; + +class FocusChangeEventArgs : public UiEventArgs { + public: + FocusChangeEventArgs(Object* sender, Object* original_sender, + const bool is_window = false) + : UiEventArgs(sender, original_sender), is_window_(is_window) {} + FocusChangeEventArgs(const FocusChangeEventArgs& other) = default; + FocusChangeEventArgs(FocusChangeEventArgs&& other) = default; + FocusChangeEventArgs& operator=(const FocusChangeEventArgs& other) = default; + FocusChangeEventArgs& operator=(FocusChangeEventArgs&& other) = default; + ~FocusChangeEventArgs() override = default; + + // Return whether the focus change is caused by the window-wide focus change. + bool IsWindow() const { return is_window_; } + + private: + bool is_window_; +}; + +/* +class ToggleEventArgs : public UiEventArgs { + public: + ToggleEventArgs(Object* sender, Object* original_sender, bool new_state) + : UiEventArgs(sender, original_sender), new_state_(new_state) {} + ToggleEventArgs(const ToggleEventArgs& other) = default; + ToggleEventArgs(ToggleEventArgs&& other) = default; + ToggleEventArgs& operator=(const ToggleEventArgs& other) = default; + ToggleEventArgs& operator=(ToggleEventArgs&& other) = default; + ~ToggleEventArgs() override = default; + + bool GetNewState() const { return new_state_; } + + private: + bool new_state_; +}; +*/ + +class KeyEventArgs : public UiEventArgs { + public: + KeyEventArgs(Object* sender, Object* original_sender, + platform::native::KeyCode key_code, + platform::native::KeyModifier key_modifier) + : UiEventArgs(sender, original_sender), + key_code_(key_code), + key_modifier_(key_modifier) {} + KeyEventArgs(const KeyEventArgs& other) = default; + KeyEventArgs(KeyEventArgs&& other) = default; + KeyEventArgs& operator=(const KeyEventArgs& other) = default; + KeyEventArgs& operator=(KeyEventArgs&& other) = default; + ~KeyEventArgs() override = default; + + platform::native::KeyCode GetKeyCode() const { return key_code_; } + platform::native::KeyModifier GetKeyModifier() const { return key_modifier_; } + + private: + platform::native::KeyCode key_code_; + platform::native::KeyModifier key_modifier_; +}; + +class CharEventArgs : public UiEventArgs { + public: + CharEventArgs(Object* sender, Object* original_sender, std::string c) + : UiEventArgs(sender, original_sender), c_(std::move(c)) {} + CharEventArgs(const CharEventArgs& other) = default; + CharEventArgs(CharEventArgs&& other) = default; + CharEventArgs& operator=(const CharEventArgs& other) = default; + CharEventArgs& operator=(CharEventArgs&& other) = default; + ~CharEventArgs() override = default; + + std::string GetChar() const { return c_; } + + private: + std::string c_; +}; +} // namespace cru::ui::event diff --git a/include/cru/ui/UiHost.hpp b/include/cru/ui/UiHost.hpp new file mode 100644 index 00000000..651dab81 --- /dev/null +++ b/include/cru/ui/UiHost.hpp @@ -0,0 +1,170 @@ +#pragma once +#include "Base.hpp" + +#include "cru/common/Event.hpp" +#include "cru/common/SelfResolvable.hpp" +#include "render/Base.hpp" + +namespace cru::ui { +struct AfterLayoutEventArgs {}; + +// The host of all controls and render objects. +// +// 3 situations on destroy: +// 1. Native window destroyed, IsRetainAfterDestroy: false: +// OnNativeDestroy(set native_window_destroyed_ to true, call ~Window due to +// deleting_ is false and IsRetainAfterDestroy is false) -> ~Window -> +// ~UiHost(not destroy native window repeatedly due to native_window_destroyed_ +// is true) +// 2. Native window destroyed, IsRetainAfterDestroy: true: +// OnNativeDestroy(set native_window_destroyed_ to true, not call ~Window +// because deleting_ is false and IsRetainAfterDestroy is true) +// then, ~Window -> ~UiHost(not destroy native window repeatedly due to +// native_window_destroyed_ is true) +// 3. Native window not destroyed, ~Window is called: +// ~Window -> ~UiHost(set deleting_ to true, destroy native window +// due to native_window_destroyed is false) -> OnNativeDestroy(not call ~Window +// due to deleting_ is true and IsRetainAfterDestroy is whatever) +// In conclusion: +// 1. Set native_window_destroyed_ to true at the beginning of OnNativeDestroy. +// 2. Set deleting_ to true at the beginning of ~UiHost. +// 3. Destroy native window when native_window_destroy_ is false in ~Window. +// 4. Delete Window when deleting_ is false and IsRetainAfterDestroy is false in +// OnNativeDestroy. +class UiHost : public Object, public SelfResolvable { + public: + // This will create root window render object and attach it to window. + // It will also create and manage a native window. + UiHost(Window* window); + + CRU_DELETE_COPY(UiHost) + CRU_DELETE_MOVE(UiHost) + + ~UiHost() override; + + public: + // Mark the layout as invalid, and arrange a re-layout later. + // This method could be called more than one times in a message cycle. But + // layout only takes place once. + void InvalidateLayout(); + + // Mark the paint as invalid, and arrange a re-paint later. + // This method could be called more than one times in a message cycle. But + // paint only takes place once. + void InvalidatePaint(); + + IEvent* AfterLayoutEvent() { + return &after_layout_event_; + } + + void Relayout(); + + // Get current control that mouse hovers on. This ignores the mouse-capture + // control. Even when mouse is captured by another control, this function + // return the control under cursor. You can use `GetMouseCaptureControl` to + // get more info. + Control* GetMouseHoverControl() const { return mouse_hover_control_; } + + //*************** region: focus *************** + + // Request focus for specified control. + bool RequestFocusFor(Control* control); + + // Get the control that has focus. + Control* GetFocusControl(); + + //*************** region: focus *************** + + // Pass nullptr to release capture. If mouse is already capture by a control, + // this capture will fail and return false. If control is identical to the + // capturing control, capture is not changed and this function will return + // true. + // + // When capturing control changes, + // appropriate event will be sent. If mouse is not on the capturing control + // and capture is released, mouse enter event will be sent to the mouse-hover + // control. If mouse is not on the capturing control and capture is set, mouse + // leave event will be sent to the mouse-hover control. + bool CaptureMouseFor(Control* control); + + // Return null if not captured. + Control* GetMouseCaptureControl(); + + Control* HitTest(const Point& point); + + void UpdateCursor(); + + std::shared_ptr + GetNativeWindowResolver() { + return native_window_resolver_; + } + + bool IsRetainAfterDestroy() { return retain_after_destroy_; } + + void SetRetainAfterDestroy(bool destroy) { retain_after_destroy_ = destroy; } + + private: + //*************** region: native messages *************** + void OnNativeDestroy(platform::native::INativeWindow* window, std::nullptr_t); + void OnNativePaint(platform::native::INativeWindow* window, std::nullptr_t); + void OnNativeResize(platform::native::INativeWindow* window, + const Size& size); + + void OnNativeFocus(platform::native::INativeWindow* window, + cru::platform::native::FocusChangeType focus); + + void OnNativeMouseEnterLeave( + platform::native::INativeWindow* window, + cru::platform::native::MouseEnterLeaveType enter); + void OnNativeMouseMove(platform::native::INativeWindow* window, + const Point& point); + void OnNativeMouseDown( + platform::native::INativeWindow* window, + const platform::native::NativeMouseButtonEventArgs& args); + void OnNativeMouseUp( + platform::native::INativeWindow* window, + const platform::native::NativeMouseButtonEventArgs& args); + + void OnNativeKeyDown(platform::native::INativeWindow* window, + const platform::native::NativeKeyEventArgs& args); + void OnNativeKeyUp(platform::native::INativeWindow* window, + const platform::native::NativeKeyEventArgs& args); + + //*************** region: event dispatcher helper *************** + + void DispatchMouseHoverControlChangeEvent(Control* old_control, + Control* new_control, + const Point& point, bool no_leave, + bool no_enter); + + private: + bool need_layout_ = false; + + Event after_layout_event_; + + std::shared_ptr + native_window_resolver_; + + // See remarks of UiHost. + bool retain_after_destroy_ = false; + // See remarks of UiHost. + bool deleting_ = false; + + // We need this because calling Resolve on resolver in handler of destroy + // event is bad and will always get the dying window. But we need to label the + // window as destroyed so the destructor will not destroy native window + // repeatedly. See remarks of UiHost. + bool native_window_destroyed_ = false; + + std::vector event_revoker_guards_; + + Window* window_control_; + std::unique_ptr root_render_object_; + + Control* mouse_hover_control_; + + Control* focus_control_; // "focus_control_" can't be nullptr + + Control* mouse_captured_control_; +}; +} // namespace cru::ui diff --git a/include/cru/ui/UiManager.hpp b/include/cru/ui/UiManager.hpp new file mode 100644 index 00000000..e6facdbd --- /dev/null +++ b/include/cru/ui/UiManager.hpp @@ -0,0 +1,35 @@ +#pragma once +#include "Base.hpp" + +#include "controls/Base.hpp" + +namespace cru::ui { +struct ThemeResources { + std::shared_ptr default_font; + std::shared_ptr text_brush; + std::shared_ptr text_selection_brush; + std::shared_ptr caret_brush; + controls::ButtonStyle button_style; + controls::TextBoxBorderStyle text_box_border_style; +}; + +class UiManager : public Object { + public: + static UiManager* GetInstance(); + + private: + UiManager(); + + public: + UiManager(const UiManager& other) = delete; + UiManager(UiManager&& other) = delete; + UiManager& operator=(const UiManager& other) = delete; + UiManager& operator=(UiManager&& other) = delete; + ~UiManager() override; + + ThemeResources* GetThemeResources() { return &theme_resource_; } + + private: + ThemeResources theme_resource_; +}; +} // namespace cru::ui diff --git a/include/cru/ui/base.hpp b/include/cru/ui/base.hpp index a1335f90..97b0dbff 100644 --- a/include/cru/ui/base.hpp +++ b/include/cru/ui/base.hpp @@ -1,7 +1,7 @@ #pragma once -#include "cru/common/base.hpp" -#include "cru/platform/graph/base.hpp" -#include "cru/platform/native/base.hpp" +#include "cru/common/Base.hpp" +#include "cru/platform/graph/Base.hpp" +#include "cru/platform/native/Base.hpp" #include #include diff --git a/include/cru/ui/click_detector.hpp b/include/cru/ui/click_detector.hpp deleted file mode 100644 index 6c4761e7..00000000 --- a/include/cru/ui/click_detector.hpp +++ /dev/null @@ -1,86 +0,0 @@ -#pragma once -#include "control.hpp" - - -namespace cru::ui { -class ClickEventArgs : Object { - public: - ClickEventArgs(Control* sender, const Point& down_point, - const Point& up_point, MouseButton button) - : sender_(sender), - down_point_(down_point), - up_point_(up_point), - button_(button) {} - - CRU_DEFAULT_COPY(ClickEventArgs) - CRU_DEFAULT_MOVE(ClickEventArgs) - - ~ClickEventArgs() override = default; - - Control* GetSender() const { return sender_; } - Point GetDownPoint() const { return down_point_; } - Point GetUpPoint() const { return up_point_; } - MouseButton GetButton() const { return button_; } - - private: - Control* sender_; - Point down_point_; - Point up_point_; - MouseButton button_; -}; - -enum class ClickState { - None, // Mouse is outside the control. - Hover, // Mouse hovers on the control but not pressed - Press, // Mouse is pressed and if released click is done. - PressInactive // Mouse is pressed but if released click is canceled. -}; - -class ClickDetector : public Object { - public: - explicit ClickDetector(Control* control); - - CRU_DELETE_COPY(ClickDetector) - CRU_DELETE_MOVE(ClickDetector) - - ~ClickDetector() override = default; - - Control* GetControl() const { return control_; } - - ClickState GetState() const { return state_; } - - // Default is enable. - bool IsEnabled() const { return enable_; } - // If disable when user is pressing, the pressing is deactivated. - void SetEnabled(bool enable); - - // Default is left and right. - MouseButton GetTriggerButton() const { return trigger_button_; } - // If unset the trigger button when user is pressing, the pressing is - // deactivated. - void SetTriggerButton(MouseButton trigger_button); - - IEvent* ClickEvent() { return &event_; } - - IEvent* StateChangeEvent() { return &state_change_event_; } - - private: - void SetState(ClickState state); - - private: - Control* control_; - - ClickState state_; - - bool enable_ = true; - MouseButton trigger_button_ = mouse_buttons::left | mouse_buttons::right; - - Event event_; - Event state_change_event_; - - std::vector event_rovoker_guards_; - - Point down_point_; - MouseButton button_; -}; -} // namespace cru::ui diff --git a/include/cru/ui/content_control.hpp b/include/cru/ui/content_control.hpp deleted file mode 100644 index f88ec157..00000000 --- a/include/cru/ui/content_control.hpp +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once -#include "control.hpp" - -namespace cru::ui { -class ContentControl : public Control { - protected: - ContentControl(); - - public: - ContentControl(const ContentControl& other) = delete; - ContentControl(ContentControl&& other) = delete; - ContentControl& operator=(const ContentControl& other) = delete; - ContentControl& operator=(ContentControl&& other) = delete; - ~ContentControl() override; - - const std::vector& GetChildren() const override final { - return child_vector_; - } - Control* GetChild() const { return child_; } - void SetChild(Control* child); - - protected: - virtual void OnChildChanged(Control* old_child, Control* new_child); - - private: - std::vector child_vector_; - Control*& child_; -}; -} // namespace cru::ui diff --git a/include/cru/ui/control.hpp b/include/cru/ui/control.hpp index d66405e6..347163be 100644 --- a/include/cru/ui/control.hpp +++ b/include/cru/ui/control.hpp @@ -1,9 +1,9 @@ #pragma once -#include "base.hpp" +#include "Base.hpp" -#include "cru/common/event.hpp" -#include "render/base.hpp" -#include "ui_event.hpp" +#include "cru/common/Event.hpp" +#include "render/Base.hpp" +#include "UiEvent.hpp" #include diff --git a/include/cru/ui/controls/FlexLayout.hpp b/include/cru/ui/controls/FlexLayout.hpp new file mode 100644 index 00000000..beacd1f9 --- /dev/null +++ b/include/cru/ui/controls/FlexLayout.hpp @@ -0,0 +1,41 @@ +#pragma once +#include "../LayoutControl.hpp" + +namespace cru::ui::controls { +class FlexLayout : public LayoutControl { + public: + static constexpr std::string_view control_type = "FlexLayout"; + + static FlexLayout* Create() { return new FlexLayout(); } + + protected: + FlexLayout(); + + public: + FlexLayout(const FlexLayout& other) = delete; + FlexLayout(FlexLayout&& other) = delete; + FlexLayout& operator=(const FlexLayout& other) = delete; + FlexLayout& operator=(FlexLayout&& other) = delete; + ~FlexLayout() override; + + std::string_view GetControlType() const final { return control_type; } + + render::RenderObject* GetRenderObject() const override; + + FlexMainAlignment GetContentMainAlign() const; + void SetContentMainAlign(FlexMainAlignment value); + + FlexDirection GetFlexDirection() const; + void SetFlexDirection(FlexDirection direction); + + FlexChildLayoutData GetChildLayoutData(Control* control); + void SetChildLayoutData(Control* control, const FlexChildLayoutData& data); + + protected: + void OnAddChild(Control* child, Index position) override; + void OnRemoveChild(Control* child, Index position) override; + + private: + std::shared_ptr render_object_; +}; +} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/StackLayout.hpp b/include/cru/ui/controls/StackLayout.hpp new file mode 100644 index 00000000..d5998cc4 --- /dev/null +++ b/include/cru/ui/controls/StackLayout.hpp @@ -0,0 +1,31 @@ +#pragma once +#include "../LayoutControl.hpp" + +namespace cru::ui::controls { +class StackLayout : public LayoutControl { + public: + static constexpr std::string_view control_type = "StackLayout"; + + static StackLayout* Create() { return new StackLayout(); } + + protected: + StackLayout(); + + public: + CRU_DELETE_COPY(StackLayout) + CRU_DELETE_MOVE(StackLayout) + + ~StackLayout() override; + + std::string_view GetControlType() const final { return control_type; } + + render::RenderObject* GetRenderObject() const override; + + protected: + void OnAddChild(Control* child, Index position) override; + void OnRemoveChild(Control* child, Index position) override; + + private: + std::shared_ptr render_object_; +}; +} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/TextBlock.hpp b/include/cru/ui/controls/TextBlock.hpp new file mode 100644 index 00000000..dd8b40b4 --- /dev/null +++ b/include/cru/ui/controls/TextBlock.hpp @@ -0,0 +1,38 @@ +#pragma once +#include "../NoChildControl.hpp" + +namespace cru::ui::controls { +template +class TextControlService; + +class TextBlock : public NoChildControl { + public: + static constexpr std::string_view control_type = "TextBlock"; + + static TextBlock* Create() { return new TextBlock(); } + + protected: + TextBlock(); + + public: + TextBlock(const TextBlock& other) = delete; + TextBlock(TextBlock&& other) = delete; + TextBlock& operator=(const TextBlock& other) = delete; + TextBlock& operator=(TextBlock&& other) = delete; + ~TextBlock() override; + + std::string_view GetControlType() const final { return control_type; } + + render::RenderObject* GetRenderObject() const override; + + std::string GetText() const; + void SetText(std::string text); + + render::TextRenderObject* GetTextRenderObject(); + + private: + std::unique_ptr text_render_object_; + + std::unique_ptr> service_; +}; +} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/TextBox.hpp b/include/cru/ui/controls/TextBox.hpp new file mode 100644 index 00000000..2f7a12b6 --- /dev/null +++ b/include/cru/ui/controls/TextBox.hpp @@ -0,0 +1,49 @@ +#pragma once +#include "../NoChildControl.hpp" +#include "Base.hpp" + +#include + +namespace cru::ui::controls { +template +class TextControlService; + +class TextBox : public NoChildControl { + public: + static constexpr std::string_view control_type = "TextBox"; + + static TextBox* Create() { return new TextBox(); } + + protected: + TextBox(); + + public: + CRU_DELETE_COPY(TextBox) + CRU_DELETE_MOVE(TextBox) + + ~TextBox() override; + + std::string_view GetControlType() const final { return control_type; } + + render::RenderObject* GetRenderObject() const override; + + render::TextRenderObject* GetTextRenderObject(); + + const TextBoxBorderStyle& GetBorderStyle(); + void SetBorderStyle(TextBoxBorderStyle border_style); + + protected: + void OnMouseHoverChange(bool newHover) override; + + private: + void UpdateBorderStyle(); + + private: + std::unique_ptr border_render_object_; + std::unique_ptr text_render_object_; + + TextBoxBorderStyle border_style_; + + std::unique_ptr> service_; +}; +} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/base.hpp b/include/cru/ui/controls/base.hpp index ebe9cdda..b550601b 100644 --- a/include/cru/ui/controls/base.hpp +++ b/include/cru/ui/controls/base.hpp @@ -1,5 +1,5 @@ #pragma once -#include "../base.hpp" +#include "../Base.hpp" namespace cru::ui::controls { using ButtonStateStyle = BorderStyle; diff --git a/include/cru/ui/controls/button.hpp b/include/cru/ui/controls/button.hpp index fb636a33..8a11409c 100644 --- a/include/cru/ui/controls/button.hpp +++ b/include/cru/ui/controls/button.hpp @@ -1,8 +1,8 @@ #pragma once -#include "../content_control.hpp" -#include "base.hpp" +#include "../ContentControl.hpp" +#include "Base.hpp" -#include "../click_detector.hpp" +#include "../ClickDetector.hpp" namespace cru::ui::controls { class Button : public ContentControl { diff --git a/include/cru/ui/controls/container.hpp b/include/cru/ui/controls/container.hpp index 7d4c0d23..e3d78365 100644 --- a/include/cru/ui/controls/container.hpp +++ b/include/cru/ui/controls/container.hpp @@ -1,5 +1,5 @@ #pragma once -#include "../content_control.hpp" +#include "../ContentControl.hpp" namespace cru::ui::controls { class Container : public ContentControl { diff --git a/include/cru/ui/controls/flex_layout.hpp b/include/cru/ui/controls/flex_layout.hpp deleted file mode 100644 index ab08a80b..00000000 --- a/include/cru/ui/controls/flex_layout.hpp +++ /dev/null @@ -1,41 +0,0 @@ -#pragma once -#include "../layout_control.hpp" - -namespace cru::ui::controls { -class FlexLayout : public LayoutControl { - public: - static constexpr std::string_view control_type = "FlexLayout"; - - static FlexLayout* Create() { return new FlexLayout(); } - - protected: - FlexLayout(); - - public: - FlexLayout(const FlexLayout& other) = delete; - FlexLayout(FlexLayout&& other) = delete; - FlexLayout& operator=(const FlexLayout& other) = delete; - FlexLayout& operator=(FlexLayout&& other) = delete; - ~FlexLayout() override; - - std::string_view GetControlType() const final { return control_type; } - - render::RenderObject* GetRenderObject() const override; - - FlexMainAlignment GetContentMainAlign() const; - void SetContentMainAlign(FlexMainAlignment value); - - FlexDirection GetFlexDirection() const; - void SetFlexDirection(FlexDirection direction); - - FlexChildLayoutData GetChildLayoutData(Control* control); - void SetChildLayoutData(Control* control, const FlexChildLayoutData& data); - - protected: - void OnAddChild(Control* child, Index position) override; - void OnRemoveChild(Control* child, Index position) override; - - private: - std::shared_ptr render_object_; -}; -} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/stack_layout.hpp b/include/cru/ui/controls/stack_layout.hpp deleted file mode 100644 index 20da0e82..00000000 --- a/include/cru/ui/controls/stack_layout.hpp +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once -#include "../layout_control.hpp" - -namespace cru::ui::controls { -class StackLayout : public LayoutControl { - public: - static constexpr std::string_view control_type = "StackLayout"; - - static StackLayout* Create() { return new StackLayout(); } - - protected: - StackLayout(); - - public: - CRU_DELETE_COPY(StackLayout) - CRU_DELETE_MOVE(StackLayout) - - ~StackLayout() override; - - std::string_view GetControlType() const final { return control_type; } - - render::RenderObject* GetRenderObject() const override; - - protected: - void OnAddChild(Control* child, Index position) override; - void OnRemoveChild(Control* child, Index position) override; - - private: - std::shared_ptr render_object_; -}; -} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/text_block.hpp b/include/cru/ui/controls/text_block.hpp deleted file mode 100644 index 61f568c4..00000000 --- a/include/cru/ui/controls/text_block.hpp +++ /dev/null @@ -1,38 +0,0 @@ -#pragma once -#include "../no_child_control.hpp" - -namespace cru::ui::controls { -template -class TextControlService; - -class TextBlock : public NoChildControl { - public: - static constexpr std::string_view control_type = "TextBlock"; - - static TextBlock* Create() { return new TextBlock(); } - - protected: - TextBlock(); - - public: - TextBlock(const TextBlock& other) = delete; - TextBlock(TextBlock&& other) = delete; - TextBlock& operator=(const TextBlock& other) = delete; - TextBlock& operator=(TextBlock&& other) = delete; - ~TextBlock() override; - - std::string_view GetControlType() const final { return control_type; } - - render::RenderObject* GetRenderObject() const override; - - std::string GetText() const; - void SetText(std::string text); - - render::TextRenderObject* GetTextRenderObject(); - - private: - std::unique_ptr text_render_object_; - - std::unique_ptr> service_; -}; -} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/text_box.hpp b/include/cru/ui/controls/text_box.hpp deleted file mode 100644 index 15fcb734..00000000 --- a/include/cru/ui/controls/text_box.hpp +++ /dev/null @@ -1,49 +0,0 @@ -#pragma once -#include "../no_child_control.hpp" -#include "base.hpp" - -#include - -namespace cru::ui::controls { -template -class TextControlService; - -class TextBox : public NoChildControl { - public: - static constexpr std::string_view control_type = "TextBox"; - - static TextBox* Create() { return new TextBox(); } - - protected: - TextBox(); - - public: - CRU_DELETE_COPY(TextBox) - CRU_DELETE_MOVE(TextBox) - - ~TextBox() override; - - std::string_view GetControlType() const final { return control_type; } - - render::RenderObject* GetRenderObject() const override; - - render::TextRenderObject* GetTextRenderObject(); - - const TextBoxBorderStyle& GetBorderStyle(); - void SetBorderStyle(TextBoxBorderStyle border_style); - - protected: - void OnMouseHoverChange(bool newHover) override; - - private: - void UpdateBorderStyle(); - - private: - std::unique_ptr border_render_object_; - std::unique_ptr text_render_object_; - - TextBoxBorderStyle border_style_; - - std::unique_ptr> service_; -}; -} // namespace cru::ui::controls diff --git a/include/cru/ui/layout_control.hpp b/include/cru/ui/layout_control.hpp deleted file mode 100644 index e1856403..00000000 --- a/include/cru/ui/layout_control.hpp +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once -#include "control.hpp" - -namespace cru::ui { -class LayoutControl : public Control { - protected: - LayoutControl() = default; - - public: - LayoutControl(const LayoutControl& other) = delete; - LayoutControl(LayoutControl&& other) = delete; - LayoutControl& operator=(const LayoutControl& other) = delete; - LayoutControl& operator=(LayoutControl&& other) = delete; - ~LayoutControl() override; - - const std::vector& GetChildren() const override final { - return children_; - } - - void AddChild(Control* control, Index position); - - void RemoveChild(Index position); - - protected: - virtual void OnAddChild(Control* child, Index position); - virtual void OnRemoveChild(Control* child, Index position); - - private: - std::vector children_; -}; -} // namespace cru::ui diff --git a/include/cru/ui/no_child_control.hpp b/include/cru/ui/no_child_control.hpp deleted file mode 100644 index 62a9fa8d..00000000 --- a/include/cru/ui/no_child_control.hpp +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once -#include "control.hpp" - -namespace cru::ui { -class NoChildControl : public Control { - private: - static const std::vector empty_control_vector; - - protected: - NoChildControl() = default; - - public: - NoChildControl(const NoChildControl& other) = delete; - NoChildControl(NoChildControl&& other) = delete; - NoChildControl& operator=(const NoChildControl& other) = delete; - NoChildControl& operator=(NoChildControl&& other) = delete; - ~NoChildControl() override = default; - - protected: - const std::vector& GetChildren() const override final { - return empty_control_vector; - } -}; -} // namespace cru::ui diff --git a/include/cru/ui/render/BorderRenderObject.hpp b/include/cru/ui/render/BorderRenderObject.hpp new file mode 100644 index 00000000..c3031f59 --- /dev/null +++ b/include/cru/ui/render/BorderRenderObject.hpp @@ -0,0 +1,102 @@ +#pragma once +#include "RenderObject.hpp" + +namespace cru::ui::render { +class BorderRenderObject : public RenderObject { + public: + BorderRenderObject(); + BorderRenderObject(const BorderRenderObject& other) = delete; + BorderRenderObject(BorderRenderObject&& other) = delete; + BorderRenderObject& operator=(const BorderRenderObject& other) = delete; + BorderRenderObject& operator=(BorderRenderObject&& other) = delete; + ~BorderRenderObject() override; + + bool IsBorderEnabled() const { return is_border_enabled_; } + void SetBorderEnabled(bool enabled) { is_border_enabled_ = enabled; } + + std::shared_ptr GetBorderBrush() { + return border_brush_; + } + + void SetBorderBrush(std::shared_ptr brush) { + if (brush == border_brush_) return; + border_brush_ = std::move(brush); + InvalidatePaint(); + } + + Thickness GetBorderThickness() { return border_thickness_; } + + void SetBorderThickness(const Thickness thickness) { + if (thickness == border_thickness_) return; + border_thickness_ = thickness; + InvalidateLayout(); + } + + CornerRadius GetBorderRadius() { return border_radius_; } + + void SetBorderRadius(const CornerRadius radius) { + if (radius == border_radius_) return; + border_radius_ = radius; + RecreateGeometry(); + } + + std::shared_ptr GetForegroundBrush() { + return foreground_brush_; + } + + void SetForegroundBrush(std::shared_ptr brush) { + if (brush == foreground_brush_) return; + foreground_brush_ = std::move(brush); + InvalidatePaint(); + } + + std::shared_ptr GetBackgroundBrush() { + return background_brush_; + } + + void SetBackgroundBrush(std::shared_ptr brush) { + if (brush == background_brush_) return; + background_brush_ = std::move(brush); + InvalidatePaint(); + } + + void SetBorderStyle(const BorderStyle& style); + + void Draw(platform::graph::IPainter* painter) override; + + RenderObject* HitTest(const Point& point) override; + + protected: + void OnMeasureCore(const Size& available_size) override; + void OnLayoutCore(const Rect& rect) override; + Size OnMeasureContent(const Size& available_size) override; + void OnLayoutContent(const Rect& content_rect) override; + + void OnAfterLayout() override; + + private: + RenderObject* GetChild() const { + return GetChildren().empty() ? nullptr : GetChildren()[0]; + } + + void RecreateGeometry(); + + private: + bool is_border_enabled_ = false; + + std::shared_ptr border_brush_; + platform::Thickness border_thickness_; + CornerRadius border_radius_; + + std::shared_ptr foreground_brush_; + std::shared_ptr background_brush_; + + // The ring. Used for painting. + std::unique_ptr geometry_; + // Area including inner area of the border. Used for painting foreground and + // background. + std::unique_ptr border_inner_geometry_; + // Area including border ring and inner area. Used for hit test. + std::unique_ptr border_outer_geometry_; +}; +} // namespace cru::ui::render diff --git a/include/cru/ui/render/CanvasRenderObject.hpp b/include/cru/ui/render/CanvasRenderObject.hpp new file mode 100644 index 00000000..ba50a985 --- /dev/null +++ b/include/cru/ui/render/CanvasRenderObject.hpp @@ -0,0 +1,39 @@ +#pragma once +#include "RenderObject.hpp" + +namespace cru::ui::render { +// 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`). +class CanvasRenderObject : public RenderObject { + public: + CanvasRenderObject(); + + CRU_DELETE_COPY(CanvasRenderObject) + CRU_DELETE_MOVE(CanvasRenderObject) + + ~CanvasRenderObject(); + + public: + void Draw(platform::graph::IPainter* painter) override; + + RenderObject* HitTest(const Point& point) override; + + Size GetDesiredSize() const { return desired_size_; } + + // Set the desired size. This is the content size excluding padding and + // margin. + void SetDesiredSize(const Size& new_size) { desired_size_ = new_size; } + + IEvent* PaintEvent() { return &paint_event_; } + + protected: + Size OnMeasureContent(const Size& available_size) override; + void OnLayoutContent(const Rect& content_rect) override; + + private: + Size desired_size_{}; + + Event paint_event_; +}; +} // namespace cru::ui::render diff --git a/include/cru/ui/render/FlexLayoutRenderObject.hpp b/include/cru/ui/render/FlexLayoutRenderObject.hpp new file mode 100644 index 00000000..bc43141d --- /dev/null +++ b/include/cru/ui/render/FlexLayoutRenderObject.hpp @@ -0,0 +1,42 @@ +#pragma once +#include "LayoutRenderObject.hpp" + +namespace cru::ui::render { +class FlexLayoutRenderObject : public LayoutRenderObject { + public: + FlexLayoutRenderObject() = default; + FlexLayoutRenderObject(const FlexLayoutRenderObject& other) = delete; + FlexLayoutRenderObject& operator=(const FlexLayoutRenderObject& other) = + delete; + FlexLayoutRenderObject(FlexLayoutRenderObject&& other) = delete; + FlexLayoutRenderObject& operator=(FlexLayoutRenderObject&& other) = delete; + ~FlexLayoutRenderObject() override = default; + + FlexDirection GetFlexDirection() const { return direction_; } + void SetFlexDirection(FlexDirection direction) { + direction_ = direction; + InvalidateLayout(); + } + + FlexMainAlignment GetContentMainAlign() const { return content_main_align_; } + void SetContentMainAlign(FlexMainAlignment align) { + content_main_align_ = align; + InvalidateLayout(); + } + + FlexCrossAlignment GetItemCrossAlign() const { return item_cross_align_; } + void SetItemCrossAlign(FlexCrossAlignment align) { + item_cross_align_ = align; + InvalidateLayout(); + } + + protected: + Size OnMeasureContent(const Size& available_size) override; + void OnLayoutContent(const Rect& content_rect) override; + + private: + FlexDirection direction_ = FlexDirection::Horizontal; + FlexMainAlignment content_main_align_ = FlexMainAlignment::Start; + FlexCrossAlignment item_cross_align_ = FlexCrossAlignment::Center; +}; +} // namespace cru::ui::render diff --git a/include/cru/ui/render/LayoutRenderObject.hpp b/include/cru/ui/render/LayoutRenderObject.hpp new file mode 100644 index 00000000..e6ca9dc0 --- /dev/null +++ b/include/cru/ui/render/LayoutRenderObject.hpp @@ -0,0 +1,88 @@ +#pragma once +#include "RenderObject.hpp" + +#include "cru/platform/graph/util/Painter.hpp" + +namespace cru::ui::render { +template +class LayoutRenderObject : public RenderObject { + public: + using ChildLayoutData = TChildLayoutData; + + protected: + LayoutRenderObject() : RenderObject(ChildMode::Multiple) {} + + public: + CRU_DELETE_COPY(LayoutRenderObject) + CRU_DELETE_MOVE(LayoutRenderObject) + + ~LayoutRenderObject() override = default; + + ChildLayoutData* GetChildLayoutData(Index position) { + Expects(position >= 0 && + position < static_cast(child_layout_data_.size())); + return &child_layout_data_[position]; + } + + void Draw(platform::graph::IPainter* painter) override; + + RenderObject* HitTest(const Point& point) override; + + protected: + void OnAddChild(RenderObject* new_child, Index position) override; + void OnRemoveChild(RenderObject* removed_child, Index position) override; + + private: + std::vector child_layout_data_{}; +}; + +template +void LayoutRenderObject::Draw( + platform::graph::IPainter* painter) { + for (const auto child : GetChildren()) { + auto offset = child->GetOffset(); + platform::graph::util::WithTransform( + painter, platform::Matrix::Translation(offset.x, offset.y), + [child](auto p) { child->Draw(p); }); + } +} + +template +RenderObject* LayoutRenderObject::HitTest( + const Point& point) { + const auto& children = GetChildren(); + for (auto i = children.crbegin(); i != children.crend(); ++i) { + auto offset = (*i)->GetOffset(); + Point p{point.x - offset.x, point.y - offset.y}; + const auto result = (*i)->HitTest(p); + if (result != nullptr) { + return result; + } + } + + const auto margin = GetMargin(); + const auto size = GetSize(); + return Rect{margin.left, margin.top, + std::max(size.width - margin.GetHorizontalTotal(), 0.0f), + std::max(size.height - margin.GetVerticalTotal(), 0.0f)} + .IsPointInside(point) + ? this + : nullptr; +} // namespace cru::ui::render + +template +void LayoutRenderObject::OnAddChild(RenderObject* new_child, + const Index position) { + CRU_UNUSED(new_child) + + child_layout_data_.emplace(child_layout_data_.cbegin() + position); +} + +template +void LayoutRenderObject::OnRemoveChild( + RenderObject* removed_child, const Index position) { + CRU_UNUSED(removed_child) + + child_layout_data_.erase(child_layout_data_.cbegin() + position); +} +} // namespace cru::ui::render diff --git a/include/cru/ui/render/LayoutUtility.hpp b/include/cru/ui/render/LayoutUtility.hpp new file mode 100644 index 00000000..63d13fd3 --- /dev/null +++ b/include/cru/ui/render/LayoutUtility.hpp @@ -0,0 +1,7 @@ +#pragma once +#include "Base.hpp" + +namespace cru::ui::render { +Size Min(const Size& left, const Size& right); +Size Max(const Size& left, const Size& right); +} // namespace cru::ui::render diff --git a/include/cru/ui/render/RenderObject.hpp b/include/cru/ui/render/RenderObject.hpp new file mode 100644 index 00000000..7cfa3883 --- /dev/null +++ b/include/cru/ui/render/RenderObject.hpp @@ -0,0 +1,118 @@ +#pragma once +#include "Base.hpp" + +#include "cru/common/Event.hpp" + +namespace cru::ui::render { +// Render object will not destroy its children when destroyed. Control must +// manage lifecycle of its render objects. Since control will destroy its +// children when destroyed, render objects will be destroyed along with it. +class RenderObject : public Object { + friend WindowRenderObject; + + protected: + enum class ChildMode { + None, + Single, + Multiple, + }; + + RenderObject() = default; + RenderObject(ChildMode child_mode) : RenderObject() { + SetChildMode(child_mode); + } + + public: + RenderObject(const RenderObject& other) = delete; + RenderObject(RenderObject&& other) = delete; + RenderObject& operator=(const RenderObject& other) = delete; + RenderObject& operator=(RenderObject&& other) = delete; + ~RenderObject() override = default; + + Control* GetAttachedControl() const { return control_; } + void SetAttachedControl(Control* new_control) { control_ = new_control; } + + UiHost* GetUiHost() const { return ui_host_; } + + RenderObject* GetParent() const { return parent_; } + + const std::vector& GetChildren() const { return children_; } + Index GetChildCount() const { return static_cast(children_.size()); } + void AddChild(RenderObject* render_object, Index position); + void RemoveChild(Index position); + + Point GetOffset() const { return offset_; } + void SetOffset(const Point& offset) { offset_ = offset; } + Point GetTotalOffset() const; + Point FromRootToContent(const Point& point) const; + Size GetSize() const { return size_; } + void SetSize(const Size& size) { size_ = size; } + + Thickness GetMargin() const { return margin_; } + void SetMargin(const Thickness& margin) { margin_ = margin; } + + Thickness GetPadding() const { return padding_; } + void SetPadding(const Thickness& padding) { padding_ = padding; } + + Size GetPreferredSize() const { return preferred_size_; } + void SetPreferredSize(const Size& preferred_size) { + preferred_size_ = preferred_size; + } + + void Measure(const Size& available_size); + void Layout(const Rect& rect); + + virtual void Draw(platform::graph::IPainter* painter) = 0; + + virtual RenderObject* HitTest(const Point& point) = 0; + + public: + void InvalidateLayout(); + void InvalidatePaint(); + + protected: + void SetChildMode(ChildMode mode) { child_mode_ = mode; } + + protected: + virtual void OnParentChanged(RenderObject* old_parent, + RenderObject* new_parent); + + // default is to invalidate both layout and paint + virtual void OnAddChild(RenderObject* new_child, Index position); + // default is to invalidate both layout and paint + virtual void OnRemoveChild(RenderObject* removed_child, Index position); + + virtual void OnMeasureCore(const Size& available_size); + virtual void OnLayoutCore(const Rect& rect); + virtual Size OnMeasureContent(const Size& available_size) = 0; + virtual void OnLayoutContent(const Rect& content_rect) = 0; + + virtual void OnAfterLayout(); + static void NotifyAfterLayoutRecursive(RenderObject* render_object); + + Rect GetPaddingRect() const; + Rect GetContentRect() const; + + private: + void SetParent(RenderObject* new_parent); + + void SetRenderHostRecursive(UiHost* host); + + private: + Control* control_ = nullptr; + UiHost* ui_host_ = nullptr; + + RenderObject* parent_ = nullptr; + std::vector children_{}; + + ChildMode child_mode_ = ChildMode::None; + + Point offset_{}; + Size size_{}; + + Thickness margin_{}; + Thickness padding_{}; + + Size preferred_size_{}; +}; +} // namespace cru::ui::render diff --git a/include/cru/ui/render/ScrollRenderObject.hpp b/include/cru/ui/render/ScrollRenderObject.hpp new file mode 100644 index 00000000..dcf6dae6 --- /dev/null +++ b/include/cru/ui/render/ScrollRenderObject.hpp @@ -0,0 +1,30 @@ +#pragma once +#include "RenderObject.hpp" + +#include "cru/platform/graph/util/Painter.hpp" + +namespace cru::ui::render { +class ScrollRenderObject : public RenderObject { + public: + ScrollRenderObject() : RenderObject(ChildMode::Single) {} + + CRU_DELETE_COPY(ScrollRenderObject) + CRU_DELETE_MOVE(ScrollRenderObject) + + ~ScrollRenderObject() override = default; + + void Draw(platform::graph::IPainter* painter) override; + + RenderObject* HitTest(const Point& point) override; + + Point GetScrollOffset() { return scroll_offset_; } + void SetScrollOffset(const Point& offset); + + protected: + void OnAddChild(RenderObject* new_child, Index position) override; + void OnRemoveChild(RenderObject* removed_child, Index position) override; + + private: + Point scroll_offset_; +}; +} // namespace cru::ui::render diff --git a/include/cru/ui/render/StackLayoutRenderObject.hpp b/include/cru/ui/render/StackLayoutRenderObject.hpp new file mode 100644 index 00000000..a5bf9335 --- /dev/null +++ b/include/cru/ui/render/StackLayoutRenderObject.hpp @@ -0,0 +1,17 @@ +#pragma once +#include "LayoutRenderObject.hpp" + +namespace cru::ui::render { +class StackLayoutRenderObject + : public LayoutRenderObject { + public: + StackLayoutRenderObject() = default; + CRU_DELETE_COPY(StackLayoutRenderObject) + CRU_DELETE_MOVE(StackLayoutRenderObject) + ~StackLayoutRenderObject() = default; + + protected: + Size OnMeasureContent(const Size& available_size) override; + void OnLayoutContent(const Rect& content_rect) override; +}; +} // namespace cru::ui::render diff --git a/include/cru/ui/render/TextRenderObject.hpp b/include/cru/ui/render/TextRenderObject.hpp new file mode 100644 index 00000000..7a81ba51 --- /dev/null +++ b/include/cru/ui/render/TextRenderObject.hpp @@ -0,0 +1,85 @@ +#pragma once +#include "RenderObject.hpp" + +#include + +namespace cru::ui::render { +class TextRenderObject : public RenderObject { + public: + constexpr static float default_caret_width = 2; + + public: + TextRenderObject(std::shared_ptr brush, + std::shared_ptr font, + std::shared_ptr selection_brush, + std::shared_ptr caret_brush); + TextRenderObject(const TextRenderObject& other) = delete; + TextRenderObject(TextRenderObject&& other) = delete; + TextRenderObject& operator=(const TextRenderObject& other) = delete; + TextRenderObject& operator=(TextRenderObject&& other) = delete; + ~TextRenderObject() override; + + std::string GetText() const; + void SetText(std::string new_text); + + std::shared_ptr GetBrush() const { return brush_; } + void SetBrush(std::shared_ptr new_brush); + + std::shared_ptr GetFont() const; + void SetFont(std::shared_ptr font); + + std::vector TextRangeRect(const TextRange& text_range); + Point TextSinglePoint(gsl::index position, bool trailing); + platform::graph::TextHitTestResult TextHitTest(const Point& point); + + std::optional GetSelectionRange() const { + return selection_range_; + } + void SetSelectionRange(std::optional new_range); + + std::shared_ptr GetSelectionBrush() const { + return selection_brush_; + } + void SetSelectionBrush(std::shared_ptr new_brush); + + bool IsDrawCaret() const { return draw_caret_; } + void SetDrawCaret(bool draw_caret); + void ToggleDrawCaret() { SetDrawCaret(!IsDrawCaret()); } + + // Caret position can be any value. When it is negative, 0 is used. When it + // exceeds the size of the string, the size of the string is used. + gsl::index GetCaretPosition() const { return caret_position_; } + void SetCaretPosition(gsl::index position); + + std::shared_ptr GetCaretBrush() const { + return caret_brush_; + } + void GetCaretBrush(std::shared_ptr brush); + + float GetCaretWidth() const { return caret_width_; } + void SetCaretWidth(float width); + + void Draw(platform::graph::IPainter* painter) override; + + RenderObject* HitTest(const Point& point) override; + + protected: + Size OnMeasureContent(const Size& available_size) override; + void OnLayoutContent(const Rect& content_rect) override; + + void OnAfterLayout() override; + + private: + std::shared_ptr brush_; + std::shared_ptr font_; + std::unique_ptr text_layout_; + + std::optional selection_range_ = std::nullopt; + std::shared_ptr selection_brush_; + + bool draw_caret_ = false; + gsl::index caret_position_ = 0; + std::shared_ptr caret_brush_; + float caret_width_ = default_caret_width; +}; +} // namespace cru::ui::render diff --git a/include/cru/ui/render/WindowRenderObject.hpp b/include/cru/ui/render/WindowRenderObject.hpp new file mode 100644 index 00000000..00bce29b --- /dev/null +++ b/include/cru/ui/render/WindowRenderObject.hpp @@ -0,0 +1,30 @@ +#pragma once +#include "RenderObject.hpp" + +namespace cru::ui::render { +class WindowRenderObject : public RenderObject { + public: + WindowRenderObject(UiHost* host); + WindowRenderObject(const WindowRenderObject& other) = delete; + WindowRenderObject(WindowRenderObject&& other) = delete; + WindowRenderObject& operator=(const WindowRenderObject& other) = delete; + WindowRenderObject& operator=(WindowRenderObject&& other) = delete; + ~WindowRenderObject() override = default; + + void Draw(platform::graph::IPainter* painter) override; + + RenderObject* HitTest(const Point& point) override; + + protected: + Size OnMeasureContent(const Size& available_size) override; + void OnLayoutContent(const Rect& content_rect) override; + + private: + RenderObject* GetChild() const { + return GetChildren().empty() ? nullptr : GetChildren()[0]; + } + + private: + EventRevokerGuard after_layout_event_guard_; +}; +} // namespace cru::ui::render diff --git a/include/cru/ui/render/base.hpp b/include/cru/ui/render/base.hpp index f9d936e0..c2af5e99 100644 --- a/include/cru/ui/render/base.hpp +++ b/include/cru/ui/render/base.hpp @@ -1,5 +1,5 @@ #pragma once -#include "../base.hpp" +#include "../Base.hpp" namespace cru::ui::render { class RenderObject; diff --git a/include/cru/ui/render/border_render_object.hpp b/include/cru/ui/render/border_render_object.hpp deleted file mode 100644 index 02672309..00000000 --- a/include/cru/ui/render/border_render_object.hpp +++ /dev/null @@ -1,102 +0,0 @@ -#pragma once -#include "render_object.hpp" - -namespace cru::ui::render { -class BorderRenderObject : public RenderObject { - public: - BorderRenderObject(); - BorderRenderObject(const BorderRenderObject& other) = delete; - BorderRenderObject(BorderRenderObject&& other) = delete; - BorderRenderObject& operator=(const BorderRenderObject& other) = delete; - BorderRenderObject& operator=(BorderRenderObject&& other) = delete; - ~BorderRenderObject() override; - - bool IsBorderEnabled() const { return is_border_enabled_; } - void SetBorderEnabled(bool enabled) { is_border_enabled_ = enabled; } - - std::shared_ptr GetBorderBrush() { - return border_brush_; - } - - void SetBorderBrush(std::shared_ptr brush) { - if (brush == border_brush_) return; - border_brush_ = std::move(brush); - InvalidatePaint(); - } - - Thickness GetBorderThickness() { return border_thickness_; } - - void SetBorderThickness(const Thickness thickness) { - if (thickness == border_thickness_) return; - border_thickness_ = thickness; - InvalidateLayout(); - } - - CornerRadius GetBorderRadius() { return border_radius_; } - - void SetBorderRadius(const CornerRadius radius) { - if (radius == border_radius_) return; - border_radius_ = radius; - RecreateGeometry(); - } - - std::shared_ptr GetForegroundBrush() { - return foreground_brush_; - } - - void SetForegroundBrush(std::shared_ptr brush) { - if (brush == foreground_brush_) return; - foreground_brush_ = std::move(brush); - InvalidatePaint(); - } - - std::shared_ptr GetBackgroundBrush() { - return background_brush_; - } - - void SetBackgroundBrush(std::shared_ptr brush) { - if (brush == background_brush_) return; - background_brush_ = std::move(brush); - InvalidatePaint(); - } - - void SetBorderStyle(const BorderStyle& style); - - void Draw(platform::graph::IPainter* painter) override; - - RenderObject* HitTest(const Point& point) override; - - protected: - void OnMeasureCore(const Size& available_size) override; - void OnLayoutCore(const Rect& rect) override; - Size OnMeasureContent(const Size& available_size) override; - void OnLayoutContent(const Rect& content_rect) override; - - void OnAfterLayout() override; - - private: - RenderObject* GetChild() const { - return GetChildren().empty() ? nullptr : GetChildren()[0]; - } - - void RecreateGeometry(); - - private: - bool is_border_enabled_ = false; - - std::shared_ptr border_brush_; - platform::Thickness border_thickness_; - CornerRadius border_radius_; - - std::shared_ptr foreground_brush_; - std::shared_ptr background_brush_; - - // The ring. Used for painting. - std::unique_ptr geometry_; - // Area including inner area of the border. Used for painting foreground and - // background. - std::unique_ptr border_inner_geometry_; - // Area including border ring and inner area. Used for hit test. - std::unique_ptr border_outer_geometry_; -}; -} // namespace cru::ui::render diff --git a/include/cru/ui/render/canvas_render_object.hpp b/include/cru/ui/render/canvas_render_object.hpp deleted file mode 100644 index cb3828b6..00000000 --- a/include/cru/ui/render/canvas_render_object.hpp +++ /dev/null @@ -1,39 +0,0 @@ -#pragma once -#include "render_object.hpp" - -namespace cru::ui::render { -// 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`). -class CanvasRenderObject : public RenderObject { - public: - CanvasRenderObject(); - - CRU_DELETE_COPY(CanvasRenderObject) - CRU_DELETE_MOVE(CanvasRenderObject) - - ~CanvasRenderObject(); - - public: - void Draw(platform::graph::IPainter* painter) override; - - RenderObject* HitTest(const Point& point) override; - - Size GetDesiredSize() const { return desired_size_; } - - // Set the desired size. This is the content size excluding padding and - // margin. - void SetDesiredSize(const Size& new_size) { desired_size_ = new_size; } - - IEvent* PaintEvent() { return &paint_event_; } - - protected: - Size OnMeasureContent(const Size& available_size) override; - void OnLayoutContent(const Rect& content_rect) override; - - private: - Size desired_size_{}; - - Event paint_event_; -}; -} // namespace cru::ui::render diff --git a/include/cru/ui/render/flex_layout_render_object.hpp b/include/cru/ui/render/flex_layout_render_object.hpp deleted file mode 100644 index 849c1a0d..00000000 --- a/include/cru/ui/render/flex_layout_render_object.hpp +++ /dev/null @@ -1,42 +0,0 @@ -#pragma once -#include "layout_render_object.hpp" - -namespace cru::ui::render { -class FlexLayoutRenderObject : public LayoutRenderObject { - public: - FlexLayoutRenderObject() = default; - FlexLayoutRenderObject(const FlexLayoutRenderObject& other) = delete; - FlexLayoutRenderObject& operator=(const FlexLayoutRenderObject& other) = - delete; - FlexLayoutRenderObject(FlexLayoutRenderObject&& other) = delete; - FlexLayoutRenderObject& operator=(FlexLayoutRenderObject&& other) = delete; - ~FlexLayoutRenderObject() override = default; - - FlexDirection GetFlexDirection() const { return direction_; } - void SetFlexDirection(FlexDirection direction) { - direction_ = direction; - InvalidateLayout(); - } - - FlexMainAlignment GetContentMainAlign() const { return content_main_align_; } - void SetContentMainAlign(FlexMainAlignment align) { - content_main_align_ = align; - InvalidateLayout(); - } - - FlexCrossAlignment GetItemCrossAlign() const { return item_cross_align_; } - void SetItemCrossAlign(FlexCrossAlignment align) { - item_cross_align_ = align; - InvalidateLayout(); - } - - protected: - Size OnMeasureContent(const Size& available_size) override; - void OnLayoutContent(const Rect& content_rect) override; - - private: - FlexDirection direction_ = FlexDirection::Horizontal; - FlexMainAlignment content_main_align_ = FlexMainAlignment::Start; - FlexCrossAlignment item_cross_align_ = FlexCrossAlignment::Center; -}; -} // namespace cru::ui::render diff --git a/include/cru/ui/render/layout_render_object.hpp b/include/cru/ui/render/layout_render_object.hpp deleted file mode 100644 index 5c4c9c5c..00000000 --- a/include/cru/ui/render/layout_render_object.hpp +++ /dev/null @@ -1,88 +0,0 @@ -#pragma once -#include "render_object.hpp" - -#include "cru/platform/graph/util/painter.hpp" - -namespace cru::ui::render { -template -class LayoutRenderObject : public RenderObject { - public: - using ChildLayoutData = TChildLayoutData; - - protected: - LayoutRenderObject() : RenderObject(ChildMode::Multiple) {} - - public: - CRU_DELETE_COPY(LayoutRenderObject) - CRU_DELETE_MOVE(LayoutRenderObject) - - ~LayoutRenderObject() override = default; - - ChildLayoutData* GetChildLayoutData(Index position) { - Expects(position >= 0 && - position < static_cast(child_layout_data_.size())); - return &child_layout_data_[position]; - } - - void Draw(platform::graph::IPainter* painter) override; - - RenderObject* HitTest(const Point& point) override; - - protected: - void OnAddChild(RenderObject* new_child, Index position) override; - void OnRemoveChild(RenderObject* removed_child, Index position) override; - - private: - std::vector child_layout_data_{}; -}; - -template -void LayoutRenderObject::Draw( - platform::graph::IPainter* painter) { - for (const auto child : GetChildren()) { - auto offset = child->GetOffset(); - platform::graph::util::WithTransform( - painter, platform::Matrix::Translation(offset.x, offset.y), - [child](auto p) { child->Draw(p); }); - } -} - -template -RenderObject* LayoutRenderObject::HitTest( - const Point& point) { - const auto& children = GetChildren(); - for (auto i = children.crbegin(); i != children.crend(); ++i) { - auto offset = (*i)->GetOffset(); - Point p{point.x - offset.x, point.y - offset.y}; - const auto result = (*i)->HitTest(p); - if (result != nullptr) { - return result; - } - } - - const auto margin = GetMargin(); - const auto size = GetSize(); - return Rect{margin.left, margin.top, - std::max(size.width - margin.GetHorizontalTotal(), 0.0f), - std::max(size.height - margin.GetVerticalTotal(), 0.0f)} - .IsPointInside(point) - ? this - : nullptr; -} // namespace cru::ui::render - -template -void LayoutRenderObject::OnAddChild(RenderObject* new_child, - const Index position) { - CRU_UNUSED(new_child) - - child_layout_data_.emplace(child_layout_data_.cbegin() + position); -} - -template -void LayoutRenderObject::OnRemoveChild( - RenderObject* removed_child, const Index position) { - CRU_UNUSED(removed_child) - - child_layout_data_.erase(child_layout_data_.cbegin() + position); -} -} // namespace cru::ui::render diff --git a/include/cru/ui/render/layout_utility.hpp b/include/cru/ui/render/layout_utility.hpp deleted file mode 100644 index 16a15d87..00000000 --- a/include/cru/ui/render/layout_utility.hpp +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once -#include "base.hpp" - -namespace cru::ui::render { -Size Min(const Size& left, const Size& right); -Size Max(const Size& left, const Size& right); -} // namespace cru::ui::render diff --git a/include/cru/ui/render/render_object.hpp b/include/cru/ui/render/render_object.hpp deleted file mode 100644 index 6a8db52f..00000000 --- a/include/cru/ui/render/render_object.hpp +++ /dev/null @@ -1,118 +0,0 @@ -#pragma once -#include "base.hpp" - -#include "cru/common/event.hpp" - -namespace cru::ui::render { -// Render object will not destroy its children when destroyed. Control must -// manage lifecycle of its render objects. Since control will destroy its -// children when destroyed, render objects will be destroyed along with it. -class RenderObject : public Object { - friend WindowRenderObject; - - protected: - enum class ChildMode { - None, - Single, - Multiple, - }; - - RenderObject() = default; - RenderObject(ChildMode child_mode) : RenderObject() { - SetChildMode(child_mode); - } - - public: - RenderObject(const RenderObject& other) = delete; - RenderObject(RenderObject&& other) = delete; - RenderObject& operator=(const RenderObject& other) = delete; - RenderObject& operator=(RenderObject&& other) = delete; - ~RenderObject() override = default; - - Control* GetAttachedControl() const { return control_; } - void SetAttachedControl(Control* new_control) { control_ = new_control; } - - UiHost* GetUiHost() const { return ui_host_; } - - RenderObject* GetParent() const { return parent_; } - - const std::vector& GetChildren() const { return children_; } - Index GetChildCount() const { return static_cast(children_.size()); } - void AddChild(RenderObject* render_object, Index position); - void RemoveChild(Index position); - - Point GetOffset() const { return offset_; } - void SetOffset(const Point& offset) { offset_ = offset; } - Point GetTotalOffset() const; - Point FromRootToContent(const Point& point) const; - Size GetSize() const { return size_; } - void SetSize(const Size& size) { size_ = size; } - - Thickness GetMargin() const { return margin_; } - void SetMargin(const Thickness& margin) { margin_ = margin; } - - Thickness GetPadding() const { return padding_; } - void SetPadding(const Thickness& padding) { padding_ = padding; } - - Size GetPreferredSize() const { return preferred_size_; } - void SetPreferredSize(const Size& preferred_size) { - preferred_size_ = preferred_size; - } - - void Measure(const Size& available_size); - void Layout(const Rect& rect); - - virtual void Draw(platform::graph::IPainter* painter) = 0; - - virtual RenderObject* HitTest(const Point& point) = 0; - - public: - void InvalidateLayout(); - void InvalidatePaint(); - - protected: - void SetChildMode(ChildMode mode) { child_mode_ = mode; } - - protected: - virtual void OnParentChanged(RenderObject* old_parent, - RenderObject* new_parent); - - // default is to invalidate both layout and paint - virtual void OnAddChild(RenderObject* new_child, Index position); - // default is to invalidate both layout and paint - virtual void OnRemoveChild(RenderObject* removed_child, Index position); - - virtual void OnMeasureCore(const Size& available_size); - virtual void OnLayoutCore(const Rect& rect); - virtual Size OnMeasureContent(const Size& available_size) = 0; - virtual void OnLayoutContent(const Rect& content_rect) = 0; - - virtual void OnAfterLayout(); - static void NotifyAfterLayoutRecursive(RenderObject* render_object); - - Rect GetPaddingRect() const; - Rect GetContentRect() const; - - private: - void SetParent(RenderObject* new_parent); - - void SetRenderHostRecursive(UiHost* host); - - private: - Control* control_ = nullptr; - UiHost* ui_host_ = nullptr; - - RenderObject* parent_ = nullptr; - std::vector children_{}; - - ChildMode child_mode_ = ChildMode::None; - - Point offset_{}; - Size size_{}; - - Thickness margin_{}; - Thickness padding_{}; - - Size preferred_size_{}; -}; -} // namespace cru::ui::render diff --git a/include/cru/ui/render/scroll_render_object.hpp b/include/cru/ui/render/scroll_render_object.hpp deleted file mode 100644 index 1527db6c..00000000 --- a/include/cru/ui/render/scroll_render_object.hpp +++ /dev/null @@ -1,30 +0,0 @@ -#pragma once -#include "render_object.hpp" - -#include "cru/platform/graph/util/painter.hpp" - -namespace cru::ui::render { -class ScrollRenderObject : public RenderObject { - public: - ScrollRenderObject() : RenderObject(ChildMode::Single) {} - - CRU_DELETE_COPY(ScrollRenderObject) - CRU_DELETE_MOVE(ScrollRenderObject) - - ~ScrollRenderObject() override = default; - - void Draw(platform::graph::IPainter* painter) override; - - RenderObject* HitTest(const Point& point) override; - - Point GetScrollOffset() { return scroll_offset_; } - void SetScrollOffset(const Point& offset); - - protected: - void OnAddChild(RenderObject* new_child, Index position) override; - void OnRemoveChild(RenderObject* removed_child, Index position) override; - - private: - Point scroll_offset_; -}; -} // 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 deleted file mode 100644 index c259b98d..00000000 --- a/include/cru/ui/render/stack_layout_render_object.hpp +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once -#include "layout_render_object.hpp" - -namespace cru::ui::render { -class StackLayoutRenderObject - : public LayoutRenderObject { - public: - StackLayoutRenderObject() = default; - CRU_DELETE_COPY(StackLayoutRenderObject) - CRU_DELETE_MOVE(StackLayoutRenderObject) - ~StackLayoutRenderObject() = default; - - protected: - Size OnMeasureContent(const Size& available_size) override; - void OnLayoutContent(const Rect& content_rect) override; -}; -} // namespace cru::ui::render diff --git a/include/cru/ui/render/text_render_object.hpp b/include/cru/ui/render/text_render_object.hpp deleted file mode 100644 index 4b1e91e0..00000000 --- a/include/cru/ui/render/text_render_object.hpp +++ /dev/null @@ -1,85 +0,0 @@ -#pragma once -#include "render_object.hpp" - -#include - -namespace cru::ui::render { -class TextRenderObject : public RenderObject { - public: - constexpr static float default_caret_width = 2; - - public: - TextRenderObject(std::shared_ptr brush, - std::shared_ptr font, - std::shared_ptr selection_brush, - std::shared_ptr caret_brush); - TextRenderObject(const TextRenderObject& other) = delete; - TextRenderObject(TextRenderObject&& other) = delete; - TextRenderObject& operator=(const TextRenderObject& other) = delete; - TextRenderObject& operator=(TextRenderObject&& other) = delete; - ~TextRenderObject() override; - - std::string GetText() const; - void SetText(std::string new_text); - - std::shared_ptr GetBrush() const { return brush_; } - void SetBrush(std::shared_ptr new_brush); - - std::shared_ptr GetFont() const; - void SetFont(std::shared_ptr font); - - std::vector TextRangeRect(const TextRange& text_range); - Point TextSinglePoint(gsl::index position, bool trailing); - platform::graph::TextHitTestResult TextHitTest(const Point& point); - - std::optional GetSelectionRange() const { - return selection_range_; - } - void SetSelectionRange(std::optional new_range); - - std::shared_ptr GetSelectionBrush() const { - return selection_brush_; - } - void SetSelectionBrush(std::shared_ptr new_brush); - - bool IsDrawCaret() const { return draw_caret_; } - void SetDrawCaret(bool draw_caret); - void ToggleDrawCaret() { SetDrawCaret(!IsDrawCaret()); } - - // Caret position can be any value. When it is negative, 0 is used. When it - // exceeds the size of the string, the size of the string is used. - gsl::index GetCaretPosition() const { return caret_position_; } - void SetCaretPosition(gsl::index position); - - std::shared_ptr GetCaretBrush() const { - return caret_brush_; - } - void GetCaretBrush(std::shared_ptr brush); - - float GetCaretWidth() const { return caret_width_; } - void SetCaretWidth(float width); - - void Draw(platform::graph::IPainter* painter) override; - - RenderObject* HitTest(const Point& point) override; - - protected: - Size OnMeasureContent(const Size& available_size) override; - void OnLayoutContent(const Rect& content_rect) override; - - void OnAfterLayout() override; - - private: - std::shared_ptr brush_; - std::shared_ptr font_; - std::unique_ptr text_layout_; - - std::optional selection_range_ = std::nullopt; - std::shared_ptr selection_brush_; - - bool draw_caret_ = false; - gsl::index caret_position_ = 0; - std::shared_ptr caret_brush_; - float caret_width_ = default_caret_width; -}; -} // namespace cru::ui::render diff --git a/include/cru/ui/render/window_render_object.hpp b/include/cru/ui/render/window_render_object.hpp deleted file mode 100644 index 76b17b82..00000000 --- a/include/cru/ui/render/window_render_object.hpp +++ /dev/null @@ -1,30 +0,0 @@ -#pragma once -#include "render_object.hpp" - -namespace cru::ui::render { -class WindowRenderObject : public RenderObject { - public: - WindowRenderObject(UiHost* host); - WindowRenderObject(const WindowRenderObject& other) = delete; - WindowRenderObject(WindowRenderObject&& other) = delete; - WindowRenderObject& operator=(const WindowRenderObject& other) = delete; - WindowRenderObject& operator=(WindowRenderObject&& other) = delete; - ~WindowRenderObject() override = default; - - void Draw(platform::graph::IPainter* painter) override; - - RenderObject* HitTest(const Point& point) override; - - protected: - Size OnMeasureContent(const Size& available_size) override; - void OnLayoutContent(const Rect& content_rect) override; - - private: - RenderObject* GetChild() const { - return GetChildren().empty() ? nullptr : GetChildren()[0]; - } - - private: - EventRevokerGuard after_layout_event_guard_; -}; -} // namespace cru::ui::render diff --git a/include/cru/ui/ui_event.hpp b/include/cru/ui/ui_event.hpp deleted file mode 100644 index a9d6028a..00000000 --- a/include/cru/ui/ui_event.hpp +++ /dev/null @@ -1,229 +0,0 @@ -#pragma once -#include "base.hpp" - -#include "cru/common/event.hpp" -#include "cru/platform/native/keyboard.hpp" - -#include -#include -#include -#include - -namespace cru::platform::graph { -struct IPainter; -} - -namespace cru::ui { -class Control; -} - -namespace cru::ui::event { -class UiEventArgs : public Object { - public: - UiEventArgs(Object* sender, Object* original_sender) - : sender_(sender), original_sender_(original_sender), handled_(false) {} - - UiEventArgs(const UiEventArgs& other) = default; - UiEventArgs(UiEventArgs&& other) = default; - UiEventArgs& operator=(const UiEventArgs& other) = default; - UiEventArgs& operator=(UiEventArgs&& other) = default; - ~UiEventArgs() override = default; - - Object* GetSender() const { return sender_; } - - Object* GetOriginalSender() const { return original_sender_; } - - bool IsHandled() const { return handled_; } - void SetHandled(const bool handled = true) { handled_ = handled; } - - private: - Object* sender_; - Object* original_sender_; - bool handled_; -}; - -// TEventArgs must not be a reference type. This class help add reference. -// EventArgs must be reference because the IsHandled property must be settable. -template -class RoutedEvent { - public: - static_assert(std::is_base_of_v, - "TEventArgs must be subclass of UiEventArgs."); - static_assert(!std::is_reference_v, - "TEventArgs must not be reference."); - - using EventArgs = TEventArgs; - - RoutedEvent() = default; - RoutedEvent(const RoutedEvent& other) = delete; - RoutedEvent(RoutedEvent&& other) = delete; - RoutedEvent& operator=(const RoutedEvent& other) = delete; - RoutedEvent& operator=(RoutedEvent&& other) = delete; - ~RoutedEvent() = default; - - IEvent* Direct() { return &direct_; } - - IEvent* Bubble() { return &bubble_; } - - IEvent* Tunnel() { return &tunnel_; } - - private: - Event direct_; - Event bubble_; - Event tunnel_; -}; - -class MouseEventArgs : public UiEventArgs { - public: - MouseEventArgs(Object* sender, Object* original_sender, - const std::optional& point = std::nullopt) - : UiEventArgs(sender, original_sender), point_(point) {} - MouseEventArgs(const MouseEventArgs& other) = default; - MouseEventArgs(MouseEventArgs&& other) = default; - MouseEventArgs& operator=(const MouseEventArgs& other) = default; - MouseEventArgs& operator=(MouseEventArgs&& other) = default; - ~MouseEventArgs() override = default; - - // This point is relative to window client lefttop. - Point GetPoint() const { return point_.value_or(Point{}); } - - private: - std::optional point_; -}; - -class MouseButtonEventArgs : public MouseEventArgs { - public: - MouseButtonEventArgs(Object* sender, Object* original_sender, - const Point& point, const MouseButton button, - platform::native::KeyModifier key_modifier) - : MouseEventArgs(sender, original_sender, point), - button_(button), - key_modifier_(key_modifier) {} - MouseButtonEventArgs(Object* sender, Object* original_sender, - const MouseButton button, - platform::native::KeyModifier key_modifier) - : MouseEventArgs(sender, original_sender), - button_(button), - key_modifier_(key_modifier) {} - MouseButtonEventArgs(const MouseButtonEventArgs& other) = default; - MouseButtonEventArgs(MouseButtonEventArgs&& other) = default; - MouseButtonEventArgs& operator=(const MouseButtonEventArgs& other) = default; - MouseButtonEventArgs& operator=(MouseButtonEventArgs&& other) = default; - ~MouseButtonEventArgs() override = default; - - MouseButton GetButton() const { return button_; } - platform::native::KeyModifier GetKeyModifier() const { return key_modifier_; } - - private: - MouseButton button_; - platform::native::KeyModifier key_modifier_; -}; - -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 PaintEventArgs : public UiEventArgs { - public: - PaintEventArgs(Object* sender, Object* original_sender, - platform::graph::IPainter* painter) - : UiEventArgs(sender, original_sender), painter_(painter) {} - PaintEventArgs(const PaintEventArgs& other) = default; - PaintEventArgs(PaintEventArgs&& other) = default; - PaintEventArgs& operator=(const PaintEventArgs& other) = default; - PaintEventArgs& operator=(PaintEventArgs&& other) = default; - ~PaintEventArgs() = default; - - platform::graph::IPainter* GetPainter() const { return painter_; } - - private: - platform::graph::IPainter* painter_; -}; - -class FocusChangeEventArgs : public UiEventArgs { - public: - FocusChangeEventArgs(Object* sender, Object* original_sender, - const bool is_window = false) - : UiEventArgs(sender, original_sender), is_window_(is_window) {} - FocusChangeEventArgs(const FocusChangeEventArgs& other) = default; - FocusChangeEventArgs(FocusChangeEventArgs&& other) = default; - FocusChangeEventArgs& operator=(const FocusChangeEventArgs& other) = default; - FocusChangeEventArgs& operator=(FocusChangeEventArgs&& other) = default; - ~FocusChangeEventArgs() override = default; - - // Return whether the focus change is caused by the window-wide focus change. - bool IsWindow() const { return is_window_; } - - private: - bool is_window_; -}; - -/* -class ToggleEventArgs : public UiEventArgs { - public: - ToggleEventArgs(Object* sender, Object* original_sender, bool new_state) - : UiEventArgs(sender, original_sender), new_state_(new_state) {} - ToggleEventArgs(const ToggleEventArgs& other) = default; - ToggleEventArgs(ToggleEventArgs&& other) = default; - ToggleEventArgs& operator=(const ToggleEventArgs& other) = default; - ToggleEventArgs& operator=(ToggleEventArgs&& other) = default; - ~ToggleEventArgs() override = default; - - bool GetNewState() const { return new_state_; } - - private: - bool new_state_; -}; -*/ - -class KeyEventArgs : public UiEventArgs { - public: - KeyEventArgs(Object* sender, Object* original_sender, - platform::native::KeyCode key_code, - platform::native::KeyModifier key_modifier) - : UiEventArgs(sender, original_sender), - key_code_(key_code), - key_modifier_(key_modifier) {} - KeyEventArgs(const KeyEventArgs& other) = default; - KeyEventArgs(KeyEventArgs&& other) = default; - KeyEventArgs& operator=(const KeyEventArgs& other) = default; - KeyEventArgs& operator=(KeyEventArgs&& other) = default; - ~KeyEventArgs() override = default; - - platform::native::KeyCode GetKeyCode() const { return key_code_; } - platform::native::KeyModifier GetKeyModifier() const { return key_modifier_; } - - private: - platform::native::KeyCode key_code_; - platform::native::KeyModifier key_modifier_; -}; - -class CharEventArgs : public UiEventArgs { - public: - CharEventArgs(Object* sender, Object* original_sender, std::string c) - : UiEventArgs(sender, original_sender), c_(std::move(c)) {} - CharEventArgs(const CharEventArgs& other) = default; - CharEventArgs(CharEventArgs&& other) = default; - CharEventArgs& operator=(const CharEventArgs& other) = default; - CharEventArgs& operator=(CharEventArgs&& other) = default; - ~CharEventArgs() override = default; - - std::string GetChar() const { return c_; } - - private: - std::string c_; -}; -} // namespace cru::ui::event diff --git a/include/cru/ui/ui_host.hpp b/include/cru/ui/ui_host.hpp deleted file mode 100644 index ca2b70a4..00000000 --- a/include/cru/ui/ui_host.hpp +++ /dev/null @@ -1,170 +0,0 @@ -#pragma once -#include "base.hpp" - -#include "cru/common/event.hpp" -#include "cru/common/self_resolvable.hpp" -#include "render/base.hpp" - -namespace cru::ui { -struct AfterLayoutEventArgs {}; - -// The host of all controls and render objects. -// -// 3 situations on destroy: -// 1. Native window destroyed, IsRetainAfterDestroy: false: -// OnNativeDestroy(set native_window_destroyed_ to true, call ~Window due to -// deleting_ is false and IsRetainAfterDestroy is false) -> ~Window -> -// ~UiHost(not destroy native window repeatedly due to native_window_destroyed_ -// is true) -// 2. Native window destroyed, IsRetainAfterDestroy: true: -// OnNativeDestroy(set native_window_destroyed_ to true, not call ~Window -// because deleting_ is false and IsRetainAfterDestroy is true) -// then, ~Window -> ~UiHost(not destroy native window repeatedly due to -// native_window_destroyed_ is true) -// 3. Native window not destroyed, ~Window is called: -// ~Window -> ~UiHost(set deleting_ to true, destroy native window -// due to native_window_destroyed is false) -> OnNativeDestroy(not call ~Window -// due to deleting_ is true and IsRetainAfterDestroy is whatever) -// In conclusion: -// 1. Set native_window_destroyed_ to true at the beginning of OnNativeDestroy. -// 2. Set deleting_ to true at the beginning of ~UiHost. -// 3. Destroy native window when native_window_destroy_ is false in ~Window. -// 4. Delete Window when deleting_ is false and IsRetainAfterDestroy is false in -// OnNativeDestroy. -class UiHost : public Object, public SelfResolvable { - public: - // This will create root window render object and attach it to window. - // It will also create and manage a native window. - UiHost(Window* window); - - CRU_DELETE_COPY(UiHost) - CRU_DELETE_MOVE(UiHost) - - ~UiHost() override; - - public: - // Mark the layout as invalid, and arrange a re-layout later. - // This method could be called more than one times in a message cycle. But - // layout only takes place once. - void InvalidateLayout(); - - // Mark the paint as invalid, and arrange a re-paint later. - // This method could be called more than one times in a message cycle. But - // paint only takes place once. - void InvalidatePaint(); - - IEvent* AfterLayoutEvent() { - return &after_layout_event_; - } - - void Relayout(); - - // Get current control that mouse hovers on. This ignores the mouse-capture - // control. Even when mouse is captured by another control, this function - // return the control under cursor. You can use `GetMouseCaptureControl` to - // get more info. - Control* GetMouseHoverControl() const { return mouse_hover_control_; } - - //*************** region: focus *************** - - // Request focus for specified control. - bool RequestFocusFor(Control* control); - - // Get the control that has focus. - Control* GetFocusControl(); - - //*************** region: focus *************** - - // Pass nullptr to release capture. If mouse is already capture by a control, - // this capture will fail and return false. If control is identical to the - // capturing control, capture is not changed and this function will return - // true. - // - // When capturing control changes, - // appropriate event will be sent. If mouse is not on the capturing control - // and capture is released, mouse enter event will be sent to the mouse-hover - // control. If mouse is not on the capturing control and capture is set, mouse - // leave event will be sent to the mouse-hover control. - bool CaptureMouseFor(Control* control); - - // Return null if not captured. - Control* GetMouseCaptureControl(); - - Control* HitTest(const Point& point); - - void UpdateCursor(); - - std::shared_ptr - GetNativeWindowResolver() { - return native_window_resolver_; - } - - bool IsRetainAfterDestroy() { return retain_after_destroy_; } - - void SetRetainAfterDestroy(bool destroy) { retain_after_destroy_ = destroy; } - - private: - //*************** region: native messages *************** - void OnNativeDestroy(platform::native::INativeWindow* window, std::nullptr_t); - void OnNativePaint(platform::native::INativeWindow* window, std::nullptr_t); - void OnNativeResize(platform::native::INativeWindow* window, - const Size& size); - - void OnNativeFocus(platform::native::INativeWindow* window, - cru::platform::native::FocusChangeType focus); - - void OnNativeMouseEnterLeave( - platform::native::INativeWindow* window, - cru::platform::native::MouseEnterLeaveType enter); - void OnNativeMouseMove(platform::native::INativeWindow* window, - const Point& point); - void OnNativeMouseDown( - platform::native::INativeWindow* window, - const platform::native::NativeMouseButtonEventArgs& args); - void OnNativeMouseUp( - platform::native::INativeWindow* window, - const platform::native::NativeMouseButtonEventArgs& args); - - void OnNativeKeyDown(platform::native::INativeWindow* window, - const platform::native::NativeKeyEventArgs& args); - void OnNativeKeyUp(platform::native::INativeWindow* window, - const platform::native::NativeKeyEventArgs& args); - - //*************** region: event dispatcher helper *************** - - void DispatchMouseHoverControlChangeEvent(Control* old_control, - Control* new_control, - const Point& point, bool no_leave, - bool no_enter); - - private: - bool need_layout_ = false; - - Event after_layout_event_; - - std::shared_ptr - native_window_resolver_; - - // See remarks of UiHost. - bool retain_after_destroy_ = false; - // See remarks of UiHost. - bool deleting_ = false; - - // We need this because calling Resolve on resolver in handler of destroy - // event is bad and will always get the dying window. But we need to label the - // window as destroyed so the destructor will not destroy native window - // repeatedly. See remarks of UiHost. - bool native_window_destroyed_ = false; - - std::vector event_revoker_guards_; - - Window* window_control_; - std::unique_ptr root_render_object_; - - Control* mouse_hover_control_; - - Control* focus_control_; // "focus_control_" can't be nullptr - - Control* mouse_captured_control_; -}; -} // namespace cru::ui diff --git a/include/cru/ui/ui_manager.hpp b/include/cru/ui/ui_manager.hpp deleted file mode 100644 index 9ad184c4..00000000 --- a/include/cru/ui/ui_manager.hpp +++ /dev/null @@ -1,35 +0,0 @@ -#pragma once -#include "base.hpp" - -#include "controls/base.hpp" - -namespace cru::ui { -struct ThemeResources { - std::shared_ptr default_font; - std::shared_ptr text_brush; - std::shared_ptr text_selection_brush; - std::shared_ptr caret_brush; - controls::ButtonStyle button_style; - controls::TextBoxBorderStyle text_box_border_style; -}; - -class UiManager : public Object { - public: - static UiManager* GetInstance(); - - private: - UiManager(); - - public: - UiManager(const UiManager& other) = delete; - UiManager(UiManager&& other) = delete; - UiManager& operator=(const UiManager& other) = delete; - UiManager& operator=(UiManager&& other) = delete; - ~UiManager() override; - - ThemeResources* GetThemeResources() { return &theme_resource_; } - - private: - ThemeResources theme_resource_; -}; -} // namespace cru::ui diff --git a/include/cru/ui/window.hpp b/include/cru/ui/window.hpp index 2f5df4da..eb2ecfbb 100644 --- a/include/cru/ui/window.hpp +++ b/include/cru/ui/window.hpp @@ -1,5 +1,5 @@ #pragma once -#include "content_control.hpp" +#include "ContentControl.hpp" namespace cru::ui { class Window final : public ContentControl { diff --git a/include/cru/win/WinPreConfig.hpp b/include/cru/win/WinPreConfig.hpp new file mode 100644 index 00000000..1613da95 --- /dev/null +++ b/include/cru/win/WinPreConfig.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include "cru/common/PreConfig.hpp" + +#define NOMINMAX +#define WIN32_LEAN_AND_MEAN +#include +#undef CreateWindow +#undef DrawText +#undef CreateFont + +#include +#include +#include +#include +#include diff --git a/include/cru/win/exception.hpp b/include/cru/win/exception.hpp index 95eb4079..234aea69 100644 --- a/include/cru/win/exception.hpp +++ b/include/cru/win/exception.hpp @@ -1,7 +1,7 @@ #pragma once -#include "win_pre_config.hpp" +#include "WinPreConfig.hpp" -#include "cru/platform/exception.hpp" +#include "cru/platform/Exception.hpp" #include #include diff --git a/include/cru/win/graph/direct/ComResource.hpp b/include/cru/win/graph/direct/ComResource.hpp new file mode 100644 index 00000000..2ac332cd --- /dev/null +++ b/include/cru/win/graph/direct/ComResource.hpp @@ -0,0 +1,11 @@ +#pragma once +#include "../../WinPreConfig.hpp" + +#include "cru/common/Base.hpp" + +namespace cru::platform::graph::win::direct { +template +struct IComResource : virtual Interface { + virtual TInterface* GetComInterface() const = 0; +}; +} // namespace cru::platform::graph::win::direct diff --git a/include/cru/win/graph/direct/ConvertUtil.hpp b/include/cru/win/graph/direct/ConvertUtil.hpp new file mode 100644 index 00000000..12a04c7b --- /dev/null +++ b/include/cru/win/graph/direct/ConvertUtil.hpp @@ -0,0 +1,107 @@ +#pragma once +#include "../../WinPreConfig.hpp" + +#include "cru/platform/graph/Base.hpp" + +namespace cru::platform::graph::win::direct { +inline D2D1_MATRIX_3X2_F Convert(const platform::Matrix& matrix) { + D2D1_MATRIX_3X2_F m; + m._11 = matrix.m11; + m._12 = matrix.m12; + m._21 = matrix.m21; + m._22 = matrix.m22; + m._31 = matrix.m31; + m._32 = matrix.m32; + return m; +} + +inline D2D1_COLOR_F Convert(const Color& color) { + return D2D1::ColorF(color.red / 255.0f, color.green / 255.0f, + color.blue / 255.0f, color.alpha / 255.0f); +} + +inline D2D1_POINT_2F Convert(const Point& point) { + return D2D1::Point2F(point.x, point.y); +} + +inline D2D1_RECT_F Convert(const Rect& rect) { + return D2D1::RectF(rect.left, rect.top, rect.left + rect.width, + rect.top + rect.height); +} + +inline D2D1_ROUNDED_RECT Convert(const RoundedRect& rounded_rect) { + return D2D1::RoundedRect(Convert(rounded_rect.rect), rounded_rect.radius_x, + rounded_rect.radius_y); +} + +inline D2D1_ELLIPSE Convert(const Ellipse& ellipse) { + return D2D1::Ellipse(Convert(ellipse.center), ellipse.radius_x, + ellipse.radius_y); +} + +inline platform::Matrix Convert(const D2D1_MATRIX_3X2_F& matrix) { + return platform::Matrix{matrix._11, matrix._12, matrix._21, + matrix._22, matrix._31, matrix._32}; +} + +inline Color Convert(const D2D1_COLOR_F& color) { + auto floor = [](float n) { return static_cast(n + 0.5f); }; + return Color{floor(color.r * 255.0f), floor(color.g * 255.0f), + floor(color.b * 255.0f), floor(color.a * 255.0f)}; +} + +inline Point Convert(const D2D1_POINT_2F& point) { + return Point(point.x, point.y); +} + +inline Rect Convert(const D2D1_RECT_F& rect) { + return Rect(rect.left, rect.top, rect.right - rect.left, + rect.bottom - rect.top); +} + +inline RoundedRect Convert(const D2D1_ROUNDED_RECT& rounded_rect) { + return RoundedRect(Convert(rounded_rect.rect), rounded_rect.radiusX, + rounded_rect.radiusY); +} + +inline Ellipse Convert(const D2D1_ELLIPSE& ellipse) { + return Ellipse(Convert(ellipse.point), ellipse.radiusX, ellipse.radiusY); +} + +inline bool operator==(const D2D1_POINT_2F& left, const D2D1_POINT_2F& right) { + return left.x == right.x && left.y == right.y; +} + +inline bool operator!=(const D2D1_POINT_2F& left, const D2D1_POINT_2F& right) { + return !(left == right); +} + +inline bool operator==(const D2D1_RECT_F& left, const D2D1_RECT_F& right) { + return left.left == right.left && left.top == right.top && + left.right == right.right && left.bottom == right.bottom; +} + +inline bool operator!=(const D2D1_RECT_F& left, const D2D1_RECT_F& right) { + return !(left == right); +} + +inline bool operator==(const D2D1_ROUNDED_RECT& left, + const D2D1_ROUNDED_RECT& right) { + return left.rect == right.rect && left.radiusX == right.radiusX && + left.radiusY == right.radiusY; +} + +inline bool operator!=(const D2D1_ROUNDED_RECT& left, + const D2D1_ROUNDED_RECT& right) { + return !(left == right); +} + +inline bool operator==(const D2D1_ELLIPSE& left, const D2D1_ELLIPSE& right) { + return left.point == right.point && left.radiusX == right.radiusX && + left.radiusY == right.radiusY; +} + +inline bool operator!=(const D2D1_ELLIPSE& left, const D2D1_ELLIPSE& right) { + return !(left == right); +} +} // namespace cru::platform::graph::win::direct diff --git a/include/cru/win/graph/direct/TextLayout.hpp b/include/cru/win/graph/direct/TextLayout.hpp new file mode 100644 index 00000000..1a378ed4 --- /dev/null +++ b/include/cru/win/graph/direct/TextLayout.hpp @@ -0,0 +1,54 @@ +#pragma once +#include "ComResource.hpp" +#include "Resource.hpp" + +#include "cru/platform/graph/TextLayout.hpp" + +#include + +namespace cru::platform::graph::win::direct { +class DWriteFont; + +class DWriteTextLayout : public DirectGraphResource, + public virtual ITextLayout, + public virtual IComResource { + public: + DWriteTextLayout(DirectGraphFactory* factory, std::shared_ptr font, + std::string text); + + CRU_DELETE_COPY(DWriteTextLayout) + CRU_DELETE_MOVE(DWriteTextLayout) + + ~DWriteTextLayout() override; + + public: + IDWriteTextLayout* GetComInterface() const override { + return text_layout_.Get(); + } + + public: + std::string GetText() override; + void SetText(std::string new_text) override; + + std::shared_ptr GetFont() override; + void SetFont(std::shared_ptr font) override; + + void SetMaxWidth(float max_width) override; + void SetMaxHeight(float max_height) override; + + Rect GetTextBounds() override; + // Return empty vector if text_range.count is 0. Text range could be in + // reverse direction, it should be normalized first in implementation. + std::vector TextRangeRect(const TextRange& text_range) override; + Point TextSinglePoint(gsl::index position, bool trailing) override; + TextHitTestResult HitTest(const Point& point) override; + + private: + std::string text_; + std::wstring w_text_; + std::shared_ptr font_; + float max_width_ = 10000.0f; + float max_height_ = 10000.0f; + Microsoft::WRL::ComPtr text_layout_; +}; +} // namespace cru::platform::graph::win::direct diff --git a/include/cru/win/graph/direct/brush.hpp b/include/cru/win/graph/direct/brush.hpp index 0726ec3f..df1debe3 100644 --- a/include/cru/win/graph/direct/brush.hpp +++ b/include/cru/win/graph/direct/brush.hpp @@ -1,8 +1,8 @@ #pragma once -#include "com_resource.hpp" -#include "resource.hpp" +#include "ComResource.hpp" +#include "Resource.hpp" -#include "cru/platform/graph/brush.hpp" +#include "cru/platform/graph/Brush.hpp" namespace cru::platform::graph::win::direct { struct ID2DBrush : virtual IBrush { diff --git a/include/cru/win/graph/direct/com_resource.hpp b/include/cru/win/graph/direct/com_resource.hpp deleted file mode 100644 index a6c83434..00000000 --- a/include/cru/win/graph/direct/com_resource.hpp +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once -#include "../../win_pre_config.hpp" - -#include "cru/common/base.hpp" - -namespace cru::platform::graph::win::direct { -template -struct IComResource : virtual Interface { - virtual TInterface* GetComInterface() const = 0; -}; -} // namespace cru::platform::graph::win::direct diff --git a/include/cru/win/graph/direct/convert_util.hpp b/include/cru/win/graph/direct/convert_util.hpp deleted file mode 100644 index ef343a3f..00000000 --- a/include/cru/win/graph/direct/convert_util.hpp +++ /dev/null @@ -1,107 +0,0 @@ -#pragma once -#include "../../win_pre_config.hpp" - -#include "cru/platform/graph/base.hpp" - -namespace cru::platform::graph::win::direct { -inline D2D1_MATRIX_3X2_F Convert(const platform::Matrix& matrix) { - D2D1_MATRIX_3X2_F m; - m._11 = matrix.m11; - m._12 = matrix.m12; - m._21 = matrix.m21; - m._22 = matrix.m22; - m._31 = matrix.m31; - m._32 = matrix.m32; - return m; -} - -inline D2D1_COLOR_F Convert(const Color& color) { - return D2D1::ColorF(color.red / 255.0f, color.green / 255.0f, - color.blue / 255.0f, color.alpha / 255.0f); -} - -inline D2D1_POINT_2F Convert(const Point& point) { - return D2D1::Point2F(point.x, point.y); -} - -inline D2D1_RECT_F Convert(const Rect& rect) { - return D2D1::RectF(rect.left, rect.top, rect.left + rect.width, - rect.top + rect.height); -} - -inline D2D1_ROUNDED_RECT Convert(const RoundedRect& rounded_rect) { - return D2D1::RoundedRect(Convert(rounded_rect.rect), rounded_rect.radius_x, - rounded_rect.radius_y); -} - -inline D2D1_ELLIPSE Convert(const Ellipse& ellipse) { - return D2D1::Ellipse(Convert(ellipse.center), ellipse.radius_x, - ellipse.radius_y); -} - -inline platform::Matrix Convert(const D2D1_MATRIX_3X2_F& matrix) { - return platform::Matrix{matrix._11, matrix._12, matrix._21, - matrix._22, matrix._31, matrix._32}; -} - -inline Color Convert(const D2D1_COLOR_F& color) { - auto floor = [](float n) { return static_cast(n + 0.5f); }; - return Color{floor(color.r * 255.0f), floor(color.g * 255.0f), - floor(color.b * 255.0f), floor(color.a * 255.0f)}; -} - -inline Point Convert(const D2D1_POINT_2F& point) { - return Point(point.x, point.y); -} - -inline Rect Convert(const D2D1_RECT_F& rect) { - return Rect(rect.left, rect.top, rect.right - rect.left, - rect.bottom - rect.top); -} - -inline RoundedRect Convert(const D2D1_ROUNDED_RECT& rounded_rect) { - return RoundedRect(Convert(rounded_rect.rect), rounded_rect.radiusX, - rounded_rect.radiusY); -} - -inline Ellipse Convert(const D2D1_ELLIPSE& ellipse) { - return Ellipse(Convert(ellipse.point), ellipse.radiusX, ellipse.radiusY); -} - -inline bool operator==(const D2D1_POINT_2F& left, const D2D1_POINT_2F& right) { - return left.x == right.x && left.y == right.y; -} - -inline bool operator!=(const D2D1_POINT_2F& left, const D2D1_POINT_2F& right) { - return !(left == right); -} - -inline bool operator==(const D2D1_RECT_F& left, const D2D1_RECT_F& right) { - return left.left == right.left && left.top == right.top && - left.right == right.right && left.bottom == right.bottom; -} - -inline bool operator!=(const D2D1_RECT_F& left, const D2D1_RECT_F& right) { - return !(left == right); -} - -inline bool operator==(const D2D1_ROUNDED_RECT& left, - const D2D1_ROUNDED_RECT& right) { - return left.rect == right.rect && left.radiusX == right.radiusX && - left.radiusY == right.radiusY; -} - -inline bool operator!=(const D2D1_ROUNDED_RECT& left, - const D2D1_ROUNDED_RECT& right) { - return !(left == right); -} - -inline bool operator==(const D2D1_ELLIPSE& left, const D2D1_ELLIPSE& right) { - return left.point == right.point && left.radiusX == right.radiusX && - left.radiusY == right.radiusY; -} - -inline bool operator!=(const D2D1_ELLIPSE& left, const D2D1_ELLIPSE& right) { - return !(left == right); -} -} // namespace cru::platform::graph::win::direct diff --git a/include/cru/win/graph/direct/exception.hpp b/include/cru/win/graph/direct/exception.hpp index 8e955825..8b62e8fa 100644 --- a/include/cru/win/graph/direct/exception.hpp +++ b/include/cru/win/graph/direct/exception.hpp @@ -1,5 +1,5 @@ #pragma once -#include "../../exception.hpp" +#include "../../Exception.hpp" namespace cru::platform::graph::win::direct { using platform::win::HResultError; diff --git a/include/cru/win/graph/direct/factory.hpp b/include/cru/win/graph/direct/factory.hpp index cf5ccaee..763d4b2b 100644 --- a/include/cru/win/graph/direct/factory.hpp +++ b/include/cru/win/graph/direct/factory.hpp @@ -1,7 +1,7 @@ #pragma once -#include "resource.hpp" +#include "Resource.hpp" -#include "cru/platform/graph/factory.hpp" +#include "cru/platform/graph/Factory.hpp" namespace cru::platform::graph::win::direct { class DirectGraphFactory : public DirectResource, public virtual IGraphFactory { diff --git a/include/cru/win/graph/direct/font.hpp b/include/cru/win/graph/direct/font.hpp index ee2e319b..ecf9fd81 100644 --- a/include/cru/win/graph/direct/font.hpp +++ b/include/cru/win/graph/direct/font.hpp @@ -1,8 +1,8 @@ #pragma once -#include "com_resource.hpp" -#include "resource.hpp" +#include "ComResource.hpp" +#include "Resource.hpp" -#include "cru/platform/graph/font.hpp" +#include "cru/platform/graph/Font.hpp" #include diff --git a/include/cru/win/graph/direct/geometry.hpp b/include/cru/win/graph/direct/geometry.hpp index 086b31cc..87987d3e 100644 --- a/include/cru/win/graph/direct/geometry.hpp +++ b/include/cru/win/graph/direct/geometry.hpp @@ -1,8 +1,8 @@ #pragma once -#include "com_resource.hpp" -#include "resource.hpp" +#include "ComResource.hpp" +#include "Resource.hpp" -#include "cru/platform/graph/geometry.hpp" +#include "cru/platform/graph/Geometry.hpp" namespace cru::platform::graph::win::direct { class D2DGeometryBuilder : public DirectGraphResource, diff --git a/include/cru/win/graph/direct/painter.hpp b/include/cru/win/graph/direct/painter.hpp index 4f2164c9..a50f962d 100644 --- a/include/cru/win/graph/direct/painter.hpp +++ b/include/cru/win/graph/direct/painter.hpp @@ -1,8 +1,8 @@ #pragma once -#include "com_resource.hpp" -#include "resource.hpp" +#include "ComResource.hpp" +#include "Resource.hpp" -#include "cru/platform/graph/painter.hpp" +#include "cru/platform/graph/Painter.hpp" #include diff --git a/include/cru/win/graph/direct/resource.hpp b/include/cru/win/graph/direct/resource.hpp index 94a91a40..d0a30dbd 100644 --- a/include/cru/win/graph/direct/resource.hpp +++ b/include/cru/win/graph/direct/resource.hpp @@ -1,7 +1,7 @@ #pragma once -#include "../../win_pre_config.hpp" +#include "../../WinPreConfig.hpp" -#include "cru/platform/graph/resource.hpp" +#include "cru/platform/graph/Resource.hpp" #include diff --git a/include/cru/win/graph/direct/text_layout.hpp b/include/cru/win/graph/direct/text_layout.hpp deleted file mode 100644 index 2870db96..00000000 --- a/include/cru/win/graph/direct/text_layout.hpp +++ /dev/null @@ -1,54 +0,0 @@ -#pragma once -#include "com_resource.hpp" -#include "resource.hpp" - -#include "cru/platform/graph/text_layout.hpp" - -#include - -namespace cru::platform::graph::win::direct { -class DWriteFont; - -class DWriteTextLayout : public DirectGraphResource, - public virtual ITextLayout, - public virtual IComResource { - public: - DWriteTextLayout(DirectGraphFactory* factory, std::shared_ptr font, - std::string text); - - CRU_DELETE_COPY(DWriteTextLayout) - CRU_DELETE_MOVE(DWriteTextLayout) - - ~DWriteTextLayout() override; - - public: - IDWriteTextLayout* GetComInterface() const override { - return text_layout_.Get(); - } - - public: - std::string GetText() override; - void SetText(std::string new_text) override; - - std::shared_ptr GetFont() override; - void SetFont(std::shared_ptr font) override; - - void SetMaxWidth(float max_width) override; - void SetMaxHeight(float max_height) override; - - Rect GetTextBounds() override; - // Return empty vector if text_range.count is 0. Text range could be in - // reverse direction, it should be normalized first in implementation. - std::vector TextRangeRect(const TextRange& text_range) override; - Point TextSinglePoint(gsl::index position, bool trailing) override; - TextHitTestResult HitTest(const Point& point) override; - - private: - std::string text_; - std::wstring w_text_; - std::shared_ptr font_; - float max_width_ = 10000.0f; - float max_height_ = 10000.0f; - Microsoft::WRL::ComPtr text_layout_; -}; -} // namespace cru::platform::graph::win::direct diff --git a/include/cru/win/native/GodWindow.hpp b/include/cru/win/native/GodWindow.hpp new file mode 100644 index 00000000..1dd99661 --- /dev/null +++ b/include/cru/win/native/GodWindow.hpp @@ -0,0 +1,27 @@ +#pragma once +#include "Base.hpp" + +#include + +namespace cru::platform::native::win { +class GodWindow : public Object { + public: + explicit GodWindow(WinUiApplication* application); + + CRU_DELETE_COPY(GodWindow) + CRU_DELETE_MOVE(GodWindow) + + ~GodWindow() override; + + HWND GetHandle() const { return hwnd_; } + + bool HandleGodWindowMessage(HWND hwnd, UINT msg, WPARAM w_param, + LPARAM l_param, LRESULT* result); + + private: + WinUiApplication* application_; + + std::unique_ptr god_window_class_; + HWND hwnd_; +}; +} // namespace cru::platform::native::win diff --git a/include/cru/win/native/InputMethod.hpp b/include/cru/win/native/InputMethod.hpp new file mode 100644 index 00000000..0e9634aa --- /dev/null +++ b/include/cru/win/native/InputMethod.hpp @@ -0,0 +1,98 @@ +// Some useful information can be found from chromium code: +// https://chromium.googlesource.com/chromium/chromium/+/refs/heads/master/ui/base/win/ime_input.h +// https://chromium.googlesource.com/chromium/chromium/+/refs/heads/master/ui/base/win/ime_input.cc + +#pragma once +#include "Resource.hpp" + +#include "cru/platform/native/InputMethod.hpp" +#include "WindowNativeMessageEventArgs.hpp" + +#include + +namespace cru::platform::native::win { +class AutoHIMC : public Object { + public: + explicit AutoHIMC(HWND hwnd); + + CRU_DELETE_COPY(AutoHIMC) + + AutoHIMC(AutoHIMC&& other); + AutoHIMC& operator=(AutoHIMC&& other); + + ~AutoHIMC() override; + + HWND GetHwnd() const { return hwnd_; } + + HIMC Get() const { return handle_; } + + private: + HWND hwnd_; + HIMC handle_; +}; + +class WinInputMethodContext : public WinNativeResource, + public virtual IInputMethodContext { + public: + WinInputMethodContext(gsl::not_null window); + + CRU_DELETE_COPY(WinInputMethodContext) + CRU_DELETE_MOVE(WinInputMethodContext) + + ~WinInputMethodContext() override; + + bool ShouldManuallyDrawCompositionText() override { return true; } + + void EnableIME() override; + + void DisableIME() override; + + void CompleteComposition() override; + + void CancelComposition() override; + + CompositionText GetCompositionText() override; + + void SetCandidateWindowPosition(const Point& point) override; + + IEvent* CompositionStartEvent() override; + + IEvent* CompositionEndEvent() override; + + IEvent* CompositionEvent() override; + + IEvent* TextEvent() override; + + private: + void OnWindowNativeMessage(WindowNativeMessageEventArgs& args); + + std::string GetResultString(); + + std::optional TryGetHIMC(); + + private: + std::shared_ptr native_window_resolver_; + + std::vector event_revoker_guards_; + + Event composition_start_event_; + Event composition_end_event_; + Event composition_event_; + Event text_event_; +}; + +class WinInputMethodManager : public WinNativeResource, + public virtual IInputMethodManager { + public: + WinInputMethodManager(WinUiApplication* application); + + CRU_DELETE_COPY(WinInputMethodManager) + CRU_DELETE_MOVE(WinInputMethodManager) + + ~WinInputMethodManager() override; + + public: + std::unique_ptr GetContext( + INativeWindow* window) override; +}; +} // namespace cru::platform::native::win diff --git a/include/cru/win/native/UiApplication.hpp b/include/cru/win/native/UiApplication.hpp new file mode 100644 index 00000000..cbc08af7 --- /dev/null +++ b/include/cru/win/native/UiApplication.hpp @@ -0,0 +1,76 @@ +#pragma once +#include "Resource.hpp" + +#include "cru/platform/native/UiApplication.hpp" + +#include + +namespace cru::platform::graph::win::direct { +class DirectGraphFactory; +} + +namespace cru::platform::native::win { +class WinUiApplication : public WinNativeResource, + public virtual IUiApplication { + public: + static WinUiApplication* GetInstance() { return instance; } + + private: + static WinUiApplication* instance; + + public: + WinUiApplication(); + + CRU_DELETE_COPY(WinUiApplication) + CRU_DELETE_MOVE(WinUiApplication) + + ~WinUiApplication() override; + + public: + int Run() override; + void RequestQuit(int quit_code) override; + + void AddOnQuitHandler(std::function handler) override; + + void InvokeLater(std::function action) override; + long long SetTimeout(std::chrono::milliseconds milliseconds, + std::function action) override; + long long SetInterval(std::chrono::milliseconds milliseconds, + std::function action) override; + void CancelTimer(long long id) override; + + std::vector GetAllWindow() override; + std::shared_ptr CreateWindow( + INativeWindow* parent) override; + + cru::platform::graph::IGraphFactory* GetGraphFactory() override; + + cru::platform::graph::win::direct::DirectGraphFactory* GetDirectFactory() { + return graph_factory_.get(); + } + + ICursorManager* GetCursorManager() override; + IInputMethodManager* GetInputMethodManager() override; + + HINSTANCE GetInstanceHandle() const { return instance_handle_; } + + GodWindow* GetGodWindow() const { return god_window_.get(); } + TimerManager* GetTimerManager() const { return timer_manager_.get(); } + WindowManager* GetWindowManager() const { return window_manager_.get(); } + + private: + HINSTANCE instance_handle_; + + std::unique_ptr + graph_factory_; + + std::unique_ptr god_window_; + std::unique_ptr timer_manager_; + std::unique_ptr window_manager_; + + std::unique_ptr cursor_manager_; + std::unique_ptr input_method_manager_; + + std::vector> quit_handlers_; +}; +} // namespace cru::platform::native::win diff --git a/include/cru/win/native/WindowClass.hpp b/include/cru/win/native/WindowClass.hpp new file mode 100644 index 00000000..fdd55065 --- /dev/null +++ b/include/cru/win/native/WindowClass.hpp @@ -0,0 +1,24 @@ +#pragma once +#include "Base.hpp" + +#include + +namespace cru::platform::native::win { +class WindowClass : public Object { + public: + WindowClass(std::wstring name, WNDPROC window_proc, HINSTANCE h_instance); + + CRU_DELETE_COPY(WindowClass) + CRU_DELETE_MOVE(WindowClass) + + ~WindowClass() override = default; + + const wchar_t* GetName() const { return name_.c_str(); } + + ATOM GetAtom() const { return atom_; } + + private: + std::wstring name_; + ATOM atom_; +}; +} // namespace cru::platform::native::win diff --git a/include/cru/win/native/WindowNativeMessageEventArgs.hpp b/include/cru/win/native/WindowNativeMessageEventArgs.hpp new file mode 100644 index 00000000..84a7a123 --- /dev/null +++ b/include/cru/win/native/WindowNativeMessageEventArgs.hpp @@ -0,0 +1,40 @@ +#pragma once +#include "../WinPreConfig.hpp" + +#include "cru/common/Base.hpp" + +namespace cru::platform::native::win { +struct WindowNativeMessage { + HWND hwnd; + UINT msg; + WPARAM w_param; + LPARAM l_param; +}; + +class WindowNativeMessageEventArgs : public Object { + public: + WindowNativeMessageEventArgs(const WindowNativeMessage& message) + : message_(message) {} + CRU_DEFAULT_COPY(WindowNativeMessageEventArgs) + CRU_DEFAULT_MOVE(WindowNativeMessageEventArgs) + ~WindowNativeMessageEventArgs() override = default; + + const WindowNativeMessage& GetWindowMessage() const { return message_; } + + LRESULT GetResult() const { return result_; } + void SetResult(LRESULT result) { result_ = result; } + + bool IsHandled() const { return handled_; } + void SetHandled(bool handled) { handled_ = handled; } + + void HandleWithResult(LRESULT result) { + handled_ = true; + result_ = result; + } + + private: + WindowNativeMessage message_; + LRESULT result_; + bool handled_ = false; +}; +} // namespace cru::platform::native::win diff --git a/include/cru/win/native/WindowRenderTarget.hpp b/include/cru/win/native/WindowRenderTarget.hpp new file mode 100644 index 00000000..83ac1e03 --- /dev/null +++ b/include/cru/win/native/WindowRenderTarget.hpp @@ -0,0 +1,47 @@ +#pragma once +#include "Base.hpp" + +namespace cru::platform::graph::win::direct { +class DirectGraphFactory; +} + +namespace cru::platform::native::win { +// Represents a window render target. +class WindowRenderTarget : public Object { + public: + WindowRenderTarget(graph::win::direct::DirectGraphFactory* factory, + HWND hwnd); + + CRU_DELETE_COPY(WindowRenderTarget) + CRU_DELETE_MOVE(WindowRenderTarget) + + ~WindowRenderTarget() override = default; + + public: + graph::win::direct::DirectGraphFactory* GetDirectFactory() const { + return factory_; + } + + ID2D1DeviceContext* GetD2D1DeviceContext() { + return d2d1_device_context_.Get(); + } + + // Resize the underlying buffer. + void ResizeBuffer(int width, int height); + + // Set this render target as the d2d device context's target. + void SetAsTarget(); + + // Present the data of the underlying buffer to the window. + void Present(); + + private: + void CreateTargetBitmap(); + + private: + graph::win::direct::DirectGraphFactory* factory_; + Microsoft::WRL::ComPtr d2d1_device_context_; + Microsoft::WRL::ComPtr dxgi_swap_chain_; + Microsoft::WRL::ComPtr target_bitmap_; +}; +} // namespace cru::platform::native::win diff --git a/include/cru/win/native/base.hpp b/include/cru/win/native/base.hpp index 61c9f4da..a50c6dd1 100644 --- a/include/cru/win/native/base.hpp +++ b/include/cru/win/native/base.hpp @@ -1,7 +1,7 @@ #pragma once -#include "../win_pre_config.hpp" +#include "../WinPreConfig.hpp" -#include "cru/common/base.hpp" +#include "cru/common/Base.hpp" namespace cru::platform::native::win { class GodWindow; diff --git a/include/cru/win/native/cursor.hpp b/include/cru/win/native/cursor.hpp index e5728b1c..152374d8 100644 --- a/include/cru/win/native/cursor.hpp +++ b/include/cru/win/native/cursor.hpp @@ -1,7 +1,7 @@ #pragma once -#include "resource.hpp" +#include "Resource.hpp" -#include "cru/platform/native/cursor.hpp" +#include "cru/platform/native/Cursor.hpp" #include diff --git a/include/cru/win/native/exception.hpp b/include/cru/win/native/exception.hpp index ac15ab2d..6a5265c1 100644 --- a/include/cru/win/native/exception.hpp +++ b/include/cru/win/native/exception.hpp @@ -1,5 +1,5 @@ #pragma once -#include "../exception.hpp" +#include "../Exception.hpp" namespace cru::platform::native::win { using platform::win::Win32Error; diff --git a/include/cru/win/native/god_window.hpp b/include/cru/win/native/god_window.hpp deleted file mode 100644 index 33218a55..00000000 --- a/include/cru/win/native/god_window.hpp +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once -#include "base.hpp" - -#include - -namespace cru::platform::native::win { -class GodWindow : public Object { - public: - explicit GodWindow(WinUiApplication* application); - - CRU_DELETE_COPY(GodWindow) - CRU_DELETE_MOVE(GodWindow) - - ~GodWindow() override; - - HWND GetHandle() const { return hwnd_; } - - bool HandleGodWindowMessage(HWND hwnd, UINT msg, WPARAM w_param, - LPARAM l_param, LRESULT* result); - - private: - WinUiApplication* application_; - - std::unique_ptr god_window_class_; - HWND hwnd_; -}; -} // namespace cru::platform::native::win diff --git a/include/cru/win/native/input_method.hpp b/include/cru/win/native/input_method.hpp deleted file mode 100644 index ff867f3f..00000000 --- a/include/cru/win/native/input_method.hpp +++ /dev/null @@ -1,98 +0,0 @@ -// Some useful information can be found from chromium code: -// https://chromium.googlesource.com/chromium/chromium/+/refs/heads/master/ui/base/win/ime_input.h -// https://chromium.googlesource.com/chromium/chromium/+/refs/heads/master/ui/base/win/ime_input.cc - -#pragma once -#include "resource.hpp" - -#include "cru/platform/native/input_method.hpp" -#include "window_native_message_event_args.hpp" - -#include - -namespace cru::platform::native::win { -class AutoHIMC : public Object { - public: - explicit AutoHIMC(HWND hwnd); - - CRU_DELETE_COPY(AutoHIMC) - - AutoHIMC(AutoHIMC&& other); - AutoHIMC& operator=(AutoHIMC&& other); - - ~AutoHIMC() override; - - HWND GetHwnd() const { return hwnd_; } - - HIMC Get() const { return handle_; } - - private: - HWND hwnd_; - HIMC handle_; -}; - -class WinInputMethodContext : public WinNativeResource, - public virtual IInputMethodContext { - public: - WinInputMethodContext(gsl::not_null window); - - CRU_DELETE_COPY(WinInputMethodContext) - CRU_DELETE_MOVE(WinInputMethodContext) - - ~WinInputMethodContext() override; - - bool ShouldManuallyDrawCompositionText() override { return true; } - - void EnableIME() override; - - void DisableIME() override; - - void CompleteComposition() override; - - void CancelComposition() override; - - CompositionText GetCompositionText() override; - - void SetCandidateWindowPosition(const Point& point) override; - - IEvent* CompositionStartEvent() override; - - IEvent* CompositionEndEvent() override; - - IEvent* CompositionEvent() override; - - IEvent* TextEvent() override; - - private: - void OnWindowNativeMessage(WindowNativeMessageEventArgs& args); - - std::string GetResultString(); - - std::optional TryGetHIMC(); - - private: - std::shared_ptr native_window_resolver_; - - std::vector event_revoker_guards_; - - Event composition_start_event_; - Event composition_end_event_; - Event composition_event_; - Event text_event_; -}; - -class WinInputMethodManager : public WinNativeResource, - public virtual IInputMethodManager { - public: - WinInputMethodManager(WinUiApplication* application); - - CRU_DELETE_COPY(WinInputMethodManager) - CRU_DELETE_MOVE(WinInputMethodManager) - - ~WinInputMethodManager() override; - - public: - std::unique_ptr GetContext( - INativeWindow* window) override; -}; -} // namespace cru::platform::native::win diff --git a/include/cru/win/native/keyboard.hpp b/include/cru/win/native/keyboard.hpp index afa51c92..790e0015 100644 --- a/include/cru/win/native/keyboard.hpp +++ b/include/cru/win/native/keyboard.hpp @@ -1,7 +1,7 @@ #pragma once -#include "base.hpp" +#include "Base.hpp" -#include "cru/platform/native/keyboard.hpp" +#include "cru/platform/native/Keyboard.hpp" namespace cru::platform::native::win { KeyCode VirtualKeyToKeyCode(int virtual_key); diff --git a/include/cru/win/native/resource.hpp b/include/cru/win/native/resource.hpp index 5601e40e..7afaca0f 100644 --- a/include/cru/win/native/resource.hpp +++ b/include/cru/win/native/resource.hpp @@ -1,7 +1,7 @@ #pragma once -#include "base.hpp" +#include "Base.hpp" -#include "cru/platform/resource.hpp" +#include "cru/platform/Resource.hpp" namespace cru::platform::native::win { class WinNativeResource : public Object, public virtual INativeResource { diff --git a/include/cru/win/native/ui_application.hpp b/include/cru/win/native/ui_application.hpp deleted file mode 100644 index 250e855c..00000000 --- a/include/cru/win/native/ui_application.hpp +++ /dev/null @@ -1,76 +0,0 @@ -#pragma once -#include "resource.hpp" - -#include "cru/platform/native/ui_application.hpp" - -#include - -namespace cru::platform::graph::win::direct { -class DirectGraphFactory; -} - -namespace cru::platform::native::win { -class WinUiApplication : public WinNativeResource, - public virtual IUiApplication { - public: - static WinUiApplication* GetInstance() { return instance; } - - private: - static WinUiApplication* instance; - - public: - WinUiApplication(); - - CRU_DELETE_COPY(WinUiApplication) - CRU_DELETE_MOVE(WinUiApplication) - - ~WinUiApplication() override; - - public: - int Run() override; - void RequestQuit(int quit_code) override; - - void AddOnQuitHandler(std::function handler) override; - - void InvokeLater(std::function action) override; - long long SetTimeout(std::chrono::milliseconds milliseconds, - std::function action) override; - long long SetInterval(std::chrono::milliseconds milliseconds, - std::function action) override; - void CancelTimer(long long id) override; - - std::vector GetAllWindow() override; - std::shared_ptr CreateWindow( - INativeWindow* parent) override; - - cru::platform::graph::IGraphFactory* GetGraphFactory() override; - - cru::platform::graph::win::direct::DirectGraphFactory* GetDirectFactory() { - return graph_factory_.get(); - } - - ICursorManager* GetCursorManager() override; - IInputMethodManager* GetInputMethodManager() override; - - HINSTANCE GetInstanceHandle() const { return instance_handle_; } - - GodWindow* GetGodWindow() const { return god_window_.get(); } - TimerManager* GetTimerManager() const { return timer_manager_.get(); } - WindowManager* GetWindowManager() const { return window_manager_.get(); } - - private: - HINSTANCE instance_handle_; - - std::unique_ptr - graph_factory_; - - std::unique_ptr god_window_; - std::unique_ptr timer_manager_; - std::unique_ptr window_manager_; - - std::unique_ptr cursor_manager_; - std::unique_ptr input_method_manager_; - - std::vector> quit_handlers_; -}; -} // namespace cru::platform::native::win diff --git a/include/cru/win/native/window.hpp b/include/cru/win/native/window.hpp index 59b38ab5..80bee39e 100644 --- a/include/cru/win/native/window.hpp +++ b/include/cru/win/native/window.hpp @@ -1,8 +1,8 @@ #pragma once -#include "resource.hpp" +#include "Resource.hpp" -#include "cru/platform/native/window.hpp" -#include "window_native_message_event_args.hpp" +#include "cru/platform/native/Window.hpp" +#include "WindowNativeMessageEventArgs.hpp" #include diff --git a/include/cru/win/native/window_class.hpp b/include/cru/win/native/window_class.hpp deleted file mode 100644 index 2140c304..00000000 --- a/include/cru/win/native/window_class.hpp +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once -#include "base.hpp" - -#include - -namespace cru::platform::native::win { -class WindowClass : public Object { - public: - WindowClass(std::wstring name, WNDPROC window_proc, HINSTANCE h_instance); - - CRU_DELETE_COPY(WindowClass) - CRU_DELETE_MOVE(WindowClass) - - ~WindowClass() override = default; - - const wchar_t* GetName() const { return name_.c_str(); } - - ATOM GetAtom() const { return atom_; } - - private: - std::wstring name_; - ATOM atom_; -}; -} // namespace cru::platform::native::win diff --git a/include/cru/win/native/window_native_message_event_args.hpp b/include/cru/win/native/window_native_message_event_args.hpp deleted file mode 100644 index f5476735..00000000 --- a/include/cru/win/native/window_native_message_event_args.hpp +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once -#include "../win_pre_config.hpp" - -#include "cru/common/base.hpp" - -namespace cru::platform::native::win { -struct WindowNativeMessage { - HWND hwnd; - UINT msg; - WPARAM w_param; - LPARAM l_param; -}; - -class WindowNativeMessageEventArgs : public Object { - public: - WindowNativeMessageEventArgs(const WindowNativeMessage& message) - : message_(message) {} - CRU_DEFAULT_COPY(WindowNativeMessageEventArgs) - CRU_DEFAULT_MOVE(WindowNativeMessageEventArgs) - ~WindowNativeMessageEventArgs() override = default; - - const WindowNativeMessage& GetWindowMessage() const { return message_; } - - LRESULT GetResult() const { return result_; } - void SetResult(LRESULT result) { result_ = result; } - - bool IsHandled() const { return handled_; } - void SetHandled(bool handled) { handled_ = handled; } - - void HandleWithResult(LRESULT result) { - handled_ = true; - result_ = result; - } - - private: - WindowNativeMessage message_; - LRESULT result_; - bool handled_ = false; -}; -} // namespace cru::platform::native::win diff --git a/include/cru/win/native/window_render_target.hpp b/include/cru/win/native/window_render_target.hpp deleted file mode 100644 index 552e87bc..00000000 --- a/include/cru/win/native/window_render_target.hpp +++ /dev/null @@ -1,47 +0,0 @@ -#pragma once -#include "base.hpp" - -namespace cru::platform::graph::win::direct { -class DirectGraphFactory; -} - -namespace cru::platform::native::win { -// Represents a window render target. -class WindowRenderTarget : public Object { - public: - WindowRenderTarget(graph::win::direct::DirectGraphFactory* factory, - HWND hwnd); - - CRU_DELETE_COPY(WindowRenderTarget) - CRU_DELETE_MOVE(WindowRenderTarget) - - ~WindowRenderTarget() override = default; - - public: - graph::win::direct::DirectGraphFactory* GetDirectFactory() const { - return factory_; - } - - ID2D1DeviceContext* GetD2D1DeviceContext() { - return d2d1_device_context_.Get(); - } - - // Resize the underlying buffer. - void ResizeBuffer(int width, int height); - - // Set this render target as the d2d device context's target. - void SetAsTarget(); - - // Present the data of the underlying buffer to the window. - void Present(); - - private: - void CreateTargetBitmap(); - - private: - graph::win::direct::DirectGraphFactory* factory_; - Microsoft::WRL::ComPtr d2d1_device_context_; - Microsoft::WRL::ComPtr dxgi_swap_chain_; - Microsoft::WRL::ComPtr target_bitmap_; -}; -} // namespace cru::platform::native::win diff --git a/include/cru/win/string.hpp b/include/cru/win/string.hpp index aec0e3d9..3d68cff7 100644 --- a/include/cru/win/string.hpp +++ b/include/cru/win/string.hpp @@ -14,9 +14,9 @@ way.) */ #pragma once -#include "win_pre_config.hpp" +#include "WinPreConfig.hpp" -#include "cru/common/base.hpp" +#include "cru/common/Base.hpp" #include #include diff --git a/include/cru/win/win_pre_config.hpp b/include/cru/win/win_pre_config.hpp deleted file mode 100644 index a7dc72f3..00000000 --- a/include/cru/win/win_pre_config.hpp +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#include "cru/common/pre_config.hpp" - -#define NOMINMAX -#define WIN32_LEAN_AND_MEAN -#include -#undef CreateWindow -#undef DrawText -#undef CreateFont - -#include -#include -#include -#include -#include -- cgit v1.2.3