-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRubberAndroid.pde
More file actions
executable file
·277 lines (225 loc) · 5.84 KB
/
Copy pathRubberAndroid.pde
File metadata and controls
executable file
·277 lines (225 loc) · 5.84 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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// rubber banding example
import android.os.Vibrator;
import android.content.Context;
import android.media.MediaPlayer;
import android.content.res.AssetFileDescriptor;
import android.app.Activity;
import android.content.Intent;
import android.os.Environment;
import android.graphics.Bitmap;
import java.io.FileOutputStream;
import android.view.View;
import android.widget.Toast;
import android.provider.MediaStore;
import android.widget.ImageView;
import android.os.Bundle;
import android.net.Uri;
import java.io.File;
import android.support.v4.content.FileProvider;
static final int PICK_FILE_REQUEST = 100;
static final int RESULT_OK = -1;
static final int RESULT_CANCELED = 0;
static final int REQUEST_IMAGE_CAPTURE = 1;
float sX, sY, tmpX, tmpY;
ArrayList <Rect> rects;
boolean positioning;
int rectIndex;
Table table;
PImage webImg;
// media player
MediaPlayer mp;
Context context;
Activity act;
AssetFileDescriptor afd;
//======================================
void setup() {
// init canvas
fullScreen();
colorMode(HSB, 360,100,100);
background(255);
stroke(1);
strokeWeight(2);
positioning = false;
// rect array list
rects = new ArrayList<Rect>();
// init media player
act = this.getActivity();
context = act.getApplicationContext();
try {
mp = new MediaPlayer();
afd = context.getAssets().openFd("test.mp3");
mp.setDataSource(afd.getFileDescriptor());
mp.prepare();
}
catch(IOException e) {
println("mp3 file did not load");
}
// load rects csv
loadRectCSV();
// String url = "https://processing.org/img/processing-web.png";
// Load image from a web server
// webImg = loadImage(url);
// play welcome sound
mp.start();
}
//======================================
void loadRectCSV() {
// check if file exists
String csvFileName = new String(Environment.getExternalStorageDirectory().getAbsolutePath() + "/rect.csv");
File csvFile = new File( csvFileName );
if( ! csvFile.exists() ) {
// create empty table
table = new Table();
table.addColumn( "x" );
table.addColumn( "y" );
table.addColumn( "width" );
table.addColumn( "height" );
} else {
// load rect csv file
try {
table = loadTable( csvFileName,"header");
// create rect array
for (int i = 0; i<table.getRowCount(); i++) {
TableRow row = table.getRow(i);
float x = row.getFloat("x");
float y = row.getFloat("y");
float w = row.getFloat("width");
float h = row.getFloat("height");
rects.add(new Rect(x,y,w,h));
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
//======================================
void mousePressed() {
// rubber band rect
sX = mouseX;
sY = mouseY;
tmpX = mouseX;
tmpY = mouseY;
// are we moving an existing rect?
for (int i = rects.size() - 1; i >= 0; i--) {
Rect r = rects.get(i);
if (mouseX >= r.x && mouseX <= (r.x+r.w) && mouseY >= r.y && mouseY <= (r.y+r.h) ) {
positioning = true;
rectIndex = i;
vibrate(50);
// toast text
act.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(act, "Moving...", Toast.LENGTH_SHORT).show();
}
});
break;
}
}
}
//======================================
void mouseDragged() {
// rubber banding
if( ! positioning ) {
if(mouseX < sX ) {
tmpX = mouseX;
}
if(mouseY < sY ) {
tmpY = mouseY;
}
} else {
// moving existing rect
Rect r = rects.get(rectIndex);
r.x -= tmpX-mouseX;
r.y -= tmpY-mouseY;
tmpX = mouseX;
tmpY = mouseY;
// update table row
TableRow row = table.getRow( rectIndex );
row.setFloat( "x", r.x );
row.setFloat( "y", r.y );
}
}
//======================================
void mouseReleased() {
// add new rect to list
if( ! positioning ) {
// prevent slivers
float w = abs(sX-mouseX);
float h = abs(sY-mouseY);
if( w > 10 && h > 10 ) {
rects.add(new Rect(tmpX,tmpY,w,h));
TableRow row = table.addRow();
row.setFloat("x",tmpX);
row.setFloat("y",tmpY);
row.setFloat("width",w);
row.setFloat("height",h);
}
} else {
// delete rect from list
if( mouseX > width-40 || mouseX < 40 ) {
for (int i = rects.size() - 1; i >= 0; i--) {
Rect r = rects.get(i);
if (mouseX >= r.x && mouseX <= (r.x+r.w) && mouseY >= r.y && mouseY <= (r.y+r.h) ) {
rects.remove(i);
table.removeRow(i);
break;
}
}
}
}
// vibrate phone
//vibrate(50);
positioning = false;
}
//======================================
void vibrate(int millis) {
Vibrator v;
v = (Vibrator)act.getSystemService( Context.VIBRATOR_SERVICE);
v.vibrate(millis);
}
//======================================
void draw() {
// clear
background(255);
//image( webImg,0,0);
// draw rect list
for (int i = 0; i < rects.size(); i++ ) {
Rect r = rects.get(i);
r.display();
}
// draw rubber band rect
if( mousePressed && ! positioning ) {
// draw temp rubber rect
noFill();
rect(tmpX,tmpY,abs(sX-mouseX),abs(sY-mouseY));
}
}
//======================================
void onStart() {
super.onStart();
try {
// play sound
//mp.seekTo(0);
//mp.start();
}
catch(Exception e)
{
println("bad pointer" + e.getMessage());
}
}
//======================================
void onDestroy() {
super.onDestroy();
mp.release();
}
//======================================
void onStop() {
println("table count " + table.getRowCount());
super.onStop();
String csvFileName = new String(Environment.getExternalStorageDirectory().getAbsolutePath() + "/rect.csv");
saveTable( table, csvFileName );
println( csvFileName );
}
//======================================