summaryrefslogtreecommitdiffstats
path: root/EditManager.cpp
blob: bae061de665c40c20ec359f4e04633eef1dc4dde (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "EditManager.h"
#include "Window.h"


bool EditManager::lineOk(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;
  
  selectedObject = NULL;
  mode = VIEW;
  
  hoveredObject = NULL;
  
  hasHoveredVertex = false;
}

void EditManager::redraw() {
  window->redraw();
}

void EditManager::addRoom() {
  if(mode == VIEW) {
    newRoom = Room();
    mode = ADD;
    
    selectedObject = &newRoom;
    
    window->update();
  }
}

void EditManager::finishRoom() {
  if(mode == ADD) {
    mode = VIEW;
    
    if(newRoom.size() > 2 && polygonOk(newRoom)) {
      newRoom.setName(idManager.generate("room"));
      window->getLevel().push_back(SharedPtr<LevelObject>(new Room(newRoom)));
      selectedObject = &*window->getLevel().back();
    }
    else {
      selectedObject = NULL;
    }
    
    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;
    
    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();
}

void EditManager::buttonPress(unsigned int button) {
  switch(button) {
    case 1:
      if(!hasHoveredVertex)
        break;
      
      switch(mode) {
        case VIEW:
          selectedObject = hoveredObject;
          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)->isOfType("Room") && ((Room*)&**room)->contains(v)) return false;
  }
  
  if(newRoom.empty())
    return true;
  
  l.setVertex1(newRoom.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 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;
}