FifeGUI 0.2.0
A C++ GUI library designed for games.
panel.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later OR BSD-3-Clause
2// SPDX-FileCopyrightText: 2026 Fifengine contributors
3
4// Corresponding header include
5#include "fifechan/widgets/panel.hpp"
6
7// Standard library includes
8#include <string>
9
10// Project headers (subdirs before local)
11#include "fifechan/font.hpp"
12#include "fifechan/graphics.hpp"
13
14namespace fcn
15{
20 {
21 addMouseListener(this);
22 }
23
25 {
26 mVisibilityState = state;
27 }
28
30 {
31 return mVisibilityState;
32 }
33
35 {
36 mCollapsedWidth = width;
37 }
38
40 {
41 return mCollapsedWidth;
42 }
43
44 // cppcheck-suppress duplInheritedMember
45 void Panel::setVisible(bool visible)
46 {
47 if (visible) {
48 mVisibilityState = VisibilityState::Visible;
49 } else {
50 mVisibilityState = VisibilityState::Hidden;
51 }
52 Widget::setVisible(visible);
53 }
54
56 {
57 if (mVisibilityState == VisibilityState::Collapsed) {
58 int const width = mCollapsedWidth + (2 * getBorderSize()) + getPaddingLeft() + getPaddingRight();
59 setWidth(width);
60 } else if (mVisibilityState == VisibilityState::Hidden) {
61 // Don't adjust size when hidden - maintain original dimensions
62 // for when it becomes visible again
63 } else {
65 }
66 }
67
68 void Panel::setTitle(std::string const & title)
69 {
70 mTitle = title;
71 }
72
73 std::string const & Panel::getTitle() const
74 {
75 return mTitle;
76 }
77
78 void Panel::setClosable(bool closable)
79 {
80 mClosable = closable;
81 }
82
83 bool Panel::isClosable() const
84 {
85 return mClosable;
86 }
87
88 void Panel::draw(Graphics* graphics)
89 {
90 // Handle collapsed state - draw only title bar strip without content
91 if (mVisibilityState == VisibilityState::Collapsed) {
92 int const titleBarHeight = getBorderSize() + getPaddingTop() + 20;
93
94 // Draw background for collapsed title bar strip
95 if (isOpaque()) {
96 graphics->setColor(getBaseColor());
97 graphics->fillRectangle(0, 0, getWidth(), titleBarHeight);
98 }
99
100 // Draw border
101 if (getBorderSize() > 0) {
102 drawBorder(graphics);
103 }
104
105 // Draw title text (clipped to collapsed width)
106 if (!mTitle.empty() && getFont() != nullptr) {
107 graphics->setColor(getForegroundColor());
108 graphics->setFont(getFont());
109
110 int const textX = getBorderSize() + getPaddingLeft() + 4;
111 int const textY = (titleBarHeight - getFont()->getHeight()) / 2;
112
113 Rectangle const clipArea(
116 getWidth() - (2 * getBorderSize()) - getPaddingLeft() - getPaddingRight() - 24,
117 titleBarHeight - 1);
118 graphics->pushClipArea(clipArea);
119 graphics->drawText(mTitle, textX, textY);
120 graphics->popClipArea();
121 }
122
123 // Draw close button (if closable)
124 if (mClosable) {
125 int const closeX = getWidth() - getBorderSize() - getPaddingRight() - 16;
126 int const closeY = (titleBarHeight - 12) / 2;
127 mCloseButtonBounds = Rectangle(closeX, closeY, 12, 12);
128
129 graphics->setColor(getForegroundColor());
130 graphics->drawLine(
131 mCloseButtonBounds.x,
132 mCloseButtonBounds.y,
133 mCloseButtonBounds.x + mCloseButtonBounds.width,
134 mCloseButtonBounds.y + mCloseButtonBounds.height);
135 graphics->drawLine(
136 mCloseButtonBounds.x,
137 mCloseButtonBounds.y + mCloseButtonBounds.height,
138 mCloseButtonBounds.x + mCloseButtonBounds.width,
139 mCloseButtonBounds.y);
140 }
141
142 return;
143 }
144
145 // Normal visible state - Container draws background and children
146 Container::draw(graphics);
147
148 if (mTitle.empty()) {
149 return;
150 }
151
152 int const titleBarHeight = getBorderSize() + getPaddingTop() + 20;
153 int const textX = getBorderSize() + getPaddingLeft() + 4;
154 int const textY = (titleBarHeight - getFont()->getHeight()) / 2;
155
156 graphics->setColor(getForegroundColor());
157 graphics->setFont(getFont());
158
159 Rectangle const clipArea(
162 getWidth() - (2 * getBorderSize()) - getPaddingLeft() - getPaddingRight() - 24,
163 titleBarHeight - 1);
164 graphics->pushClipArea(clipArea);
165 graphics->drawText(mTitle, textX, textY);
166 graphics->popClipArea();
167
168 if (mClosable) {
169 int const closeX = getWidth() - getBorderSize() - getPaddingRight() - 16;
170 int const closeY = (titleBarHeight - 12) / 2;
171 mCloseButtonBounds = Rectangle(closeX, closeY, 12, 12);
172
173 graphics->setColor(getForegroundColor());
174 graphics->drawLine(
175 mCloseButtonBounds.x,
176 mCloseButtonBounds.y,
177 mCloseButtonBounds.x + mCloseButtonBounds.width,
178 mCloseButtonBounds.y + mCloseButtonBounds.height);
179 graphics->drawLine(
180 mCloseButtonBounds.x,
181 mCloseButtonBounds.y + mCloseButtonBounds.height,
182 mCloseButtonBounds.x + mCloseButtonBounds.width,
183 mCloseButtonBounds.y);
184 }
185 }
186
188 {
189 if (event.getSource() != this) {
190 return;
191 }
192
193 if (mClosable && !mCloseButtonBounds.isEmpty()) {
194 int const x = event.getX();
195 int const y = event.getY();
196
197 if (x >= mCloseButtonBounds.x && x < mCloseButtonBounds.x + mCloseButtonBounds.width &&
198 y >= mCloseButtonBounds.y && y < mCloseButtonBounds.y + mCloseButtonBounds.height) {
199 setVisible(false);
200 return;
201 }
202 }
203
204 int const titleBarHeight = getBorderSize() + getPaddingTop() + 20;
205 if (event.getY() > titleBarHeight) {
206 handleContentClick(event);
207 }
208 }
209
211 {
212 return false;
213 }
214} // namespace fcn
void draw(Graphics *graphics) override
Draws the widget.
Definition container.cpp:25
virtual bool isOpaque() const
Checks if the container is opaque or not.
Definition container.cpp:87
void adjustSize() override
Adjust the size of the container after layout computations.
Widget * getSource() const
Gets the source widget of the event.
Definition event.cpp:20
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:58
virtual void popClipArea()
Removes the top most clip area from the stack.
Definition graphics.cpp:65
void drawText(std::string const &text, int x, int y)
Draws text with a default left alignment.
Definition graphics.hpp:401
virtual bool pushClipArea(Rectangle area)
Pushes a clip area onto the stack.
Definition graphics.cpp:25
virtual void setFont(Font *font)
Sets the font to use when drawing text.
Definition graphics.cpp:91
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.
Represents a mouse event.
int getY() const
Gets the y coordinate of the mouse event.
virtual bool handleContentClick(MouseEvent &event)
Handles content area mouse clicks.
Definition panel.cpp:210
int getCollapsedWidth() const
Gets the collapsed width.
Definition panel.cpp:39
void setClosable(bool closable)
Sets whether the panel has a close button.
Definition panel.cpp:78
void draw(Graphics *graphics) override
Draws the panel content.
Definition panel.cpp:88
VisibilityState getVisibilityState() const
Gets the visibility state of the panel.
Definition panel.cpp:29
Panel()
Constructor.
Definition panel.cpp:19
void setCollapsedWidth(int width)
Sets the width when the panel is collapsed.
Definition panel.cpp:34
void adjustSize() override
Adjusts the panel size based on its visibility state.
Definition panel.cpp:55
void setTitle(std::string const &title)
Sets the panel title.
Definition panel.cpp:68
void setVisibilityState(VisibilityState state)
Sets the visibility state of the panel.
Definition panel.cpp:24
bool isClosable() const
Checks if the panel has a close button.
Definition panel.cpp:83
void mousePressed(MouseEvent &event) override
Definition panel.cpp:187
std::string const & getTitle() const
Gets the panel title.
Definition panel.cpp:73
void setVisible(bool visible)
Sets whether the panel is visible.
Definition panel.cpp:45
Represents a rectangular area (X, Y, Width, Height).
Definition rectangle.hpp:22
Color const & getBaseColor() const
Gets the base color.
Definition widget.cpp:741
void setVisible(bool visible)
Sets the widget to be visible, or not.
Definition widget.cpp:684
int getWidth() const
Gets the width of the widget.
Definition widget.cpp:251
unsigned int getPaddingLeft() const
Gets the left padding.
Definition widget.cpp:603
void addMouseListener(MouseListener *mouseListener)
Adds a mouse listener to the widget.
Definition widget.cpp:906
virtual void setWidth(int width)
Sets the width of the widget.
Definition widget.cpp:243
virtual void drawBorder(Graphics *graphics)
Called when a widget have a border.
Definition widget.cpp:155
unsigned int getPaddingTop() const
Gets the top padding.
Definition widget.cpp:573
unsigned int getBorderSize() const
Gets the size of the widget's border.
Definition widget.cpp:473
Color const & getForegroundColor() const
Gets the foreground color.
Definition widget.cpp:751
Font * getFont() const
Gets the font set for the widget.
Definition widget.cpp:999
unsigned int getPaddingRight() const
Gets the right padding.
Definition widget.cpp:583
Used replacement tokens by configure_file():
VisibilityState
Defines the visibility state of a Panel.
Definition panel.hpp:28