aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CruUI/main.cpp2
-rw-r--r--CruUI/ui/control.cpp44
-rw-r--r--CruUI/ui/control.h29
3 files changed, 43 insertions, 32 deletions
diff --git a/CruUI/main.cpp b/CruUI/main.cpp
index 90743e81..65e63af9 100644
--- a/CruUI/main.cpp
+++ b/CruUI/main.cpp
@@ -74,7 +74,7 @@ int APIENTRY wWinMain(
const auto layout = LinearLayout::Create();
- layout->GetLayoutParams()->width.mode = MeasureMode::Stretch;
+ layout->GetLayoutParams()->width = LayoutLength::Exactly(500);
layout->mouse_click_event.AddHandler([layout](cru::ui::events::MouseButtonEventArgs& args)
{
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)
diff --git a/CruUI/ui/control.h b/CruUI/ui/control.h
index ced7f0e3..7b66ea57 100644
--- a/CruUI/ui/control.h
+++ b/CruUI/ui/control.h
@@ -296,31 +296,36 @@ namespace cru
bool is_container_;
protected:
- Window * window_; // protected for Window class to write it as itself in constructor.
+ Window * window_ = nullptr; // protected for Window class to write it as itself in constructor.
private:
- Control * parent_;
- Vector<Control*> children_;
+ Control * parent_ = nullptr;
+ Vector<Control*> children_{};
// When position is changed and notification hasn't been
// sent, it will be the old position. When position is changed
// more than once, it will be the oldest position since last
// notification. If notification has been sent, it will be updated
// to position_.
- Point old_position_;
- Point position_;
- Size size_;
+ Point old_position_ = Point::Zero();
+ Point position_ = Point::Zero();
+ Size size_ = Size::Zero();
- ControlPositionCache position_cache_;
+ ControlPositionCache position_cache_{};
- bool is_mouse_inside_;
+ bool is_mouse_inside_ = false;
- std::unordered_map<MouseButton, bool> is_mouse_leave_; // used for clicking determination
+ std::unordered_map<MouseButton, bool> is_mouse_leave_
+ {
+ { MouseButton::Left, true },
+ { MouseButton::Middle, true },
+ { MouseButton::Right, true }
+ }; // used for clicking determination
- BasicLayoutParams layout_params_;
- Size desired_size_;
+ BasicLayoutParams layout_params_{};
+ Size desired_size_ = Size::Zero();
- std::unordered_map<String, std::any> additional_properties_;
+ std::unordered_map<String, std::any> additional_properties_{};
};
// Find the lowest common ancestor.