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

65 lines
1.9 KiB
C++

#include <psemek/ui/impl/fixed_size_base.hpp>
#include <psemek/ui/alignment.hpp>
#include <psemek/react/join.hpp>
#include <psemek/react/map.hpp>
namespace psemek::ui::impl
{
namespace
{
size_constraints compute_size_constraints(geom::vector<std::optional<float>, 2> const & size)
{
return {
.box = {{
size[0] ? geom::interval<float>::singleton(*(size[0])) : geom::interval<float>(0.f, size_constraints::infinity),
size[1] ? geom::interval<float>::singleton(*(size[1])) : geom::interval<float>(0.f, size_constraints::infinity),
}},
};
}
geom::box<float, 2> compute_shape(geom::box<float, 2> const & shape, geom::vector<std::optional<float>, 2> const & size)
{
return {{
size[0] ? align(shape[0], *(size[0]), halignment::center) : shape[0],
size[1] ? align(shape[1], *(size[1]), valignment::center) : shape[1],
}};
}
}
fixed_size_base::fixed_size_base()
: size_({0.f, 0.f})
, size_constraints_(react::map(compute_size_constraints, react::join(size_)))
{}
void fixed_size_base::reshape(geom::box<float, 2> const & new_shape)
{
single_container::reshape(new_shape);
if (auto child = this->child())
child->reshape(compute_shape(new_shape, **size_));
}
react::value<struct size_constraints> fixed_size_base::size_constraints() const
{
return size_constraints_;
}
void fixed_size_base::update(fixed_size const & value)
{
size_.set(react::map([](geom::vector<float, 2> const & size){ return geom::vector<std::optional<float>, 2>{size[0], size[1]}; }, value.size));
}
void fixed_size_base::update(fixed_width const & value)
{
size_.set(react::map([](float width){ return geom::vector<std::optional<float>, 2>{width, std::nullopt}; }, value.width));
}
void fixed_size_base::update(fixed_height const & value)
{
size_.set(react::map([](float height){ return geom::vector<std::optional<float>, 2>{std::nullopt, height}; }, value.height));
}
}