Separate ui::button into button & rich_button

This commit is contained in:
Nikita Lisitsa 2021-03-05 22:57:39 +03:00
parent c54a9944f1
commit 4383ce7e8b
8 changed files with 83 additions and 71 deletions

View file

@ -12,17 +12,6 @@ namespace psemek::ui
struct button
: element
{
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(); }
void set_label(std::shared_ptr<struct label> label);
void set_icon(std::shared_ptr<image_view> icon);
children_range children() const override;
bool on_event(mouse_move const & e) override;
bool on_event(mouse_click const & e) override;
@ -33,8 +22,6 @@ namespace psemek::ui
callback_ = std::move(callback);
}
~button() override;
protected:
enum class state_t
{
@ -51,10 +38,6 @@ namespace psemek::ui
state_t state_ = state_t::normal;
on_click_callback callback_;
std::shared_ptr<struct label> label_;
std::shared_ptr<image_view> icon_;
element * children_[1]{nullptr};
};
}

View file

@ -2,15 +2,6 @@
#include <psemek/ui/element_factory.hpp>
#include <psemek/ui/button.hpp>
#include <psemek/ui/label.hpp>
#include <psemek/ui/frame.hpp>
#include <psemek/ui/window.hpp>
#include <psemek/ui/screen.hpp>
#include <psemek/ui/grid_layout.hpp>
#include <psemek/gfx/texture.hpp>
#include <psemek/util/pimpl.hpp>
namespace psemek::ui
@ -22,7 +13,7 @@ namespace psemek::ui
default_element_factory();
~default_element_factory();
std::shared_ptr<button> make_button() override;
std::shared_ptr<rich_button> make_button() override;
std::shared_ptr<frame> make_frame() override;
std::shared_ptr<window> make_window(std::string caption) override;
std::shared_ptr<slider> make_slider() override;

View file

@ -1,6 +1,7 @@
#pragma once
#include <psemek/ui/button.hpp>
#include <psemek/ui/rich_button.hpp>
#include <psemek/ui/label.hpp>
#include <psemek/ui/frame.hpp>
#include <psemek/ui/window.hpp>
@ -17,9 +18,9 @@ namespace psemek::ui
struct element_factory
{
virtual std::shared_ptr<button> make_button();
virtual std::shared_ptr<button> make_button(std::string text);
virtual std::shared_ptr<button> make_button(std::shared_ptr<gfx::texture_2d> icon);
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(std::shared_ptr<gfx::texture_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

@ -0,0 +1,31 @@
#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(); }
void set_label(std::shared_ptr<struct label> label);
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,11 +3,6 @@
namespace psemek::ui
{
element::children_range button::children() const
{
return children_range{children_};
}
bool button::on_event(mouse_move const & e)
{
bool const over = shape().contains(geom::cast<float>(e.position));
@ -64,11 +59,6 @@ namespace psemek::ui
return false;
}
button::~button()
{
release_children();
}
void button::on_state_changed(state_t old)
{
if (state() == state_t::mousedown || old == state_t::mousedown)
@ -77,28 +67,4 @@ 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);
children_[0] = label_.get();
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);
}
}

View file

@ -13,7 +13,7 @@ namespace psemek::ui
{
struct button_impl
: button
: rich_button
{
struct shape const & shape() const override
{
@ -35,7 +35,7 @@ namespace psemek::ui
void on_state_changed(state_t old) override
{
button::on_state_changed(old);
rich_button::on_state_changed(old);
gfx::color_rgba color{0, 0, 0, 0};
@ -307,7 +307,7 @@ namespace psemek::ui
private:
std::shared_ptr<label> caption_;
std::shared_ptr<button> close_button_;
std::shared_ptr<rich_button> close_button_;
std::shared_ptr<element> child_;
box_shape shape_;
@ -417,7 +417,7 @@ namespace psemek::ui
default_element_factory::~default_element_factory() = default;
std::shared_ptr<button> default_element_factory::make_button()
std::shared_ptr<rich_button> default_element_factory::make_button()
{
return std::make_shared<button_impl>();
}

View file

@ -3,9 +3,9 @@
namespace psemek::ui
{
std::shared_ptr<button> element_factory::make_button() { return nullptr; }
std::shared_ptr<rich_button> element_factory::make_button() { return nullptr; }
std::shared_ptr<button> element_factory::make_button(std::string text)
std::shared_ptr<rich_button> element_factory::make_button(std::string text)
{
auto b = make_button();
if (b)
@ -23,7 +23,7 @@ namespace psemek::ui
return b;
}
std::shared_ptr<button> element_factory::make_button(std::shared_ptr<gfx::texture_2d> image)
std::shared_ptr<rich_button> element_factory::make_button(std::shared_ptr<gfx::texture_2d> image)
{
auto b = make_button();
if (b)

View file

@ -0,0 +1,40 @@
#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);
}
}