FifeGUI 0.3.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// Standard library includes
9#include <string>
10
11// Platform config include
12#include "fifechan/platform.hpp"
13
14// Project headers (subdirs before local)
15#include "fifechan/events/mouseevent.hpp"
16#include "fifechan/graphics.hpp"
17#include "fifechan/listeners/focuslistener.hpp"
18#include "fifechan/listeners/keylistener.hpp"
19#include "fifechan/listeners/mouselistener.hpp"
20#include "fifechan/listeners/widgetlistener.hpp"
21#include "fifechan/widget.hpp"
22
23namespace fcn
24{
36 class FIFEGUI_API Button :
37 public Widget,
38 public MouseListener,
39 public KeyListener,
40 public FocusListener,
41 public WidgetListener
42 {
43 public:
44 Button();
45
52 explicit Button(std::string caption);
53
54 ~Button() override;
55
56 Button(Button const &) = delete;
57 Button& operator=(Button const &) = delete;
58 Button(Button&&) = delete;
59 Button& operator=(Button&&) = delete;
60
67 void setCaption(std::string const & caption);
68
74 std::string const & getCaption() const;
75
82 void setActive(bool state);
83
89 bool isActive() const;
90
98 void setAlignment(Graphics::Alignment alignment);
99
107
114 void setDownXOffset(int offset);
115
121 int getDownXOffset() const;
122
129 void setDownYOffset(int offset);
130
136 int getDownYOffset() const;
137
145 void setDownOffset(int x, int y);
146
147 // Inherited from Widget
148
150
151 void resizeToContent(bool recursion = true) override;
152 void adjustSize() override;
153 void draw(Graphics* graphics) override;
154 void fontChanged() override;
155
156 // Inherited from FocusListener
157
158 void focusLost(Event const & event) override;
159
160 // Inherited from MouseListener
161
162 void mousePressed(MouseEvent& mouseEvent) override;
163 void mouseReleased(MouseEvent& mouseEvent) override;
164 void mouseEntered(MouseEvent& mouseEvent) override;
165 void mouseExited(MouseEvent& mouseEvent) override;
166 void mouseDragged(MouseEvent& mouseEvent) override;
167
168 // Inherited from KeyListener
169
170 void keyPressed(KeyEvent& keyEvent) override;
171 void keyReleased(KeyEvent& keyEvent) override;
172
173 // Inherited from WidgetListener
174
175 void ancestorHidden(Event const & e) override;
176
177 protected:
187 virtual void adjustSizeImpl(); // cppcheck-suppress virtualCallInConstructor
188
195 virtual bool isPressed() const;
196
200 std::string mCaption;
201
205 bool mHasMouse{false};
206
210 bool mKeyPressed{false};
211
215 bool mMousePressed{false};
216
220 bool mState{true};
221
225 Graphics::Alignment mAlignment{Graphics::Alignment::Center};
226
230 int mXOffset{1};
231
235 int mYOffset{1};
236 };
237} // namespace fcn
238
239#endif // INCLUDE_FIFECHAN_WIDGETS_BUTTON_HPP_
std::string const & getCaption() const
Gets the caption of the button.
Definition button.cpp:52
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:261
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:97
bool isActive() const
Returns the button state.
Definition button.cpp:62
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:77
Graphics::Alignment getAlignment() const
Gets the alignment of the caption.
Definition button.cpp:72
virtual bool isPressed() const
Checks if the button is pressed.
Definition button.cpp:188
void keyReleased(KeyEvent &keyEvent) override
Called if a key is released when the widget has keyboard focus.
Definition button.cpp:243
bool mHasMouse
True if the mouse is on top of the button, false otherwise.
Definition button.hpp:205
std::string mCaption
Holds the caption of the button.
Definition button.hpp:200
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:87
void mouseReleased(MouseEvent &mouseEvent) override
Called when a mouse button has been released on the widget area.
Definition button.cpp:216
virtual void adjustSizeImpl()
Adjusts the size of the button to fit the caption.
Definition button.cpp:181
void mouseEntered(MouseEvent &mouseEvent) override
Called when the mouse has entered into the widget area.
Definition button.cpp:211
bool mKeyPressed
True if a key has been pressed, false otherwise.
Definition button.hpp:210
void mouseDragged(MouseEvent &mouseEvent) override
Called when the mouse has moved and the mouse has previously been pressed on the widget.
Definition button.cpp:228
void adjustSize() override
Resizes the widget's size to fit the content exactly.
Definition button.cpp:176
void draw(Graphics *graphics) override
Draws the widget.
Definition button.cpp:108
void focusLost(Event const &event) override
Called when a widget loses focus.
Definition button.cpp:254
void setAlignment(Graphics::Alignment alignment)
Sets the alignment of the caption.
Definition button.cpp:67
bool mState
True if the button is active.
Definition button.hpp:220
bool mMousePressed
True if a mouse has been pressed, false otherwise.
Definition button.hpp:215
void resizeToContent(bool recursion=true) override
Resizes the widget's size to fit the content exactly, calls recursively all childs.
Definition button.cpp:170
int mYOffset
Holds the y down offset of the caption.
Definition button.hpp:235
void keyPressed(KeyEvent &keyEvent) override
Called if a key is pressed when the widget has keyboard focus.
Definition button.cpp:233
void fontChanged() override
Called when the font has changed.
Definition button.cpp:103
int mXOffset
Holds the x down offset of the caption.
Definition button.hpp:230
Graphics::Alignment mAlignment
Holds the alignment of the caption.
Definition button.hpp:225
int getDownYOffset() const
Gets the number of pixels the image or text will be offset.
Definition button.cpp:92
void mouseExited(MouseEvent &mouseEvent) override
Called when the mouse has exited the widget area.
Definition button.cpp:206
void mousePressed(MouseEvent &mouseEvent) override
Called when a mouse button has been pressed down on the widget area.
Definition button.cpp:196
int getDownXOffset() const
Gets the number of pixels the image or text will be offset.
Definition button.cpp:82
void setCaption(std::string const &caption)
Sets the caption of the button.
Definition button.cpp:46
void setActive(bool state)
Sets the button state.
Definition button.cpp:57
Base class for all GUI event objects.
Definition event.hpp:25
Abstract interface providing primitive drawing functions (lines, rectangles, etc.).
Definition graphics.hpp:58
Alignment
Horizontal alignments for text drawing.
Definition graphics.hpp:64
Represents a key event.
Definition keyevent.hpp:26
KeyListener(KeyListener const &)=default
Copy constructor.
Represents a mouse event.
MouseListener(MouseListener const &)=default
Copy constructor.
WidgetListener(WidgetListener const &)=default
Copy constructor.
Widget()
Constructor.
Definition widget.cpp:52
virtual void resizeToContent(bool recursion=true)
Resizes the widget's size to fit the content exactly, calls recursively all childs.
Definition widget.hpp:1580
Used replacement tokens by configure_file():