Support button onclick offset in ui styles

This commit is contained in:
Nikita Lisitsa 2021-03-03 16:54:42 +03:00
parent 8aa6c8a3bb
commit fe60386ad7
5 changed files with 36 additions and 8 deletions

View file

@ -40,7 +40,7 @@ namespace psemek::ui
virtual state_t state() const { return state_; }
virtual void on_state_changed() {}
virtual void on_state_changed(state_t old);
void set_label(std::shared_ptr<struct label> label);
void set_icon(std::shared_ptr<image_view> icon);

View file

@ -15,6 +15,8 @@ namespace psemek::ui
std::optional<gfx::color_rgba> highlight_color;
std::optional<gfx::color_rgba> action_color;
std::optional<geom::vector<int, 2>> action_offset;
std::optional<gfx::color_rgba> border_color;
std::optional<int> border_width;

View file

@ -17,15 +17,16 @@ namespace psemek::ui
if (over)
{
state_ = state_t::mouseover;
on_state_changed();
on_state_changed(state_t::normal);
}
break;
case state_t::mouseover:
case state_t::mousedown:
if (!over)
{
auto old = state_;
state_ = state_t::normal;
on_state_changed();
on_state_changed(old);
}
break;
}
@ -46,7 +47,7 @@ namespace psemek::ui
state_ = state_t::mousedown;
if (callback_)
post(callback_);
on_state_changed();
on_state_changed(state_t::mouseover);
return true;
}
break;
@ -54,7 +55,7 @@ namespace psemek::ui
if (!e.down)
{
state_ = state_t::mouseover;
on_state_changed();
on_state_changed(state_t::mousedown);
return true;
}
break;
@ -63,6 +64,22 @@ namespace psemek::ui
return false;
}
void button::on_state_changed(state_t old)
{
auto s = merged_style();
if (state() == state_t::mousedown)
{
for (auto c : children())
if (c) c->reshape(c->shape().bbox() + geom::cast<float>(*s->action_offset));
}
else if (old == state_t::mousedown)
{
for (auto c : children())
if (c) c->reshape(c->shape().bbox() - geom::cast<float>(*s->action_offset));
}
}
void button::set_label(std::shared_ptr<struct label> label)
{
if (icon())

View file

@ -46,8 +46,10 @@ namespace psemek::ui
if (st && c) c->reshape(geom::shrink(bbox, 1.f * (*st->border_width + *st->inner_margin)));
}
void on_state_changed() override
void on_state_changed(state_t old) override
{
button::on_state_changed(old);
gfx::color_rgba color{0, 0, 0, 0};
switch (state()) {
@ -73,15 +75,19 @@ namespace psemek::ui
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, *st->border_color);
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)), color);
p.draw_rect(geom::shrink(shape_.box, 1.f * (*st->border_width)) + offset, color);
}
geom::box<float, 2> size_constraints() const override

View file

@ -21,6 +21,7 @@ namespace psemek::ui
merge(dst.fg_color, src.fg_color);
merge(dst.highlight_color, src.highlight_color);
merge(dst.action_color, src.action_color);
merge(dst.action_offset, src.action_offset);
merge(dst.border_color, src.border_color);
merge(dst.border_width, src.border_width);
merge(dst.shadow_offset, src.shadow_offset);
@ -41,6 +42,8 @@ namespace psemek::ui
s.highlight_color = {159, 159, 159, 255};
s.action_color = {63, 63, 63, 255};
s.action_offset = {1, 1};
s.border_color = {255, 255, 255, 255};
s.border_width = 3;