From 2b027e09e5d2cbd0f3eeff6badad617f760efb2d Mon Sep 17 00:00:00 2001 From: lisyarus Date: Thu, 31 Mar 2022 20:15:37 +0300 Subject: [PATCH] Support ui::positioner clamping the positioned element within provided shape --- libs/ui/include/psemek/ui/positioner.hpp | 3 ++- libs/ui/source/positioner.cpp | 13 +++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/libs/ui/include/psemek/ui/positioner.hpp b/libs/ui/include/psemek/ui/positioner.hpp index 7fce5944..6fb09112 100644 --- a/libs/ui/include/psemek/ui/positioner.hpp +++ b/libs/ui/include/psemek/ui/positioner.hpp @@ -26,7 +26,7 @@ namespace psemek::ui struct shape const & shape() const override; void reshape(geom::box const & box) override; - virtual void set_position(geom::point const & p, x_align x, y_align y); + virtual void set_position(geom::point const & p, x_align x, y_align y, bool clamp = true); void draw(ui::painter &) const override {} @@ -36,6 +36,7 @@ namespace psemek::ui geom::point position_{0.f, 0.f}; x_align x_; y_align y_; + bool clamp_; }; } diff --git a/libs/ui/source/positioner.cpp b/libs/ui/source/positioner.cpp index 0a8260d7..b77cde1e 100644 --- a/libs/ui/source/positioner.cpp +++ b/libs/ui/source/positioner.cpp @@ -51,16 +51,25 @@ namespace psemek::ui break; } + if (clamp_) + { + cbox[0] += std::max(0.f, box[0].min - cbox[0].min); + cbox[0] += std::min(0.f, box[0].max - cbox[0].max); + cbox[1] += std::max(0.f, box[1].min - cbox[1].min); + cbox[1] += std::min(0.f, box[1].max - cbox[1].max); + } + child_->reshape(cbox); } - void positioner::set_position(geom::point const & p, x_align x, y_align y) + void positioner::set_position(geom::point const & p, x_align x, y_align y, bool clamp) { - if (p != position_ || x_ != x || y_ != y) + if (p != position_ || x_ != x || y_ != y || clamp_ != clamp) { position_ = p; x_ = x; y_ = y; + clamp_ = clamp; post_reshape(); }