Skip to content

Commit 9203f86

Browse files
committed
README: Widget documentation
1 parent 5ac35d3 commit 9203f86

File tree

10 files changed

+213
-13
lines changed

10 files changed

+213
-13
lines changed

README.md

+159-12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ SFML Widgets
33

44
A simple GUI module for SFML.
55

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+
610
![workflow](https://github.com/abodelot/sfml-widgets/actions/workflows/ci.yml/badge.svg)
711

812
- 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.
1216

1317
You can then run the demo: `./sfml-widgets-demo`
1418

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+
1583
## Widgets
1684

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.

demo/demo.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include "Gui/Menu.hpp"
21
#include "Gui/Theme.hpp"
32
#include "Gui/Gui.hpp"
43
#include <SFML/Graphics.hpp>

doc/button.png

1.05 KB
Loading

doc/checkbox.png

256 Bytes
Loading

doc/optionsbox.png

769 Bytes
Loading

doc/progress-bar.png

3.54 KB
Loading

doc/slider.png

1.07 KB
Loading

doc/textbox.png

1.88 KB
Loading

main.cpp

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <iostream>
2+
#include <SFML/Graphics.hpp>
3+
#include "Gui/Gui.hpp"
4+
5+
int main()
6+
{
7+
sf::RenderWindow app(sf::VideoMode(800, 600), "SFML Widgets", sf::Style::Close);
8+
9+
// Declare menu
10+
gui::Menu menu(app);
11+
12+
gui::Theme::loadFont("demo/tahoma.ttf");
13+
gui::Theme::loadTexture("demo/texture-default.png");
14+
15+
// Create some button widget
16+
gui::Button* button = new gui::Button("My button");
17+
18+
// Insert button into menu
19+
menu.add(button);
20+
21+
// Define a callback
22+
button->setCallback([] {
23+
std::cout << "click!" << std::endl;
24+
});
25+
26+
// Start the application loop
27+
while (app.isOpen())
28+
{
29+
// Process events
30+
sf::Event event;
31+
while (app.pollEvent(event))
32+
{
33+
// Send events to the menu
34+
menu.onEvent(event);
35+
36+
if (event.type == sf::Event::Closed)
37+
app.close();
38+
}
39+
40+
// Optional: clear window with theme background color
41+
app.clear(gui::Theme::windowBgColor);
42+
43+
// Render menu
44+
app.draw(menu);
45+
46+
// Update the window
47+
app.display();
48+
}
49+
50+
return 0;
51+
}

src/Gui/Gui.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#ifndef GUI_GUI_HPP
22
#define GUI_GUI_HPP
33

4+
#include "Menu.hpp"
5+
#include "Theme.hpp"
6+
47
// Enums
58
#include "Enums/Enums.hpp"
69

0 commit comments

Comments
 (0)