Don't store font in painter or controller, use it in factory instead

This commit is contained in:
Nikita Lisitsa 2021-02-25 09:50:30 +03:00
parent 3cf308e7fe
commit 937a011577
8 changed files with 33 additions and 24 deletions

View file

@ -17,7 +17,7 @@ struct ui_example
ui_example()
: app("UI example", 1)
{
ui_controller.set_font(ui::make_default_9x12_font());
element_factory.set_font(ui::make_default_9x12_font());
auto screen = element_factory.make_screen();
screen->add(element_factory.make_button(), ui::screen::x_policy::center, ui::screen::y_policy::center);

View file

@ -16,7 +16,6 @@ namespace psemek::ui
std::unique_ptr<element> set_root(std::unique_ptr<element> r);
element * root();
void set_font(std::shared_ptr<font> f);
void reshape(geom::box<float, 2> const & shape);
bool event(mouse_move const & e);

View file

@ -3,13 +3,24 @@
#include <psemek/ui/button.hpp>
#include <psemek/ui/screen.hpp>
#include <psemek/util/pimpl.hpp>
namespace psemek::ui
{
struct default_element_factory
{
default_element_factory();
~default_element_factory();
void set_font(std::shared_ptr<struct font> f);
std::shared_ptr<struct font> font() const;
std::unique_ptr<button> make_button();
std::unique_ptr<screen> make_screen();
private:
psemek_declare_pimpl
};
}

View file

@ -9,8 +9,6 @@ namespace psemek::ui
struct painter
{
virtual struct font const * font() const = 0;
virtual void draw_rect(geom::box<float, 2> const & rect, gfx::color_rgba const & color) = 0;
virtual ~painter() {}

View file

@ -15,10 +15,6 @@ namespace psemek::ui
painter_impl();
~painter_impl();
void set_font(std::shared_ptr<struct font> font);
struct font const * font() const override;
void draw_rect(geom::box<float, 2> const & rect, gfx::color_rgba const & color) override;
void render(geom::matrix<float, 4, 4> const & transform);

View file

@ -60,11 +60,6 @@ namespace psemek::ui
return impl().root.get();
}
void controller::set_font(std::shared_ptr<font> f)
{
impl().painter.set_font(std::move(f));
}
void controller::reshape(geom::box<float, 2> const & shape)
{
impl().shape = shape;

View file

@ -42,6 +42,27 @@ namespace psemek::ui
}
struct default_element_factory::impl
{
std::shared_ptr<struct font> font;
};
default_element_factory::default_element_factory()
: pimpl_{make_impl()}
{}
default_element_factory::~default_element_factory() = default;
void default_element_factory::set_font(std::shared_ptr<struct font> f)
{
impl().font = std::move(f);
}
std::shared_ptr<struct font> default_element_factory::font() const
{
return impl().font;
}
std::unique_ptr<button> default_element_factory::make_button()
{
return std::make_unique<button_impl>();

View file

@ -48,7 +48,6 @@ void main()
struct painter_impl::impl
{
std::shared_ptr<struct font> font;
std::uint32_t depth = 0;
gfx::program colored_program;
@ -82,16 +81,6 @@ void main()
painter_impl::~painter_impl() = default;
void painter_impl::set_font(std::shared_ptr<struct font> font)
{
impl().font = font;
}
font const * painter_impl::font() const
{
return impl().font.get();
}
void painter_impl::draw_rect(geom::box<float, 2> const & rect, gfx::color_rgba const & color)
{
std::uint32_t const depth = impl().depth++;