Support ui::positioner clamping the positioned element within provided shape

This commit is contained in:
Nikita Lisitsa 2022-03-31 20:15:37 +03:00
parent 440b7b6214
commit 2b027e09e5
2 changed files with 13 additions and 3 deletions

View file

@ -26,7 +26,7 @@ namespace psemek::ui
struct shape const & shape() const override; struct shape const & shape() const override;
void reshape(geom::box<float, 2> const & box) override; void reshape(geom::box<float, 2> const & box) override;
virtual void set_position(geom::point<float, 2> const & p, x_align x, y_align y); virtual void set_position(geom::point<float, 2> const & p, x_align x, y_align y, bool clamp = true);
void draw(ui::painter &) const override {} void draw(ui::painter &) const override {}
@ -36,6 +36,7 @@ namespace psemek::ui
geom::point<float, 2> position_{0.f, 0.f}; geom::point<float, 2> position_{0.f, 0.f};
x_align x_; x_align x_;
y_align y_; y_align y_;
bool clamp_;
}; };
} }

View file

@ -51,16 +51,25 @@ namespace psemek::ui
break; 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); child_->reshape(cbox);
} }
void positioner::set_position(geom::point<float, 2> const & p, x_align x, y_align y) void positioner::set_position(geom::point<float, 2> 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; position_ = p;
x_ = x; x_ = x;
y_ = y; y_ = y;
clamp_ = clamp;
post_reshape(); post_reshape();
} }