FifeGUI 0.2.0
A C++ GUI library designed for games.
window.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/window.hpp"
6
7#include <algorithm>
8#include <string>
9
10#include "fifechan/exception.hpp"
11#include "fifechan/font.hpp"
12#include "fifechan/graphics.hpp"
13#include "fifechan/mouseinput.hpp"
14
15namespace fcn
16{
17 Window::Window()
18 {
20 setPadding(2);
21
22 addMouseListener(this);
23 }
24
25 Window::Window(std::string const & caption)
26 {
27 setCaption(caption);
29 setPadding(2);
30
31 addMouseListener(this);
32 }
33
34 Window::~Window() = default;
35
36 void Window::setTitleBarHeight(unsigned int height)
37 {
38 mTitleBarHeight = height;
39 }
40
41 unsigned int Window::getTitleBarHeight() const
42 {
43 return mTitleBarHeight;
44 }
45
46 void Window::setInnerBorderSize(unsigned int border)
47 {
48 mInnerBorderSize = border;
49 }
50
51 unsigned int Window::getInnerBorderSize() const
52 {
53 return mInnerBorderSize;
54 }
55
56 void Window::setCaption(std::string const & caption)
57 {
58 mCaption = caption;
59 }
60
61 std::string const & Window::getCaption() const
62 {
63 return mCaption;
64 }
65
67 {
68 mAlignment = alignment;
69 }
70
75
77 {
78 Color const & faceColor = getBaseColor();
79 Color highlightColor;
80 Color shadowColor;
81 int const alpha = getBaseColor().a;
82 highlightColor = faceColor + 0x303030;
83 highlightColor.a = alpha;
84 shadowColor = faceColor - 0x303030;
85 shadowColor.a = alpha;
86
87 int const x = getBorderSize() + getPaddingLeft();
88 int const y = getBorderSize() + getPaddingTop() + getTitleBarHeight();
89 int const width = getWidth() - getBorderSize() - getPaddingRight() - 1;
90 int const height = getHeight() - getBorderSize() - getPaddingBottom() - 1;
91
92 unsigned int i = 0;
93 for (i = 0; i < getInnerBorderSize(); ++i) {
94 graphics->setColor(shadowColor);
95 graphics->drawLine(x + i, y + i, width - i, y + i);
96 graphics->drawLine(x + i, y + i + 1, x + i, height - i - 1);
97 graphics->setColor(highlightColor);
98 graphics->drawLine(width - i, y + i + 1, width - i, height - i);
99 graphics->drawLine(x + i, height - i, width - i - 1, height - i);
100 }
101 }
102
103 void Window::draw(Graphics* graphics)
104 {
105 Color const & faceColor = getBaseColor();
106 Color highlightColor;
107 Color shadowColor;
108 int const alpha = getBaseColor().a;
109 highlightColor = faceColor + 0x303030;
110 highlightColor.a = alpha;
111 shadowColor = faceColor - 0x303030;
112 shadowColor.a = alpha;
113
114 if (isOpaque()) {
115 // Fill the background around the content
116 graphics->setColor(faceColor);
117 graphics->fillRectangle(
120 getWidth() - (2 * getBorderSize()),
121 getHeight() - (2 * getBorderSize()));
122 }
123 if (mBackgroundWidget != nullptr) {
124 Rectangle const rec(
127 getWidth() - (2 * getBorderSize()),
128 getHeight() - (2 * getBorderSize()));
129 mBackgroundWidget->setDimension(rec);
130 mBackgroundWidget->_draw(graphics);
131 }
132 if (getBorderSize() > 0) {
133 drawBorder(graphics);
134 }
135
136 // draw inner/content border
137 if (getInnerBorderSize() > 0) {
138 drawInnerBorder(graphics);
139 }
140
141 // draw text
142 int textX = 0;
143 int const textY = (static_cast<int>(getTitleBarHeight()) - getFont()->getHeight()) / 2;
144
145 switch (getAlignment()) {
146 case Graphics::Alignment::Left:
147 textX = 0;
148 break;
149 case Graphics::Alignment::Center:
150 textX = (getWidth() - 2 * getBorderSize() - getPaddingLeft() - getPaddingRight()) / 2;
151 break;
152 case Graphics::Alignment::Right:
153 textX = getWidth() - getBorderSize() - getPaddingRight();
154 break;
155 default:
156 throwException("Unknown alignment.");
157 }
158 // text clip area
159 Rectangle const rec(
163 getTitleBarHeight() - 1);
164
165 graphics->setColor(getForegroundColor());
166 graphics->setFont(getFont());
167 graphics->pushClipArea(rec);
168 graphics->drawText(getCaption(), textX, textY, getAlignment());
169 graphics->popClipArea();
170 }
171
173 {
174 if (mouseEvent.getSource() != this) {
175 return;
176 }
177
178 if (getParent() != nullptr) {
179 getParent()->moveToTop(this);
180 }
181
182 mDragOffsetX = mouseEvent.getX();
183 mDragOffsetY = mouseEvent.getY();
184
185 int const height = getBorderSize() + getPaddingTop() + getTitleBarHeight();
186 mMoved = mouseEvent.getY() <= height;
187 }
188
189 void Window::mouseReleased(MouseEvent& /*mouseEvent*/)
190 {
191 mMoved = false;
192 }
193
195 {
196 if (mouseEvent.isConsumed() || mouseEvent.getSource() != this) {
197 return;
198 }
199
200 if (isMovable() && mMoved) {
201 setPosition(mouseEvent.getX() - mDragOffsetX + getX(), mouseEvent.getY() - mDragOffsetY + getY());
202 }
203
204 mouseEvent.consume();
205 }
206
208 {
210 int const w = std::max(getFont()->getWidth(mCaption), getWidth()) + (2 * getBorderSize()) + getPaddingLeft() +
212 int const h = getHeight() + (2 * getBorderSize()) + getPaddingTop() + getPaddingBottom() +
214 setSize(w, h);
215 }
216
227
228 void Window::setMovable(bool movable)
229 {
230 mMovable = movable;
231 }
232
233 bool Window::isMovable() const
234 {
235 return mMovable;
236 }
237
238 void Window::setOpaque(bool opaque)
239 {
240 mOpaque = opaque;
241 }
242
243 bool Window::isOpaque() const
244 {
245 return mOpaque;
246 }
247} // namespace fcn
Color.
Definition color.hpp:56
uint8_t a
Alpha color component (0-255).
Definition color.hpp:322
bool mOpaque
True if the container is opaque, false otherwise.
Widget * mBackgroundWidget
Optional widget that is rendered behind other children as the container background.
Widget * getSource() const
Gets the source widget of the event.
Definition event.cpp:11
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
virtual void popClipArea()
Removes the top most clip area from the stack.
Definition graphics.cpp:55
void drawText(std::string const &text, int x, int y)
Draws text with a default left alignment.
Definition graphics.hpp:362
virtual bool pushClipArea(Rectangle area)
Pushes a clip area onto the stack.
Definition graphics.cpp:18
virtual void setFont(Font *font)
Sets the font to use when drawing text.
Definition graphics.cpp:79
virtual void drawLine(int x1, int y1, int x2, int y2)=0
Draws a line.
virtual void setColor(Color const &color)=0
Sets the color to use when drawing.
virtual void fillRectangle(Rectangle const &rectangle)=0
Draws a filled rectangle.
bool isConsumed() const
Checks if the input event is consumed.
void consume()
Marks the event as consumed.
Represents a mouse event.
int getX() const
Gets the x coordinate of the mouse event.
int getY() const
Gets the y coordinate of the mouse event.
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.
Color const & getBaseColor() const
Gets the base color.
Definition widget.cpp:596
int getY() const
Gets the y coordinate of the widget.
Definition widget.cpp:209
int getWidth() const
Gets the width of the widget.
Definition widget.cpp:170
virtual Widget * getParent() const
Gets the widget's parent container.
Definition widget.cpp:157
void setBorderSize(unsigned int size)
Sets the size of the widget's border.
Definition widget.cpp:376
void resizeToChildren()
Resizes the widget to fit it's children exactly.
Definition widget.cpp:1170
virtual void moveToTop(Widget *widget)
Moves a widget to the top of this widget.
Definition widget.cpp:1290
int getX() const
Gets the x coordinate of the widget.
Definition widget.cpp:196
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
void addMouseListener(MouseListener *mouseListener)
Adds a mouse listener to the widget.
Definition widget.cpp:754
void setPosition(int x, int y)
Sets position of the widget.
Definition widget.cpp:214
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
unsigned int getPaddingBottom() const
Gets the bottom padding.
Definition widget.cpp:467
Color const & getForegroundColor() const
Gets the foreground color.
Definition widget.cpp:606
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
void setPadding(unsigned int padding)
Sets all 4 paddings to one value.
Definition widget.cpp:434
void mousePressed(MouseEvent &mouseEvent) override
Called when a mouse button has been pressed on the widget area.
Definition window.cpp:172
void adjustSize() override
Adjust the size of the container after layout computations.
Definition window.cpp:207
bool mMoved
True if the window is being moved, false otherwise.
Definition window.hpp:193
bool mMovable
True if the window is movable, false otherwise.
Definition window.hpp:174
unsigned int getTitleBarHeight() const
Gets the title bar height.
Definition window.cpp:41
unsigned int mTitleBarHeight
Holds the title bar height of the window.
Definition window.hpp:164
void setMovable(bool movable)
Sets the window to be movable or not.
Definition window.cpp:228
void setTitleBarHeight(unsigned int height)
Sets the title bar height.
Definition window.cpp:36
void setAlignment(Graphics::Alignment alignment)
Sets the alignment of the caption.
Definition window.cpp:66
unsigned int getInnerBorderSize() const
Get the size of the inner border (pixels).
Definition window.cpp:51
Graphics::Alignment mAlignment
Holds the alignment of the caption.
Definition window.hpp:159
std::string mCaption
Holds the caption of the window.
Definition window.hpp:154
bool isMovable() const
Checks if the window is movable.
Definition window.cpp:233
Rectangle getChildrenArea() override
Gets the area of the widget occupied by the widget's children.
Definition window.cpp:217
void mouseReleased(MouseEvent &mouseEvent) override
Called when a mouse button has been released on the widget area.
Definition window.cpp:189
void mouseDragged(MouseEvent &mouseEvent) override
Called when the mouse has moved and the mouse has previously been pressed on the widget.
Definition window.cpp:194
bool isOpaque() const override
Checks if the window is opaque.
Definition window.cpp:243
int mDragOffsetX
Holds a drag offset as an x coordinate where the drag of the window started if the window is being dr...
Definition window.hpp:181
void setInnerBorderSize(unsigned int border)
Set the size of the inner border (pixels).
Definition window.cpp:46
virtual void drawInnerBorder(Graphics *graphics)
Draw the inner border (override to customize appearance).
Definition window.cpp:76
void draw(Graphics *graphics) override
Draws the widget.
Definition window.cpp:103
unsigned int mInnerBorderSize
Holds the size of the inner border.
Definition window.hpp:169
std::string const & getCaption() const
Gets the caption of the window.
Definition window.cpp:61
void setCaption(std::string const &caption)
Sets the caption of the window.
Definition window.cpp:56
Graphics::Alignment getAlignment() const
Gets the alignment of the caption.
Definition window.cpp:71
int mDragOffsetY
Holds a drag offset as an y coordinate where the drag of the window started if the window is being dr...
Definition window.hpp:188
void setOpaque(bool opaque) override
Sets the window to be opaque or not.
Definition window.cpp:238