psemek/libs/ui/source/default_element_factory.cpp

373 lines
9.1 KiB
C++

#include <psemek/ui/default_element_factory.hpp>
#include <psemek/ui/box_shape.hpp>
#include <psemek/util/memory_stream.hpp>
#include <psemek/ui/resources/cross_red_16x16_png.hpp>
namespace psemek::ui
{
namespace
{
struct button_impl
: button
{
button_impl(std::string text)
{
set_label(std::make_shared<struct label>());
label()->set_text(std::move(text));
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);
}
button_impl(std::shared_ptr<gfx::texture_2d> tex)
{
set_icon(std::make_shared<image_view>());
icon()->set_image(std::move(tex));
icon()->set_downscale(false);
icon()->set_upscale(false);
}
struct shape const & shape() const override
{
return shape_;
}
void reshape(geom::box<float, 2> const & bbox) override
{
shape_.box = bbox;
auto st = merged_style();
element * c = label() ? (element *)label() : icon();
if (st && c) c->reshape(geom::shrink(bbox, 1.f * (*st->border_width + *st->inner_margin)));
}
void on_state_changed(state_t old) override
{
button::on_state_changed(old);
gfx::color_rgba color{0, 0, 0, 0};
switch (state()) {
case state_t::mouseover:
color = {255, 255, 255, 63};
break;
case state_t::mousedown:
color = {0, 0, 0, 63};
break;
default:
break;
}
if (icon())
icon()->set_color(color);
}
void draw(painter & p) const override
{
auto st = merged_style();
if (!st) return;
if (st->shadow_offset != geom::vector{0, 0})
p.draw_rect(shape_.box + geom::cast<float>(*st->shadow_offset), *st->shadow_color);
auto offset = geom::vector{0.f, 0.f};
if (state() == state_t::mousedown)
offset = geom::cast<float>(*st->action_offset);
if (st->border_width > 0)
p.draw_rect(shape_.box + offset, *st->border_color);
gfx::color_rgba color = *st->fg_color;
if (state() == state_t::mouseover)
color = *st->highlight_color;
else if (state() == state_t::mousedown)
color = *st->action_color;
p.draw_rect(geom::shrink(shape_.box, 1.f * (*st->border_width)) + offset, color);
}
geom::box<float, 2> size_constraints() const override
{
auto sc = element::size_constraints();
element const * c = label() ? (element const *)label() : icon();
if (c)
{
sc = c->size_constraints();
if (auto st = merged_style())
{
float extra = 2.f * (*st->border_width + *st->inner_margin);
sc[0] += extra;
sc[1] += extra;
}
}
return sc;
}
private:
box_shape shape_;
};
struct frame_impl
: frame
{
struct shape const & shape() const override { return shape_; }
void reshape(geom::box<float, 2> const & bbox) override
{
shape_.box = bbox;
auto st = merged_style();
if (!st) return;
for (auto c : children())
if (c) c->reshape(geom::shrink(bbox, 1.f * (*st->border_width + *st->outer_margin)));
}
geom::box<float, 2> size_constraints() const override
{
geom::box<float, 2> r = element::size_constraints();
for (auto c : children())
if (c)
r = c->size_constraints();
auto st = merged_style();
if (st)
{
float extra = 2.f * (*st->border_width + *st->outer_margin);
r[0] += extra;
r[1] += extra;
}
if (min_size())
{
auto s = *min_size();
r[0].min = s[0];
r[1].min = s[1];
}
if (max_size())
{
auto s = *max_size();
r[0].max = s[0];
r[1].max = s[1];
}
return r;
}
void draw(painter & p) const override
{
auto st = merged_style();
if (!st) return;
if (st->shadow_offset != geom::vector{0, 0})
p.draw_rect(shape_.box + geom::cast<float>(*st->shadow_offset), *st->shadow_color);
if (st->border_width > 0)
p.draw_rect(shape_.box, *st->border_color);
p.draw_rect(geom::shrink(shape_.box, 1.f * (*st->border_width)), *st->bg_color);
}
private:
box_shape shape_;
};
struct window_impl
: window
{
window_impl(std::shared_ptr<gfx::texture_2d> close_icon)
: caption_(std::make_shared<label>())
, close_button_(std::make_shared<button_impl>(close_icon))
{
caption_->set_parent(this);
close_button_->set_parent(this);
caption_->set_valign(label::valignment::center);
caption_->set_overflow(label::overflow_mode::ellipsis);
auto close_button_style = std::make_shared<struct style>();
close_button_style->shadow_offset = {0, 0};
close_button_->set_style(close_button_style);
children_[0] = caption_.get();
children_[1] = close_button_.get();
children_[2] = nullptr;
}
void setup_close()
{
auto weak_self = weak_from_this();
close_button_->on_click([weak_self]{
if (auto self = weak_self.lock())
{
if (auto p = dynamic_cast<container *>(self->parent()))
p->remove_child(self.get());
else
throw std::runtime_error("Cannot remove window from non-container parent");
}
});
}
void set_caption(std::string caption) override
{
caption_->set_text(caption);
}
std::shared_ptr<element> set_child(std::shared_ptr<element> c) override
{
if (child_) child_->set_parent(nullptr);
std::swap(child_, c);
if (child_) child_->set_parent(this);
children_[2] = child_.get();
return c;
}
children_range children() const override
{
return children_range{children_};
}
struct shape const & shape() const override
{
return shape_;
}
void reshape(geom::box<float, 2> const & bbox) override
{
shape_.box = bbox;
auto lc = caption_->size_constraints();
auto bc = close_button_->size_constraints();
auto st = merged_style();
float const header_height = std::max(lc[1].min + 2.f * (*st->border_width), bc[1].min);
{
geom::box<float, 2> b;
b[0].min = bbox[0].max - bc[0].min;
b[0].max = bbox[0].max;
b[1].min = bbox[1].min;
b[1].max = bbox[1].min + header_height;
close_button_->reshape(b);
}
{
geom::box<float, 2> b;
b[0].min = bbox[0].min + (*st->border_width);
b[0].max = bbox[0].max - bc[0].min;
b[1].min = bbox[1].min + (*st->border_width);
b[1].max = bbox[1].min + header_height - (*st->border_width);
caption_->reshape(b);
}
if (child_)
{
geom::box<float, 2> b;
b[0].min = bbox[0].min + (*st->border_width);
b[0].max = bbox[0].max - (*st->border_width);
b[1].min = bbox[1].min + header_height;
b[1].max = bbox[1].max - (*st->border_width);
child_->reshape(b);
}
}
geom::box<float, 2> size_constraints() const override
{
geom::box<float, 2> r = element::size_constraints();
if (child_) r = child_->size_constraints();
auto lc = caption_->size_constraints();
auto bc = close_button_->size_constraints();
auto st = merged_style();
float const header_height = std::max(lc[1].min + 2.f * (*st->border_width), bc[1].min);
r[0].min = std::max(r[0].min + 2.f * (*st->border_width), (*st->border_width) + lc[0].min + bc[0].min);
r[1] += (*st->border_width) * 1.f + header_height;
return r;
}
void draw(painter & p) const override
{
auto st = merged_style();
auto lc = caption_->size_constraints();
auto bc = close_button_->size_constraints();
auto const & bb = shape_.box;
if (*st->shadow_offset != geom::vector{0, 0})
p.draw_rect(bb + geom::cast<float>(*st->shadow_offset), *st->shadow_color);
p.draw_rect(bb, *st->border_color);
float const header_height = std::max(lc[1].min + 2.f * (*st->border_width), bc[1].min);
p.draw_rect({{{bb[0].min + (*st->border_width), bb[0].max - bc[0].min}, {bb[1].min + (*st->border_width), bb[1].min + header_height - (*st->border_width)}}},
*st->fg_color);
}
private:
std::shared_ptr<label> caption_;
std::shared_ptr<button> close_button_;
std::shared_ptr<element> child_;
box_shape shape_;
element * children_[3];
};
}
struct default_element_factory::impl
{
std::shared_ptr<gfx::texture_2d> cross_red_16x16;
impl();
};
default_element_factory::impl::impl()
{
util::memory_istream is{resources::cross_red_16x16_png};
cross_red_16x16 = std::make_shared<gfx::texture_2d>(gfx::texture_2d::from_pixmap(gfx::read_png(is)));
cross_red_16x16->nearest_filter();
}
default_element_factory::default_element_factory()
: pimpl_{make_impl()}
{}
default_element_factory::~default_element_factory() = default;
std::shared_ptr<button> default_element_factory::make_button(std::string text)
{
return std::make_shared<button_impl>(std::move(text));
}
std::shared_ptr<button> default_element_factory::make_button(std::shared_ptr<gfx::texture_2d> icon)
{
return std::make_shared<button_impl>(std::move(icon));
}
std::shared_ptr<frame> default_element_factory::make_frame()
{
return std::make_shared<frame_impl>();
}
std::shared_ptr<window> default_element_factory::make_window(std::string caption)
{
auto r = std::make_shared<window_impl>(impl().cross_red_16x16);
r->setup_close();
r->set_caption(std::move(caption));
return r;
}
}