From fe60386ad773651bc6e2b6519171b25166c9c9c4 Mon Sep 17 00:00:00 2001 From: lisyarus Date: Wed, 3 Mar 2021 16:54:42 +0300 Subject: [PATCH] Support button onclick offset in ui styles --- libs/ui/include/psemek/ui/button.hpp | 2 +- libs/ui/include/psemek/ui/style.hpp | 2 ++ libs/ui/source/button.cpp | 25 ++++++++++++++++++---- libs/ui/source/default_element_factory.cpp | 12 ++++++++--- libs/ui/source/style.cpp | 3 +++ 5 files changed, 36 insertions(+), 8 deletions(-) diff --git a/libs/ui/include/psemek/ui/button.hpp b/libs/ui/include/psemek/ui/button.hpp index 914d3a33..6f6429d9 100644 --- a/libs/ui/include/psemek/ui/button.hpp +++ b/libs/ui/include/psemek/ui/button.hpp @@ -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 label); void set_icon(std::shared_ptr icon); diff --git a/libs/ui/include/psemek/ui/style.hpp b/libs/ui/include/psemek/ui/style.hpp index 5176c4bc..f3bed65d 100644 --- a/libs/ui/include/psemek/ui/style.hpp +++ b/libs/ui/include/psemek/ui/style.hpp @@ -15,6 +15,8 @@ namespace psemek::ui std::optional highlight_color; std::optional action_color; + std::optional> action_offset; + std::optional border_color; std::optional border_width; diff --git a/libs/ui/source/button.cpp b/libs/ui/source/button.cpp index 9fc3e8c4..cdfacb21 100644 --- a/libs/ui/source/button.cpp +++ b/libs/ui/source/button.cpp @@ -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(*s->action_offset)); + } + else if (old == state_t::mousedown) + { + for (auto c : children()) + if (c) c->reshape(c->shape().bbox() - geom::cast(*s->action_offset)); + } + } + void button::set_label(std::shared_ptr label) { if (icon()) diff --git a/libs/ui/source/default_element_factory.cpp b/libs/ui/source/default_element_factory.cpp index 44065a57..c8e204c5 100644 --- a/libs/ui/source/default_element_factory.cpp +++ b/libs/ui/source/default_element_factory.cpp @@ -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(*st->shadow_offset), *st->shadow_color); + auto offset = geom::vector{0.f, 0.f}; + if (state() == state_t::mousedown) + offset = geom::cast(*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 size_constraints() const override diff --git a/libs/ui/source/style.cpp b/libs/ui/source/style.cpp index b36f7ba6..1f19a2c2 100644 --- a/libs/ui/source/style.cpp +++ b/libs/ui/source/style.cpp @@ -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;