psemek/libs/ui/source/impl/floating_base.cpp

49 lines
1.3 KiB
C++

#include <psemek/ui/impl/floating_base.hpp>
#include <psemek/ui/alignment.hpp>
#include <psemek/react/join.hpp>
#include <psemek/react/map.hpp>
namespace psemek::ui::impl
{
namespace
{
geom::box<float, 2> compute_child_shape(geom::box<float, 2> const & size_constraints, geom::point<float, 2> const & anchor, halignment halign, valignment valign)
{
geom::vector size{size_constraints[0].min, size_constraints[1].min};
geom::box<float, 2> result;
result[0] = geom::interval{- size[0], 0.f} + (1.f - lerp_factor(halign)) * size[0] + anchor[0];
result[1] = geom::interval{- size[1], 0.f} + (1.f - lerp_factor(valign)) * size[1] + anchor[1];
return result;
}
}
floating_base::floating_base()
: size_constraints_(impl::size_constraints::max())
{}
void floating_base::reshape(geom::box<float, 2> const & new_shape)
{
single_container::reshape(new_shape);
if (auto child = this->child())
child->reshape(compute_child_shape((*(child->size_constraints())).box, *anchor_, *halign_, *valign_));
}
react::value<struct size_constraints> floating_base::size_constraints() const
{
return size_constraints_;
}
void floating_base::update(floating const & value)
{
anchor_ = value.anchor;
halign_ = value.halign;
valign_ = value.valign;
}
}