-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.pde
263 lines (212 loc) · 6 KB
/
main.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
// Stephen Aldriedge, 2013
int windowWidth = 960;
int windowHeight = 630;
String debugmsg = "";
// FOR DEBUGGING PURPOSES: If you need to run this on your computer to check for errors and
// such, uncomment the DEBUG code below. Make sure to leave it commented when you are running
// the code on a mobile device.
/*
//------------------ DEBUG BEGIN -------------------------------------------------------------
class Touch {
Touch(int x, int y) {
offsetX = x;
offsetY = y;
}
Touch(float x, float y) {
offsetX = (int)x;
offsetY = (int)y;
}
int offsetX, offsetY;
}
class TouchEvent {
TouchEvent(Touch[] t) {
touches = t;
changedTouches = t;
}
Touch[] touches;
Touch[] changedTouches;
}
PVector[] pts;
TouchEvent te;
void touchSimulateSetup() {
pts = new PVector[3];
pts[0] = new PVector(-57, -12);
pts[1] = new PVector(-10, 42);
pts[2] = new PVector(68, -30);
}
void mousePressed() {
Touch[] simTouchPts = new Touch[3];
simTouchPts[0] = new Touch(pts[0].x+mouseX, pts[0].y+mouseY);
simTouchPts[1] = new Touch(pts[1].x+mouseX, pts[1].y+mouseY);
simTouchPts[2] = new Touch(pts[2].x+mouseX, pts[2].y+mouseY);
te = new TouchEvent(simTouchPts);
touchStart(te);
}
void mouseDragged() {
//debugmsg = str(mouseX);
te.touches[0].offsetX = (int)pts[0].x+mouseX;
te.touches[0].offsetY = (int)pts[0].y+mouseY;
te.touches[1].offsetX = (int)pts[1].x+mouseX;
te.touches[1].offsetY = (int)pts[1].y+mouseY;
te.touches[2].offsetX = (int)pts[2].x+mouseX;
te.touches[2].offsetY = (int)pts[2].y+mouseY;
te.changedTouches = te.touches;
touchMove(te);
}
void mouseReleased() {
te.changedTouches = te.touches;
te.touches = new Touch[0];
touchEnd(te);
}
//----------------- DEBUG END ----------------------------------------------------------------
*/
PFont f;
ArrayList tShapes;
ArrayList unassigned; // ArrayList to hold unassigned touches
//--------------- Derive your own class from TouchShape class ------------------
class MyObject extends TouchShape {
MyObject(Shape s, int tol) {
super(s,tol);
}
// create your own draw method here
void draw() {
resetMatrix();
translate(pos.x, pos.y);
rotate(rotAngle);
// draw shape here
fill(0,255,255);
strokeWeight(10);
stroke(0,255,255);
line(0, 30, 0, -100);
ellipse(0, 30, 70, 70);
fill(255,0,0);
ellipse(-20, 30, 20, 20);
ellipse(20, 30, 20, 20);
}
}
/*
class MyObject2 extends TouchShape {
MyObject2(Shape s, int tol) {
super(s,tol);
}
// create your own draw method here
void draw() {
resetMatrix();
translate(pos.x, pos.y);
rotate(rotAngle);
// draw shape here
fill(255,0,0);
strokeWeight(10);
stroke(255,0,0);
line(0, 30, 0, -100);
ellipse(0, 30, 70, 70);
fill(0,255,255);
ellipse(-20, 30, 20, 20);
ellipse(20, 30, 20, 20);
}
}
*/
//----------------- SETUP ----------------------------------
void setup() {
//---------- DEBUG ---------------------
//touchSimulateSetup();
//---------- DEBUG END -----------------
size( windowWidth , windowHeight );
background( 0 );
f = createFont("Arial",16,true);
smooth();
tShapes = new ArrayList();
unassigned = new ArrayList();
//-------------- Create shapes to use -----------------------
PVector[] shVerts01 = new PVector[3];
shVerts01[0] = new PVector(-57, -12);
shVerts01[1] = new PVector(-10, 42);
shVerts01[2] = new PVector(68, -30);
PVector[] shVerts02 = new PVector[1];
shVerts02[0] = new PVector(0,0);
//--------------- Register shapes --------------------------
registerShape(new MyObject(new Shape(shVerts01), 8));
//registerShape(new MyObject2(new Shape(shVerts02), 8));
}
//------------------- DRAW ----------------------------------------
void draw() {
background( 0 );
pushMatrix();
int shSize = tShapes.size();
for(int i=0; i<shSize; i++) {
if(((TouchShape)tShapes.get(i)).active) {
((TouchShape)tShapes.get(i)).draw();
}
}
popMatrix();
textFont(f,36);
fill(255);
text("dbg: " + debugmsg, 10, windowHeight-30);
debugmsg = "";
}
// register shapes to use
void registerShape(TouchShape ts) {
tShapes.add(ts);
}
void showPatternCoordinates(TouchEvent touchEvent) {
int touchCount = touchEvent.touches.length;
//calculate central pt
int xTotal=0, yTotal=0;
for(int i=0; i<touchCount; i++) {
Touch curTouch = touchEvent.touches[i];
xTotal += curTouch.offsetX;
yTotal += curTouch.offsetY;
}
PVector centralPt = new PVector(xTotal/touchCount, yTotal/touchCount);
for(int i=0; i<touchCount; i++) {
Touch curTouch = touchEvent.touches[i];
debugmsg += "(" + str(curTouch.offsetX - (int)centralPt.x) + "," +
str(curTouch.offsetY - (int)centralPt.y) + ") ";
}
}
void touchStart(TouchEvent touchEvent) {
// Uncomment this if you want to see the coordinates of your pattern
//showPatternCoordinates(touchEvent);
for(int i=0; i<touchEvent.changedTouches.length; i++) {
unassigned.add( touchEvent.changedTouches[i] );
}
int shSize = tShapes.size();
for(int i=0; i<shSize; i++) {
TouchShape curShape = (TouchShape)tShapes.get(i);
// skip shapes that are already active
if(curShape.active) continue;
// notify it of a touchMove event
curShape.touchStart(unassigned);
// if shape became active
if(curShape.active) {
// remove its touches from the unassigned list
for(int j=0; j<curShape.count; j++) {
unassigned.remove(curShape.touches[j]);
}
}
}
}
// respond to multitouch events
void touchMove(TouchEvent touchEvent) {
int shSize = tShapes.size();
for(int i=0; i<shSize; i++) {
// notify it of a touchMove event
((TouchShape)tShapes.get(i)).touchMove(touchEvent);
}
}
void touchEnd(TouchEvent touchEvent) {
int removeCount = touchEvent.changedTouches.length;
// see if the lifted touches were in the unassigned list
for(int i=0; i<touchEvent.changedTouches.length; i++) {
if(unassigned.contains(touchEvent.changedTouches[i])) {
unassigned.remove(touchEvent.changedTouches[i]);
removeCount--;
}
}
if( removeCount == 0 ) return;
int shSize = tShapes.size();
for(int i=0; i<shSize; i++) {
// notify it of a touchEnd event
((TouchShape)tShapes.get(i)).touchEnd(touchEvent);
}
}