Skip to content

Commit 2284b02

Browse files
committed
Added multiple themes support in demo, Added progress bar texture in theme
1 parent 51a4ea3 commit 2284b02

27 files changed

+181
-117
lines changed

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,18 @@ Author: Alexandre Bodelot <alexandre.bodelot@gmail.com>
88
Run `make` to build the library (`lib/libsfml-widgets.a`) and the demo program.
99

1010
You can then run the demo: `./sfml-widgets-demo`
11+
12+
## Widgets
13+
14+
* `gui::Button`: a simple press button
15+
* `gui::Checkbox`: a button with enabled/disabled state
16+
* `gui::Image`: displays an SFML texture
17+
* `gui::Label`: a text element
18+
* `gui::OptionsBox`: a list of label/value pairs
19+
* `gui::ProgressBar`: a simple horizontal progress bar
20+
* `gui::Slider`: provides a vertical or horizontal slider
21+
* `gui::TextBox`: a one-line text editor
22+
* Layouts:
23+
* `gui::HBoxLayout`: lines up widgets horizontally
24+
* `gui::VBoxLayout`: lines up widgets vertically
25+
* `gui::FormLayout`: manages forms of input widgets and their associated labels

demo/demo.cpp

+38-14
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#include "Gui/Menu.hpp"
22
#include "Gui/Theme.hpp"
33
#include "Gui/Gui.hpp"
4+
#include <SFML/Graphics.hpp>
45

56

6-
sf::Color mkcolor(const std::string& hexcolor)
7+
sf::Color hex2color(const std::string& hexcolor)
78
{
89
sf::Color color = sf::Color::Black;
910
if (hexcolor.size() == 7) // #ffffff
@@ -21,26 +22,41 @@ sf::Color mkcolor(const std::string& hexcolor)
2122
return color;
2223
}
2324

25+
struct Theme
26+
{
27+
sf::Color backgroundColor;
28+
std::string texturePath;
29+
};
2430

2531
int main()
2632
{
33+
Theme defaultTheme = {
34+
hex2color("#dddbde"),
35+
"demo/texture-default.png"
36+
};
37+
38+
Theme win98Theme = {
39+
hex2color("#d4d0c8"),
40+
"demo/texture-win98.png"
41+
};
42+
2743
// Create the main window
2844
sf::RenderWindow app(sf::VideoMode(640, 480), "SFML Widgets", sf::Style::Close);
2945

3046
gui::Menu menu(app);
3147
menu.setPosition(10, 10);
3248

3349
gui::Theme::loadFont("demo/tahoma.ttf");
34-
gui::Theme::loadTexture("demo/texture.png");
50+
gui::Theme::loadTexture(defaultTheme.texturePath);
3551
gui::Theme::textSize = 11;
36-
gui::Theme::click.textColor = mkcolor("#191B18");
37-
gui::Theme::click.textColorHover = mkcolor("#191B18");
38-
gui::Theme::click.textColorFocus = mkcolor("#000");
39-
gui::Theme::input.textColor = mkcolor("#000");
40-
gui::Theme::input.textColorHover = mkcolor("#000");
41-
gui::Theme::input.textColorFocus = mkcolor("#000");
52+
gui::Theme::click.textColor = hex2color("#191B18");
53+
gui::Theme::click.textColorHover = hex2color("#191B18");
54+
gui::Theme::click.textColorFocus = hex2color("#000");
55+
gui::Theme::input.textColor = hex2color("#000");
56+
gui::Theme::input.textColorHover = hex2color("#000");
57+
gui::Theme::input.textColorFocus = hex2color("#000");
4258
gui::Theme::PADDING = 2.f;
43-
gui::Theme::windowBgColor = mkcolor("#dddbde");
59+
gui::Theme::windowBgColor = defaultTheme.backgroundColor;
4460

4561
gui::HBoxLayout* hbox = menu.addHBoxLayout();
4662
gui::FormLayout* form = hbox->addFormLayout();
@@ -67,7 +83,7 @@ int main()
6783

6884
// Slider for rotation
6985
gui::Slider* sliderRotation = new gui::Slider();
70-
sliderRotation->setQuantum(1);
86+
sliderRotation->setStep(1);
7187
sliderRotation->setCallback([&]() {
7288
text.setRotation(sliderRotation->getValue() * 360 / 100.f);
7389
pbar0->setValue(sliderRotation->getValue());
@@ -125,14 +141,23 @@ int main()
125141
sf::Texture imgbutton;
126142
imgbutton.loadFromFile("demo/themed-button.png");
127143

128-
gui::SpriteButton* customButton = new gui::SpriteButton(imgbutton, "Play game");
129-
130-
customButton->setTextSize(18);
144+
gui::SpriteButton* customButton = new gui::SpriteButton(imgbutton, "Play");
145+
customButton->setTextSize(20);
131146
form->addRow("Custom button", customButton);
132147

133148
gui::VBoxLayout* vbox = hbox->addVBoxLayout();
134149
vbox->addLabel("This pannel is on the left");
135150

151+
gui::OptionsBox<Theme>* themeBox = new gui::OptionsBox<Theme>();
152+
themeBox->addItem("Windows 98", win98Theme);
153+
themeBox->addItem("Default", defaultTheme);
154+
themeBox->setCallback([&]() {
155+
const Theme& theme = themeBox->getSelectedValue();
156+
gui::Theme::loadTexture(theme.texturePath);
157+
gui::Theme::windowBgColor = theme.backgroundColor;
158+
});
159+
vbox->add(themeBox);
160+
136161
// Textbox
137162
gui::HBoxLayout* hbox2 = vbox->addHBoxLayout();
138163
gui::TextBox* textbox3 = new gui::TextBox(100);
@@ -174,7 +199,6 @@ int main()
174199
{
175200
// Send events to menu
176201
menu.onEvent(event);
177-
178202
if (event.type == sf::Event::Closed)
179203
app.close();
180204
}

demo/texture-default.png

7.49 KB
Loading

demo/texture-win98.png

7.08 KB
Loading

demo/texture.png

-435 Bytes
Binary file not shown.

src/Gui/Button.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ void Button::onMouseMoved(float x, float y)
5959
}
6060

6161

62+
void Button::onMousePressed(float x, float y)
63+
{
64+
m_box.press();
65+
}
66+
67+
6268
void Button::onMouseReleased(float x, float y)
6369
{
6470
if (containsPoint({x, y}))

src/Gui/Button.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class Button: public Widget
2727

2828
void onStateChanged(State state) override;
2929
void onMouseMoved(float x, float y) override;
30+
void onMousePressed(float x, float y) override;
3031
void onMouseReleased(float x, float y) override;
3132
void onKeyPressed(const sf::Event::KeyEvent& key) override;
3233
void onKeyReleased(const sf::Event::KeyEvent& key) override;

src/Gui/CheckBox.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ CheckBox::CheckBox(bool checked):
1111
float box_size = m_cross.getSize().x + offset * 2;
1212
m_box.setSize(box_size, box_size);
1313
m_cross.setPosition(offset, offset);
14-
m_cross.setColor(Theme::input.textColor);
1514
check(checked);
1615

1716
setSize(m_box.getSize());

src/Gui/Image.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ namespace gui
44
{
55

66
Image::Image():
7-
m_texture(NULL)
7+
m_texture(nullptr)
88
{
99
setSelectable(false);
1010
}
1111

1212

1313
Image::Image(const sf::Texture& texture):
14-
m_texture(NULL)
14+
m_texture(nullptr)
1515
{
1616
setSelectable(false);
1717
setTexture(texture);
@@ -42,7 +42,7 @@ void Image::setColor(const sf::Color& color)
4242

4343
void Image::draw(sf::RenderTarget& target, sf::RenderStates states) const
4444
{
45-
if (m_texture != NULL)
45+
if (m_texture != nullptr)
4646
{
4747
states.transform *= getTransform();
4848
states.texture = m_texture;

src/Gui/Layouts/FormLayout.cpp

+1-7
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@ FormLayout::FormLayout():
1111
}
1212

1313

14-
Widget* FormLayout::add(Widget* widget)
15-
{
16-
return addRow("", widget);
17-
}
18-
19-
2014
Widget* FormLayout::addRow(const sf::String& str, Widget* widget)
2115
{
2216
gui::Label* label = new gui::Label(str);
@@ -35,7 +29,7 @@ void FormLayout::recomputeGeometry()
3529
size_t i = 0;
3630
sf::Vector2f pos;
3731
sf::Vector2f size;
38-
for (Widget* widget = getFirstWidget(); widget != NULL; widget = widget->m_next)
32+
for (Widget* widget = getFirstWidget(); widget != nullptr; widget = widget->m_next)
3933
{
4034
if (i % 2 == 0)
4135
{

src/Gui/Layouts/FormLayout.hpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ class FormLayout: public Layout
1414
public:
1515
FormLayout();
1616

17-
Widget* add(Widget*) override;
18-
1917
/**
18+
* Add a new label/widget row in the form
2019
* @param label: label displayed before the widget
2120
* @param widget: widget to be added
2221
*/

src/Gui/Layouts/HBoxLayout.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ void HBoxLayout::recomputeGeometry()
88
{
99
sf::Vector2f pos;
1010
sf::Vector2f size;
11-
for (Widget* w = getFirstWidget(); w != NULL; w = w->m_next)
11+
for (Widget* w = getFirstWidget(); w != nullptr; w = w->m_next)
1212
{
1313
w->setPosition(pos);
1414
pos.x += w->getSize().x + Theme::MARGIN;

src/Gui/Layouts/HBoxLayout.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace gui
1111
*/
1212
class HBoxLayout: public Layout
1313
{
14+
public:
1415
private:
1516
void recomputeGeometry() override;
1617
};

0 commit comments

Comments
 (0)