ASW Lib
A.D.S. Games SDL Wrapper Library. A library targeted at Allegro4 users who want to switch to SDL3 and use modern c++.
Loading...
Searching...
No Matches
label.cpp
Go to the documentation of this file.
2
5
6void asw::ui::Label::set_text(const std::string& t, bool auto_size)
7{
8 text = t;
9 _fit_text = auto_size;
10}
11
13{
14 if (!_fit_text) {
15 return;
16 }
17 const auto size = asw::util::get_text_size(pick_font(font, ctx.theme), text);
18 transform.size = { static_cast<float>(size.x), static_cast<float>(size.y) };
19}
20
22{
23 if (const auto& f = pick_font(font, ctx.theme); !text.empty() && f != nullptr) {
24 // Text is drawn from its left, middle or right, so a sized label
25 // lines its text up inside its own box
26 auto position = transform.position;
27 if (_fit_text || transform.size.x > 0.0F) {
28 if (justify == asw::TextJustify::Center) {
29 position.x += transform.size.x / 2.0F;
30 } else if (justify == asw::TextJustify::Right) {
31 position.x += transform.size.x;
32 }
33 }
34 asw::draw::text(f, text, position, color.value_or(ctx.theme.text), justify);
35 }
36
37 Widget::draw(ctx);
38}
Shared state for the UI system.
Definition context.h:97
Theme theme
The current UI theme.
Definition context.h:104
void draw(Context &ctx) override
Draw the label.
Definition label.cpp:21
void measure(Context &ctx) override
Size to the text if set_text asked for it.
Definition label.cpp:12
std::string text
The text to display.
Definition label.h:56
bool _fit_text
Definition label.h:65
void set_text(const std::string &t, bool auto_size=false)
Set the text, optionally sizing the label to fit it, so layouts such as VBox and Modal give it room.
Definition label.cpp:6
virtual void draw(Context &ctx)
Draw this widget and its children.
Definition widget.cpp:163
Routines for drawing sprites and primitives to the screen.
Label widget for the ASW UI module.
void text(const asw::Font &font, const std::string &text, const asw::Vec2< float > &position, asw::Color color, asw::TextJustify justify=asw::TextJustify::Left)
Draw text. The position is rounded to whole pixels after justification so glyphs stay sharp....
Definition draw.cpp:357
const asw::Font & pick_font(const asw::Font &own, const Theme &theme)
The font a widget draws with: its own, or the theme font.
Definition theme.h:304
asw::Vec2< int > get_text_size(const asw::Font &font, std::string_view text)
Get text size.
Definition util.cpp:81
asw::Color text
Default text color.
Definition theme.h:252
General utility functions.