FifeGUI 0.2.0
A C++ GUI library designed for games.
button.hpp
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#ifndef INCLUDE_FIFECHAN_WIDGETS_BUTTON_HPP_
6#define INCLUDE_FIFECHAN_WIDGETS_BUTTON_HPP_
7
8#include <string>
9
10#include "fifechan/focuslistener.hpp"
11#include "fifechan/graphics.hpp"
12#include "fifechan/keylistener.hpp"
13#include "fifechan/mouseevent.hpp"
14#include "fifechan/mouselistener.hpp"
15#include "fifechan/platform.hpp"
16#include "fifechan/widget.hpp"
17#include "fifechan/widgetlistener.hpp"
18
19namespace fcn
20{
32 class FIFEGUI_API Button :
33 public Widget,
34 public MouseListener,
35 public KeyListener,
36 public FocusListener,
37 public WidgetListener
38 {
39 public:
40 Button();
41
48 explicit Button(std::string caption);
49
50 ~Button() override;
51
52 Button(Button const &) = delete;
53 Button& operator=(Button const &) = delete;
54 Button(Button&&) = delete;
55 Button& operator=(Button&&) = delete;
56
63 void setCaption(std::string const & caption);
64
70 std::string const & getCaption() const;
71
78 void setActive(bool state);
79
85 bool isActive() const;
86
94 void setAlignment(Graphics::Alignment alignment);
95
103
110 void setDownXOffset(int offset);
111
117 int getDownXOffset() const;
118
125 void setDownYOffset(int offset);
126
132 int getDownYOffset() const;
133
141 void setDownOffset(int x, int y);
142
143 // Inherited from Widget
144
146
147 void resizeToContent(bool recursion) override;
148 void adjustSize() override;
149 void draw(Graphics* graphics) override;
150 void fontChanged() override;
151
152 // Inherited from FocusListener
153
154 void focusLost(Event const & event) override;
155
156 // Inherited from MouseListener
157
158 void mousePressed(MouseEvent& mouseEvent) override;
159 void mouseReleased(MouseEvent& mouseEvent) override;
160 void mouseEntered(MouseEvent& mouseEvent) override;
161 void mouseExited(MouseEvent& mouseEvent) override;
162 void mouseDragged(MouseEvent& mouseEvent) override;
163
164 // Inherited from KeyListener
165
166 void keyPressed(KeyEvent& keyEvent) override;
167 void keyReleased(KeyEvent& keyEvent) override;
168
169 // Inherited from WidgetListener
170
171 void ancestorHidden(Event const & e) override;
172
173 protected:
183 virtual void adjustSizeImpl();
184
191 virtual bool isPressed() const;
192
196 std::string mCaption;
197
201 bool mHasMouse{false};
202
206 bool mKeyPressed{false};
207
211 bool mMousePressed{false};
212
216 bool mState{true};
217
221 Graphics::Alignment mAlignment{Graphics::Alignment::Center};
222
226 int mXOffset{1};
227
231 int mYOffset{1};
232 };
233} // namespace fcn
234
235#endif // INCLUDE_FIFECHAN_WIDGETS_BUTTON_HPP_
std::string const & getCaption() const
Gets the caption of the button.
Definition button.cpp:49
void ancestorHidden(Event const &e) override
Invoked when an ancestor of a widget is hidden, i.e its set to be not visible.
Definition button.cpp:256
void setDownOffset(int x, int y)
Sets the number of pixels the image or text will be offset from the top left corner of button when th...
Definition button.cpp:94
bool isActive() const
Returns the button state.
Definition button.cpp:59
void setDownXOffset(int offset)
Sets the number of pixels the image or text will be offset from the top left corner of button when th...
Definition button.cpp:74
Graphics::Alignment getAlignment() const
Gets the alignment of the caption.
Definition button.cpp:69
virtual bool isPressed() const
Checks if the button is pressed.
Definition button.cpp:185
void keyReleased(KeyEvent &keyEvent) override
Called if a key is released when the widget has keyboard focus.
Definition button.cpp:238
bool mHasMouse
True if the mouse is on top of the button, false otherwise.
Definition button.hpp:201
std::string mCaption
Holds the caption of the button.
Definition button.hpp:196
void setDownYOffset(int offset)
Sets the number of pixels the image or text will be offset from the top left corner of button when th...
Definition button.cpp:84
void mouseReleased(MouseEvent &mouseEvent) override
Called when a mouse button has been released on the widget area.
Definition button.cpp:211
virtual void adjustSizeImpl()
Adjusts the size of the button to fit the caption.
Definition button.cpp:178
void mouseEntered(MouseEvent &mouseEvent) override
Called when the mouse has entered into the widget area.
Definition button.cpp:206
bool mKeyPressed
True if a key has been pressed, false otherwise.
Definition button.hpp:206
void mouseDragged(MouseEvent &mouseEvent) override
Called when the mouse has moved and the mouse has previously been pressed on the widget.
Definition button.cpp:223
void adjustSize() override
Resizes the widget's size to fit the content exactly.
Definition button.cpp:173
void draw(Graphics *graphics) override
Draws the widget.
Definition button.cpp:105
void focusLost(Event const &event) override
Called when a widget loses focus.
Definition button.cpp:249
void setAlignment(Graphics::Alignment alignment)
Sets the alignment of the caption.
Definition button.cpp:64
bool mState
True if the button is active.
Definition button.hpp:216
bool mMousePressed
True if a mouse has been pressed, false otherwise.
Definition button.hpp:211
int mYOffset
Holds the y down offset of the caption.
Definition button.hpp:231
void keyPressed(KeyEvent &keyEvent) override
Called if a key is pressed when the widget has keyboard focus.
Definition button.cpp:228
void fontChanged() override
Called when the font has changed.
Definition button.cpp:100
int mXOffset
Holds the x down offset of the caption.
Definition button.hpp:226
Graphics::Alignment mAlignment
Holds the alignment of the caption.
Definition button.hpp:221
int getDownYOffset() const
Gets the number of pixels the image or text will be offset.
Definition button.cpp:89
void mouseExited(MouseEvent &mouseEvent) override
Called when the mouse has exited the widget area.
Definition button.cpp:201
void resizeToContent(bool recursion) override
Resize this widget to fit its content.
Definition button.cpp:167
void mousePressed(MouseEvent &mouseEvent) override
Called when a mouse button has been pressed on the widget area.
Definition button.cpp:193
int getDownXOffset() const
Gets the number of pixels the image or text will be offset.
Definition button.cpp:79
void setCaption(std::string const &caption)
Sets the caption of the button.
Definition button.cpp:43
void setActive(bool state)
Sets the button state.
Definition button.cpp:54
Base class for all GUI event objects.
Definition event.hpp:24
Abstract interface providing primitive drawing functions (lines, rectangles, etc.).
Definition graphics.hpp:57
Alignment
Alignments for text drawing.
Definition graphics.hpp:63
Represents a key event.
Definition keyevent.hpp:22
Represents a mouse event.
Widget()
Constructor.
Definition widget.cpp:36
void resizeToContent()
Resizes the widget's size to fit the content exactly, calls recursively all childs.
Definition widget.hpp:1417