Add style description & use it in default element factory

This commit is contained in:
Nikita Lisitsa 2021-02-25 10:36:34 +03:00
parent 937a011577
commit b7014f2eda
7 changed files with 68 additions and 15 deletions

View file

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

View file

@ -1,5 +1,6 @@
#pragma once
#include <psemek/ui/style.hpp>
#include <psemek/ui/button.hpp>
#include <psemek/ui/screen.hpp>
@ -13,8 +14,8 @@ namespace psemek::ui
default_element_factory();
~default_element_factory();
void set_font(std::shared_ptr<struct font> f);
std::shared_ptr<struct font> font() const;
void set_style(std::shared_ptr<struct style> st);
std::shared_ptr<struct style> style() const;
std::unique_ptr<button> make_button();
std::unique_ptr<screen> make_screen();

View file

@ -3,6 +3,7 @@
#include <psemek/ui/shape.hpp>
#include <psemek/ui/event.hpp>
#include <psemek/ui/painter.hpp>
#include <psemek/ui/style.hpp>
#include <psemek/async/executor.hpp>
@ -40,6 +41,9 @@ namespace psemek::ui
virtual void enable() { set_enabled(true); }
virtual void disable() { set_enabled(false); }
virtual std::shared_ptr<struct style const> style() const { return style_; }
virtual std::shared_ptr<struct style const> set_style(std::shared_ptr<struct style const> st);
virtual void draw(painter & p) const = 0;
virtual ~element() {}
@ -48,6 +52,7 @@ namespace psemek::ui
element * parent_ = nullptr;
async::executor * loop_ = nullptr;
bool enabled_ = true;
std::shared_ptr<struct style const> style_;
};
}

View file

@ -0,0 +1,25 @@
#pragma once
#include <psemek/ui/font.hpp>
#include <psemek/gfx/color.hpp>
namespace psemek::ui
{
struct style
{
gfx::color_rgba bg_color{95, 95, 95, 255};
gfx::color_rgba fg_color{127, 127, 127, 255};
gfx::color_rgba highlight_color{159, 159, 159, 255};
gfx::color_rgba action_color{63, 63, 63, 255};
int border_width = 3;
int shadow_width = 1;
gfx::color_rgba shadow_color{0, 0, 0, 127};
gfx::color_rgba text_color{0, 0, 0, 255};
std::shared_ptr<struct font> font;
};
}

View file

@ -23,11 +23,14 @@ namespace psemek::ui
void draw(painter & p) const override
{
gfx::color_rgba color = gfx::red;
auto s = style();
if (!s) return;
gfx::color_rgba color = s->fg_color;
if (state() == state_t::mouseover)
color = gfx::light(color);
color = s->highlight_color;
else if (state() == state_t::mousedown)
color = gfx::dark(color);
color = s->action_color;
p.draw_rect(shape_.box, color);
}
@ -44,28 +47,42 @@ namespace psemek::ui
struct default_element_factory::impl
{
std::shared_ptr<struct font> font;
std::shared_ptr<struct style> style;
impl();
template <typename T>
std::unique_ptr<T> create()
{
auto r = std::make_unique<T>();
r->set_style(style);
return r;
}
};
default_element_factory::impl::impl()
: style{std::make_shared<struct style>()}
{}
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)
void default_element_factory::set_style(std::shared_ptr<struct style> st)
{
impl().font = std::move(f);
impl().style = st;
}
std::shared_ptr<struct font> default_element_factory::font() const
std::shared_ptr<struct style> default_element_factory::style() const
{
return impl().font;
return impl().style;
}
std::unique_ptr<button> default_element_factory::make_button()
{
return std::make_unique<button_impl>();
return impl().create<button_impl>();
}
std::unique_ptr<screen> default_element_factory::make_screen()

View file

@ -28,4 +28,10 @@ namespace psemek::ui
return {{{0.f, inf}, {0.f, inf}}};
}
std::shared_ptr<style const> element::set_style(std::shared_ptr<struct style const> st)
{
std::swap(st, style_);
return st;
}
}

View file

@ -27,9 +27,6 @@
* text layout in label with cached glyphs
* grid layout element with equal cells
* grid layout quadratic optimization
* style description
* default element factory accept style
* per-element stylization
* draggable window element
* screen children sorting
* slider element