Add ui::element::style_updated & own_style_updated replacing set_style(style())

This commit is contained in:
Nikita Lisitsa 2022-03-11 13:11:47 +03:00
parent 4922877431
commit e237959894
5 changed files with 38 additions and 38 deletions

View file

@ -56,6 +56,9 @@ namespace psemek::ui
virtual std::shared_ptr<struct style const> set_style(std::shared_ptr<struct style const> st);
virtual std::shared_ptr<struct style const> set_own_style(std::shared_ptr<struct style const> st);
virtual void style_updated();
virtual void own_style_updated();
virtual void update(float /* dt */) {}
virtual void draw(painter & p) const = 0;

View file

@ -60,8 +60,8 @@ namespace psemek::ui
geom::box<float, 2> size_constraints() const override;
std::shared_ptr<struct style const> set_style(std::shared_ptr<struct style const> st) override;
std::shared_ptr<struct style const> set_own_style(std::shared_ptr<struct style const> st) override;
void style_updated() override;
void own_style_updated() override;
void draw(painter & p) const override;

View file

@ -57,52 +57,47 @@ namespace psemek::ui
std::shared_ptr<style const> element::set_style(std::shared_ptr<struct style const> st)
{
if (style_ != st)
{
if (style_)
style_->use_as_style.erase(this);
if (style_)
style_->use_as_style.erase(this);
std::swap(st, style_);
std::swap(st, style_);
if (style_)
style_->use_as_style.insert(this);
}
if (style_)
style_->use_as_style.insert(this);
auto visitor = util::recursive([](auto && self, element * e) -> void {
e->merged_style_ = nullptr;
e->merged_own_style_ = nullptr;
for (auto c : e->children())
if (c) self(c);
});
visitor(this);
style_updated();
return st;
}
std::shared_ptr<struct style const> element::set_own_style(std::shared_ptr<struct style const> st)
{
if (style_ != st)
{
if (style_)
style_->use_as_own_style.erase(this);
if (style_)
style_->use_as_own_style.erase(this);
std::swap(st, own_style_);
std::swap(st, own_style_);
if (style_)
style_->use_as_own_style.insert(this);
}
if (style_)
style_->use_as_own_style.insert(this);
auto visitor = util::recursive([](auto && self, element * e) -> void {
e->merged_style_ = nullptr;
e->merged_own_style_ = nullptr;
for (auto c : e->children())
if (c) self(c);
});
visitor(this);
own_style_updated();
return st;
}
void element::style_updated()
{
merged_style_ = nullptr;
own_style_updated();
for (auto c : children())
if (c) c->style_updated();
}
void element::own_style_updated()
{
merged_own_style_ = nullptr;
}
std::shared_ptr<struct style const> element::merged_style() const
{
if (!merged_style_)

View file

@ -62,16 +62,18 @@ namespace psemek::ui
return {{{cached_state_inf_->size[0], inf}, {cached_state_inf_->size[1], inf}}};
}
std::shared_ptr<style const> label::set_style(std::shared_ptr<struct style const> st)
void label::style_updated()
{
element::style_updated();
cached_state_.reset();
return element::set_style(st);
cached_state_inf_.reset();
}
std::shared_ptr<style const> label::set_own_style(std::shared_ptr<struct style const> st)
void label::own_style_updated()
{
element::own_style_updated();
cached_state_.reset();
return element::set_own_style(st);
cached_state_inf_.reset();
}
void label::draw(painter & p) const

View file

@ -43,10 +43,10 @@ namespace psemek::ui
void style::on_changed()
{
for (auto e : use_as_style)
e->set_style(e->style());
e->style_updated();
for (auto e : use_as_own_style)
e->set_own_style(e->own_style());
e->own_style_updated();
}
void merge(style & dst, style const & src)