-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuadTreeLayout.cpp
149 lines (136 loc) · 4.05 KB
/
QuadTreeLayout.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "QuadTreeLayout.h"
#include "QuadTree.h"
namespace mercury {
QuadTreeLayout::QuadTreeLayout(tree::QuadTree* tree)
: m_tree{ tree }
{
this->Init();
}
QuadTreeLayout::~QuadTreeLayout() {
for (auto& child : m_children) {
delete child;
child = nullptr;
}
}
void QuadTreeLayout::Update(float dt) {
// remove all existing marks
m_marks->Clear();
// remove all existing quads
m_rects.clear();
// first scrap all drawables from the tree
this->AddTree();
this->AddSelectPoints();
// now update all children (points)
Node::Update(dt);
}
void QuadTreeLayout::Init() {
const float pointSize = 4.f;
m_marks = new Marks{ pointSize };
this->AddChild(m_marks);
m_selected.setFillColor({255, 0, 0, 80});
auto listener = [this](const sf::Event& event) {
switch (event.type) {
case sf::Event::MouseButtonPressed: {
// select region
if (event.mouseButton.button == sf::Mouse::Left) {
m_mouse.emplace(
static_cast<float>(event.mouseButton.x),
static_cast<float>(event.mouseButton.y)
);
m_selected.setPosition({ m_mouse->x, m_mouse->y });
m_selected.setSize({ 1.f, 1.f });
}
} break;
case sf::Event::MouseMoved: {
if (m_mouse) {
const mt::Pt mouse{
static_cast<float>(event.mouseMove.x),
static_cast<float>(event.mouseMove.y)
};
const mt::Rect selected{
std::min(mouse.x, m_mouse->x),
std::min(mouse.y, m_mouse->y),
std::max(mouse.x, m_mouse->x) - std::min(mouse.x, m_mouse->x),
std::max(mouse.y, m_mouse->y) - std::min(mouse.y, m_mouse->y)
};
m_selected.setPosition(selected.GetMinX(), selected.GetMinY());
m_selected.setSize({ selected.size.width, selected.size.height });
// update points
m_selectedPoints = m_tree->GetPointsAt(selected);
}
} break;
case sf::Event::MouseButtonReleased: {
// deselect region
if (event.mouseButton.button == sf::Mouse::Left) {
const mt::Pt mouse{
static_cast<float>(event.mouseButton.x),
static_cast<float>(event.mouseButton.y)
};
const mt::Rect selected {
std::min(mouse.x, m_mouse->x),
std::min(mouse.y, m_mouse->y),
std::max(mouse.x, m_mouse->x) - std::min(mouse.x, m_mouse->x),
std::max(mouse.y, m_mouse->y) - std::min(mouse.y, m_mouse->y)
};
// update points
m_selectedPoints = m_tree->GetPointsAt(selected);
// clean up
m_mouse.reset();
}
} break;
case sf::Event::KeyPressed: {
// insert point
if (event.key.code == sf::Keyboard::F) {
for (auto&point : m_selectedPoints) {
m_tree->Erase(point);
}
m_selectedPoints.clear();
}
} break;
default: break;
}
return false;
};
this->AddEventListener(std::move(listener));
}
void QuadTreeLayout::AddTree() {
m_tree->PostOrderVisit([this](tree::Node::pointer& node) {
// add quad shape:
sf::RectangleShape shape;
shape.setFillColor(sf::Color::Transparent);
shape.setOutlineColor(sf::Color::Blue);
shape.setOutlineThickness(2.f);
sf::Vector2f vec2{ node->m_box.origin.x, node->m_box.origin.y };
shape.setPosition(vec2);
shape.setSize({ node->m_box.size.width - 1.f, node->m_box.size.height - 1.f });
m_rects.push_back(shape);
// add points
for (const auto& point : node->m_data) {
m_marks->AddPoint({ point.x, point.y });
}
});
}
void QuadTreeLayout::AddSelectPoints() {
const sf::Color color{ 0, 255, 0, 255 };
const float pointSize{ m_marks->GetSize() + 2.f };
for (const auto& point : m_selectedPoints) {
sf::Vector2f position{
point.x - pointSize / 2.f,
point.y - pointSize / 2.f
};
sf::RectangleShape shape;
shape.setPosition(position);
shape.setSize({ pointSize, pointSize });
shape.setFillColor(color);
m_rects.push_back(shape);
}
}
void QuadTreeLayout::OnDraw(sf::RenderTarget& target, const sf::RenderStates& states) const {
for (auto&& rect : m_rects) {
target.draw(rect, states);
}
if (m_mouse) {
target.draw(m_selected, states);
}
}
} // namespace mercury