1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2016 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Distorted. //
|
5
|
// //
|
6
|
// Distorted is free software: you can redistribute it and/or modify //
|
7
|
// it under the terms of the GNU General Public License as published by //
|
8
|
// the Free Software Foundation, either version 2 of the License, or //
|
9
|
// (at your option) any later version. //
|
10
|
// //
|
11
|
// Distorted is distributed in the hope that it will be useful, //
|
12
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
14
|
// GNU General Public License for more details. //
|
15
|
// //
|
16
|
// You should have received a copy of the GNU General Public License //
|
17
|
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
18
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19
|
|
20
|
package org.distorted.examples.dynamic;
|
21
|
|
22
|
import android.app.ActivityManager;
|
23
|
import android.content.Context;
|
24
|
import android.content.pm.ConfigurationInfo;
|
25
|
import android.graphics.Rect;
|
26
|
import android.opengl.GLSurfaceView;
|
27
|
import android.view.MotionEvent;
|
28
|
import android.util.AttributeSet;
|
29
|
import android.graphics.Canvas;
|
30
|
import android.graphics.Paint.Style;
|
31
|
import android.graphics.Paint;
|
32
|
|
33
|
import org.distorted.library.type.Dynamic;
|
34
|
import org.distorted.library.type.Dynamic1D;
|
35
|
import org.distorted.library.type.Dynamic2D;
|
36
|
import org.distorted.library.type.Dynamic3D;
|
37
|
import org.distorted.library.type.Static1D;
|
38
|
import org.distorted.library.type.Static2D;
|
39
|
import org.distorted.library.type.Static3D;
|
40
|
|
41
|
///////////////////////////////////////////////////////////////////
|
42
|
|
43
|
public class DynamicSurfaceView extends GLSurfaceView
|
44
|
{
|
45
|
public static final int DIM_1D = 0;
|
46
|
public static final int DIM_2D = 1;
|
47
|
public static final int DIM_3DXY = 2;
|
48
|
public static final int DIM_3DXZ = 3;
|
49
|
|
50
|
private static final int NUM_POINTS = 250;
|
51
|
private static final Object lock = new Object();
|
52
|
|
53
|
private static int halfScreenHeight=0;
|
54
|
private static int halfScreenWidth =0;
|
55
|
|
56
|
private Dynamic1D di1D;
|
57
|
private Dynamic2D di2D;
|
58
|
private Dynamic3D di3D;
|
59
|
|
60
|
private Paint mPaint;
|
61
|
private int mMoving;
|
62
|
private int mDuration;
|
63
|
private int mPosition;
|
64
|
private long mDiffTime, mLastTime, mStartTime;
|
65
|
private float mNoise0, mNoise1, mNoise2;
|
66
|
private float mCount;
|
67
|
|
68
|
private int mSize1, mSize2, mSizeT, mAvg;
|
69
|
private float mFontHeight;
|
70
|
|
71
|
private int currentDim= DIM_2D;
|
72
|
|
73
|
private Static1D p1D;
|
74
|
private Static2D p2D;
|
75
|
private Static3D p3D;
|
76
|
|
77
|
private Static1D p1N;
|
78
|
private Static2D p2N;
|
79
|
private Static3D p3N;
|
80
|
|
81
|
private float[] mPoints = new float[3*NUM_POINTS];
|
82
|
private boolean mRunning;
|
83
|
|
84
|
///////////////////////////////////////////////////////////////////
|
85
|
|
86
|
public DynamicSurfaceView(Context context, AttributeSet attrs)
|
87
|
{
|
88
|
super(context, attrs);
|
89
|
|
90
|
mPaint = new Paint();
|
91
|
mPaint.setStyle(Style.FILL);
|
92
|
mPaint.setAntiAlias(true);
|
93
|
|
94
|
mMoving = -1;
|
95
|
mDuration = 10000;
|
96
|
mCount = 0.0f;
|
97
|
mPosition = 0;
|
98
|
mNoise0 = 0.0f;
|
99
|
mNoise1 = 0.0f;
|
100
|
mNoise2 = 0.0f;
|
101
|
mDiffTime = -1;
|
102
|
mLastTime = -1;
|
103
|
mStartTime= -1;
|
104
|
|
105
|
mRunning = false;
|
106
|
|
107
|
di1D = new Dynamic1D(mDuration,mCount);
|
108
|
p1N = new Static1D(mNoise0);
|
109
|
di2D = new Dynamic2D(mDuration,mCount);
|
110
|
p2N = new Static2D(mNoise0,mNoise1);
|
111
|
di3D = new Dynamic3D(mDuration,mCount);
|
112
|
p3N = new Static3D(mNoise0,mNoise1,mNoise2);
|
113
|
|
114
|
di1D.setAccessType(Dynamic.ACCESS_TYPE_SEQUENTIAL);
|
115
|
di2D.setAccessType(Dynamic.ACCESS_TYPE_SEQUENTIAL);
|
116
|
di3D.setAccessType(Dynamic.ACCESS_TYPE_SEQUENTIAL);
|
117
|
|
118
|
if(!isInEditMode())
|
119
|
{
|
120
|
setFocusable(true);
|
121
|
setFocusableInTouchMode(true);
|
122
|
DynamicRenderer mRenderer = new DynamicRenderer(this);
|
123
|
final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
|
124
|
final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
|
125
|
setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
|
126
|
setRenderer(mRenderer);
|
127
|
}
|
128
|
}
|
129
|
|
130
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
131
|
|
132
|
public void onSurfaceChanged(int width,int height)
|
133
|
{
|
134
|
mAvg = (width+height)/2;
|
135
|
|
136
|
mSize1 = mAvg/150;
|
137
|
mSize2 = mAvg/50;
|
138
|
mSizeT = mAvg/30;
|
139
|
|
140
|
mPaint.setTextSize(mSizeT);
|
141
|
mPaint.setTextAlign(Paint.Align.CENTER);
|
142
|
|
143
|
final Rect textBounds = new Rect();
|
144
|
String text = "1";
|
145
|
mPaint.getTextBounds(text, 0, text.length(), textBounds);
|
146
|
mFontHeight = textBounds.exactCenterY();
|
147
|
|
148
|
clearPoints();
|
149
|
}
|
150
|
|
151
|
///////////////////////////////////////////////////////////////////
|
152
|
|
153
|
public static void setHalfWidth(int hw)
|
154
|
{
|
155
|
halfScreenWidth = hw;
|
156
|
}
|
157
|
|
158
|
///////////////////////////////////////////////////////////////////
|
159
|
|
160
|
public static void setHalfHeight(int hh)
|
161
|
{
|
162
|
halfScreenHeight = hh;
|
163
|
}
|
164
|
|
165
|
///////////////////////////////////////////////////////////////////
|
166
|
|
167
|
public void setMode(int mode)
|
168
|
{
|
169
|
di1D.setMode(mode);
|
170
|
di2D.setMode(mode);
|
171
|
di3D.setMode(mode);
|
172
|
}
|
173
|
|
174
|
///////////////////////////////////////////////////////////////////
|
175
|
|
176
|
public void setDuration(int duration)
|
177
|
{
|
178
|
mDuration = duration;
|
179
|
|
180
|
di1D.setDuration(duration);
|
181
|
di2D.setDuration(duration);
|
182
|
di3D.setDuration(duration);
|
183
|
}
|
184
|
|
185
|
///////////////////////////////////////////////////////////////////
|
186
|
|
187
|
public void setCount(float count)
|
188
|
{
|
189
|
mCount = count;
|
190
|
|
191
|
di1D.setCount(count);
|
192
|
di2D.setCount(count);
|
193
|
di3D.setCount(count);
|
194
|
}
|
195
|
|
196
|
///////////////////////////////////////////////////////////////////
|
197
|
|
198
|
public void setNoise(float noise0, float noise1, float noise2)
|
199
|
{
|
200
|
mNoise0 = noise0;
|
201
|
mNoise1 = noise1;
|
202
|
mNoise2 = noise2;
|
203
|
|
204
|
p1N.set(mNoise0);
|
205
|
p2N.set(mNoise0,mNoise1);
|
206
|
p3N.set(mNoise0,mNoise1,mNoise2);
|
207
|
|
208
|
di1D.setNoise(p1N);
|
209
|
di2D.setNoise(p2N);
|
210
|
di3D.setNoise(p3N);
|
211
|
}
|
212
|
|
213
|
///////////////////////////////////////////////////////////////////
|
214
|
|
215
|
public void setDimension(int dim)
|
216
|
{
|
217
|
if( currentDim != dim )
|
218
|
{
|
219
|
if( !(currentDim==DIM_3DXY && dim==DIM_3DXZ) && !(currentDim==DIM_3DXZ && dim==DIM_3DXY) )
|
220
|
{
|
221
|
resetPoints();
|
222
|
}
|
223
|
|
224
|
currentDim = dim;
|
225
|
}
|
226
|
}
|
227
|
|
228
|
///////////////////////////////////////////////////////////////////
|
229
|
|
230
|
public void drawCurve(Canvas c, long time)
|
231
|
{
|
232
|
if( mLastTime<0 )
|
233
|
{
|
234
|
mLastTime = time;
|
235
|
}
|
236
|
else
|
237
|
{
|
238
|
mDiffTime = time - mLastTime;
|
239
|
}
|
240
|
|
241
|
synchronized(lock)
|
242
|
{
|
243
|
switch(currentDim)
|
244
|
{
|
245
|
case DIM_1D: drawHorizontalAxis(c,"x");
|
246
|
drawPath(c,di1D,1,time);
|
247
|
drawRedPoints1D(c);
|
248
|
break;
|
249
|
case DIM_2D: drawHorizontalAxis(c,"x");
|
250
|
drawVerticalAxis (c,"y");
|
251
|
drawPath(c,di2D,1,time);
|
252
|
drawRedPoints2D(c);
|
253
|
break;
|
254
|
default : drawHorizontalAxis(c,"x");
|
255
|
drawVerticalAxis (c, currentDim==DIM_3DXY ? "y" : "z" );
|
256
|
drawPath(c,di3D,(currentDim==DIM_3DXY ? 1:2),time);
|
257
|
drawRedPoints3D(c);
|
258
|
break;
|
259
|
}
|
260
|
}
|
261
|
|
262
|
mLastTime = time;
|
263
|
}
|
264
|
|
265
|
///////////////////////////////////////////////////////////////////
|
266
|
|
267
|
private void clearPoints()
|
268
|
{
|
269
|
for(int i=0; i<3*NUM_POINTS; i++)
|
270
|
{
|
271
|
mPoints[i] = -10.0f;
|
272
|
}
|
273
|
}
|
274
|
|
275
|
///////////////////////////////////////////////////////////////////
|
276
|
|
277
|
public void resetPoints()
|
278
|
{
|
279
|
synchronized(lock)
|
280
|
{
|
281
|
clearPoints();
|
282
|
|
283
|
switch(currentDim)
|
284
|
{
|
285
|
case DIM_1D : di1D.removeAll(); break;
|
286
|
case DIM_2D : di2D.removeAll(); break;
|
287
|
case DIM_3DXY:
|
288
|
case DIM_3DXZ: di3D.removeAll(); break;
|
289
|
}
|
290
|
}
|
291
|
}
|
292
|
|
293
|
///////////////////////////////////////////////////////////////////
|
294
|
|
295
|
public void startDynamic()
|
296
|
{
|
297
|
mRunning = true;
|
298
|
mLastTime= -1;
|
299
|
mStartTime = System.currentTimeMillis();
|
300
|
|
301
|
clearPoints();
|
302
|
di1D.resetToBeginning();
|
303
|
di2D.resetToBeginning();
|
304
|
di3D.resetToBeginning();
|
305
|
}
|
306
|
|
307
|
///////////////////////////////////////////////////////////////////
|
308
|
|
309
|
public void stopDynamic()
|
310
|
{
|
311
|
mRunning = false;
|
312
|
}
|
313
|
|
314
|
///////////////////////////////////////////////////////////////////
|
315
|
|
316
|
private void drawHorizontalAxis(Canvas c, String label)
|
317
|
{
|
318
|
mPaint.setColor(0xff000000);
|
319
|
|
320
|
c.drawLine(0, halfScreenHeight, halfScreenWidth*2, halfScreenHeight, mPaint);
|
321
|
c.drawText( label, 0.95f*halfScreenWidth*2, halfScreenHeight + mSizeT , mPaint);
|
322
|
}
|
323
|
|
324
|
|
325
|
///////////////////////////////////////////////////////////////////
|
326
|
|
327
|
private void drawVerticalAxis(Canvas c, String label)
|
328
|
{
|
329
|
mPaint.setColor(0xff000000);
|
330
|
|
331
|
c.drawLine(halfScreenWidth, 0, halfScreenWidth, halfScreenHeight*2, mPaint);
|
332
|
c.drawText(label, halfScreenWidth + mSizeT, mSizeT , mPaint);
|
333
|
}
|
334
|
|
335
|
///////////////////////////////////////////////////////////////////
|
336
|
|
337
|
private void drawPath(Canvas c, Dynamic dyn, int index, long time)
|
338
|
{
|
339
|
int len = dyn.getNumPoints();
|
340
|
|
341
|
if( len>=2 )
|
342
|
{
|
343
|
if( mRunning )
|
344
|
{
|
345
|
if ( ++mPosition >= NUM_POINTS ) mPosition=0;
|
346
|
|
347
|
if( dyn.getDimension()==1 )
|
348
|
{
|
349
|
mPoints[3*mPosition+index] = halfScreenHeight;
|
350
|
}
|
351
|
|
352
|
if( dyn.get(mPoints,3*mPosition, time-mStartTime, mDiffTime) )
|
353
|
{
|
354
|
stopDynamic();
|
355
|
}
|
356
|
}
|
357
|
|
358
|
for(int i=0; i<NUM_POINTS; i++)
|
359
|
{
|
360
|
int color = i<=mPosition ? 0xff - (mPosition -i)*0xff/(NUM_POINTS-1)
|
361
|
: 0xff - (mPosition+NUM_POINTS-i)*0xff/(NUM_POINTS-1);
|
362
|
|
363
|
mPaint.setColor( 0xffffff + ((color&0xff)<<24) );
|
364
|
c.drawCircle(mPoints[3*i], mPoints[3*i+index] , mSize1, mPaint );
|
365
|
}
|
366
|
}
|
367
|
}
|
368
|
|
369
|
///////////////////////////////////////////////////////////////////
|
370
|
|
371
|
private void drawRedPoints1D(Canvas c)
|
372
|
{
|
373
|
int len = di1D.getNumPoints();
|
374
|
|
375
|
for(int curr=0; curr<len; curr++)
|
376
|
{
|
377
|
p1D = di1D.getPoint(curr);
|
378
|
drawRedPoint(c,curr+"", p1D.get1(), halfScreenHeight);
|
379
|
}
|
380
|
}
|
381
|
|
382
|
///////////////////////////////////////////////////////////////////
|
383
|
|
384
|
private void drawRedPoints2D(Canvas c)
|
385
|
{
|
386
|
int len = di2D.getNumPoints();
|
387
|
|
388
|
for(int curr=0; curr<len; curr++)
|
389
|
{
|
390
|
p2D = di2D.getPoint(curr);
|
391
|
drawRedPoint(c,curr+"", p2D.get1(), p2D.get2());
|
392
|
}
|
393
|
}
|
394
|
|
395
|
///////////////////////////////////////////////////////////////////
|
396
|
|
397
|
private void drawRedPoints3D(Canvas c)
|
398
|
{
|
399
|
int len = di3D.getNumPoints();
|
400
|
|
401
|
for(int curr=0; curr<len; curr++)
|
402
|
{
|
403
|
p3D = di3D.getPoint(curr);
|
404
|
drawRedPoint(c,curr+"", p3D.get1(), currentDim==DIM_3DXY ? p3D.get2():p3D.get3());
|
405
|
}
|
406
|
}
|
407
|
|
408
|
///////////////////////////////////////////////////////////////////
|
409
|
|
410
|
private void drawRedPoint(Canvas c, String label, float width, float height)
|
411
|
{
|
412
|
mPaint.setColor(0xffff0000);
|
413
|
c.drawCircle( width, height, mSize2, mPaint);
|
414
|
mPaint.setColor(0xffffffff);
|
415
|
c.drawText(label, width,height-mFontHeight, mPaint);
|
416
|
}
|
417
|
|
418
|
///////////////////////////////////////////////////////////////////
|
419
|
|
420
|
private void addNewPoint(int x, int y)
|
421
|
{
|
422
|
float gx,gy,gz;
|
423
|
int len;
|
424
|
|
425
|
switch(currentDim)
|
426
|
{
|
427
|
case DIM_1D: len = di1D.getNumPoints();
|
428
|
|
429
|
for(int g=0; g<len; g++)
|
430
|
{
|
431
|
p1D = di1D.getPoint(g);
|
432
|
gx = p1D.get1();
|
433
|
|
434
|
if( (x-gx)*(x-gx) < (mAvg*mAvg/100) )
|
435
|
{
|
436
|
mMoving = g;
|
437
|
break;
|
438
|
}
|
439
|
}
|
440
|
if( mMoving <0 )
|
441
|
{
|
442
|
synchronized(lock)
|
443
|
{
|
444
|
di1D.add(new Static1D(x));
|
445
|
}
|
446
|
}
|
447
|
break;
|
448
|
case DIM_2D: len = di2D.getNumPoints();
|
449
|
|
450
|
for(int g=0; g<len; g++)
|
451
|
{
|
452
|
p2D = di2D.getPoint(g);
|
453
|
gx = p2D.get1();
|
454
|
gy = p2D.get2();
|
455
|
|
456
|
if( (x-gx)*(x-gx) + (y-gy)*(y-gy) < (mAvg*mAvg/100) )
|
457
|
{
|
458
|
mMoving = g;
|
459
|
break;
|
460
|
}
|
461
|
}
|
462
|
if( mMoving <0 )
|
463
|
{
|
464
|
synchronized(lock)
|
465
|
{
|
466
|
di2D.add(new Static2D(x,y));
|
467
|
}
|
468
|
}
|
469
|
break;
|
470
|
default : len = di3D.getNumPoints();
|
471
|
|
472
|
for(int g=0; g<len; g++)
|
473
|
{
|
474
|
p3D = di3D.getPoint(g);
|
475
|
gx = p3D.get1();
|
476
|
gy = p3D.get2();
|
477
|
gz = p3D.get3();
|
478
|
|
479
|
if( currentDim==DIM_3DXY )
|
480
|
{
|
481
|
if( (x-gx)*(x-gx) + (y-gy)*(y-gy) < (mAvg*mAvg/100) )
|
482
|
{
|
483
|
mMoving = g;
|
484
|
break;
|
485
|
}
|
486
|
}
|
487
|
if( currentDim==DIM_3DXZ )
|
488
|
{
|
489
|
if( (x-gx)*(x-gx) + (y-gz)*(y-gz) < (mAvg*mAvg/100) )
|
490
|
{
|
491
|
mMoving = g;
|
492
|
break;
|
493
|
}
|
494
|
}
|
495
|
}
|
496
|
if( mMoving <0 )
|
497
|
{
|
498
|
synchronized(lock)
|
499
|
{
|
500
|
if( currentDim==DIM_3DXY )
|
501
|
{
|
502
|
di3D.add(new Static3D(x,y, halfScreenHeight));
|
503
|
}
|
504
|
if( currentDim==DIM_3DXZ )
|
505
|
{
|
506
|
di3D.add(new Static3D(x, halfScreenHeight,y));
|
507
|
}
|
508
|
}
|
509
|
}
|
510
|
break;
|
511
|
}
|
512
|
}
|
513
|
|
514
|
///////////////////////////////////////////////////////////////////
|
515
|
|
516
|
@Override
|
517
|
public boolean onTouchEvent(MotionEvent event)
|
518
|
{
|
519
|
int action = event.getAction();
|
520
|
int xDown, yDown;
|
521
|
|
522
|
switch(action)
|
523
|
{
|
524
|
case MotionEvent.ACTION_DOWN: xDown = (int)event.getX();
|
525
|
yDown = (int)event.getY();
|
526
|
|
527
|
addNewPoint(xDown,yDown);
|
528
|
|
529
|
break;
|
530
|
case MotionEvent.ACTION_MOVE: if( mMoving >=0 )
|
531
|
{
|
532
|
xDown = (int)event.getX();
|
533
|
yDown = (int)event.getY();
|
534
|
|
535
|
switch(currentDim)
|
536
|
{
|
537
|
case DIM_1D : di1D.setPoint(mMoving, xDown);
|
538
|
break;
|
539
|
case DIM_2D : di2D.setPoint(mMoving, xDown, yDown);
|
540
|
break;
|
541
|
case DIM_3DXY: di3D.setPoint(mMoving, xDown, yDown, (int)di3D.getPoint(mMoving).get3());
|
542
|
break;
|
543
|
case DIM_3DXZ: di3D.setPoint(mMoving, xDown, (int)di3D.getPoint(mMoving).get2(), yDown);
|
544
|
break;
|
545
|
}
|
546
|
}
|
547
|
break;
|
548
|
case MotionEvent.ACTION_UP : mMoving = -1;
|
549
|
break;
|
550
|
}
|
551
|
|
552
|
return true;
|
553
|
}
|
554
|
|
555
|
///////////////////////////////////////////////////////////////////
|
556
|
}
|