Make slider stick to max shift

This commit is contained in:
Nikita Lisitsa 2022-03-04 14:08:18 +03:00
parent 343a0aed78
commit 2c0001be28
2 changed files with 25 additions and 2 deletions

View file

@ -60,6 +60,9 @@ namespace psemek::ui
bool horizontal_ = false;
bool vertical_ = true;
bool horizontal_stick_ = false;
bool vertical_stick_ = false;
geom::vector<float, 2> shift_{0.f, 0.f};
geom::vector<float, 2> shift_tgt_{0.f, 0.f};

View file

@ -218,12 +218,26 @@ namespace psemek::ui
if (horizontal_scroll())
{
horizontal_stick_ |= child_bbox[0].length() > child_constraints[0].min;
if (horizontal_stick_)
{
shift_tgt_[0] = std::min(child_bbox[0].length() - child_constraints[0].min, 0.f);
shift_[0] = shift_tgt_[0];
}
child_bbox[0].min += shift_[0];
child_bbox[0].max = child_bbox[0].min + child_constraints[0].min;
}
if (vertical_scroll())
{
vertical_stick_ |= child_bbox[1].length() > child_constraints[1].min;
if (vertical_stick_)
{
shift_tgt_[1] = std::min(child_bbox[1].length() - child_constraints[1].min, 0.f);
shift_[1] = shift_tgt_[1];
}
child_bbox[1].min += shift_[1];
child_bbox[1].max = child_bbox[1].min + child_constraints[1].min;
}
@ -343,10 +357,16 @@ namespace psemek::ui
auto c = child_->size_constraints();
shift_tgt_[0] = std::max(shift_tgt_[0], shape_.box[0].length() - c[0].min);
geom::vector<float, 2> min;
min[0] = shape_.box[0].length() - c[0].min;
min[1] = shape_.box[1].length() - c[1].min;
shift_tgt_[0] = std::max(shift_tgt_[0], min[0]);
horizontal_stick_ = (shift_tgt_[0] == min[0]) || (min[0] > 0.f);
shift_tgt_[0] = std::min(shift_tgt_[0], 0.f);
shift_tgt_[1] = std::max(shift_tgt_[1], shape_.box[1].length() - c[1].min);
shift_tgt_[1] = std::max(shift_tgt_[1], min[1]);
vertical_stick_ = (shift_tgt_[1] == min[1]) || (min[1] > 0.f);
shift_tgt_[1] = std::min(shift_tgt_[1], 0.f);
}