Snoke
widget.cpp
Go to the documentation of this file.
1 #include "widget.h"
2 
8 {}
9 
15 {
16  this->parentWidget = parentWidget;
17 }
18 
24 {
25  return parentWidget;
26 }
27 
33 {
34  return start;
35 }
36 
42 {
43  return ending;
44 }
45 
51 {
52  this->nextWidget = nextWidget;
53 }
54 
60 {
61  return nextWidget;
62 }
63 
69 {
70  this->previousWidget = previousWidget;
71 }
72 
78 {
79  return previousWidget;
80 }
81 
86 void Widget::add(Widget* child)
87 {
88  length++;
89  child->_parent(this);
90  this->ending = child;
91 
92  if (start == NULL)
93  {
94  start = child;
95  return;
96  }
97 
98  Widget* previousChild = start;
99  while (previousChild->next() != NULL)
100  {
101  previousChild = previousChild->next();
102  }
103 
104  previousChild->_next(child);
105  child->_previous(previousChild);
106 }
107 
112 unsigned short Widget::childLength()
113 {
114  return length;
115 }
116 
121 std::vector<Widget*> Widget::children()
122 {
123  std::vector<Widget*> childWidgets;
124  Widget* child = start;
125 
126  while (child != NULL)
127  {
128  childWidgets.push_back(child);
129  child = child->next();
130  }
131 
132  return childWidgets;
133 }
134 
140 bool Widget::event(unsigned char name)
141 {
142  return events & name;
143 }
144 
150 void Widget::listener(unsigned char name, Listener function)
151 {
152  listeners[name].push_back(function);
153  events = events | name;
154 }
155 
161 void Widget::dispatch(unsigned char name)
162 {
163  for (auto function = listeners[name].begin(); function != listeners[name].end(); function++)
164  {
165  (*function)();
166  }
167 }
168 
172 Widget::Widget() : parentWidget(NULL), start(NULL), ending(NULL), nextWidget(NULL), previousWidget(NULL), length(0)
173 {}
std::vector< Widget * > children()
Definition: widget.cpp:121
Widget * lastChild()
Definition: widget.cpp:41
bool event(unsigned char name)
Definition: widget.cpp:140
Widget * start
Definition: widget.h:40
std::function< void()> Listener
Definition: widget.h:22
void _previous(Widget *previousWidget)
Definition: widget.cpp:68
unsigned short childLength()
Definition: widget.cpp:112
Widget * ending
Definition: widget.h:41
Widget * parentWidget
Definition: widget.h:39
Widget * previous()
Definition: widget.cpp:77
unsigned short length
Definition: widget.h:44
virtual void draw()
Definition: widget.cpp:7
unsigned char events
Definition: widget.h:45
Widget * parent()
Definition: widget.cpp:23
void dispatch(unsigned char name)
Definition: widget.cpp:161
void _parent(Widget *parentWidget)
Definition: widget.cpp:14
Widget * previousWidget
Definition: widget.h:43
Widget * nextWidget
Definition: widget.h:42
void listener(unsigned char name, Listener function)
Definition: widget.cpp:150
Definition: widget.h:36
void _next(Widget *nextWidget)
Definition: widget.cpp:50
Widget()
Definition: widget.cpp:172
void add(Widget *child)
Definition: widget.cpp:86
Widget * next()
Definition: widget.cpp:59
std::map< unsigned char, std::list< Listener > > listeners
Definition: widget.h:46
Widget * firstChild()
Definition: widget.cpp:32