125 lines
2.6 KiB
C++
125 lines
2.6 KiB
C++
#include <psemek/ui/default_element_factory.hpp>
|
|
|
|
#include <psemek/ui/box_shape.hpp>
|
|
|
|
namespace psemek::ui
|
|
{
|
|
|
|
namespace
|
|
{
|
|
|
|
struct button_impl
|
|
: button
|
|
{
|
|
button_impl()
|
|
{
|
|
set_label(std::make_shared<struct label>());
|
|
label()->set_valign(label::valignment::center);
|
|
label()->set_halign(label::halignment::center);
|
|
label()->set_overflow(label::overflow_mode::drop);
|
|
label()->set_multiline(label::multiline_mode::none);
|
|
}
|
|
|
|
struct shape const & shape() const override
|
|
{
|
|
return shape_;
|
|
}
|
|
|
|
void reshape(geom::box<float, 2> const & bbox) override
|
|
{
|
|
shape_.box = bbox;
|
|
auto s = style();
|
|
if (s && label()) label()->reshape(geom::shrink(bbox, 1.f * (s->border_width + s->inner_margin)));
|
|
}
|
|
|
|
void draw(painter & p) const override
|
|
{
|
|
auto s = style();
|
|
if (!s) return;
|
|
|
|
if (s->shadow_offset != geom::vector{0, 0})
|
|
p.draw_rect(shape_.box + geom::cast<float>(s->shadow_offset), s->shadow_color);
|
|
|
|
if (s->border_width > 0)
|
|
p.draw_rect(shape_.box, s->border_color);
|
|
|
|
gfx::color_rgba color = s->fg_color;
|
|
if (state() == state_t::mouseover)
|
|
color = s->highlight_color;
|
|
else if (state() == state_t::mousedown)
|
|
color = s->action_color;
|
|
p.draw_rect(geom::shrink(shape_.box, 1.f * s->border_width), color);
|
|
}
|
|
|
|
geom::box<float, 2> size_constraints() const override
|
|
{
|
|
auto sc = element::size_constraints();
|
|
|
|
if (label())
|
|
sc = label()->size_constraints();
|
|
|
|
if (auto st = style())
|
|
{
|
|
float extra = 2.f * (st->border_width + st->inner_margin);
|
|
sc[0] += extra;
|
|
sc[1] += extra;
|
|
}
|
|
|
|
return sc;
|
|
}
|
|
|
|
private:
|
|
box_shape shape_;
|
|
};
|
|
|
|
}
|
|
|
|
struct default_element_factory::impl
|
|
{
|
|
std::shared_ptr<struct style> style;
|
|
|
|
impl();
|
|
|
|
template <typename T>
|
|
std::shared_ptr<T> create()
|
|
{
|
|
auto r = std::make_shared<T>();
|
|
r->set_style(style);
|
|
return r;
|
|
}
|
|
};
|
|
|
|
default_element_factory::impl::impl()
|
|
: style{std::make_shared<struct style>()}
|
|
{}
|
|
|
|
default_element_factory::default_element_factory()
|
|
: pimpl_{make_impl()}
|
|
{}
|
|
|
|
default_element_factory::~default_element_factory() = default;
|
|
|
|
void default_element_factory::set_style(std::shared_ptr<struct style> st)
|
|
{
|
|
impl().style = st;
|
|
}
|
|
|
|
std::shared_ptr<struct style> default_element_factory::style() const
|
|
{
|
|
return impl().style;
|
|
}
|
|
|
|
std::shared_ptr<button> default_element_factory::make_button(std::string text)
|
|
{
|
|
auto r = impl().create<button_impl>();
|
|
r->label()->set_text(std::move(text));
|
|
r->label()->set_style(impl().style);
|
|
return r;
|
|
}
|
|
|
|
std::shared_ptr<screen> default_element_factory::make_screen()
|
|
{
|
|
return std::make_shared<screen>();
|
|
}
|
|
|
|
}
|