diff options
author | crupest <crupest@outlook.com> | 2022-03-12 21:26:56 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-03-12 21:26:56 +0800 |
commit | f3b5009040a05e8efd15c385cacf5dd594ecf801 (patch) | |
tree | a6f84d87ad81ea14499d77dc48089190043098d6 | |
parent | c6d05af374ef11cec4a550eb63c73abdb2cd5f72 (diff) | |
download | cru-f3b5009040a05e8efd15c385cacf5dd594ecf801.tar.gz cru-f3b5009040a05e8efd15c385cacf5dd594ecf801.tar.bz2 cru-f3b5009040a05e8efd15c385cacf5dd594ecf801.zip |
...
-rw-r--r-- | include/cru/platform/GraphicsBase.h | 7 | ||||
-rw-r--r-- | include/cru/ui/render/RenderObject.h | 27 | ||||
-rw-r--r-- | src/platform/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/platform/GraphicsBase.cpp | 9 | ||||
-rw-r--r-- | src/ui/render/RenderObject.cpp | 46 |
5 files changed, 88 insertions, 2 deletions
diff --git a/include/cru/platform/GraphicsBase.h b/include/cru/platform/GraphicsBase.h index 62a8bf29..f134e74d 100644 --- a/include/cru/platform/GraphicsBase.h +++ b/include/cru/platform/GraphicsBase.h @@ -1,5 +1,5 @@ #pragma once -#include "cru/common/Base.h" +#include "Base.h" #include "cru/common/Format.h" #include "cru/common/Range.h" @@ -51,7 +51,10 @@ inline String ToString(const Point& point) { return Format(u"(x: {}, y: {})", point.x, point.y); } -struct Size final { +struct CRU_PLATFORM_API Size final { + static const Size kMax; + static const Size kZero; + constexpr Size() = default; constexpr Size(const float width, const float height) : width(width), height(height) {} diff --git a/include/cru/ui/render/RenderObject.h b/include/cru/ui/render/RenderObject.h index 9f2b69d9..134fb935 100644 --- a/include/cru/ui/render/RenderObject.h +++ b/include/cru/ui/render/RenderObject.h @@ -5,6 +5,18 @@ #include "cru/common/String.h" namespace cru::ui::render { +struct BoxConstraint { + static const BoxConstraint kNotLimit; + + Size max; + Size min; + + constexpr bool Validate() const { + return max.width >= min.width && max.height >= min.height && + min.width >= 0 && min.height >= 0; + } +}; + /** * ### Layout * @@ -82,6 +94,12 @@ class CRU_UI_API RenderObject : public Object { return custom_measure_requirement_; } + Size GetMinSize1() const { return min_size_; } + void SetMinSize1(const Size& min_size); + + Size GetMaxSize1() const { return max_size_; } + void SetMaxSize1(const Size& max_size); + // This method will merge requirement passed by argument and requirement of // the render object using MeasureRequirement::Merge and then call // MeasureRequirement::Normalize on it. And it will use preferred size of the @@ -94,6 +112,8 @@ class CRU_UI_API RenderObject : public Object { // This will set offset of this render object and call OnLayoutCore. void Layout(const Point& offset); + void Measure1(const BoxConstraint& constraint); + virtual Thickness GetTotalSpaceThickness() const; virtual Thickness GetInnerSpaceThickness() const; @@ -125,6 +145,8 @@ class CRU_UI_API RenderObject : public Object { virtual Size OnMeasureCore(const MeasureRequirement& requirement, const MeasureSize& preferred_size); + virtual Size OnMeasureCore1(const BoxConstraint& constraint); + // Please reduce margin and padding or other custom things and pass the result // content rect to OnLayoutContent. virtual void OnLayoutCore(); @@ -136,6 +158,8 @@ class CRU_UI_API RenderObject : public Object { virtual Size OnMeasureContent(const MeasureRequirement& requirement, const MeasureSize& preferred_size) = 0; + virtual Size OnMeasureContent1(const BoxConstraint& constraint); + // Layout all content and children(Call Layout on them). // Lefttop of content_rect should be added when calculated children's offset. virtual void OnLayoutContent(const Rect& content_rect) = 0; @@ -160,5 +184,8 @@ class CRU_UI_API RenderObject : public Object { MeasureSize preferred_size_{}; MeasureRequirement custom_measure_requirement_{}; + + Size min_size_ = Size::kZero; + Size max_size_ = Size::kMax; }; } // namespace cru::ui::render diff --git a/src/platform/CMakeLists.txt b/src/platform/CMakeLists.txt index 03754987..fbca4205 100644 --- a/src/platform/CMakeLists.txt +++ b/src/platform/CMakeLists.txt @@ -1,6 +1,7 @@ add_library(cru_platform_base SHARED ForDllExport.cpp Color.cpp + GraphicsBase.cpp ) target_link_libraries(cru_platform_base PUBLIC cru_base) target_compile_definitions(cru_platform_base PRIVATE CRU_PLATFORM_EXPORT_API) diff --git a/src/platform/GraphicsBase.cpp b/src/platform/GraphicsBase.cpp new file mode 100644 index 00000000..c9e57cfc --- /dev/null +++ b/src/platform/GraphicsBase.cpp @@ -0,0 +1,9 @@ +#include "cru/platform/GraphicsBase.h" + +#include <limits> + +namespace cru::platform { +const Size Size::kZero(0.0f, 0.0f); +const Size Size::kMax(std::numeric_limits<float>::max(), + std::numeric_limits<float>::max()); +} // namespace cru::platform diff --git a/src/ui/render/RenderObject.cpp b/src/ui/render/RenderObject.cpp index a2ecab8b..8904f0dc 100644 --- a/src/ui/render/RenderObject.cpp +++ b/src/ui/render/RenderObject.cpp @@ -1,5 +1,6 @@ #include "cru/ui/render/RenderObject.h" +#include "cru/common/Exception.h" #include "cru/common/log/Logger.h" #include "cru/platform/GraphicsBase.h" #include "cru/ui/DebugFlags.h" @@ -7,6 +8,7 @@ #include "cru/ui/host/WindowHost.h" namespace cru::ui::render { +const BoxConstraint BoxConstraint::kNotLimit{Size::kMax, Size::kZero}; void RenderObject::SetParent(RenderObject* new_parent) { #ifdef CRU_DEBUG @@ -72,6 +74,16 @@ void RenderObject::SetMaxSize(const MeasureSize& max_size) { InvalidateLayout(); } +void RenderObject::SetMinSize1(const Size& min_size) { + min_size_ = min_size; + InvalidateLayout(); +} + +void RenderObject::SetMaxSize1(const Size& max_size) { + max_size_ = max_size; + InvalidateLayout(); +} + void RenderObject::Measure(const MeasureRequirement& requirement, const MeasureSize& preferred_size) { MeasureRequirement merged_requirement = @@ -97,6 +109,11 @@ void RenderObject::Measure(const MeasureRequirement& requirement, Ensures(desired_size_.height >= 0); } +void RenderObject::Measure1(const BoxConstraint& constraint) { + Expects(constraint.Validate()); + desired_size_ = OnMeasureCore1(constraint); +} + void RenderObject::Layout(const Point& offset) { if constexpr (cru::ui::debug_flags::layout) { CRU_LOG_DEBUG(u"{} Layout :\noffset: {} size: {}", @@ -138,6 +155,31 @@ Size RenderObject::OnMeasureCore(const MeasureRequirement& requirement, return space_size + content_size; } +Size RenderObject::OnMeasureCore1(const BoxConstraint& constraint) { + const Thickness outer_space = GetTotalSpaceThickness(); + Size space_size{outer_space.GetHorizontalTotal(), + outer_space.GetVerticalTotal()}; + + if (space_size.width > constraint.max.width) { + space_size.width = constraint.max.width; + CRU_LOG_WARN(u"{} space width is over constraint.max.width", + this->GetDebugPathInTree()); + } + + if (space_size.height > constraint.max.height) { + space_size.height = constraint.max.height; + CRU_LOG_WARN(u"{} space height is over constraint.max.height", + this->GetDebugPathInTree()); + } + + BoxConstraint content_constraint{constraint.max - space_size, + constraint.min - space_size}; + + const auto content_size = OnMeasureContent1(content_constraint); + + return space_size + content_size; +} + void RenderObject::OnLayoutCore() { Size total_size = GetDesiredSize(); const Thickness outer_space = GetTotalSpaceThickness(); @@ -166,6 +208,10 @@ void RenderObject::OnLayoutCore() { OnLayoutContent(content_rect); } +Size RenderObject::OnMeasureContent1(const BoxConstraint& constraint) { + throw Exception(u"Not implemented."); +} + Rect RenderObject::GetPaddingRect() const { const auto size = GetDesiredSize(); Rect rect{Point{}, size}; |