· 8 years ago · Feb 24, 2018, 07:08 AM
1
2// rubber banding example
3import android.os.Vibrator;
4import android.content.Context;
5import android.media.MediaPlayer;
6import android.content.res.AssetFileDescriptor;
7import android.app.Activity;
8import android.content.Intent;
9import android.os.Environment;
10import android.graphics.Bitmap;
11import java.io.FileOutputStream;
12import android.view.View;
13import android.widget.Toast;
14import android.provider.MediaStore;
15import android.widget.ImageView;
16import android.os.Bundle;
17import android.net.Uri;
18import java.io.File;
19import android.support.v4.content.FileProvider;
20
21static final int PICK_FILE_REQUEST = 100;
22static final int RESULT_OK = -1;
23static final int RESULT_CANCELED = 0;
24static final int REQUEST_IMAGE_CAPTURE = 1;
25
26float sX, sY, tmpX, tmpY;
27ArrayList <Rect> rects;
28boolean positioning;
29int rectIndex;
30Table table;
31PImage webImg;
32
33// media player
34MediaPlayer mp;
35Context context;
36Activity act;
37AssetFileDescriptor afd;
38
39//======================================
40
41void setup() {
42
43 // init canvas
44 fullScreen();
45 colorMode(HSB, 360,100,100);
46 background(255);
47 stroke(1);
48 strokeWeight(2);
49 positioning = false;
50
51 // rect array list
52 rects = new ArrayList<Rect>();
53
54 // init media player
55 act = this.getActivity();
56 context = act.getApplicationContext();
57
58 try {
59 mp = new MediaPlayer();
60 afd = context.getAssets().openFd("test.mp3");
61 mp.setDataSource(afd.getFileDescriptor());
62 mp.prepare();
63 }
64
65 catch(IOException e) {
66 println("mp3 file did not load");
67 }
68
69 // load rects csv
70 loadRectCSV();
71
72 // String url = "https://processing.org/img/processing-web.png";
73 // Load image from a web server
74 // webImg = loadImage(url);
75
76 // play welcome sound
77 mp.start();
78}
79
80//======================================
81
82void loadRectCSV() {
83
84 // check if file exists
85 String csvFileName = new String(Environment.getExternalStorageDirectory().getAbsolutePath() + "/rect.csv");
86
87 File csvFile = new File( csvFileName );
88 if( ! csvFile.exists() ) {
89 // create empty table
90 table = new Table();
91 table.addColumn( "x" );
92 table.addColumn( "y" );
93 table.addColumn( "width" );
94 table.addColumn( "height" );
95 } else {
96 // load rect csv file
97 try {
98 table = loadTable( csvFileName,"header");
99
100 // create rect array
101 for (int i = 0; i<table.getRowCount(); i++) {
102 TableRow row = table.getRow(i);
103 float x = row.getFloat("x");
104 float y = row.getFloat("y");
105 float w = row.getFloat("width");
106 float h = row.getFloat("height");
107 rects.add(new Rect(x,y,w,h));
108 }
109 }
110
111 catch(Exception e)
112 {
113 e.printStackTrace();
114 }
115 }
116}
117
118//======================================
119
120void mousePressed() {
121
122 // rubber band rect
123 sX = mouseX;
124 sY = mouseY;
125 tmpX = mouseX;
126 tmpY = mouseY;
127
128 // are we moving an existing rect?
129 for (int i = rects.size() - 1; i >= 0; i--) {
130 Rect r = rects.get(i);
131 if (mouseX >= r.x && mouseX <= (r.x+r.w) && mouseY >= r.y && mouseY <= (r.y+r.h) ) {
132 positioning = true;
133 rectIndex = i;
134 vibrate(50);
135
136 // toast text
137 act.runOnUiThread(new Runnable() {
138 public void run() {
139 Toast.makeText(act, "Moving...", Toast.LENGTH_SHORT).show();
140 }
141 });
142 break;
143 }
144 }
145}
146
147//======================================
148
149void mouseDragged() {
150
151 // rubber banding
152 if( ! positioning ) {
153 if(mouseX < sX ) {
154 tmpX = mouseX;
155 }
156
157 if(mouseY < sY ) {
158 tmpY = mouseY;
159 }
160 } else {
161 // moving existing rect
162 Rect r = rects.get(rectIndex);
163 r.x -= tmpX-mouseX;
164 r.y -= tmpY-mouseY;
165 tmpX = mouseX;
166 tmpY = mouseY;
167
168 // update table row
169 TableRow row = table.getRow( rectIndex );
170 row.setFloat( "x", r.x );
171 row.setFloat( "y", r.y );
172 }
173}
174
175//======================================
176
177void mouseReleased() {
178
179 // add new rect to list
180 if( ! positioning ) {
181 // prevent slivers
182 float w = abs(sX-mouseX);
183 float h = abs(sY-mouseY);
184 if( w > 10 && h > 10 ) {
185 rects.add(new Rect(tmpX,tmpY,w,h));
186 TableRow row = table.addRow();
187 row.setFloat("x",tmpX);
188 row.setFloat("y",tmpY);
189 row.setFloat("width",w);
190 row.setFloat("height",h);
191 }
192 } else {
193 // delete rect from list
194 if( mouseX > width-40 || mouseX < 40 ) {
195 for (int i = rects.size() - 1; i >= 0; i--) {
196 Rect r = rects.get(i);
197 if (mouseX >= r.x && mouseX <= (r.x+r.w) && mouseY >= r.y && mouseY <= (r.y+r.h) ) {
198 rects.remove(i);
199 table.removeRow(i);
200 break;
201 }
202 }
203 }
204 }
205
206 // vibrate phone
207 //vibrate(50);
208 positioning = false;
209}
210
211//======================================
212
213void vibrate(int millis) {
214 Vibrator v;
215 v = (Vibrator)act.getSystemService( Context.VIBRATOR_SERVICE);
216 v.vibrate(millis);
217}
218
219//======================================
220
221void draw() {
222
223 // clear
224 background(255);
225 //image( webImg,0,0);
226
227 // draw rect list
228 for (int i = 0; i < rects.size(); i++ ) {
229 Rect r = rects.get(i);
230 r.display();
231 }
232
233 // draw rubber band rect
234 if( mousePressed && ! positioning ) {
235 // draw temp rubber rect
236 noFill();
237 rect(tmpX,tmpY,abs(sX-mouseX),abs(sY-mouseY));
238 }
239}
240
241//======================================
242
243void onStart() {
244 super.onStart();
245 try {
246 // play sound
247 //mp.seekTo(0);
248 //mp.start();
249 }
250
251 catch(Exception e)
252 {
253 println("bad pointer" + e.getMessage());
254 }
255}
256
257//======================================
258
259void onDestroy() {
260 super.onDestroy();
261 mp.release();
262}
263
264//======================================
265
266void onStop() {
267 println("table count " + table.getRowCount());
268 super.onStop();
269
270 String csvFileName = new String(Environment.getExternalStorageDirectory().getAbsolutePath() + "/rect.csv");
271 saveTable( table, csvFileName );
272 println( csvFileName );
273
274}
275
276//======================================