Gamedev Framework (gf) 1.2.0
A C++17 framework for 2D games
Widget.h
1/*
2 * Gamedev Framework (gf)
3 * Copyright (C) 2016-2022 Julien Bernard
4 * Copyright (C) 2018 Lilian Franchi
5 *
6 * This software is provided 'as-is', without any express or implied
7 * warranty. In no event will the authors be held liable for any damages
8 * arising from the use of this software.
9 *
10 * Permission is granted to anyone to use this software for any purpose,
11 * including commercial applications, and to alter it and redistribute it
12 * freely, subject to the following restrictions:
13 *
14 * 1. The origin of this software must not be misrepresented; you must not
15 * claim that you wrote the original software. If you use this software
16 * in a product, an acknowledgment in the product documentation would be
17 * appreciated but is not required.
18 * 2. Altered source versions must be plainly marked as such, and must not be
19 * misrepresented as being the original software.
20 * 3. This notice may not be removed or altered from any source distribution.
21 */
22#ifndef GF_WIDGET_H
23#define GF_WIDGET_H
24
25#include <functional>
26
27#include "GraphicsApi.h"
28#include "Transformable.h"
29#include "Vector.h"
30
31namespace gf {
32#ifndef DOXYGEN_SHOULD_SKIP_THIS
33inline namespace v1 {
34#endif
35
36 struct RenderStates;
37 class RenderTarget;
38
45 enum class WidgetState {
46 Disabled,
47 Default,
48 Selected,
49 };
50
55 class GF_GRAPHICS_API Widget : public Transformable {
56 public:
63
70 virtual bool contains(Vector2f coords) = 0;
71
76
82 bool isDisabled() const noexcept {
83 return m_state == WidgetState::Disabled;
84 }
85
89 void setDefault();
90
96 bool isDefault() const noexcept {
97 return m_state == WidgetState::Default;
98 }
99
104
110 bool isSelected() const {
111 return m_state == WidgetState::Selected;
112 }
113
121
127 WidgetState getState() const noexcept {
128 return m_state;
129 }
130
137 void setCallback(std::function<void()> callback);
138
145
146 protected:
150 virtual void onStateChanged();
151
157 virtual void triggered();
158
159 private:
160 WidgetState m_state;
161 std::function<void()> m_callback;
162 };
163
164#ifndef DOXYGEN_SHOULD_SKIP_THIS
165}
166#endif
167}
168
169#endif // GF_WIDGET_H
Decomposed transform defined by a position, a rotation and a scale.
Definition: Transformable.h:95
The widgets abstract class.
Definition: Widget.h:55
WidgetState getState() const noexcept
Get the state of the widget.
Definition: Widget.h:127
void setCallback(std::function< void()> callback)
Set the callback of the widget.
void setSelected()
Select the widget.
virtual void triggered()
Function called when the callback is triggered.
virtual void onStateChanged()
Function called when the state changes.
bool isDisabled() const noexcept
Check if the widget is disabled.
Definition: Widget.h:82
bool isDefault() const noexcept
Check if the widget is in default state.
Definition: Widget.h:96
Widget()
Constructor.
void triggerCallback()
Execute the callback function.
void setDisabled()
Disable the widget.
void setState(WidgetState state)
Set the state of the widget directly.
virtual bool contains(Vector2f coords)=0
Check if the widget contains the coordinates.
void setDefault()
Set the widget to it's default state.
bool isSelected() const
Check if the widget is selected.
Definition: Widget.h:110
WidgetState
State of a widget.
Definition: Widget.h:45
@ Default
The default widget state.
@ Selected
The widget is selected.
@ Disabled
The widget is disabled.
The namespace for gf classes.