-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathVisitExample.java
More file actions
155 lines (139 loc) · 3.92 KB
/
VisitExample.java
File metadata and controls
155 lines (139 loc) · 3.92 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
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
import nub.core.Graph;
import nub.core.Node;
import nub.primitives.Vector;
import nub.processing.Scene;
import processing.core.PApplet;
import processing.core.PGraphics;
import processing.event.MouseEvent;
/**
* This example modifies the visual representation on a Node
* depending on the scene it is drawn. It demos custom scene behavior
*
* By Sebastian Chaparro
*/
public class VisitExample extends PApplet {
boolean relativeToGraph = true;
int rows = 2, cols = 2;
int n = rows * cols;
Scene[] scenes = new Scene[n];
Scene focus;
int w = 1400, h = 1400;
int[] strokeColors = new int[n];
int[] fillColors = new int[n];
public void settings() {
size(w, h, P3D);
}
@Override
public void setup() {
//create the scenes
for(int i = 0; i < n; i++){
final int idx = i;
scenes[i] = new Scene(createGraphics(w / cols, h / rows, P3D));
scenes[i].setRadius(50);
strokeColors[i] = color(random(255), random(255), random(255));
fillColors[i] = color(random(255), random(255), random(255));
scenes[i].fit();
if(Math.random() < 0.5) scenes[i].togglePerspective();
}
generateRandomNodes(10);
}
@Override
public void draw() {
for(int i = 0; i < n; i++){
focus = scenes[i].hasFocus() ? scenes[i] : focus;
scenes[i].openContext();
scenes[i].context().background(125);
scenes[i].render();
scenes[i].drawAxes();
scenes[i].beginHUD();
sceneHUD(i, scenes[i].context());
scenes[i].endHUD();
scenes[i].closeContext();
scenes[i].image(i / rows * h / rows, (i % cols) * w / cols);
}
}
public void sceneHUD(int i, PGraphics pg) {
pg.pushStyle();
pg.stroke(255);
pg.fill(255);
pg.text("On scene " + i + " radius : " + (int) (scenes[i].radius()), 10, pg.height / 10);
pg.popStyle();
}
public void generateRandomNodes(int n) {
for(int i = 0; i < n; i++){
Node node = new CustomNode();
node.randomize(new Vector(), 150, true);
node.setMagnitude(1);
}
}
class CustomNode extends Node {
//Add some attributes
float radiusInPixels = 50, radius;
int strokeCol, fillCol;
public CustomNode() {
super();
//without modifying Node class
//for(Scene scene : scenes) setVisit(scene, (g , n) -> setupAttributesByGraph(g));
//modifying the node class
for (Scene scene : scenes) setBehavior(scene, this::setupAttributesByGraph);
setShape(pg -> {
pg.pushStyle();
pg.stroke(strokeCol);
pg.fill(fillCol);
pg.box(radius);
pg.popStyle();
});
}
public void setupAttributesByGraph(Graph g) {
if (relativeToGraph) {
//keep the size of the node relative to the graph such that it always occupies 10 pixels
this.radius = this.radiusInPixels * g.sceneToPixelRatio(worldPosition());
//change color based on Graph
for (int i = 0; i < n; i++) {
if (g == scenes[i]) {
this.strokeCol = strokeColors[i];
this.fillCol = fillColors[i];
}
}
} else{
this.radius = 10;
this.strokeCol = color(255);
this.fillCol = color(255,255, 0);
}
}
}
@Override
public void mouseMoved(){
focus.tag();
}
@Override
public void mouseDragged() {
if(mouseButton == LEFT)
focus.spin();
else if(mouseButton == RIGHT)
focus.shift();
else
focus.zoom(focus.mouseDX());
}
@Override
public void mouseWheel(MouseEvent event) {
focus.moveForward(event.getCount() * 40);
}
@Override
public void mouseClicked(MouseEvent event) {
if (event.getCount() == 2)
if (event.getButton() == LEFT)
focus.focus();
else
focus.align();
}
@Override
public void keyPressed() {
if (key == ' ') {
relativeToGraph = !relativeToGraph;
}
}
public static void main(String[] args) {
PApplet.main(new String[]{"VisitExample"});
}
}