From dea11f34bfab5bfd5f66dec9f2fa0239abf44d89 Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 19 Sep 2018 23:43:26 +0800 Subject: Improve linear layout. Add debug border visualization. --- CruUI/ui/controls/linear_layout.cpp | 60 +++++++++++++++++++++++++++++++------ 1 file changed, 51 insertions(+), 9 deletions(-) (limited to 'CruUI/ui/controls/linear_layout.cpp') diff --git a/CruUI/ui/controls/linear_layout.cpp b/CruUI/ui/controls/linear_layout.cpp index 33a7855f..5f55ba0a 100644 --- a/CruUI/ui/controls/linear_layout.cpp +++ b/CruUI/ui/controls/linear_layout.cpp @@ -40,23 +40,61 @@ namespace cru::ui::controls auto rest_available_size_for_children = total_available_size_for_children; - ForeachChild([this, &rest_available_size_for_children](Control* const control) + std::list stretch_control_list; + + // First measure Content and Exactly and count Stretch. + if (orientation_ == Orientation::Horizontal) + ForeachChild([&rest_available_size_for_children, &stretch_control_list](Control* const control) { - control->Measure(rest_available_size_for_children); - if (orientation_ == Orientation::Horizontal) + const auto mode = control->GetLayoutParams()->width.mode; + if (mode == MeasureMode::Content || mode == MeasureMode::Exactly) { + control->Measure(rest_available_size_for_children); rest_available_size_for_children.width -= control->GetDesiredSize().width; if (rest_available_size_for_children.width < 0) rest_available_size_for_children.width = 0; } else + stretch_control_list.push_back(control); + }); + else + ForeachChild([&rest_available_size_for_children, &stretch_control_list](Control* const control) + { + const auto mode = control->GetLayoutParams()->height.mode; + if (mode == MeasureMode::Content || mode == MeasureMode::Exactly) { + control->Measure(rest_available_size_for_children); rest_available_size_for_children.height -= control->GetDesiredSize().height; if (rest_available_size_for_children.height < 0) rest_available_size_for_children.height = 0; } + else + stretch_control_list.push_back(control); }); + if (orientation_ == Orientation::Horizontal) + { + const auto available_width = rest_available_size_for_children.width / stretch_control_list.size(); + for (const auto control : stretch_control_list) + { + control->Measure(Size(available_width, rest_available_size_for_children.height)); + rest_available_size_for_children.width -= control->GetDesiredSize().width; + if (rest_available_size_for_children.width < 0) + rest_available_size_for_children.width = 0; + } + } + else + { + const auto available_height = rest_available_size_for_children.height / stretch_control_list.size(); + for (const auto control : stretch_control_list) + { + control->Measure(Size(rest_available_size_for_children.width, available_height)); + rest_available_size_for_children.height -= control->GetDesiredSize().height; + if (rest_available_size_for_children.height < 0) + rest_available_size_for_children.height = 0; + } + } + auto actual_size_for_children = total_available_size_for_children; if (orientation_ == Orientation::Horizontal) actual_size_for_children.width -= rest_available_size_for_children.width; @@ -85,16 +123,20 @@ namespace cru::ui::controls void LinearLayout::OnLayout(const Rect& rect) { - auto current_anchor = Point::zero; - ForeachChild([this, ¤t_anchor](Control* control) + float current_anchor_length = 0; + ForeachChild([this, ¤t_anchor_length, rect](Control* control) { const auto size = control->GetDesiredSize(); - control->Layout(Rect(current_anchor, size)); - if (orientation_ == Orientation::Horizontal) - current_anchor.x += size.width; + { + control->Layout(Rect(Point(current_anchor_length, (rect.height - size.height) / 2), size)); + current_anchor_length += size.width; + } else - current_anchor.y += size.height; + { + control->Layout(Rect(Point((rect.width - size.width) / 2, current_anchor_length), size)); + current_anchor_length += size.height; + } }); } } -- cgit v1.2.3