-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathCustomNodeInteraction.java
More file actions
127 lines (111 loc) · 3.04 KB
/
CustomNodeInteraction.java
File metadata and controls
127 lines (111 loc) · 3.04 KB
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
import nub.core.Node;
import nub.processing.Scene;
import processing.core.PApplet;
import processing.core.PFont;
import processing.core.PGraphics;
import processing.event.MouseEvent;
/**
* Node interaction customization
*/
public class CustomNodeInteraction extends PApplet {
Scene scene;
Torus[] shapes;
PFont font36;
int totalShapes;
//Choose P2D or P3D
String renderer = P3D;
public void settings() {
size(1200, 700, renderer);
}
public void setup() {
font36 = loadFont("FreeSans-36.vlw");
scene = new Scene(this);
scene.fit(1000);
shapes = new Torus[10];
for (int i = 0; i < shapes.length; i++) {
shapes[i] = new Torus();
}
}
public void draw() {
background(0);
scene.render();
}
public void keyPressed() {
int value = Character.getNumericValue(key);
if (value >= 0 && value < 10)
scene.tag(shapes[value].node);
if (key == ' ')
scene.removeTag();
if (key == CODED)
if (keyCode == UP)
scene.shift(0, -10, 0);
else if (keyCode == DOWN)
scene.shift(0, 10, 0);
else if (keyCode == LEFT)
scene.interact( "menos");
else if (keyCode == RIGHT)
scene.interact("mas");
}
public void mouseDragged() {
if (mouseButton == LEFT)
scene.spin();
else if (mouseButton == CENTER)
scene.zoom(scene.mouseDX());
else
scene.shift();
}
public void mouseWheel(MouseEvent event) {
scene.interact(event.getCount());
}
public void mouseClicked(MouseEvent event) {
if (event.getCount() == 1)
scene.interact();
if (event.getCount() == 2)
scene.tag();
}
public class Torus {
int id = totalShapes++, faces = randomFaces(), colour = randomColor();
Node node;
Torus() {
node = new Node();
node.enableHint(Node.TORUS, colour, faces);
node.setInteraction(this::interact);
node.setHUD(this::hud);
scene.randomize(node);
}
void hud(PGraphics pg) {
pg.fill(node.isTagged(scene) ? 0 : 255, node.isTagged(scene) ? 255 : 0, node.isTagged(scene) ? 0 : 255);
pg.textFont(font36);
pg.text(id, 0, 0);
}
void interact(Object[] gesture) {
if (gesture.length == 0){
colour = randomColor();
node.configHint(Node.TORUS, colour, faces);
}
if (gesture.length == 1) {
if (gesture[0] instanceof String) {
if (((String) gesture[0]).matches("mas"))
faces++;
else if (((String) gesture[0]).matches("menos"))
if (faces > 2)
faces--;
} else if (gesture[0] instanceof Integer) {
int delta = (Integer) gesture[0];
if (faces + delta > 1)
faces = faces + delta;
}
node.configHint(Node.TORUS, colour, faces);
}
}
int randomColor() {
return color(random(255), random(255), random(255), random(125, 255));
}
int randomFaces() {
return (int) random(3, 15);
}
}
public static void main(String[] args) {
PApplet.main(new String[]{"CustomNodeInteraction"});
}
}