@@ -3,6 +3,10 @@ SFML Widgets
3
3
4
4
A simple GUI module for SFML.
5
5
6
+ - Spritesheet based: a single image file to customize widget style
7
+ - Simple events: set a ` std::function<void(void)> ` callback on widgets to trigger functions on UI events.
8
+ - Layouts: automatically align content without computing positions
9
+
6
10
![ workflow] ( https://github.com/abodelot/sfml-widgets/actions/workflows/ci.yml/badge.svg )
7
11
8
12
- Author: Alexandre Bodelot <alexandre.bodelot@gmail.com >
@@ -12,17 +16,160 @@ Run `make` to build the library (`lib/libsfml-widgets.a`) and the demo program.
12
16
13
17
You can then run the demo: ` ./sfml-widgets-demo `
14
18
19
+ ## Setup
20
+
21
+ 1 . Load resources (font, spritesheet) in static class ` gui::Theme `
22
+ 2 . Use ` gui::Menu ` to create a new sfml-widgets menu. It needs to be connected to your SFML render window, which is given to the constructor.
23
+ 3 . Create widgets, add theme to the menu and define callbacks on them. NOTE: widgets must be dynamically allocated (` new ` ). The ` gui::Menu ` destructor will take care of deallocating widgets.
24
+
25
+ Minimal example:
26
+
27
+ ``` cpp
28
+ #include < iostream>
29
+ #include < SFML/Graphics.hpp>
30
+ #include " Gui/Gui.hpp"
31
+
32
+ int main ()
33
+ {
34
+ sf::RenderWindow app(sf::VideoMode(800, 600), "SFML Widgets", sf::Style::Close);
35
+
36
+ // Declare menu
37
+ gui::Menu menu(app);
38
+
39
+ gui::Theme::loadFont ("demo/tahoma.ttf");
40
+ gui::Theme::loadTexture("demo/texture-default.png");
41
+
42
+ // Create some button widget
43
+ gui::Button* button = new gui::Button("My button");
44
+
45
+ // Insert button into menu
46
+ menu.add(button);
47
+
48
+ // Define a callback
49
+ button->setCallback([] {
50
+ std::cout << "click!" << std::endl;
51
+ });
52
+
53
+ // Start the application loop
54
+ while (app.isOpen())
55
+ {
56
+ // Process events
57
+ sf::Event event;
58
+ while (app.pollEvent(event))
59
+ {
60
+ // Send events to the menu
61
+ menu.onEvent(event);
62
+
63
+ if (event.type == sf::Event::Closed)
64
+ app.close();
65
+ }
66
+
67
+ // Optional: clear window with theme background color
68
+ app.clear(gui::Theme::windowBgColor);
69
+
70
+ // Render menu
71
+ app.draw(menu);
72
+
73
+ // Update the window
74
+ app.display();
75
+ }
76
+
77
+ return 0;
78
+ }
79
+ ```
80
+
81
+ ` demo/demo.cpp ` conains a more complex example, featuring all widgets.
82
+
15
83
## Widgets
16
84
17
- * ` gui::Button ` : a simple press button
18
- * ` gui::Checkbox ` : a button with enabled/disabled state
19
- * ` gui::Image ` : displays an SFML texture
20
- * ` gui::Label ` : a text element
21
- * ` gui::OptionsBox ` : a list of label/value pairs
22
- * ` gui::ProgressBar ` : a simple horizontal or vertical progress bar
23
- * ` gui::Slider ` : provides an horizontal or vertical slider
24
- * ` gui::TextBox ` : a one-line text editor
25
- * Layouts:
26
- * ` gui::HBoxLayout ` : lines up widgets horizontally
27
- * ` gui::VBoxLayout ` : lines up widgets vertically
28
- * ` gui::FormLayout ` : manages forms of input widgets and their associated labels
85
+ ### ` gui::Button `
86
+
87
+ A simple press button.
88
+
89
+ ![ button] ( doc/button.png )
90
+
91
+ ### ` gui::Checkbox `
92
+
93
+ A button with enabled/disabled state.
94
+
95
+ ![ checkbox] ( doc/checkbox.png )
96
+
97
+ ### ` gui::Image `
98
+
99
+ Displays an SFML texture.
100
+
101
+ It's a simple wrapper around ` sf::Texture ` , to display a texture as part of the UI.
102
+
103
+ ### ` gui::Label `
104
+
105
+ A static text element.
106
+
107
+ It's a simple wrapper around ` sf::Text ` , to display a text as part of the UI.
108
+
109
+ ### ` gui::OptionsBox `
110
+
111
+ A list of label/value pairs.
112
+
113
+ ![ optionsbox] ( doc/optionsbox.png )
114
+
115
+ Use templates to define value type. Example: ` gui::OptionsBox<sf::Color> ` .
116
+
117
+ Add value with: ` optionsBox->addItem("Red", sf::Color::Red) ` ;
118
+
119
+ ### ` gui::ProgressBar `
120
+
121
+ A simple horizontal or vertical progress bar.
122
+
123
+ ![ progress-bar] ( doc/progress-bar.png )
124
+
125
+ * ` orientation ` : ` gui::Horizontal ` or ` gui::Vertical `
126
+ * ` labelPlacement ` : ` gui::LabelNone ` , or ` gui::LabelOver ` , or ` gui::Outside `
127
+
128
+ ### ` gui::Slider `
129
+
130
+ Provides an horizontal or vertical slider.
131
+
132
+ ![ slider] ( doc/slider.png )
133
+
134
+ * ` orientation ` : ` gui::Horizontal ` or ` gui::Vertical `
135
+
136
+ ### ` gui::TextBox `
137
+
138
+ A one-line text editor.
139
+
140
+ ![ textbox] ( doc/textbox.png )
141
+
142
+ It supports text cursor, and text selection (with mouse or keyboard shortcuts).
143
+
144
+ ## Layouts
145
+
146
+ Layouts are containers for widgets. They are also widgets themselves, and can be nested!
147
+
148
+ ### ` gui::Menu `
149
+
150
+ The special, unique root layout. It behave like a ` VBoxLayout ` .
151
+
152
+ ### ` gui::HBoxLayout `
153
+
154
+ Lines up widgets horizontally.
155
+
156
+ Use ` layout->add(widget) ` to append a widget on a new line.
157
+
158
+ ### ` gui::VBoxLayout `
159
+
160
+ Lines up widgets vertically.
161
+
162
+ Use ` layout->add(widget) ` to append a widget on a new column.
163
+
164
+ ### ` gui::FormLayout `
165
+
166
+ Manages forms of input widgets and their associated labels.
167
+
168
+ Use ` layout->addRow("my label", widget) ` to add a new line with label on the left, and widget on the right.
169
+
170
+ ## Theming
171
+
172
+ To customize the theme, you can:
173
+
174
+ - Change the theme values (padding, color, font, etc.) defined the static class ` gui::Theme ` .
175
+ - Use a custom spritesheet image.
0 commit comments