aboutsummaryrefslogtreecommitdiff
path: root/CruUI/ui/control.cpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2018-09-21 23:36:11 +0800
committercrupest <crupest@outlook.com>2018-09-21 23:36:11 +0800
commit7abc054f1d33eeb72d665008083b82630781f6e0 (patch)
treec4175355b63d7a83c970ddecb41f8258b603280d /CruUI/ui/control.cpp
parentf9d3795e6dbb33fa5ffc0cb6e990462c4dd4974c (diff)
downloadcru-7abc054f1d33eeb72d665008083b82630781f6e0.tar.gz
cru-7abc054f1d33eeb72d665008083b82630781f6e0.tar.bz2
cru-7abc054f1d33eeb72d665008083b82630781f6e0.zip
Change the default layout pattern of control.
Diffstat (limited to 'CruUI/ui/control.cpp')
-rw-r--r--CruUI/ui/control.cpp44
1 files changed, 25 insertions, 19 deletions
diff --git a/CruUI/ui/control.cpp b/CruUI/ui/control.cpp
index 81543a4d..2f5a7ef8 100644
--- a/CruUI/ui/control.cpp
+++ b/CruUI/ui/control.cpp
@@ -13,22 +13,7 @@ namespace cru {
using namespace events;
Control::Control(const bool container) :
- is_container_(container),
- window_(nullptr),
- parent_(nullptr),
- children_(),
- old_position_(),
- position_(),
- size_(),
- position_cache_(),
- is_mouse_inside_(false),
- is_mouse_leave_
- {
- { MouseButton::Left, true },
- { MouseButton::Middle, true },
- { MouseButton::Right, true }
- },
- desired_size_()
+ is_container_(container)
{
}
@@ -559,9 +544,30 @@ namespace cru {
void Control::OnLayout(const Rect& rect)
{
- ForeachChild([](Control* control)
+ ForeachChild([rect](Control* control)
{
- control->Layout(Rect(Point::Zero(), control->GetDesiredSize()));
+ const auto layout_params = control->GetLayoutParams();
+ const auto size = control->GetDesiredSize();
+
+ auto&& calculate_anchor = [](const Alignment alignment, const float layout_length, const float control_length) -> float
+ {
+ switch (alignment)
+ {
+ case Alignment::Center:
+ return (layout_length - control_length) / 2;
+ case Alignment::Start:
+ return 0;
+ case Alignment::End:
+ return layout_length - control_length;
+ default:
+ UnreachableCode();
+ }
+ };
+
+ control->Layout(Rect(Point(
+ calculate_anchor(layout_params->width.alignment, rect.width, size.width),
+ calculate_anchor(layout_params->height.alignment, rect.height, size.height)
+ ), size));
});
}
@@ -598,7 +604,7 @@ namespace cru {
if (left_list.front() != right_list.front())
return nullptr;
- // find the last same control or the last control (one is the other's ancestor)
+ // find the last same control or the last control (one is ancestor of the other)
auto left_i = left_list.cbegin();
auto right_i = right_list.cbegin();
while (true)