Refactor button creation + create image view via element factory
This commit is contained in:
parent
0f9b8f72c1
commit
0bf4324ba4
4 changed files with 62 additions and 30 deletions
|
|
@ -22,8 +22,7 @@ namespace psemek::ui
|
||||||
default_element_factory();
|
default_element_factory();
|
||||||
~default_element_factory();
|
~default_element_factory();
|
||||||
|
|
||||||
std::shared_ptr<button> make_button(std::string text) override;
|
std::shared_ptr<button> make_button() override;
|
||||||
std::shared_ptr<button> make_button(std::shared_ptr<gfx::texture_2d> icon) override;
|
|
||||||
std::shared_ptr<frame> make_frame() override;
|
std::shared_ptr<frame> make_frame() override;
|
||||||
std::shared_ptr<window> make_window(std::string caption) override;
|
std::shared_ptr<window> make_window(std::string caption) override;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,8 @@
|
||||||
#include <psemek/ui/window.hpp>
|
#include <psemek/ui/window.hpp>
|
||||||
#include <psemek/ui/screen.hpp>
|
#include <psemek/ui/screen.hpp>
|
||||||
#include <psemek/ui/grid_layout.hpp>
|
#include <psemek/ui/grid_layout.hpp>
|
||||||
|
#include <psemek/ui/image_view.hpp>
|
||||||
|
#include <psemek/ui/rich_image_view.hpp>
|
||||||
|
|
||||||
#include <psemek/gfx/texture.hpp>
|
#include <psemek/gfx/texture.hpp>
|
||||||
|
|
||||||
|
|
@ -14,6 +16,7 @@ namespace psemek::ui
|
||||||
|
|
||||||
struct element_factory
|
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::string text);
|
||||||
virtual std::shared_ptr<button> make_button(std::shared_ptr<gfx::texture_2d> icon);
|
virtual std::shared_ptr<button> make_button(std::shared_ptr<gfx::texture_2d> icon);
|
||||||
virtual std::shared_ptr<label> make_label(std::string text);
|
virtual std::shared_ptr<label> make_label(std::string text);
|
||||||
|
|
@ -21,6 +24,8 @@ namespace psemek::ui
|
||||||
virtual std::shared_ptr<window> make_window(std::string caption);
|
virtual std::shared_ptr<window> make_window(std::string caption);
|
||||||
virtual std::shared_ptr<screen> make_screen();
|
virtual std::shared_ptr<screen> make_screen();
|
||||||
virtual std::shared_ptr<grid_layout> make_grid_layout();
|
virtual std::shared_ptr<grid_layout> make_grid_layout();
|
||||||
|
virtual std::shared_ptr<image_view> make_image_view(std::shared_ptr<gfx::texture_2d> image);
|
||||||
|
virtual std::shared_ptr<rich_image_view> make_rich_image_view(std::shared_ptr<gfx::texture_2d> image);
|
||||||
|
|
||||||
virtual ~element_factory() {}
|
virtual ~element_factory() {}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -15,24 +15,6 @@ namespace psemek::ui
|
||||||
struct button_impl
|
struct button_impl
|
||||||
: button
|
: button
|
||||||
{
|
{
|
||||||
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
|
struct shape const & shape() const override
|
||||||
{
|
{
|
||||||
return shape_;
|
return shape_;
|
||||||
|
|
@ -184,8 +166,11 @@ namespace psemek::ui
|
||||||
{
|
{
|
||||||
window_impl(std::shared_ptr<gfx::texture_2d> close_icon)
|
window_impl(std::shared_ptr<gfx::texture_2d> close_icon)
|
||||||
: caption_(std::make_shared<label>())
|
: caption_(std::make_shared<label>())
|
||||||
, close_button_(std::make_shared<button_impl>(close_icon))
|
, close_button_(std::make_shared<button_impl>())
|
||||||
{
|
{
|
||||||
|
close_button_->set_icon(std::make_shared<image_view>());
|
||||||
|
close_button_->icon()->set_image(close_icon);
|
||||||
|
|
||||||
caption_->set_parent(this);
|
caption_->set_parent(this);
|
||||||
close_button_->set_parent(this);
|
close_button_->set_parent(this);
|
||||||
|
|
||||||
|
|
@ -347,14 +332,9 @@ namespace psemek::ui
|
||||||
|
|
||||||
default_element_factory::~default_element_factory() = default;
|
default_element_factory::~default_element_factory() = default;
|
||||||
|
|
||||||
std::shared_ptr<button> default_element_factory::make_button(std::string text)
|
std::shared_ptr<button> default_element_factory::make_button()
|
||||||
{
|
{
|
||||||
return std::make_shared<button_impl>(std::move(text));
|
return std::make_shared<button_impl>();
|
||||||
}
|
|
||||||
|
|
||||||
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<frame> default_element_factory::make_frame()
|
std::shared_ptr<frame> default_element_factory::make_frame()
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,43 @@
|
||||||
|
|
||||||
namespace psemek::ui
|
namespace psemek::ui
|
||||||
{
|
{
|
||||||
std::shared_ptr<button> element_factory::make_button(std::string) { return nullptr; }
|
|
||||||
|
|
||||||
std::shared_ptr<button> element_factory::make_button(std::shared_ptr<gfx::texture_2d>) { return nullptr; }
|
std::shared_ptr<button> element_factory::make_button() { return nullptr; }
|
||||||
|
|
||||||
|
std::shared_ptr<button> element_factory::make_button(std::string text)
|
||||||
|
{
|
||||||
|
auto b = make_button();
|
||||||
|
if (b)
|
||||||
|
{
|
||||||
|
auto l = make_label(std::move(text));
|
||||||
|
if (l)
|
||||||
|
{
|
||||||
|
l->set_valign(label::valignment::center);
|
||||||
|
l->set_halign(label::halignment::center);
|
||||||
|
l->set_overflow(label::overflow_mode::ellipsis);
|
||||||
|
l->set_multiline(label::multiline_mode::none);
|
||||||
|
b->set_label(l);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<button> element_factory::make_button(std::shared_ptr<gfx::texture_2d> image)
|
||||||
|
{
|
||||||
|
auto b = make_button();
|
||||||
|
if (b)
|
||||||
|
{
|
||||||
|
auto i = make_image_view(std::move(image));
|
||||||
|
if (i)
|
||||||
|
{
|
||||||
|
i->set_downscale(false);
|
||||||
|
i->set_upscale(false);
|
||||||
|
b->set_icon(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return b;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
std::shared_ptr<label> element_factory::make_label(std::string text)
|
std::shared_ptr<label> element_factory::make_label(std::string text)
|
||||||
{
|
{
|
||||||
|
|
@ -25,4 +59,18 @@ namespace psemek::ui
|
||||||
return std::make_shared<grid_layout>();
|
return std::make_shared<grid_layout>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<image_view> element_factory::make_image_view(std::shared_ptr<gfx::texture_2d> image)
|
||||||
|
{
|
||||||
|
auto i = std::make_shared<image_view>();
|
||||||
|
i->set_image(std::move(image));
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<rich_image_view> element_factory::make_rich_image_view(std::shared_ptr<gfx::texture_2d> image)
|
||||||
|
{
|
||||||
|
auto i = std::make_shared<rich_image_view>();
|
||||||
|
i->set_image(std::move(image));
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue