Skip to content

Commit 51a4ea3

Browse files
committed
Use std::function for user callbacks
1 parent 378319f commit 51a4ea3

17 files changed

+133
-205
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ OBJDIR := obj
55
OBJ := $(SRC:%.cpp=$(OBJDIR)/%.o)
66

77
CC := g++
8-
CFLAGS := -I$(SRCDIR) -std=c++11 -pedantic -Wall -Wextra -Wwrite-strings -O2
8+
CFLAGS := -I$(SRCDIR) -std=c++11 -pedantic -Wall -Wextra -Wshadow -Wwrite-strings -O2
99
LDFLAGS := -lsfml-graphics -lsfml-window -lsfml-system -lGL
1010

1111
# Demo

demo/demo.cpp

Lines changed: 58 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,6 @@ sf::Color mkcolor(const std::string& hexcolor)
2424

2525
int main()
2626
{
27-
enum Callback
28-
{
29-
C_TEXT,
30-
C_COLOR,
31-
C_ROTATION,
32-
C_BOLD,
33-
C_UNDERLINED,
34-
C_SCALE,
35-
C_NEW_BUTTON,
36-
C_VSLIDER,
37-
C_QUIT
38-
};
39-
4027
// Create the main window
4128
sf::RenderWindow app(sf::VideoMode(640, 480), "SFML Widgets", sf::Style::Close);
4229

@@ -58,24 +45,42 @@ int main()
5845
gui::HBoxLayout* hbox = menu.addHBoxLayout();
5946
gui::FormLayout* form = hbox->addFormLayout();
6047

48+
sf::Text text("Hello world!", gui::Theme::getFont());
49+
text.setOrigin(text.getLocalBounds().width / 2, text.getLocalBounds().height / 2);
50+
text.setPosition(320, 360);
51+
6152
// Textbox
6253
gui::TextBox* textbox = new gui::TextBox();
6354
textbox->setText("Hello world!");
64-
form->addRow("Text", textbox, C_TEXT);
55+
textbox->setCallback([&]() {
56+
text.setString(textbox->getText());
57+
text.setOrigin(text.getLocalBounds().width / 2, text.getLocalBounds().height / 2);
58+
});
59+
form->addRow("Text", textbox);
6560

6661
gui::TextBox* textbox2 = new gui::TextBox();
6762
textbox2->setText("Hello world!");
6863
textbox2->setMaxLength(5);
6964
form->addRow("Text with limit (5)", textbox2);
7065

66+
gui::ProgressBar* pbar0 = new gui::ProgressBar();
67+
7168
// Slider for rotation
7269
gui::Slider* sliderRotation = new gui::Slider();
7370
sliderRotation->setQuantum(1);
74-
form->addRow("Rotation", sliderRotation, C_ROTATION);
71+
sliderRotation->setCallback([&]() {
72+
text.setRotation(sliderRotation->getValue() * 360 / 100.f);
73+
pbar0->setValue(sliderRotation->getValue());
74+
});
75+
form->addRow("Rotation", sliderRotation);
7576

7677
// Slider for scale
7778
gui::Slider* sliderScale = new gui::Slider();
78-
form->addRow("Scale", sliderScale, C_SCALE);
79+
sliderScale->setCallback([&]() {
80+
float scale = 1 + sliderScale->getValue() * 2 / 100.f;
81+
text.setScale(scale, scale);
82+
});
83+
form->addRow("Scale", sliderScale);
7984

8085
// OptionsBox for color
8186
gui::OptionsBox<sf::Color>* opt = new gui::OptionsBox<sf::Color>();
@@ -84,17 +89,35 @@ int main()
8489
opt->addItem("Green", sf::Color::Green);
8590
opt->addItem("Yellow", sf::Color::Yellow);
8691
opt->addItem("White", sf::Color::White);
87-
form->addRow("Color", opt, C_COLOR);
92+
opt->setCallback([&]() {
93+
text.setFillColor(opt->getSelectedValue());
94+
});
95+
form->addRow("Color", opt);
8896

8997
// Checbkox
9098
gui::CheckBox* checkboxBold = new gui::CheckBox();
91-
form->addRow("Bold text", checkboxBold, C_BOLD);
99+
checkboxBold->setCallback([&]() {
100+
int style = text.getStyle();
101+
if (checkboxBold->isChecked())
102+
style |= sf::Text::Bold;
103+
else
104+
style &= ~sf::Text::Bold;
105+
text.setStyle(style);
106+
});
107+
form->addRow("Bold text", checkboxBold);
92108

93109
gui::CheckBox* checkboxUnderlined = new gui::CheckBox();
94-
form->addRow("Underlined text", checkboxUnderlined, C_UNDERLINED);
110+
checkboxUnderlined->setCallback([&]() {
111+
int style = text.getStyle();
112+
if (checkboxUnderlined->isChecked())
113+
style |= sf::Text::Underlined;
114+
else
115+
style &= ~sf::Text::Underlined;
116+
text.setStyle(style);
117+
});
118+
form->addRow("Underlined text", checkboxUnderlined);
95119

96120
// Progress bar
97-
gui::ProgressBar* pbar0 = new gui::ProgressBar();
98121
form->addRow("Progress bar", pbar0);
99122
form->addRow("Default button", new gui::Button("button"));
100123

@@ -114,8 +137,10 @@ int main()
114137
gui::HBoxLayout* hbox2 = vbox->addHBoxLayout();
115138
gui::TextBox* textbox3 = new gui::TextBox(100);
116139
textbox3->setText("Button name");
117-
hbox2->add(textbox3, C_NEW_BUTTON);
118-
hbox2->addButton("Create button", C_NEW_BUTTON);
140+
hbox2->add(textbox3);
141+
hbox2->addButton("Create button", [&]() {
142+
vbox->add(new gui::Button(textbox3->getText()));
143+
});
119144

120145
// Small progress bar
121146
gui::HBoxLayout* hbox3 = vbox->addHBoxLayout();
@@ -124,9 +149,14 @@ int main()
124149
hbox3->add(pbar);
125150

126151
gui::Slider* vslider = new gui::Slider(100, gui::Slider::Vertical);
127-
hbox->add(vslider, C_VSLIDER);
152+
vslider->setCallback([&]() {
153+
pbar->setValue(vslider->getValue());
154+
});
155+
hbox->add(vslider);
128156

129-
menu.addButton("Quit", C_QUIT);
157+
menu.addButton("Quit", [&]() {
158+
app.close();
159+
});
130160

131161
sf::Texture texture;
132162
texture.loadFromFile("demo/sfml.png");
@@ -135,68 +165,16 @@ int main()
135165
sprite.setOrigin(texture.getSize().x / 2, texture.getSize().y / 2);
136166
sprite.setPosition(300, 360);
137167

138-
sf::Text text(textbox->getText(), gui::Theme::getFont());
139-
text.setOrigin(text.getLocalBounds().width / 2, text.getLocalBounds().height / 2);
140-
text.setPosition(320, 360);
141-
142-
// Start the game loop
168+
// Start the application loop
143169
while (app.isOpen())
144170
{
145171
// Process events
146172
sf::Event event;
147173
while (app.pollEvent(event))
148174
{
149-
int id = menu.onEvent(event);
150-
switch (id)
151-
{
152-
case C_TEXT:
153-
text.setString(textbox->getText());
154-
text.setOrigin(text.getLocalBounds().width / 2, text.getLocalBounds().height / 2);
155-
break;
156-
case C_COLOR:
157-
text.setFillColor(opt->getSelectedValue());
158-
break;
159-
case C_ROTATION:
160-
text.setRotation(sliderRotation->getValue() * 360 / 100.f);
161-
pbar0->setValue(sliderRotation->getValue());
162-
break;
163-
case C_UNDERLINED:
164-
{
165-
int style = text.getStyle();
166-
if (checkboxUnderlined->isChecked())
167-
style |= sf::Text::Underlined;
168-
else
169-
style &= ~sf::Text::Underlined;
170-
text.setStyle(style);
171-
break;
172-
}
173-
case C_BOLD:
174-
{
175-
int style = text.getStyle();
176-
if (checkboxBold->isChecked())
177-
style |= sf::Text::Bold;
178-
else
179-
style &= ~sf::Text::Bold;
180-
text.setStyle(style);
181-
break;
182-
}
183-
case C_SCALE:
184-
{
185-
float scale = 1 + sliderScale->getValue() * 2 / 100.f;
186-
text.setScale(scale, scale);
187-
break;
188-
}
189-
case C_QUIT:
190-
app.close();
191-
break;
192-
case C_VSLIDER:
193-
pbar->setValue(vslider->getValue());
194-
break;
195-
case C_NEW_BUTTON:
196-
vbox->addButton(textbox2->getText());
197-
break;
198-
}
199-
// Close window : exit
175+
// Send events to menu
176+
menu.onEvent(event);
177+
200178
if (event.type == sf::Event::Closed)
201179
app.close();
202180
}

src/Gui/Layouts/FormLayout.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@ FormLayout::FormLayout():
1111
}
1212

1313

14-
Widget* FormLayout::add(Widget* widget, int id)
14+
Widget* FormLayout::add(Widget* widget)
1515
{
16-
return addRow("", widget, id);
16+
return addRow("", widget);
1717
}
1818

1919

20-
Widget* FormLayout::addRow(const sf::String& str, Widget* widget, int id)
20+
Widget* FormLayout::addRow(const sf::String& str, Widget* widget)
2121
{
2222
gui::Label* label = new gui::Label(str);
2323
if (label->getSize().x > m_labelWidth)
24+
{
2425
m_labelWidth = label->getSize().x;
25-
push(label);
26-
27-
widget->setID(id);
28-
push(widget);
26+
}
27+
Layout::add(label);
28+
Layout::add(widget);
2929
return widget;
3030
}
3131

src/Gui/Layouts/FormLayout.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ class FormLayout: public Layout
1414
public:
1515
FormLayout();
1616

17-
Widget* add(Widget* widget, int id = -1) override;
17+
Widget* add(Widget*) override;
1818

1919
/**
2020
* @param label: label displayed before the widget
2121
* @param widget: widget to be added
2222
*/
23-
Widget* addRow(const sf::String& label, Widget* widget, int id = -1);
23+
Widget* addRow(const sf::String& label, Widget* widget);
2424

2525
private:
2626
void recomputeGeometry() override;

src/Gui/Layouts/HBoxLayout.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@
44
namespace gui
55
{
66

7-
Widget* HBoxLayout::add(Widget* widget, int id)
8-
{
9-
widget->setID(id);
10-
push(widget);
11-
return widget;
12-
}
13-
14-
157
void HBoxLayout::recomputeGeometry()
168
{
179
sf::Vector2f pos;

src/Gui/Layouts/HBoxLayout.hpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ namespace gui
1111
*/
1212
class HBoxLayout: public Layout
1313
{
14-
public:
15-
Widget* add(Widget* widget, int id = -1) override;
16-
17-
protected:
14+
private:
1815
void recomputeGeometry() override;
1916
};
2017

src/Gui/Layouts/Layout.cpp

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,30 @@ Layout::~Layout()
3030
}
3131

3232

33-
Button* Layout::addButton(const sf::String& string, int id)
33+
Widget* Layout::add(Widget* widget)
34+
{
35+
widget->setParent(this);
36+
37+
if (m_first == NULL)
38+
{
39+
m_first = m_last = widget;
40+
}
41+
else
42+
{
43+
m_last->m_next = widget;
44+
widget->m_previous = m_last;
45+
m_last = widget;
46+
}
47+
recomputeGeometry();
48+
return widget;
49+
}
50+
51+
52+
Button* Layout::addButton(const sf::String& string, std::function<void(void)> callback)
3453
{
3554
Button* button = new Button(string);
36-
add(button, id);
55+
button->setCallback(callback);
56+
add(button);
3757
return button;
3858
}
3959

@@ -70,25 +90,6 @@ VBoxLayout* Layout::addVBoxLayout()
7090
}
7191

7292

73-
Widget* Layout::push(Widget* widget)
74-
{
75-
widget->setParent(this);
76-
77-
if (m_first == NULL)
78-
{
79-
m_first = m_last = widget;
80-
}
81-
else
82-
{
83-
m_last->m_next = widget;
84-
widget->m_previous = m_last;
85-
m_last = widget;
86-
}
87-
recomputeGeometry();
88-
return widget;
89-
}
90-
91-
9293
Widget* Layout::getFirstWidget()
9394
{
9495
return m_first;

src/Gui/Layouts/Layout.hpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ class Layout: public Widget
2828
* The container will take care of widget deallocation
2929
* @return added widget
3030
*/
31-
virtual Widget* add(Widget* widget, int id = -1) = 0;
31+
virtual Widget* add(Widget* widget);
3232

3333
/// Helpers
34-
Button* addButton(const sf::String& string, int id = -1);
34+
Button* addButton(const sf::String& string, std::function<void(void)> callback);
3535
Label* addLabel(const sf::String& string);
3636
FormLayout* addFormLayout();
3737
HBoxLayout* addHBoxLayout();
@@ -54,11 +54,6 @@ class Layout: public Widget
5454
bool focusNextWidget();
5555
bool focusPreviousWidget();
5656

57-
/**
58-
* Append a new widget in the container
59-
*/
60-
Widget* push(Widget* widget);
61-
6257
Widget* getFirstWidget();
6358

6459
private:

src/Gui/Layouts/VBoxLayout.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@
44
namespace gui
55
{
66

7-
Widget* VBoxLayout::add(Widget* widget, int id)
8-
{
9-
widget->setID(id);
10-
push(widget);
11-
return widget;
12-
}
13-
14-
157
void VBoxLayout::recomputeGeometry()
168
{
179
sf::Vector2f pos;

src/Gui/Layouts/VBoxLayout.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ namespace gui
1111
*/
1212
class VBoxLayout: public Layout
1313
{
14-
public:
15-
Widget* add(Widget* widget, int id = -1) override;
16-
1714
private:
1815
void recomputeGeometry() override;
1916
};

0 commit comments

Comments
 (0)