-
Notifications
You must be signed in to change notification settings - Fork 0
/
Widget.cpp
121 lines (103 loc) · 1.78 KB
/
Widget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "Widget.h"
Widget::Widget(Widget * parent)
: _childs(),
_parent(parent),
_focus(false),
_callbacks()
{
if (this->_parent)
{
this->_parent->addChild(this);
}
}
Widget::~Widget()
{
this->_parent->removeChild(this);
}
void Widget::setParent(Widget * parent)
{
if (this->_parent)
{
this->_parent->removeChild(this);
}
this->_parent = parent;
if (this->_parent)
{
this->_parent->addChild(this);
}
}
void Widget::setFocus(bool focus)
{
if (focus != this->_focus)
{
this->_focus = focus;
this->focusChanged();
if (this->_focus)
{
this->emit(Widget::Event::Focus);
}
else
{
this->emit(Widget::Event::Blur);
}
}
}
bool Widget::getFocus() const
{
return this->_focus;
}
void Widget::bind(Widget::Event event, ICallback * callback)
{
this->_callbacks[event].push_back(callback);
}
void Widget::on(const sf::Event & event)
{
if (!this->handleEvent(event))
{
vector<Widget *>::iterator it = this->_childs.begin();
vector<Widget *>::iterator end = this->_childs.end();
while (it != end)
{
(*it)->on(event);
++it;
}
}
}
void Widget::addChild(Widget * child)
{
this->_childs.push_back(child);
}
void Widget::removeChild(Widget * child)
{
vector<Widget *>::const_iterator it = this->_childs.cbegin();
vector<Widget *>::const_iterator end = this->_childs.cend();
while (it != end)
{
if (*it == child)
{
this->_childs.erase(it);
return;
}
++it;
}
}
void Widget::emit(Widget::Event event)
{
if (this->_callbacks.count(event))
{
vector<ICallback *>::iterator it = this->_callbacks[event].begin();
vector<ICallback *>::iterator end = this->_callbacks[event].end();
while (it != end)
{
(**it)();
++it;
}
}
}
void Widget::focusChanged()
{
}
bool Widget::handleEvent(const sf::Event & event)
{
return false;
}