blob: 5822ffc10d0e4c9f746183033d50366e7718c389 (
plain)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
#include "EditManager.h"
#include "Window.h"
bool EditManager::lineOk(const Line& l) const {
Line l2;
if(activeRoom) {
for(size_t i = 0; i+2 < activeRoom->size(); i++) {
l2.setVertex1(activeRoom->at(i));
l2.setVertex2(activeRoom->at(i+1));
if(l.intersects(l2, NULL) == INTERSECTION_SEGMENT_SEGMENT) return false;
}
if(activeRoom->size() > 1) {
l2.setVertex1(activeRoom->at(activeRoom->size()-2));
l2.setVertex2(activeRoom->back());
if(l2.contains(l.getVertex2())) return false;
}
}
for(Level::iterator room = window->getLevel().begin(); room != window->getLevel().end(); room++) {
if(room->intersects(l))
return false;
}
return true;
}
EditManager::EditManager(Window *window) {
this->window = window;
activeRoom = NULL;
mode = VIEW;
hoveredRoom = NULL;
hasHoveredVertex = false;
}
void EditManager::addRoom() {
newRoom = Room(idManager.generate("room"));
mode = ADD;
activeRoom = &newRoom;
window->update();
}
void EditManager::finishRoom() {
mode = VIEW;
if(newRoom.size() > 2 && polygonOk(newRoom)) {
window->getLevel().push_back(newRoom);
activeRoom = &window->getLevel().back();
}
window->update();
}
void EditManager::addVertex(const Vertex &v) {
if(mode != ADD)
return;
newRoom.push_back(v);
window->update();
}
Vertex* EditManager::getHoveredVertex() {
if(hasHoveredVertex) return &hoveredVertex;
else return NULL;
}
void EditManager::setHoveredVertex(Vertex *v) {
if(v) {
hasHoveredVertex = true;
hoveredVertex = *v;
hoveredRoom = NULL;
for(Level::iterator room = window->getLevel().begin(); room != window->getLevel().end(); room++) {
if(room->contains(*v)) {
hoveredRoom = &*room;
break;
}
}
}
else {
hasHoveredVertex = false;
hoveredRoom = NULL;
}
window->update();
}
void EditManager::buttonPress(unsigned int button) {
switch(button) {
case 1:
if(!hasHoveredVertex)
break;
switch(mode) {
case VIEW:
activeRoom = getHoveredRoom();
break;
case ADD:
if(vertexOk(hoveredVertex))
addVertex(hoveredVertex);
}
window->update();
}
}
bool EditManager::vertexOk(const Vertex& v) const {
Line l;
for(Level::iterator room = window->getLevel().begin(); room != window->getLevel().end(); room++) {
if(room->contains(v)) return false;
}
if(!(activeRoom && !activeRoom->empty()))
return true;
l.setVertex1(activeRoom->back());
l.setVertex2(v);
return lineOk(l);
}
bool EditManager::polygonOk(const Polygon& polygon) const {
Line l, l2;
if(polygon.empty()) return false;
for(Level::iterator room = window->getLevel().begin(); room != window->getLevel().end(); room++) {
if(room->empty()) continue;
if(room->contains(polygon.front()))
return false;
if(polygon.contains(room->front()))
return false;
}
if(polygon.size() == 1)
return true;
if(!polygon.isSimple())
return false;
for(Polygon::const_iterator it = polygon.begin(); it != polygon.end(); it++) {
Polygon::const_iterator it2 = it+1;
if(it2 == polygon.end()) it2 = polygon.begin();
l.setVertex1(*it);
l.setVertex2(*it2);
for(Level::iterator room = window->getLevel().begin(); room != window->getLevel().end(); room++) {
if(room->intersects(l))
return false;
}
}
return true;
}
|