-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContainer.cpp
93 lines (76 loc) · 2.12 KB
/
Container.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
#include "Container.hpp"
#include <SFML/Window/Event.hpp>
#include <SFML/Graphics/RenderTarget.hpp>
gui::Container::Container()
: children(), selectedChild(-1)
{}
void gui::Container::pack(gui::Component::Ptr component) {
component->setParent(this);
children.push_back(component);
if (!hasSelection() && component->selectable())
select(children.size() - 1);
}
bool gui::Container::selectable() const {
return false;
}
void gui::Container::handleEvent(const sf::Event &event) {
if (event.type == sf::Event::KeyReleased) {
if (event.key.code == sf::Keyboard::Up) {
selectPrevious();
} else if (event.key.code == sf::Keyboard::Down) {
selectNext();
}
}
for(auto& child : children) {
child->handleEvent(event);
}
}
void gui::Container::draw(sf::RenderTarget &target, sf::RenderStates states) const {
states.transform *= getTransform();
for(const Component::Ptr& child : children) {
target.draw(*child, states);
}
}
bool gui::Container::hasSelection() const {
return selectedChild >= 0;
}
void gui::Container::select(std::size_t index) {
if (children[index]->selectable())
{
if (hasSelection())
children[selectedChild]->deselect();
children[index]->select();
selectedChild = index;
}
}
void gui::Container::selectNext() {
if (!hasSelection())
return;
// Search next component that is selectable, wrap around if necessary
int next = selectedChild;
do
next = (next + 1) % children.size();
while (!children[next]->selectable());
// Select that component
select(next);
}
void gui::Container::selectPrevious() {
if (!hasSelection())
return;
// Search previous component that is selectable, wrap around if necessary
int prev = selectedChild;
do
prev = (prev + children.size() - 1) % children.size();
while (!children[prev]->selectable());
// Select that component
select(prev);
}
sf::FloatRect gui::Container::getBounds() const {
// TODO: berechne Bounds anhand der Rects der Kinder.
return sf::FloatRect();
}
void gui::Container::update(sf::Time dt) {
for(const Component::Ptr& child : children) {
child->update(dt);
}
}