81 lines
1.6 KiB
C++
81 lines
1.6 KiB
C++
#include <psemek/ui/label.hpp>
|
|
|
|
namespace psemek::ui
|
|
{
|
|
|
|
void label::set_text(std::string text)
|
|
{
|
|
text_ = std::move(text);
|
|
on_state_changed();
|
|
}
|
|
|
|
void label::set_halign(halignment value)
|
|
{
|
|
halign_ = value;
|
|
on_state_changed();
|
|
}
|
|
|
|
void label::set_valign(valignment value)
|
|
{
|
|
valign_ = value;
|
|
on_state_changed();
|
|
}
|
|
|
|
void label::set_multiline(multiline_mode value)
|
|
{
|
|
multiline_ = value;
|
|
on_state_changed();
|
|
}
|
|
|
|
void label::set_overflow(overflow_mode value)
|
|
{
|
|
overflow_ = value;
|
|
on_state_changed();
|
|
}
|
|
|
|
void label::reshape(geom::box<float, 2> const & bbox)
|
|
{
|
|
shape_.box = bbox;
|
|
cached_state_.reset();
|
|
}
|
|
|
|
void label::draw(painter & p) const
|
|
{
|
|
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;
|
|
opts.scale = st->text_scale;
|
|
cached_state_->glyphs = st->font->shape(text_, opts);
|
|
|
|
geom::box<float, 2> bbox;
|
|
for (auto const & g : cached_state_->glyphs)
|
|
bbox |= g.position;
|
|
|
|
geom::vector<float, 2> offset;
|
|
offset[0] = shape_.box[0].center() - bbox[0].length() / 2.f;
|
|
offset[1] = shape_.box[1].center() - st->text_scale * st->font->size()[1] / 2.f;
|
|
|
|
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();
|
|
}
|
|
|
|
}
|