FifeGUI 0.2.0
A C++ GUI library designed for games.
label.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later OR BSD-3-Clause
2// SPDX-FileCopyrightText: 2004 - 2008 Olof Naessén and Per Larsson
3// SPDX-FileCopyrightText: 2013 - 2026 Fifengine contributors
4
5#include "fifechan/widgets/label.hpp"
6
7#include <string>
8#include <utility>
9
10#include "fifechan/exception.hpp"
11#include "fifechan/font.hpp"
12#include "fifechan/graphics.hpp"
13
14namespace fcn
15{
16 Label::Label() : mAlignment(Graphics::Alignment::Left)
17 {
18 adjustSizeImpl();
19 }
20
21 Label::Label(std::string caption) : mCaption(std::move(caption)), mAlignment(Graphics::Alignment::Left)
22 {
24 }
25
26 std::string const & Label::getCaption() const
27 {
28 return mCaption;
29 }
30
31 void Label::setCaption(std::string const & caption)
32 {
33 mCaption = caption;
35 }
36
38 {
39 mAlignment = alignment;
40 }
41
46
47 void Label::resizeToContent(bool recursion)
48 {
49 (void)recursion; // unused parameter
50
52 }
53
55 {
57 }
58
65
66 void Label::draw(Graphics* graphics)
67 {
68 // draw border or frame
69 if (getBorderSize() > 0) {
70 if (isFocused() && (getSelectionMode() & Widget::SelectionMode::Border) == Widget::SelectionMode::Border) {
71 drawSelectionFrame(graphics);
72 } else {
73 drawBorder(graphics);
74 }
75 }
76 Rectangle const offsetRec(getBorderSize(), getBorderSize(), 2 * getBorderSize(), 2 * getBorderSize());
77 int textX = 0;
78 int const textY =
79 offsetRec.y + getPaddingTop() +
80 ((getHeight() - offsetRec.height - getPaddingTop() - getPaddingBottom() - getFont()->getHeight()) / 2);
81
82 switch (getAlignment()) {
83 case Graphics::Alignment::Left:
84 textX = offsetRec.x + getPaddingLeft();
85 break;
86 case Graphics::Alignment::Center:
87 textX = offsetRec.x + getPaddingLeft() +
88 (getWidth() - offsetRec.width - getPaddingLeft() - getPaddingRight()) / 2;
89 break;
90 case Graphics::Alignment::Right:
91 textX = getWidth() - offsetRec.x - getPaddingRight();
92 break;
93 default:
94 throwException("Unknown alignment.");
95 }
96
97 graphics->setFont(getFont());
98 graphics->setColor(getForegroundColor());
99 graphics->drawText(getCaption(), textX, textY, getAlignment());
100 }
101} // namespace fcn
virtual int getHeight() const =0
Gets the height of the glyphs in the font.
Abstract interface providing primitive drawing functions (lines, rectangles, etc.).
Definition graphics.hpp:57
Alignment
Alignments for text drawing.
Definition graphics.hpp:63
void drawText(std::string const &text, int x, int y)
Draws text with a default left alignment.
Definition graphics.hpp:362
virtual void setFont(Font *font)
Sets the font to use when drawing text.
Definition graphics.cpp:79
virtual void setColor(Color const &color)=0
Sets the color to use when drawing.
void draw(Graphics *graphics) override
Draws the widget.
Definition label.cpp:66
void setAlignment(Graphics::Alignment alignment)
Sets the alignment of the caption.
Definition label.cpp:37
void adjustSize() override
Resizes the widget's size to fit the content exactly.
Definition label.cpp:54
std::string const & getCaption() const
Gets the caption of the label.
Definition label.cpp:26
Graphics::Alignment getAlignment() const
Gets the alignment of the caption.
Definition label.cpp:42
void setCaption(std::string const &caption)
Sets the caption of the label.
Definition label.cpp:31
Graphics::Alignment mAlignment
Holds the alignment of the caption.
Definition label.hpp:98
void resizeToContent()
Resizes the widget's size to fit the content exactly, calls recursively all childs.
Definition widget.hpp:1417
std::string mCaption
Holds the caption of the label.
Definition label.hpp:93
void adjustSizeImpl()
Adjusts the size of the label to fit the caption.
Definition label.cpp:59
Represents a rectangular area (X, Y, Width, Height).
Definition rectangle.hpp:20
int width
Holds the width of the rectangle.
int y
Holds the x coordinate of the rectangle.
int x
Holds the x coordinate of the rectangle.
int height
Holds the height of the rectangle.
int getWidth() const
Gets the width of the widget.
Definition widget.cpp:170
virtual bool isFocused() const
Checks if the widget is focused.
Definition widget.cpp:497
unsigned int getPaddingLeft() const
Gets the left padding.
Definition widget.cpp:477
void setSize(int width, int height)
Sets the size of the widget.
Definition widget.cpp:855
virtual void drawBorder(Graphics *graphics)
Called when a widget have a border.
Definition widget.cpp:111
unsigned int getPaddingTop() const
Gets the top padding.
Definition widget.cpp:447
unsigned int getBorderSize() const
Gets the size of the widget's border.
Definition widget.cpp:381
virtual void drawSelectionFrame(Graphics *graphics)
Called when a widget is "active" and the selection mode is Frame or FrameWithBackground.
Definition widget.cpp:135
unsigned int getPaddingBottom() const
Gets the bottom padding.
Definition widget.cpp:467
Color const & getForegroundColor() const
Gets the foreground color.
Definition widget.cpp:606
SelectionMode getSelectionMode() const
Gets the selection mode.
Definition widget.cpp:656
Font * getFont() const
Gets the font set for the widget.
Definition widget.cpp:796
int getHeight() const
Gets the height of the widget.
Definition widget.cpp:183
unsigned int getPaddingRight() const
Gets the right padding.
Definition widget.cpp:457