Remove rich_button, make any button able to contain a child element

This commit is contained in:
Nikita Lisitsa 2022-05-17 21:25:01 +03:00
parent 0f95a5bcee
commit bfb8ba407b
9 changed files with 33 additions and 95 deletions

View file

@ -4,7 +4,7 @@
#include <psemek/ui/grid_layout.hpp>
#include <psemek/ui/frame.hpp>
#include <psemek/ui/scroller.hpp>
#include <psemek/ui/rich_button.hpp>
#include <psemek/ui/button.hpp>
#include <psemek/util/recursive.hpp>

View file

@ -1,6 +1,6 @@
#pragma once
#include <psemek/ui/element.hpp>
#include <psemek/ui/single_container.hpp>
#include <psemek/ui/label.hpp>
#include <psemek/ui/image_view.hpp>
@ -10,8 +10,12 @@ namespace psemek::ui
{
struct button
: element
: single_container
{
struct label * label() const;
struct image_view * icon() const;
bool on_event(mouse_move const & e) override;
bool on_event(mouse_click const & e) override;

View file

@ -13,7 +13,7 @@ namespace psemek::ui
default_element_factory();
~default_element_factory();
std::shared_ptr<rich_button> make_button() override;
std::shared_ptr<button> make_button() override;
using element_factory::make_button;
std::shared_ptr<frame> make_frame() override;
std::shared_ptr<window> make_window(std::string caption) override;

View file

@ -9,7 +9,6 @@ namespace psemek::ui
{
struct button;
struct rich_button;
struct label;
struct frame;
struct window;
@ -29,9 +28,9 @@ namespace psemek::ui
struct element_factory
{
virtual std::shared_ptr<rich_button> make_button();
virtual std::shared_ptr<rich_button> make_button(std::string text);
virtual std::shared_ptr<rich_button> make_button(gfx::texture_view_2d icon);
virtual std::shared_ptr<button> make_button();
virtual std::shared_ptr<button> make_button(std::string text);
virtual std::shared_ptr<button> make_button(gfx::texture_view_2d icon);
virtual std::shared_ptr<label> make_label(std::string text);
virtual std::shared_ptr<frame> make_frame();
virtual std::shared_ptr<window> make_window(std::string caption);

View file

@ -1,31 +0,0 @@
#pragma once
#include <psemek/ui/button.hpp>
namespace psemek::ui
{
struct rich_button
: button
{
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(); }
virtual void set_label(std::shared_ptr<struct label> label);
virtual void set_icon(std::shared_ptr<image_view> icon);
children_range children() const override;
~rich_button() override;
private:
std::shared_ptr<struct label> label_;
std::shared_ptr<image_view> icon_;
element * children_[1]{nullptr};
};
}

View file

@ -3,6 +3,16 @@
namespace psemek::ui
{
label * button::label() const
{
return dynamic_cast<struct label *>(child().get());
}
image_view * button::icon() const
{
return dynamic_cast<struct image_view *>(child().get());
}
bool button::on_event(mouse_move const & e)
{
bool const over = shape().contains(geom::cast<float>(e.position));

View file

@ -1,6 +1,6 @@
#include <psemek/ui/default_element_factory.hpp>
#include <psemek/ui/rich_button.hpp>
#include <psemek/ui/button.hpp>
#include <psemek/ui/frame.hpp>
#include <psemek/ui/window.hpp>
#include <psemek/ui/checkbox.hpp>
@ -22,7 +22,7 @@ namespace psemek::ui
{
struct button_impl
: rich_button
: button
{
struct shape const & shape() const override
{
@ -44,7 +44,7 @@ namespace psemek::ui
void on_state_changed(state_t old) override
{
rich_button::on_state_changed(old);
button::on_state_changed(old);
gfx::color_rgba color{0, 0, 0, 0};
@ -221,7 +221,7 @@ namespace psemek::ui
, caption_(std::make_shared<label>())
, close_button_(std::make_shared<button_impl>())
{
close_button_->set_icon(std::make_shared<image_view>());
close_button_->set_child(std::make_shared<image_view>());
close_button_->icon()->set_image({close_texture_.get()});
caption_->set_parent(this);
@ -403,7 +403,7 @@ namespace psemek::ui
private:
std::shared_ptr<gfx::texture_2d> close_texture_;
std::shared_ptr<label> caption_;
std::shared_ptr<rich_button> close_button_;
std::shared_ptr<button> close_button_;
std::shared_ptr<element> child_;
on_close_callback on_close_;
@ -738,7 +738,7 @@ namespace psemek::ui
default_element_factory::~default_element_factory() = default;
std::shared_ptr<rich_button> default_element_factory::make_button()
std::shared_ptr<button> default_element_factory::make_button()
{
return std::make_shared<button_impl>();
}

View file

@ -1,7 +1,7 @@
#include <psemek/ui/element_factory.hpp>
#include <psemek/ui/label.hpp>
#include <psemek/ui/rich_button.hpp>
#include <psemek/ui/button.hpp>
#include <psemek/ui/toggle_button.hpp>
#include <psemek/ui/image_view.hpp>
#include <psemek/ui/rich_image_view.hpp>
@ -12,9 +12,9 @@
namespace psemek::ui
{
std::shared_ptr<rich_button> element_factory::make_button() { return nullptr; }
std::shared_ptr<button> element_factory::make_button() { return nullptr; }
std::shared_ptr<rich_button> element_factory::make_button(std::string text)
std::shared_ptr<button> element_factory::make_button(std::string text)
{
auto b = make_button();
if (b)
@ -25,13 +25,13 @@ namespace psemek::ui
l->set_valign(label::valignment::center);
l->set_halign(label::halignment::center);
l->set_overflow(label::overflow_mode::ellipsis);
b->set_label(l);
b->set_child(l);
}
}
return b;
}
std::shared_ptr<rich_button> element_factory::make_button(gfx::texture_view_2d icon)
std::shared_ptr<button> element_factory::make_button(gfx::texture_view_2d icon)
{
auto b = make_button();
if (b)
@ -41,7 +41,7 @@ namespace psemek::ui
{
i->set_downscale(false);
i->set_upscale(false);
b->set_icon(i);
b->set_child(i);
}
}
return b;

View file

@ -1,44 +0,0 @@
#include <psemek/ui/rich_button.hpp>
namespace psemek::ui
{
element::children_range rich_button::children() const
{
return children_range{children_};
}
rich_button::~rich_button()
{
release_children();
}
void rich_button::set_label(std::shared_ptr<struct label> label)
{
if (icon())
set_icon(nullptr);
if (label_) label_->set_parent(nullptr);
label_ = std::move(label);
children_[0] = label_.get();
if (label_) label_->set_parent(this);
}
void rich_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);
icon_->set_downscale(false);
}
}
}