443 lines
11 KiB
C++
443 lines
11 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
|
|
{
|
|
struct shape const & shape() const override
|
|
{
|
|
return shape_;
|
|
}
|
|
|
|
void reshape(geom::box<float, 2> const & bbox) override
|
|
{
|
|
shape_.box = bbox;
|
|
auto st = merged_style();
|
|
|
|
geom::vector offset{0.f, 0.f};
|
|
if (state() == state_t::mousedown)
|
|
offset = geom::cast<float>(*st->action_offset);
|
|
|
|
element * c = label() ? (element *)label() : icon();
|
|
if (c) c->reshape(geom::shrink(bbox, 1.f * (*st->border_width + *st->inner_margin)) + offset);
|
|
}
|
|
|
|
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_button_->set_icon(std::make_shared<image_view>());
|
|
close_button_->icon()->set_image(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 slider_impl
|
|
: slider
|
|
{
|
|
struct shape const & shape() const override { return shape_; }
|
|
|
|
void reshape(geom::box<float, 2> const & bbox)
|
|
{
|
|
shape_.box = bbox;
|
|
}
|
|
|
|
geom::box<float, 2> size_constraints() const
|
|
{
|
|
static float const inf = std::numeric_limits<float>::infinity();
|
|
return {{{button_width(), inf}, {button_height(), inf}}};
|
|
}
|
|
|
|
void draw(painter & p) const
|
|
{
|
|
auto st = merged_style();
|
|
|
|
geom::box<float, 2> ab;
|
|
ab[0] = shape_.box[0];
|
|
ab[1] = geom::expand(geom::interval<float>::singleton(shape_.box[1].center()), axis_width() / 2.f);
|
|
|
|
gfx::color_rgba c = *st->fg_color;
|
|
if (state() == state_t::mouseover)
|
|
c = *st->highlight_color;
|
|
else if (state() == state_t::mousedown)
|
|
c = *st->action_color;
|
|
|
|
auto const r = slider_range();
|
|
|
|
float x = geom::lerp(r, geom::unlerp<float>(geom::cast<float>(value_range()), value()));
|
|
|
|
geom::box<float, 2> sb;
|
|
sb[0] = geom::expand(geom::interval<float>::singleton(x), button_width() / 2.f);
|
|
sb[1] = geom::expand(geom::interval<float>::singleton(shape_.box[1].center()), button_height() / 2.f);
|
|
|
|
if (*st->shadow_offset != geom::vector{0, 0})
|
|
{
|
|
auto off = geom::cast<float>(*st->shadow_offset);
|
|
p.draw_rect(ab + off, *st->shadow_color);
|
|
p.draw_rect(sb + off, *st->shadow_color);
|
|
}
|
|
|
|
p.draw_rect(ab, *st->axis_color);
|
|
if (*st->border_width != 0)
|
|
p.draw_rect(sb, *st->border_color);
|
|
p.draw_rect(geom::shrink(sb, 1.f * (*st->border_width)), c);
|
|
}
|
|
|
|
protected:
|
|
|
|
geom::interval<float> slider_range() const override
|
|
{
|
|
return geom::shrink(shape_.box[0], button_width() / 2.f);
|
|
}
|
|
|
|
private:
|
|
box_shape shape_;
|
|
|
|
int button_width() const
|
|
{
|
|
auto st = merged_style();
|
|
return *st->ref_height / 3;
|
|
}
|
|
|
|
int button_height() const
|
|
{
|
|
auto st = merged_style();
|
|
return *st->ref_height;
|
|
}
|
|
|
|
int axis_width() const
|
|
{
|
|
auto st = merged_style();
|
|
return *st->ref_height / 5;
|
|
}
|
|
};
|
|
|
|
}
|
|
|
|
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()
|
|
{
|
|
return std::make_shared<button_impl>();
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
std::shared_ptr<slider> default_element_factory::make_slider()
|
|
{
|
|
return std::make_shared<slider_impl>();
|
|
}
|
|
|
|
}
|