blob: 0052c8f7215a986940726aa009018f9a2a72650a (
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
|
#include "EditManager.h"
#include "Window.h"
bool EditManager::lineOk(const Room &newRoom, const Line &l) const {
Line l2;
for(size_t i = 0; i+2 < newRoom.size(); i++) {
l2.setVertex1(newRoom.at(i));
l2.setVertex2(newRoom.at(i+1));
if(l.intersects(l2, NULL) == INTERSECTION_SEGMENT_SEGMENT) return false;
}
if(newRoom.size() > 1) {
l2.setVertex1(newRoom.at(newRoom.size()-2));
l2.setVertex2(newRoom.back());
if(l2.contains(l.getVertex2())) return false;
}
for(Level::iterator room = window->getLevel().begin(); room != window->getLevel().end(); room++) {
if((*room)->isOfType("Room") && ((Room*)&**room)->intersects(l))
return false;
}
return true;
}
EditManager::EditManager(Window *window) {
this->window = window;
hoveredObject = NULL;
highlightedObject = NULL;
selectedObject = NULL;
hasHoveredVertex = false;
}
void EditManager::redraw() {
window->redraw();
}
bool EditManager::addRoom(const Room &newRoom) {
if(newRoom.size() > 2 && polygonOk(newRoom)) {
Room *room = new Room(newRoom);
room->setName(idManager.generate("room"));
window->getLevel().push_back(SharedPtr<LevelObject>(room));
selectedObject = room;
window->resetTool();
return true;
}
return false;
}
const Vertex* EditManager::getHoveredVertex() const {
if(hasHoveredVertex) return &hoveredVertex;
else return NULL;
}
void EditManager::setHoveredVertex(const Vertex *v) {
if(v) {
hasHoveredVertex = true;
hoveredVertex = *v;
hoveredObject = NULL;
for(Level::iterator object = window->getLevel().begin(); object != window->getLevel().end(); object++) {
if((*object)->hit(*v)) {
if(hoveredObject && (*object)->getPriority() < hoveredObject->getPriority())
continue;
hoveredObject = &**object;
}
}
}
else {
hasHoveredVertex = false;
hoveredObject = NULL;
}
window->update();
}
bool EditManager::vertexOk(const Vertex &v, const Room *newRoom) const {
Line l;
for(Level::iterator room = window->getLevel().begin(); room != window->getLevel().end(); room++) {
if((*room)->isOfType("Room") && ((Room*)&**room)->contains(v)) return false;
}
if(!newRoom)
return true;
if(newRoom->empty())
return true;
l.setVertex1(newRoom->back());
l.setVertex2(v);
return lineOk(*newRoom, l);
}
bool EditManager::polygonOk(const Polygon& polygon) const {
Line l, l2;
if(polygon.empty()) return false;
for(Level::iterator object = window->getLevel().begin(); object != window->getLevel().end(); object++) {
if(!(*object)->isOfType("Room"))
continue;
Room *room = (Room*)&**object;
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)->isOfType("Room") && ((Room*)&**room)->intersects(l))
return false;
}
}
return true;
}
|