forked from yangsu/Synaptic-Wall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SynapticWall.pde
413 lines (382 loc) · 12 KB
/
SynapticWall.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
public class SynapticWall extends Constants {
private boolean gMagnify;
private int gCurrentMode;
private String gCurrentModeLabel;
private PImage gMagnified;
private Selector gSelector;
private ObjectCollection gObjs;
private ControlPanel gCPanel;
private Interactive gLastSelected;
private Initiator gCurrInitiator;
private Soma gCurrShape;
private Path gCurrPath;
private PVector gIndicator;
private PVector gIndicator2;
public SynapticWall() {
gCurrentMode = CREATION;
gCurrentModeLabel = "CREATION";
gMagnify = false;
gMagnified = createImage(HALFWIDTH, HALFHEIGHT, ARGB);
gSelector = new Selector();
gObjs = new ObjectCollection();
gCPanel = new ControlPanel(gObjs);
gCurrShape = null;
gCurrPath = null;
gIndicator = new PVector(MIN, MIN);
gIndicator2 = new PVector(MIN, MIN);
gLastSelected = null;
gCurrInitiator = null;
}
private void snapIndicators(float x, float y, float size) {
float angle = Util.getAngleNorm(x, y, mouseX, mouseY);
float tloc = size-SOMA_RING_WIDTH/2;
gIndicator.set(cos(angle)*tloc+x, sin(angle)*tloc+y, 0);
}
private void clearIndicators() {
gIndicator.set(MIN, MIN, 0);
gIndicator2.set(MIN, MIN, 0);
}
private void drawBackground(color cc) {
pushStyle();
noStroke();
fill(cc);
rect(0, 0, width, height);
gGrid.draw();
popStyle();
}
private void drawAndUpdateContent() {
drawBackground(BG_COLOR);
gObjs.drawAndUpdate();
gCPanel.draw();
if (gCurrShape != null)
gCurrShape.drawAndUpdate();
if (gCurrPath != null)
gCurrPath.drawAndUpdate();
if (gCurrInitiator != null && gCurrInitiator.fMovable)
gCurrInitiator.drawAndUpdate();
}
private void drawContent() {
drawBackground(BG_COLOR);
gObjs.draw();
gCPanel.draw();
if (gCurrShape != null)
gCurrShape.draw();
if (gCurrPath != null)
gCurrPath.draw();
if (gCurrInitiator != null && gCurrInitiator.fMovable)
gCurrInitiator.draw();
}
private void drawMagnified() {
pushStyle();
pushMatrix();
translate(mouseX, mouseY);
scale(ZOOM_FACTOR);
translate(-mouseX, -mouseY);
fill(FADE_COLOR);
rect(0, 0, width, height);
drawContent();
popMatrix();
int magnifiedX = constrain(mouseX-QUARTERWIDTH, 0, HALFWIDTH);
int magnifiedY = constrain(mouseY-QUARTERHEIGHT, 0, HALFHEIGHT);
gMagnified = get(magnifiedX, magnifiedY, HALFWIDTH, HALFHEIGHT);
drawContent();
image(gMagnified, magnifiedX, magnifiedY);
noFill();
stroke(255);
strokeWeight(5);
rect(magnifiedX, magnifiedY, HALFWIDTH, HALFHEIGHT);
popStyle();
}
public void draw() {
if (gMagnify)
drawMagnified();
else
drawAndUpdateContent();
gSelector.draw();
pushStyle();
fill(255);
float s = PATH_JUNCTION_WIDTH;
ellipse(gIndicator.x, gIndicator.y, s, s);
ellipse(gIndicator2.x, gIndicator2.y, s, s);
text(gCurrentModeLabel, 0, 20);
String fps = nf(frameRate, 2, 2) + " FPS";
text(fps, width - 60, 20);
popStyle();
}
private void clear() {
gObjs.reset();
gCurrShape = null;
gCurrPath = null;
gCurrInitiator = null;
clearIndicators();
}
public void onDblClick() {
if (gCurrentMode == INTERACTION) {
gObjs.onDblClick(mouseX, mouseY);
}
}
public void onMousePressed() {
gGrid.onMouseDown(mouseX, mouseY);
cursor(CROSS);
Interactive selected = null;
if (gCurrentMode == CREATION) {
if (gObjs.select(mouseX, mouseY)) {
selected = gObjs.getSelected().get(0);
gLastSelected = selected;
// If selected is the gCurrInitiator, then create axon
if (selected.getType() == INITIATOR ||
selected.getType() == SOMA) {
Cell c = (Cell)selected;
snapIndicators(c.x(), c.y(), c.fSize);
}
// if selected is a dendrite or an axon
else if (selected.getType() == AXON) {
Path p = (Path)selected;
gIndicator.set(p.getCurrVertex());
gCurrPath = new Axon(p, gIndicator.x, gIndicator.y, p.fColor);
}
else if (selected.getType() == DENDRITE) {
Path p = (Path)selected;
gIndicator.set(p.getCurrVertex());
gCurrPath = new Dendrite(p, gIndicator.x, gIndicator.y, p.fColor);
}
// if selected is a synapse, the create an dendrite
else if (selected.getType() == SYNAPSE) {
Synapse s = (Synapse)selected;
snapIndicators(s.x(), s.y(), s.fSize);
}
else {}
}
else {
PVector pos = gGrid.getCurrent();
if (mouseButton == RIGHT)
gCurrInitiator = new Initiator(
pos.x,
pos.y,
SOMA_SIZE,
EX_COLOR
);
else if (mouseButton == LEFT)
gCurrShape = new Soma(pos.x, pos.y);
}
}
else if (gCurrentMode == DELETION) {
if (gObjs.select(mouseX, mouseY)) {
selected = gObjs.getSelected().get(0);
gObjs.remove(selected);
}
}
else if (gCurrentMode == INTERACTION) {
if (!gObjs.onMouseDown(mouseX, mouseY, key, keyCode) && !gCPanel.onMouseDown(mouseX, mouseY)) {
gSelector.beginSelection(mouseX, mouseY);
}
}
else {
// Do nothing
}
redraw();
}
public void onMouseDragged() {
gGrid.onMouseDragged(mouseX, mouseY);
if (gCurrentMode == CREATION) {
PVector gridPoint = gGrid.getCurrent();
if (gCurrInitiator != null && gCurrInitiator.fMovable) {
gCurrInitiator.translate(new PVector(
gridPoint.x - gCurrInitiator.x(),
gridPoint.y - gCurrInitiator.y()
));
return;
}
if (gCurrShape != null && gCurrShape.fMovable) {
gCurrShape.translate(new PVector(
gridPoint.x - gCurrShape.x(),
gridPoint.y - gCurrShape.y()
));
return;
}
if (gCurrPath != null) {
gCurrPath.add(gridPoint.x, gridPoint.y);
}
Interactive selected = null;
if (gObjs.select(mouseX, mouseY) ||
gObjs.select(gridPoint.x, gridPoint.y)) {
selected = gObjs.getSelected().get(0);
if (selected != gLastSelected) {
if (selected.getType() == INITIATOR ||
selected.getType() == SOMA) {
if (gCurrPath != null) {
gCurrPath.close();
}
}
else if (selected.getType() == DENDRITE ||
selected.getType() == AXON) {
// Show Indicator
PVector end = ((Path)selected).getCurrVertex();
gIndicator2.set(end.x, end.y, 0);
}
gLastSelected = selected;
}
else { // If not different from the last
// If still in the gCurrInitiator or the last selected soma
if (selected.getType() == INITIATOR ||
selected.getType() == SOMA ||
selected.getType() == SYNAPSE) {
Shape s = (Shape)selected;
snapIndicators(s.x(), s.y(), s.fSize);
}
}
}
else { // if not selected anything
if (gLastSelected != null && gCurrPath == null) {
if (gLastSelected.getType() == INITIATOR ||
gLastSelected.getType() == SOMA) {
Cell c = (Cell)gLastSelected;
gCurrPath = new Axon(c, gIndicator.x, gIndicator.y, c.fHighlightColor);
}
else if (gLastSelected.getType() == SYNAPSE) {
Synapse s = (Synapse)gLastSelected;
if (!s.isComplete()) {
color c = (s.fColor == EX_HIGHLIGHT_COLOR) ? EX_COLOR : IN_COLOR;
gCurrPath = new Dendrite(s, s.x(), s.y(), c);
gCurrPath.add(gIndicator.x, gIndicator.y);
}
}
}
}
}
else if (gCurrentMode == INTERACTION) {
if (gSelector.isSelecting()) {
gSelector.updateSelection(mouseX, mouseY);
}
else {
gObjs.onMouseDragged(mouseX, mouseY);
gCPanel.onMouseDragged(mouseX, mouseY);
}
}
else {
// Do nothing
}
redraw();
}
public void onMouseMoved() {
gGrid.onMouseMoved(mouseX, mouseY);
if (gCurrentMode == INTERACTION || gMagnify) {
}
gObjs.onMouseMoved(mouseX, mouseY);
gCPanel.onMouseMoved(mouseX, mouseY);
redraw();
}
public void onMouseReleased() {
gGrid.onMouseUp(mouseX, mouseY);
cursor(ARROW);
if (gCurrentMode == CREATION) {
if (gCurrInitiator != null && gCurrInitiator.fMovable) { // Hack for now
gObjs.add(gCurrInitiator);
gCurrInitiator.setMovable(false);
gCurrInitiator = null;
}
else if (gCurrShape != null) {
gObjs.add(gCurrShape);
gCurrShape.setMovable(false);
gCurrShape = null;
}
else if (gCurrPath != null) {
int l = gCurrPath.size();
if (l < 4) println ("ERROR! gCurrPath has a length less than 4");
else {
gCurrPath.setMovable(false);
if (gCurrPath.getType() == AXON) {
// Calculate offset so the edge of the Synapse is at the end of the path
PVector last = gCurrPath.getVertex(l-1);
PVector diff = PVector.sub(last, gCurrPath.getVertex(l-2));
diff.normalize();
PVector center = PVector.add(last, PVector.mult(diff, SYNAPSE_OUTER_SIZE));
Synapse s = new Synapse(gCurrPath, center.x, center.y, gCurrPath.fColor);
gCurrPath.setDest(s);
gCurrPath.attachToSource();
gCurrPath.simplify();
gObjs.add(gCurrPath);
gObjs.add(s);
}
else if (gCurrPath.getType() == DENDRITE) {
Interactive selected = null;
PVector gridPoint = gGrid.getCurrent();
if (gObjs.select(mouseX, mouseY) ||
gObjs.select(gridPoint.x, gridPoint.y)) {
selected = gObjs.getSelected().get(0);
if (selected.getType() == SOMA) {
Cell c = (Cell)selected;
// Add the indicator point to the path
snapIndicators(c.x(), c.y(), c.fSize);
gCurrPath.add(gIndicator.x, gIndicator.y);
gCurrPath.setDest(c);
c.addPath(gCurrPath);
}
else if ((selected.getType() == DENDRITE ||
selected.getType() == AXON) &&
selected != gCurrPath) {
//add end to end?
Path p = (Path)selected;
PVector end = p.getCurrVertex();
gIndicator2.set(end.x, end.y, 0);
gCurrPath.setDest(p);
}
if (selected.getType() != INITIATOR &&
selected.getType() != SYNAPSE) {
// gCurrPath.reduce();
gCurrPath.attachToSource();
gCurrPath.simplify();
gObjs.add(gCurrPath);
}
}
}
}
gCurrPath = null;
}
gLastSelected = null;
clearIndicators();
}
else if (gCurrentMode == INTERACTION) {
if (gSelector.isSelecting()) {
gSelector.endSelection(mouseX, mouseY);
gObjs.selectArea(gSelector.getStart(), gSelector.getEnd());
}
gObjs.onMouseUp(mouseX, mouseY);
gCPanel.onMouseUp(mouseX, mouseY);
}
redraw();
}
public void onKeyPressed() {
switch (key) {
case '1':
gCurrentMode = CREATION;
gCurrentModeLabel = "CREATION";
break;
case '2':
gCurrentMode = DELETION;
gCurrentModeLabel = "DELETION";
break;
case '3':
gCurrentMode = INTERACTION;
gCurrentModeLabel = "INTERACTION";
break;
case 'm':
gMagnify = !gMagnify;
break;
case 'p':
noLoop();
break;
case 'o':
loop();
break;
case 'c':
clear();
break;
case 's':
gSmoothPaths = !gSmoothPaths;
gObjs.onSmoothToggle(gSmoothPaths);
break;
}
redraw();
}
}