Support button icons
This commit is contained in:
parent
e0e1b97860
commit
fd5a9b7863
4 changed files with 71 additions and 14 deletions
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include <psemek/ui/element.hpp>
|
||||
#include <psemek/ui/label.hpp>
|
||||
#include <psemek/ui/image_view.hpp>
|
||||
|
||||
#include <functional>
|
||||
|
||||
|
|
@ -14,6 +15,9 @@ namespace psemek::ui
|
|||
struct label * label() { return label_.get(); }
|
||||
struct label const * label() const { return label_.get(); }
|
||||
|
||||
struct image_view * icon() { return icon_.get(); }
|
||||
struct image_view const * icon() const { return icon_.get(); }
|
||||
|
||||
children_range children() const override;
|
||||
|
||||
bool on_event(mouse_move const & e) override;
|
||||
|
|
@ -39,6 +43,7 @@ namespace psemek::ui
|
|||
virtual void on_state_changed() {}
|
||||
|
||||
void set_label(std::shared_ptr<struct label> label);
|
||||
void set_icon(std::shared_ptr<image_view> icon);
|
||||
|
||||
private:
|
||||
state_t state_ = state_t::normal;
|
||||
|
|
@ -46,6 +51,7 @@ namespace psemek::ui
|
|||
on_click_callback callback_;
|
||||
|
||||
std::shared_ptr<struct label> label_;
|
||||
std::shared_ptr<image_view> icon_;
|
||||
element * children_[1]{nullptr};
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@
|
|||
#include <psemek/ui/screen.hpp>
|
||||
#include <psemek/ui/grid_layout.hpp>
|
||||
|
||||
#include <psemek/gfx/texture.hpp>
|
||||
|
||||
#include <psemek/util/pimpl.hpp>
|
||||
|
||||
namespace psemek::ui
|
||||
|
|
@ -18,6 +20,7 @@ namespace psemek::ui
|
|||
~default_element_factory();
|
||||
|
||||
std::shared_ptr<button> make_button(std::string text);
|
||||
std::shared_ptr<button> make_button(std::shared_ptr<gfx::texture_2d> icon);
|
||||
std::shared_ptr<label> make_label(std::string text);
|
||||
std::shared_ptr<frame> make_frame();
|
||||
std::shared_ptr<screen> make_screen();
|
||||
|
|
|
|||
|
|
@ -5,8 +5,6 @@ namespace psemek::ui
|
|||
|
||||
element::children_range button::children() const
|
||||
{
|
||||
if (!label_)
|
||||
return {};
|
||||
return children_range{children_};
|
||||
}
|
||||
|
||||
|
|
@ -67,6 +65,9 @@ namespace psemek::ui
|
|||
|
||||
void button::set_label(std::shared_ptr<struct label> label)
|
||||
{
|
||||
if (icon())
|
||||
set_icon(nullptr);
|
||||
|
||||
if (label_) label_->set_parent(nullptr);
|
||||
|
||||
label_ = std::move(label);
|
||||
|
|
@ -74,4 +75,16 @@ namespace psemek::ui
|
|||
if (label_) label_->set_parent(this);
|
||||
}
|
||||
|
||||
void button::set_icon(std::shared_ptr<image_view> icon)
|
||||
{
|
||||
if (label())
|
||||
set_label(nullptr);
|
||||
|
||||
if (icon_) icon_->set_parent(nullptr);
|
||||
|
||||
icon_ = std::move(icon);
|
||||
children_[0] = icon_.get();
|
||||
if (icon_) icon_->set_parent(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,15 +11,24 @@ namespace psemek::ui
|
|||
struct button_impl
|
||||
: button
|
||||
{
|
||||
button_impl()
|
||||
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_;
|
||||
|
|
@ -29,7 +38,27 @@ namespace psemek::ui
|
|||
{
|
||||
shape_.box = bbox;
|
||||
auto s = style();
|
||||
if (s && label()) label()->reshape(geom::shrink(bbox, 1.f * (s->border_width + s->inner_margin)));
|
||||
element * c = label() ? (element *)label() : icon();
|
||||
if (s && c) c->reshape(geom::shrink(bbox, 1.f * (s->border_width + s->inner_margin)));
|
||||
}
|
||||
|
||||
void on_state_changed() override
|
||||
{
|
||||
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
|
||||
|
|
@ -55,14 +84,18 @@ namespace psemek::ui
|
|||
{
|
||||
auto sc = element::size_constraints();
|
||||
|
||||
if (label())
|
||||
sc = label()->size_constraints();
|
||||
element const * c = label() ? (element const *)label() : icon();
|
||||
|
||||
if (auto st = style())
|
||||
if (c)
|
||||
{
|
||||
float extra = 2.f * (st->border_width + st->inner_margin);
|
||||
sc[0] += extra;
|
||||
sc[1] += extra;
|
||||
sc = c->size_constraints();
|
||||
|
||||
if (auto st = style())
|
||||
{
|
||||
float extra = 2.f * (st->border_width + st->inner_margin);
|
||||
sc[0] += extra;
|
||||
sc[1] += extra;
|
||||
}
|
||||
}
|
||||
|
||||
return sc;
|
||||
|
|
@ -75,7 +108,6 @@ namespace psemek::ui
|
|||
struct frame_impl
|
||||
: frame
|
||||
{
|
||||
|
||||
struct shape const & shape() const override { return shape_; }
|
||||
|
||||
void reshape(geom::box<float, 2> const & bbox) override
|
||||
|
|
@ -150,9 +182,12 @@ namespace psemek::ui
|
|||
|
||||
std::shared_ptr<button> default_element_factory::make_button(std::string text)
|
||||
{
|
||||
auto r = std::make_shared<button_impl>();
|
||||
r->label()->set_text(std::move(text));
|
||||
return r;
|
||||
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<label> default_element_factory::make_label(std::string text)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue