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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
#include "draw.h"
#include "edit.h"
#include "Rectangle.h"
#include <math.h>
#include <gtk/gtk.h>
#include <gtk/gtkgl.h>
#include <GL/gl.h>
static float scale = 100.0;
static float xTranslate = 0.0, yTranslate = 0.0;
static void drawGrid(const Rectangle &rect) {
float depth = log10f(scale)-0.75f;
float depth2 = floorf(depth);
float step = powf(0.1f, depth2);
float f;
int i;
//gchar *string;
float x1 = rect.getVertex1().getX(), y1 = rect.getVertex1().getY();
float x2 = rect.getVertex2().getX(), y2 = rect.getVertex2().getY();
glBegin(GL_LINES);
//cairo_set_font_size(cr, 10.0/scale);
for(i = 0; 0.4f*(depth-depth2+i-1) < 0.5f; i++) {
f = fminf(0.4f*(depth-depth2+i), 0.5f);
glColor3f(f, f, f);
for(f = x1 - fmodf(x1, step) - step; f <= x2; f+=step) {
glVertex2f(f, y1);
glVertex2f(f, y2);
/*if(step > 0.005) {
if(step > 0.5)
string = g_strdup_printf("%i", (int)rint(d));
else
string = g_strdup_printf("%.*f", -(int)floor(log10(step*1.1)), d+step/10);
cairo_move_to(cr, d+1/scale, y1+11/scale);
cairo_show_text(cr, string);
g_free(string);
}*/
}
for(f = y1 - fmodf(y1, step) - step; f <= y2; f+=step) {
glVertex2f(x1, f);
glVertex2f(x2, f);
/*if(step > 0.005) {
if(step > 0.5)
string = g_strdup_printf("%i", (int)rint(d));
else
string = g_strdup_printf("%.*f", -(int)floor(log10(step*1.1)), d+step/10);
cairo_move_to(cr, x1+3/scale, d+11/scale);
cairo_show_text(cr, string);
g_free(string);
}*/
}
step *= 10;
}
glEnd();
}
static void fillPolygon(const Polygon &polygon) {
std::vector<Triangle> triangles;
polygon.triangulate(triangles);
glBegin(GL_TRIANGLES);
for(std::vector<Triangle>::iterator t = triangles.begin(); t != triangles.end(); t++) {
glVertex2f(t->getVertexA().getX(), t->getVertexA().getY());
glVertex2f(t->getVertexB().getX(), t->getVertexB().getY());
glVertex2f(t->getVertexC().getX(), t->getVertexC().getY());
}
glEnd();
}
static void drawPolygon(const Polygon &polygon, bool close) {
glBegin(GL_LINE_STRIP);
for(Polygon::const_iterator vertex = polygon.begin(); vertex != polygon.end(); vertex++)
glVertex2d(vertex->getX(), vertex->getY());
if(close)
glVertex2d(polygon.front().getX(), polygon.front().getY());
glEnd();
}
gboolean drawTopView(GtkWidget *widget, GdkEventExpose *event, gpointer data) {
GdkGLContext *context = gtk_widget_get_gl_context(widget);
GdkGLDrawable *drawable = gtk_widget_get_gl_drawable(widget);
Vertex v1(0, 0), v2(widget->allocation.width, widget->allocation.height);
Rectangle rect;
if(getLevel() == NULL) return FALSE;
viewToImage(&v1);
viewToImage(&v2);
rect.setVertex1(v1);
rect.setVertex2(v2);
if(!gdk_gl_drawable_gl_begin(drawable, context))
return FALSE;
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLineWidth(1.0f);
glTranslatef(getImageWidth()/2-xTranslate, getImageHeight()/2-yTranslate, 0);
glScalef(scale, scale, 1);
drawGrid(rect);
for(Level::const_iterator room = getLevel()->begin(); room != getLevel()->end(); room++) {
if(&*room == getActiveRoom() && getEditMode() == EDIT_MODE_ADD) continue;
if(&*room == getActiveRoom())
glColor4f(0.0f, 0.7f, 1.0f, 0.2f);
else
glColor4f(0.0f, 0.7f, 1.0f, 0.3f);
fillPolygon(*room);
if(&*room == getActiveRoom()) {
glColor4f(1.0f, 1.0f, 1.0f, 0.9f);
glLineWidth(2.0f);
}
else if(&*room == getHoveredRoom() && getEditMode() != EDIT_MODE_ADD) {
glColor4f(0.0f, 0.7f, 1.0f, 0.7f);
glLineWidth(2.0f);
}
else {
glColor4f(0.0f, 0.7f, 1.0f, 0.7f);
glLineWidth(1.0f);
}
drawPolygon(*room, true);
}
if(getEditMode() == EDIT_MODE_ADD) {
if(isPolygonOk(getActiveRoom()))
glColor4f(0.0f, 0.7f, 1.0f, 0.2f);
else
glColor4f(1.0f, 0.3f, 0.3f, 0.2f);
fillPolygon(*getActiveRoom());
glLineWidth(2.0f);
glColor4f(0.0f, 0.7f, 1.0f, 0.7f);
drawPolygon(*getActiveRoom(), false);
if(!getActiveRoom()->empty() && getHoveredVertex()) {
if(!isVertexOk(getHoveredVertex()))
glColor4f(1.0f, 0.3f, 0.3f, 0.7f);
glBegin(GL_LINES);
glVertex2d(getActiveRoom()->back().getX(), getActiveRoom()->back().getY());
glVertex2d(getHoveredVertex()->getX(), getHoveredVertex()->getY());
glEnd();
}
}
glPopMatrix();
gdk_gl_drawable_swap_buffers(drawable);
gdk_gl_drawable_gl_end(drawable);
return TRUE;
}
float getScale() {
return scale;
}
void setScale(float s) {
scale = MAX(0.005, MIN(s, 10000));
}
void imageToView(Vertex *v) {
v->setX(v->getX()*scale+getImageWidth()/2-xTranslate);
v->setY(v->getY()*scale+getImageHeight()/2-yTranslate);
}
void viewToImage(Vertex *v) {
v->setX((v->getX()-getImageWidth()/2+xTranslate)/scale);
v->setY((v->getY()-getImageHeight()/2+yTranslate)/scale);
}
float getImageWidth() {
float min = 0.0, max = 0.0;
if(getLevel()) {
for(Level::iterator room = getLevel()->begin(); room != getLevel()->end(); room++) {
for(Room::iterator v = room->begin(); v != room->end(); v++) {
min = fminf(min, v->getX());
max = fmaxf(max, v->getX());
}
}
}
return (max-min+10)*scale;
}
float getImageHeight() {
float min = 0.0, max = 0.0;
if(getLevel()) {
for(Level::iterator room = getLevel()->begin(); room != getLevel()->end(); room++) {
for(Room::iterator v = room->begin(); v != room->end(); v++) {
min = fminf(min, v->getY());
max = fmaxf(max, v->getY());
}
}
}
return (max-min+10)*scale;
}
float getXTranslate() {
return xTranslate;
}
void setXTranslate(float x) {
xTranslate = x;
}
float getYTranslate() {
return yTranslate;
}
void setYTranslate(float y) {
yTranslate = y;
}
|