summaryrefslogtreecommitdiffstats
path: root/edit.cpp
blob: 96dce6e753cf4143fb32823e8f61924f0267a394 (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
#include "edit.h"
#include "level.h"
#include "geometry.h"


static int editMode = EDIT_MODE_VIEW;

static ROOM *activeRoom = NULL;
static ROOM *hoveredRoom = NULL;

static Vertex hoveredVertex;
static int hasHoveredVertex = 0;

int getEditMode() {
  return editMode;
}

ROOM *getActiveRoom() {
  return activeRoom;
}

void setActiveRoom(ROOM *room) {
  activeRoom = room;
  
  if(room == NULL) {
    editMode = EDIT_MODE_VIEW;
  }
  else if(editMode == EDIT_MODE_VIEW) {
    editMode = EDIT_MODE_SELECTED;
  }
}

Vertex *getHoveredVertex() {
  if(hasHoveredVertex) return &hoveredVertex;
  else return NULL;
}

void setHoveredVertex(Vertex *v) {
  int i;
  LEVEL *l;
  
  if(v) {
    hasHoveredVertex = 1;
    hoveredVertex = *v;
    
    l = getLevel();
    hoveredRoom = NULL;
    
    for(i = 0; i < l->nRooms; i++) {
      if(l->rooms[i].polygon.contains(*v)) {
        hoveredRoom = &l->rooms[i];
        break;
      }
    }
  }
  else {
    hasHoveredVertex = 0;
    hoveredRoom = NULL;
  }
}

void startAddMode() {
  activeRoom = (ROOM*)calloc(1, sizeof(ROOM));
  activeRoom->polygon = Polygon();
  activeRoom->name = (char*)calloc(1, sizeof(unsigned char));
  
  editMode = EDIT_MODE_ADD;
}

void endAddMode() {
  editMode = activeRoom ? EDIT_MODE_SELECTED : EDIT_MODE_VIEW;
}

ROOM *getHoveredRoom() {
  return hoveredRoom;
}

static bool isLineOk(Line *l) {
  LEVEL *lvl = getLevel();
  Line l2;
  
  
  if(activeRoom) {
    for(int i = 0; i+2 < activeRoom->polygon.size(); i++) {
      l2.setVertex1(activeRoom->polygon[i]);
      l2.setVertex2(activeRoom->polygon[i+1]);
      
      if(l->intersects(l2, NULL) == INTERSECTION_SEGMENT_SEGMENT) return false;
    }
    
    if(activeRoom->polygon.size() > 1) {
      l2.setVertex1(activeRoom->polygon[activeRoom->polygon.size()-2]);
      l2.setVertex2(activeRoom->polygon.back());
      if(l2.contains(l->getVertex2())) return false;
    }
  }
  
  for(int i = 0; i < lvl->nRooms; i++) {
    if(lvl->rooms[i].polygon.intersects(*l))
      return false;
  }
  
  return true;
}

bool isVertexOk(Vertex *v) {
  LEVEL *lvl = getLevel();
  Line l;
  int i;
  
  
  for(i = 0; i < lvl->nRooms; i++) {
    if(lvl->rooms[i].polygon.contains(*v)) return false;
  }
  
  if(!(getActiveRoom() && !getActiveRoom()->polygon.empty()))
    return true;
  
  l.setVertex1(getActiveRoom()->polygon.back());
  l.setVertex2(*v);
  
  return isLineOk(&l);
}



bool isPolygonOk(Polygon *polygon) {
  LEVEL *lvl = getLevel();
  Line l, l2;
  
  if(polygon->empty()) return false;
  
  for(int i = 0; i < lvl->nRooms; i++) {
    if(lvl->rooms[i].polygon.empty()) continue;
    
    if(lvl->rooms[i].polygon.contains(polygon->front()));
      return false;
    
    if(polygon->contains(lvl->rooms[i].polygon.front()));
      return false;
  }
  
  if(polygon->size() == 1)
    return true;
  
  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(int i = 0; i < lvl->nRooms; i++) {
      if(lvl->rooms[i].polygon.intersects(l))
        return false;
    }
    
    if(it2 != polygon->begin()) {
      for(Polygon::const_iterator it3 = it2+1; it3 != polygon->end(); it3++) {
        Polygon::const_iterator it4 = it3+1;
        if(it4 == polygon->end()) it4 = polygon->begin();
        
        if(it == polygon->begin() && it4 == polygon->begin()) continue;
        
        l2.setVertex1(*it3);
        l2.setVertex2(*it4);
        
        if(l.intersects(l2, NULL) == INTERSECTION_SEGMENT_SEGMENT)
          return false;
      }
    }
  }
  
  return true;
}