Label properties change triggers reshape
This commit is contained in:
parent
33256b6549
commit
fd7fbd4d87
2 changed files with 49 additions and 23 deletions
|
|
@ -57,6 +57,9 @@ namespace psemek::ui
|
|||
|
||||
struct shape const & shape() const override { return shape_; }
|
||||
void reshape(geom::box<float, 2> const & bbox) override;
|
||||
|
||||
geom::box<float, 2> size_constraints() const override;
|
||||
|
||||
void draw(painter & p) const override;
|
||||
|
||||
protected:
|
||||
|
|
@ -75,9 +78,12 @@ namespace psemek::ui
|
|||
{
|
||||
struct font const * font = nullptr;
|
||||
std::vector<glyph> glyphs;
|
||||
geom::vector<float, 2> size{0.f, 0.f};
|
||||
};
|
||||
|
||||
mutable std::optional<cached_state> cached_state_;
|
||||
|
||||
void update_cached_state() const;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,17 +39,45 @@ namespace psemek::ui
|
|||
cached_state_.reset();
|
||||
}
|
||||
|
||||
geom::box<float, 2> label::size_constraints() const
|
||||
{
|
||||
if (!cached_state_)
|
||||
update_cached_state();
|
||||
|
||||
static float const inf = std::numeric_limits<float>::infinity();
|
||||
return {{{cached_state_->size[0], inf}, {cached_state_->size[1], inf}}};
|
||||
}
|
||||
|
||||
void label::draw(painter & p) const
|
||||
{
|
||||
if (!cached_state_)
|
||||
update_cached_state();
|
||||
|
||||
if (!cached_state_->font) return;
|
||||
|
||||
auto st = style();
|
||||
if (!st) return;
|
||||
|
||||
for (auto & g : cached_state_->glyphs)
|
||||
p.draw_glyph(*(cached_state_->font), g.character, g.position, st->text_color);
|
||||
}
|
||||
|
||||
void label::on_state_changed()
|
||||
{
|
||||
cached_state_.reset();
|
||||
post_reshape();
|
||||
}
|
||||
|
||||
void label::update_cached_state() const
|
||||
{
|
||||
cached_state_ = cached_state{};
|
||||
|
||||
if (text_.empty()) return;
|
||||
|
||||
auto st = style();
|
||||
if (!st) return;
|
||||
if (!st->font) return;
|
||||
|
||||
if (!cached_state_ || cached_state_->font != st->font.get())
|
||||
{
|
||||
cached_state_ = cached_state{};
|
||||
cached_state_->font = st->font.get();
|
||||
|
||||
shape_options opts;
|
||||
|
|
@ -66,16 +94,8 @@ namespace psemek::ui
|
|||
|
||||
for (auto & g : cached_state_->glyphs)
|
||||
g.position += offset;
|
||||
}
|
||||
|
||||
for (auto & g : cached_state_->glyphs)
|
||||
p.draw_glyph(*st->font, g.character, g.position, st->text_color);
|
||||
}
|
||||
|
||||
void label::on_state_changed()
|
||||
{
|
||||
// TODO: changes should notify parent about content change
|
||||
element::reshape();
|
||||
cached_state_->size = bbox.dimensions();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue