This repository has been archived by the owner on Jan 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EditorViews.pde
339 lines (301 loc) · 8.94 KB
/
EditorViews.pde
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
class EditorView2d extends Widget {
private PVector worldLocation = new PVector(0, 0);
private float viewZoom = 1.0;
private float wordUnitToPixels = 16;
private boolean panning = false;
private PVector panOffset = new PVector(0, 0);
private PVector selectedPoint = null;
private PVector lastSelectedPoint = null;
private boolean removeMode = false;
public EditorView2d(int minW, int minH) {
super();
setMinW(minW);
setMinH(minH);
setW(minW);
setH(minH);
lastSelectedPoint = island.getPointAt(0);
}
public void reset() {
worldLocation = new PVector(0, 0);
viewZoom = 1.0;
wordUnitToPixels = 16;
panning = false;
//panOffset = new PVector(0, 0);
selectedPoint = null;
lastSelectedPoint = null;
removeMode = false;
}
public void setLastSelectedPoint(PVector p) {
lastSelectedPoint = p;
}
public void setViewZoom(float val) {
viewZoom = val;
}
public float getViewZoom() {
return viewZoom;
}
//public void setPanOffset(PVector p) {
// panOffset = p;
//}
//public PVector getPanOffset() {
// return panOffset;
//}
public void setWorldLocation(PVector p) {
worldLocation=p;
}
public PVector getWorldLocation() {
return worldLocation;
}
public void draw(PGraphics g) {
super.draw(g);
if (!drawClip(g)) return;
g.background(255);
g.pushMatrix();
translate(getScreenX()+getW()/2.0+worldLocation.x, getScreenY()+getH()/2.0+worldLocation.y);
scale(viewZoom);
// draw location gizmo
g.strokeWeight(4);
g.stroke(255, 0, 0);
g.line(0, 0, wordUnitToPixels*2, 0);
g.stroke(0, 255, 0);
g.line(0, 0, 0, wordUnitToPixels*2);
g.strokeWeight(1);
// draw Island object
island.draw2d(g);
//draw selected point
if (lastSelectedPoint != null) {
g.noStroke();
g.fill(255, 255, 0);
g.ellipse(lastSelectedPoint.x, lastSelectedPoint.y, 16, 16);
}
// Speed hack, it's not in onMouseMouve because event in processing are blocking by default
// so the movement isn't fluid.
// here it's done every frame so framerate will drop but we will se the regeneration of the island;
if (selectedPoint != null) {
PVector m = screenToWorld(mouseX, mouseY);
island.movePoint(selectedPoint, m);
}
g.popMatrix();
g.noClip();
}
// WORLD <- TRANSFORM -> Screen+local
private final PVector screenToWorld(int x, int y) {
PVector p = new PVector(x, y);
p.sub(getScreenX()+getW()/2.0, getScreenY()+getH()/2.0);
p.sub(worldLocation);
p.div(viewZoom);
return p;
}
private final PVector localToWorld(int x, int y) {
return screenToWorld(getScreenX()+getX()+x, getScreenY()+getY()+y);
}
private final PVector worldToScreen(int x, int y) {
PVector p = new PVector(x, y);
p.mult(viewZoom);
p.add(worldLocation);
p.add(getScreenX()+getW()/2.0, getScreenY()+getH()/2.0);
return p;
}
private final PVector worldToLocal(int x, int y) {
return worldToScreen(x, y).sub(getScreenX(), getScreenY());
}
// EVENTS
public void onMouseWheel(int x, int y, int direction) {
viewZoom -= 0.1*direction;
if (viewZoom < 0.01) viewZoom = 0.01;
}
public void onMouseMove(int oldx, int oldy, int newx, int newy) {
super.onMouseMove(oldx, oldy, newx, newy);
if (panning) {
PVector p = new PVector(getScreenX()+getX()+newx, getScreenY()+getY()+newy);
p.sub(getScreenX()+getW()/2.0, getScreenY()+getH()/2.0);
worldLocation = p.add(panOffset);
}
}
public void onMouseDown(int x, int y, int button) {
super.onMouseDown(x, y, button);
if (button == CENTER) {
PVector p = new PVector(getScreenX()+getX()+x, getScreenY()+getY()+y);
p.sub(getScreenX()+getW()/2.0, getScreenY()+getH()/2.0);
panOffset = worldLocation.copy().sub(p);
panning = true;
} else if (button == LEFT) {
if (removeMode) {
for (PVector p : island.getPoints()) {
PVector m = localToWorld(x, y);
if ( sq(m.x-p.x)+sq(m.y-p.y) <= 64*viewZoom) {
island.removePoint(p);
if (selectedPoint == p)
selectedPoint = null;
if (lastSelectedPoint == p);
lastSelectedPoint = island.getPointAt(0);
break;
}
}
} else {
for (PVector p : island.getPoints()) {
PVector m = localToWorld(x, y);
if ( sq(m.x-p.x)+sq(m.y-p.y) <= 64*viewZoom) {
selectedPoint = p;
lastSelectedPoint = p;
break;
}
}
}
} else if (button == RIGHT) {
PVector m = localToWorld(x, y);
island.addPoint(m, island.getPoints().indexOf(lastSelectedPoint));
lastSelectedPoint = m;
}
}
public void onMouseUp(int x, int y, int button) {
super.onMouseUp(x, y, button);
panning = false;
selectedPoint = null;
}
public void onMouseExit() {
super.onMouseExit();
panning = false;
removeMode = false;
selectedPoint = null;
}
public void onKeyDown(char c, int code) {
super.onKeyDown(c, code);
if (code == SHIFT) {
removeMode = true;
}
}
public void onKeyUp(char c, int code) {
super.onKeyUp(c, code);
removeMode = false;
}
}
class EditorView3d extends Widget {
private PGraphics pg;
private boolean panning = false;
private boolean rotating = false;
private PVector worldLocation = new PVector(0, 0);
private PVector worldRotation = new PVector(PI/4.0, PI/4.0);
private float viewZoom = 1.0;
private PVector panOffset = new PVector(0, 0);
private float wordUnitToPixels = 16;
public EditorView3d(int minW, int minH) {
super();
pg = createGraphics(minW, minH, P3D);
setMinW(minW);
setMinH(minH);
setW(minW);
setH(minH);
}
public void reset() {
worldLocation = new PVector(0, 0);
viewZoom = 1.0;
wordUnitToPixels = 16;
panning = false;
panOffset = new PVector(0, 0);
worldRotation = new PVector(PI/4.0, PI/4.0);
rotating = false;
}
public void setViewZoom(float val) {
viewZoom = val;
}
public float getViewZoom() {
return viewZoom;
}
public void setPanOffset(PVector p) {
panOffset = p;
}
public PVector getPanOffset() {
return panOffset;
}
public void setWorldLocation(PVector p) {
worldLocation=p;
}
public PVector getWorldLocation() {
return worldLocation;
}
public void setWorldRotation(PVector p) {
worldRotation = p;
}
public PVector getWorldRotation() {
return worldRotation;
}
public void draw(PGraphics g) {
super.draw(g);
if (!drawClip(g)) return;
pg.beginDraw();
pg.background(0);
pg.camera(getW()/2.0, getH()/2.0, ((getH()/2.0) / tan(PI*30.0 / 180.0))*viewZoom, getW()/2.0, getH()/2.0, 0, 0, 1, 0);
pg.translate(getW()/2.0+worldLocation.x, getH()/2.0+worldLocation.y, 0);
// Setup ligthinpg.
pg.directionalLight(255, 255, 255, 1, 1, 0);
pg.ambientLight(100, 100, 100);
// Rotate scene
pg.rotateX(worldRotation.x);
pg.rotateZ(worldRotation.y);
// draw location gizmo
pg.strokeWeight(4);
pg.stroke(255, 0, 0);
pg.line(0, 0, 0, wordUnitToPixels*2, 0, 0);
pg.stroke(0, 255, 0);
pg.line(0, 0, 0, 0, wordUnitToPixels*2, 0);
pg.stroke(0, 0, 255);
pg.line(0, 0, 0, 0, 0, wordUnitToPixels*2);
pg.strokeWeight(1);
// draw Island object
island.draw3d(pg);
pg.endDraw();
g.image(pg, getScreenX(), getScreenY());
g.noClip();
}
public void resize(int nw, int nh) {
super.resize(nw, nh);
pg.setSize(nw, nh);
}
// WORLD <- TRANSFORM -> Screen+local
private final PVector screenToWorld(int x, int y) {
PVector p = new PVector(x, y);
p.sub(getScreenX()+getW()/2.0, getScreenY()+getH()/2.0);
return p;
}
private final PVector localToWorld(int x, int y) {
return screenToWorld(getScreenX()+getX()+x, getScreenY()+getY()+y);
}
private final PVector worldToScreen(int x, int y) {
PVector p = new PVector(x, y);
p.add(getScreenX()+getW()/2.0, getScreenY()+getH()/2.0);
return p;
}
private final PVector worldToLocal(int x, int y) {
return worldToScreen(x, y).sub(getScreenX()+getX(), getScreenY()+getY());
}
// EVENTS
public void onMouseWheel(int x, int y, int direction) {
viewZoom -= 0.1*direction;
if (viewZoom < 0.01) viewZoom = 0.01;
}
public void onMouseMove(int oldx, int oldy, int newx, int newy) {
if (panning) {
worldLocation = localToWorld(newx, newy).sub(panOffset);
} else if (rotating) {
worldRotation.x += (newy-oldy)*0.001;
worldRotation.y += (newx-oldx)*0.001;
}
}
public void onMouseDown(int x, int y, int button) {
if (button == CENTER) {
panOffset = localToWorld(x, y).sub(worldLocation.x, worldLocation.y);
panning = true;
} else if (button == RIGHT) {
rotating = true;
}
}
public void onMouseUp(int x, int y, int button) {
panning = false;
rotating = false;
}
public void onMouseExit() {
panning = false;
rotating = false;
}
}