Skip to content

Commit d15a176

Browse files
committed
example updated
1 parent 200cdab commit d15a176

2 files changed

Lines changed: 53 additions & 49 deletions

File tree

examples/demos/CustomNodeInteraction/CustomNodeInteraction.pde

Lines changed: 6 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import nub.core.*;
2020
import nub.processing.*;
2121

2222
Scene scene;
23-
Node[] shapes;
23+
Torus[] shapes;
2424
PFont font36;
2525
int totalShapes;
2626

@@ -32,58 +32,15 @@ void settings() {
3232
}
3333

3434
void setup() {
35+
font36 = loadFont("FreeSans-36.vlw");
3536
scene = new Scene(this);
3637
scene.enableHint(Scene.AXES | Scene.BACKGROUND);
3738
scene.configHint(Scene.BACKGROUND, color(0));
3839
scene.fit(1);
39-
shapes = new Node[10];
40+
shapes = new Torus[10];
4041
for (int i = 0; i < shapes.length; i++) {
41-
shapes[i] = new Node() {
42-
int _id = totalShapes++, _faces = randomFaces(), _color = randomColor();
43-
44-
@Override
45-
public void graphics(PGraphics pg) {
46-
pg.pushStyle();
47-
pg.fill(_color);
48-
Scene.drawTorusSolenoid(pg, _faces, scene.radius() / 20);
49-
scene.beginHUD();
50-
Vector position = scene.screenLocation(position());
51-
pg.fill(isTagged(scene) ? 0 : 255, isTagged(scene) ? 255 : 0, isTagged(scene) ? 0 : 255);
52-
pg.textFont(font36);
53-
pg.text(_id, position.x(), position.y());
54-
scene.endHUD();
55-
pg.popStyle();
56-
}
57-
58-
@Override
59-
public void interact(Object[] gesture) {
60-
if (gesture.length == 0)
61-
_color = randomColor();
62-
if (gesture.length == 1)
63-
if (gesture[0] instanceof String) {
64-
if (((String) gesture[0]).matches("mas"))
65-
_faces++;
66-
else if (((String) gesture[0]).matches("menos"))
67-
if (_faces > 2)
68-
_faces--;
69-
} else if (gesture[0] instanceof Integer) {
70-
int delta = (Integer) gesture[0];
71-
if (_faces + delta > 1)
72-
_faces = _faces + delta;
73-
}
74-
}
75-
};
76-
scene.randomize(shapes[i]);
42+
shapes[i] = new Torus();
7743
}
78-
font36 = loadFont("FreeSans-36.vlw");
79-
}
80-
81-
int randomColor() {
82-
return color(random(255), random(255), random(255), random(125, 255));
83-
}
84-
85-
int randomFaces() {
86-
return (int) random(3, 15);
8744
}
8845

8946
void draw() {
@@ -93,7 +50,7 @@ void draw() {
9350
void keyPressed() {
9451
int value = Character.getNumericValue(key);
9552
if (value >= 0 && value < 10)
96-
scene.tag("key", shapes[value]);
53+
scene.tag("key", shapes[value].node);
9754
if (key == ' ')
9855
scene.removeTag("key");
9956
if (key == CODED)
@@ -125,4 +82,4 @@ void mouseClicked(MouseEvent event) {
12582
scene.interactTag("key");
12683
if (event.getCount() == 2)
12784
scene.mouseTag("key");
128-
}
85+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Torus {
2+
int id = totalShapes++, faces = randomFaces(), colour = randomColor();
3+
Node node;
4+
5+
Torus() {
6+
node = new Node();
7+
node.enableHint(Node.TORUS, colour, faces);
8+
node.setInteraction(this::interact);
9+
node.setHUD(this::hud);
10+
scene.randomize(node);
11+
}
12+
13+
void hud(PGraphics pg) {
14+
pg.fill(node.isTagged(scene) ? 0 : 255, node.isTagged(scene) ? 255 : 0, node.isTagged(scene) ? 0 : 255);
15+
pg.textFont(font36);
16+
pg.text(id, 0, 0);
17+
}
18+
19+
void interact(Object[] gesture) {
20+
if (gesture.length == 0){
21+
colour = randomColor();
22+
node.configHint(Node.TORUS, colour, faces);
23+
}
24+
if (gesture.length == 1) {
25+
if (gesture[0] instanceof String) {
26+
if (((String) gesture[0]).matches("mas"))
27+
faces++;
28+
else if (((String) gesture[0]).matches("menos"))
29+
if (faces > 2)
30+
faces--;
31+
} else if (gesture[0] instanceof Integer) {
32+
int delta = (Integer) gesture[0];
33+
if (faces + delta > 1)
34+
faces = faces + delta;
35+
}
36+
node.configHint(Node.TORUS, colour, faces);
37+
}
38+
}
39+
40+
int randomColor() {
41+
return color(random(255), random(255), random(255), random(125, 255));
42+
}
43+
44+
int randomFaces() {
45+
return (int) random(3, 15);
46+
}
47+
}

0 commit comments

Comments
 (0)