-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCanvas.pde
228 lines (194 loc) · 5.27 KB
/
Canvas.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
import oscP5.*;
class Canvas {
PVector location;
PVector size;
ArrayList <Point> points;
ArrayList<OscBundle> traj = new ArrayList<OscBundle>();
PGraphics cnv;
PImage prev;
Tablet tablet;
int x, y, xSize, ySize;
float pf = 5; // pressure factor
boolean usingTablet;
int m; // marker count
PFont font;
int pm; // point marker
Scene scene;
boolean displayScene = true;
Canvas(PGraphics cnv, int x, int y, int xSize, int ySize, Tablet tablet) {
this.font = loadFont("CourierNewPSMT-48.vlw");
cnv.textFont(font, 12);
this.points = new ArrayList<Point>();
this.cnv = cnv;
this.cnv.smooth(0);
// tablet support
this.usingTablet = false;
this.tablet = tablet;
this.x = x;
this.y = y;
this.xSize = xSize;
this.ySize = ySize;
this.m = 1;
this.scene = new Scene();
}
void setUsingTablet(boolean t) {this.usingTablet = t;}
void drawScene() {
try {
image(scene.sceneRevB, x, y);
} catch (Exception _e) {
println("The scene cannot be loaded");
//e.printStackTrace();
}
}
void drawCanvas() {
cnv.beginDraw();
cnv.colorMode(HSB, 360, 100, 100, 100);
if(prev != null) { cnv.image(prev, 0, 0);}
// frame
drawRectFrame(cnv);
cnv.endDraw();
image(cnv, x, y);
}
boolean isInsideCanvas() {
if(mouseX > x && mouseX < x + xSize && mouseY > y && mouseY < y + ySize) {
return true;
}
return false;
}
void drawPoints(boolean first) {
cnv.beginDraw();
cnv.colorMode(HSB, 360, 100, 100, 100);
if(prev != null) { cnv.image(prev, 0, 0);}
if(isInsideCanvas()){
Point p = new Point(mouseX-x, mouseY-y, pmouseX-x, pmouseY-y, 250, 500);
if(usingTablet) {
p.press = tablet.getPressure();
}
drawLine(cnv, p, first);
// marks
// TODO modulo according speed
drawTextMarker(cnv, p);
// points
drawLineMarker(cnv, p);
points.add(p);
}
drawRectFrame(cnv);
cnv.endDraw();
prev = cnv.copy();
image(prev, x, y);
}
void drawLine(PGraphics cnv, Point p, boolean first) {
// if too long check interpolation for long lines
if(!first) {
float vel = map(p.vel, 0, 50, 20, 340);
float press = map(p.press, 0, 1, 0.5, pf*2);
cnv.strokeWeight(this.usingTablet ? press : 2 );
cnv.stroke(vel, 80, 80, 50);
cnv.line(p.x, p.y, p.px, p.py);
}
}
void closePath() {
if(!isInsideCanvas()){return;};
points.get(points.size() - 1).setCmd("END");
}
void capturePoints() {
}
void renderPoints() {
// CONVERT POINTS TO SHAPE
}
boolean isStartPoint(int idx) {
if (idx == 0) {return true;}
return points.get(idx-1).cmd.equals("END");
}
void saveTrajectory(String name) {
name = trim(name);
if(name.length() == 0) { name = "base";}
String[] _points = new String[points.size()];
for (int i = 0; i < points.size(); i++) {
if (isStartPoint(i)) {
points.get(i).cmd = "START";
}
_points[i] = points.get(i).toString();
}
saveStrings("traj/"+name+".tr", _points);
}
boolean isFileUsingTablet(String[] lines) {
for (int i = 0 ; i < lines.length; i++) {
String [] l = split(lines[i], " ");
if(float(l[3]) > 0) {
println("TABLET");
return true;
}
}
return false;
}
void loadTrajectory(String name) {
points.removeAll(points);
// list files an find the t traj
String[] lines = loadStrings("traj/"+name+".tr");
this.usingTablet = isFileUsingTablet(lines);
// previous points
int px = 0;
int py = 0;
println("there are " + lines.length + " lines");
cnv.beginDraw();
for (int i = 0 ; i < lines.length; i++) {
String [] l = split(lines[i], " ");
int x = int(l[0]);
int y = int(l[1]);
String cmd = l[4];
Point p = new Point(x, y, px, py, 250, 500);
px = x;
py = y;
p.cmd = cmd;
p.press = float(l[3]);
boolean first = cmd.equals("START");
points.add(p);
drawLine(cnv, p, first);
drawTextMarker(cnv, p);
}
cnv.endDraw();
prev = cnv.copy();
image(prev, x, y);
}
void removePoints() {
points.removeAll(points);
cnv =createGraphics(500, 500, P2D);
m = 1;
// cnv.background(0, 0, 100);
}
void clearTraj() {
this.removePoints();
this.drawCanvas();
this.prev = null;
}
void loadTrajBundles() {
traj.removeAll(traj);
println("Reloading points values for scene: " + scene.name);
for(int i = 0; i < points.size(); i++) {
Point p = points.get(i);
p.setValues(scene.getValuesAt(p.x, p.y));
traj.add(p.toBundle());
}
}
void drawRectFrame(PGraphics c) {
c.noFill();
c.strokeWeight(1);
c.stroke(0, 0, 100);
c.rect(0, 0, cnv.width-1, cnv.height-1);
}
void drawTextMarker(PGraphics c, Point p) {
if (points.size() % 30 == 0) {
c.fill(0, 10, 100);
c.textFont(font, 15);
c.text(m, p.x, p.y);
m++;
}
}
void drawLineMarker(PGraphics c, Point p) {
float press = this.usingTablet ? map(p.press, 0, 1, 1, pf) : 1;
c.strokeWeight(press * 2);
c.stroke(p.vel, 80, 80, 20);
c.point(mouseX-x, mouseY-y);
}
}