Project

General

Profile

Download (19.6 KB) Statistics
| Branch: | Revision:

examples / src / main / java / org / distorted / examples / dynamic / DynamicSurfaceView.java @ dea555b9

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
import java.lang.ref.WeakReference;
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

    
45
public class DynamicSurfaceView extends GLSurfaceView
46
    {
47
    public static final int DIM_1D   = 0; 
48
    public static final int DIM_2D   = 1; 
49
    public static final int DIM_3DXY = 2; 
50
    public static final int DIM_3DXZ = 3; 
51

    
52
    static final int NUM_POINTS = 250;
53
    private static final Object lock = new Object();
54

    
55
    private WeakReference<DynamicActivity> mAct;
56

    
57
    private static int halfScreenHeight=0;
58
    private static int halfScreenWidth =0;
59

    
60
    private Dynamic1D di1D;
61
    private Dynamic2D di2D;
62
    private Dynamic3D di3D;
63
    
64
    private Paint mPaint;
65
    private int mMoving;
66
    private int mDuration;
67
    private int mPosition;
68
    private long mDiffTime, mLastTime, mStartTime;
69
    private float mNoise0, mNoise1, mNoise2;
70
    private float mCount;
71

    
72
    private int mSize1, mSize2, mSizeT, mAvg;
73
    private float mFontHeight;
74

    
75
    private int currentDim= DIM_2D;
76
    
77
    private Static1D p1D;
78
    private Static2D p2D;
79
    private Static3D p3D;
80

    
81
    private Static1D p1N;
82
    private Static2D p2N;
83
    private Static3D p3N;
84

    
85
    private float[] mPoints = new float[3*NUM_POINTS];
86
    private boolean mRunning;
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89
    
90
    public DynamicSurfaceView(Context context, AttributeSet attrs)
91
      {
92
      super(context, attrs);
93

    
94
      DynamicActivity act = (DynamicActivity)context;
95
      mAct = new WeakReference<>(act);
96

    
97
      mPaint = new Paint();
98
      mPaint.setStyle(Style.FILL);
99
      mPaint.setAntiAlias(true);
100

    
101
      mMoving = -1;
102
      mDuration = 10000;
103
      mCount    = 0.0f;
104
      mPosition = 0;
105
      mNoise0   = 0.0f;
106
      mNoise1   = 0.0f;
107
      mNoise2   = 0.0f;
108
      mDiffTime = -1;
109
      mLastTime = -1;
110
      mStartTime= -1;
111

    
112
      mRunning = false;
113

    
114
      clearPoints();
115

    
116
      di1D = new Dynamic1D(mDuration,mCount);
117
      p1N  = new Static1D(mNoise0);
118
      di2D = new Dynamic2D(mDuration,mCount);
119
      p2N  = new Static2D(mNoise0,mNoise1);
120
      di3D = new Dynamic3D(mDuration,mCount);
121
      p3N  = new Static3D(mNoise0,mNoise1,mNoise2);
122

    
123
      di1D.setAccessType(Dynamic.ACCESS_TYPE_SEQUENTIAL);
124
      di2D.setAccessType(Dynamic.ACCESS_TYPE_SEQUENTIAL);
125
      di3D.setAccessType(Dynamic.ACCESS_TYPE_SEQUENTIAL);
126

    
127
      if(!isInEditMode())
128
        {
129
        setFocusable(true);
130
        setFocusableInTouchMode(true);
131
        DynamicRenderer mRenderer = new DynamicRenderer(this);
132
        final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
133
        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
134
        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
135
        setRenderer(mRenderer);
136
        }
137
      }
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

    
141
    public void onSurfaceChanged(int width,int height)
142
      {
143
      mAvg = (width+height)/2;
144

    
145
      mSize1 = mAvg/150;
146
      mSize2 = mAvg/50;
147
      mSizeT = mAvg/30;
148

    
149
      mPaint.setTextSize(mSizeT);
150
      mPaint.setTextAlign(Paint.Align.CENTER);
151

    
152
      final Rect textBounds = new Rect();
153
      String text = "1";
154
      mPaint.getTextBounds(text, 0, text.length(), textBounds);
155
      mFontHeight = textBounds.exactCenterY();
156

    
157
      clearPoints();
158
      }
159

    
160
///////////////////////////////////////////////////////////////////////////////////////////////////
161

    
162
    public static void setHalfWidth(int hw)
163
      {
164
      halfScreenWidth = hw;
165
      }
166

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168

    
169
    public static void setHalfHeight(int hh)
170
      {
171
      halfScreenHeight = hh;
172
      }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175

    
176
    public void setMode(int mode)
177
      {
178
      di1D.setMode(mode);  
179
      di2D.setMode(mode);
180
      di3D.setMode(mode);
181
      }
182

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184

    
185
    public void setDuration(int duration)
186
      {
187
      mDuration = duration;
188
      
189
      di1D.setDuration(duration);
190
      di2D.setDuration(duration);
191
      di3D.setDuration(duration);
192
      }
193

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

    
196
    public void setCount(float count)
197
      {
198
      mCount = count;
199

    
200
      di1D.setCount(count);
201
      di2D.setCount(count);
202
      di3D.setCount(count);
203
      }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

    
207
    public void setConvexity(float convexity)
208
      {
209
      di1D.setConvexity(convexity);
210
      di2D.setConvexity(convexity);
211
      di3D.setConvexity(convexity);
212
      }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

    
216
    public void setNoise(float noise0, float noise1, float noise2)
217
      {
218
      mNoise0 = noise0;
219
      mNoise1 = noise1;
220
      mNoise2 = noise2;
221

    
222
      p1N.set(mNoise0);
223
      p2N.set(mNoise0,mNoise1);
224
      p3N.set(mNoise0,mNoise1,mNoise2);
225

    
226
      di1D.setNoise(p1N);
227
      di2D.setNoise(p2N);
228
      di3D.setNoise(p3N);
229
      }
230
    
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232

    
233
    public void setDimension(int dim)
234
      {
235
      if( currentDim != dim )
236
        {
237
        if( !(currentDim==DIM_3DXY && dim==DIM_3DXZ) && !(currentDim==DIM_3DXZ && dim==DIM_3DXY) )
238
          {
239
          resetPoints();
240
          }
241

    
242
        currentDim = dim;
243
        }
244
      }
245
    
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247
    
248
    public void drawCurve(Canvas c, long time)
249
      {
250
      if( mLastTime<0 )
251
        {
252
        mLastTime = time;
253
        }
254
      else
255
        {
256
        mDiffTime = time - mLastTime;
257
        }
258

    
259
      synchronized(lock)
260
        {
261
        switch(currentDim)
262
          {
263
          case DIM_1D: drawHorizontalAxis(c,"x");
264
                       drawPath(c,di1D,1,time);
265
                       drawRedPoints1D(c);
266
                       break;
267
          case DIM_2D: drawHorizontalAxis(c,"x");
268
                       drawVerticalAxis  (c,"y");
269
                       drawPath(c,di2D,1,time);
270
                       drawRedPoints2D(c);
271
                       break;
272
          default    : drawHorizontalAxis(c,"x");
273
                       drawVerticalAxis  (c, currentDim==DIM_3DXY ? "y" : "z" );
274
                       drawPath(c,di3D,(currentDim==DIM_3DXY ? 1:2),time);
275
                       drawRedPoints3D(c);
276
                       break;
277
          }
278
        }
279

    
280
      mLastTime = time;
281
      }
282

    
283
///////////////////////////////////////////////////////////////////////////////////////////////////
284

    
285
    private void clearPoints()
286
      {
287
      for(int i=0; i<3*NUM_POINTS; i++)
288
         {
289
         mPoints[i] = -100000.0f;
290
         }
291
      }
292

    
293
///////////////////////////////////////////////////////////////////////////////////////////////////
294

    
295
    public void resetPoints()
296
      {
297
      synchronized(lock)
298
        {
299
        clearPoints();
300

    
301
        switch(currentDim)
302
          {
303
          case DIM_1D  : di1D.removeAll(); break;
304
          case DIM_2D  : di2D.removeAll(); break;
305
          case DIM_3DXY:
306
          case DIM_3DXZ: di3D.removeAll(); break;
307
          }
308

    
309
        DynamicActivity act = mAct.get();
310
        act.setNumRedPoints(0);
311
        act.clearPoints();
312
        }
313
      }
314

    
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316

    
317
    public void startDynamic()
318
      {
319
      mRunning = true;
320
      mLastTime= -1;
321
      mStartTime = System.currentTimeMillis();
322

    
323
      clearPoints();
324
      di1D.resetToBeginning();
325
      di2D.resetToBeginning();
326
      di3D.resetToBeginning();
327
      }
328

    
329
///////////////////////////////////////////////////////////////////////////////////////////////////
330

    
331
    public void stopDynamic()
332
      {
333
      mRunning = false;
334
      }
335

    
336
///////////////////////////////////////////////////////////////////////////////////////////////////
337

    
338
    private void drawHorizontalAxis(Canvas c, String label)
339
      {
340
      mPaint.setColor(0xff000000);
341

    
342
      c.drawLine(0, halfScreenHeight, halfScreenWidth*2, halfScreenHeight, mPaint);
343
      c.drawText( label, 0.95f*halfScreenWidth*2, halfScreenHeight + mSizeT , mPaint);
344
      }
345

    
346

    
347
///////////////////////////////////////////////////////////////////////////////////////////////////
348

    
349
    private void drawVerticalAxis(Canvas c, String label)
350
      {
351
      mPaint.setColor(0xff000000);
352

    
353
      c.drawLine(halfScreenWidth, 0, halfScreenWidth, halfScreenHeight*2, mPaint);
354
      c.drawText(label, halfScreenWidth + mSizeT,                mSizeT , mPaint);
355
      }
356

    
357
///////////////////////////////////////////////////////////////////////////////////////////////////
358

    
359
    private void drawPath(Canvas c, Dynamic dyn, int index, long time)
360
      {
361
      int len = dyn.getNumPoints();
362

    
363
      if( len>=2 )
364
        {
365
        if( mRunning )
366
          {
367
          if ( ++mPosition >= NUM_POINTS ) mPosition=0;
368

    
369
          if( dyn.getDimension()==1 )
370
            {
371
            mPoints[3*mPosition+index] = halfScreenHeight;
372
            }
373

    
374
          if( dyn.get(mPoints,3*mPosition, time-mStartTime, mDiffTime) )
375
            {
376
            stopDynamic();
377
            }
378

    
379
          addNewSpeedPoint(time);
380
          }
381

    
382
        for(int i=0; i<NUM_POINTS; i++)
383
          {
384
          int color = i<=mPosition ? 0xff - (mPosition           -i)*0xff/(NUM_POINTS-1)
385
                                   : 0xff - (mPosition+NUM_POINTS-i)*0xff/(NUM_POINTS-1);
386

    
387
          mPaint.setColor( 0xffffff + ((color&0xff)<<24) );
388
          c.drawCircle(mPoints[3*i], mPoints[3*i+index] , mSize1, mPaint );
389
          }
390
        }
391
      }
392

    
393
///////////////////////////////////////////////////////////////////////////////////////////////////
394

    
395
    private void drawRedPoints1D(Canvas c)
396
      {
397
      int len = di1D.getNumPoints();
398

    
399
      for(int curr=0; curr<len; curr++)
400
        {
401
        p1D = di1D.getPoint(curr);
402
        drawRedPoint(c,curr+"", p1D.get1(), halfScreenHeight);
403
        }
404
      }
405

    
406
///////////////////////////////////////////////////////////////////////////////////////////////////
407

    
408
    private void drawRedPoints2D(Canvas c)
409
      {
410
      int len = di2D.getNumPoints();
411

    
412
      for(int curr=0; curr<len; curr++)
413
        {
414
        p2D = di2D.getPoint(curr);
415
        drawRedPoint(c,curr+"", p2D.get1(), p2D.get2());
416
        }
417
      }
418

    
419
///////////////////////////////////////////////////////////////////////////////////////////////////
420

    
421
    private void drawRedPoints3D(Canvas c)
422
      {
423
      int len = di3D.getNumPoints();
424

    
425
      for(int curr=0; curr<len; curr++)
426
        {
427
        p3D = di3D.getPoint(curr);
428
        drawRedPoint(c,curr+"", p3D.get1(), currentDim==DIM_3DXY ? p3D.get2():p3D.get3());
429
        }
430
      }
431

    
432
///////////////////////////////////////////////////////////////////////////////////////////////////
433

    
434
    private void drawRedPoint(Canvas c, String label, float width, float height)
435
      {
436
      mPaint.setColor(0xffff0000);
437
      c.drawCircle( width, height, mSize2, mPaint);
438
      mPaint.setColor(0xffffffff);
439
      c.drawText(label, width,height-mFontHeight, mPaint);
440
      }
441

    
442
///////////////////////////////////////////////////////////////////////////////////////////////////
443

    
444
    private void addNewPoint(int x, int y)
445
      {
446
      float gx,gy,gz;
447
      int len;
448

    
449
      switch(currentDim)
450
        {
451
        case DIM_1D: len = di1D.getNumPoints();
452

    
453
                     for(int g=0; g<len; g++)
454
                       {
455
                       p1D = di1D.getPoint(g);  
456
                       gx = p1D.get1();
457
                                    
458
                       if( (x-gx)*(x-gx) < (mAvg*mAvg/100) )
459
                         {
460
                         mMoving = g;
461
                         break;
462
                         }
463
                       }
464
                     if( mMoving <0 )
465
                       {
466
                       synchronized(lock)
467
                         {
468
                         di1D.add(new Static1D(x));
469
                         mAct.get().setNumRedPoints(len+1);
470
                         }
471
                       }
472
                     break;
473
        case DIM_2D: len = di2D.getNumPoints();
474
                                 
475
                     for(int g=0; g<len; g++)
476
                       {
477
                       p2D = di2D.getPoint(g);  
478
                       gx = p2D.get1();
479
                       gy = p2D.get2();
480
                                    
481
                       if( (x-gx)*(x-gx) + (y-gy)*(y-gy) < (mAvg*mAvg/100) )
482
                         {
483
                         mMoving = g;
484
                         break;
485
                         }
486
                       }
487
                     if( mMoving <0 )
488
                       {
489
                       synchronized(lock)
490
                         {
491
                         di2D.add(new Static2D(x,y));
492
                         mAct.get().setNumRedPoints(len+1);
493
                         }
494
                       }
495
                     break;
496
        default    : len = di3D.getNumPoints();
497
                                 
498
                     for(int g=0; g<len; g++)
499
                       {
500
                       p3D = di3D.getPoint(g);  
501
                       gx = p3D.get1();
502
                       gy = p3D.get2();
503
                       gz = p3D.get3();
504
                               
505
                     if( currentDim==DIM_3DXY )
506
                       {
507
                       if( (x-gx)*(x-gx) + (y-gy)*(y-gy) < (mAvg*mAvg/100) )
508
                         {
509
                         mMoving = g;
510
                         break;
511
                         }
512
                       }
513
                     if( currentDim==DIM_3DXZ )
514
                       {
515
                       if( (x-gx)*(x-gx) + (y-gz)*(y-gz) < (mAvg*mAvg/100) )
516
                         {
517
                         mMoving = g;
518
                         break;
519
                         }
520
                       }
521
                     }
522
                   if( mMoving <0 )
523
                     { 
524
                     synchronized(lock)
525
                       {
526
                       if( currentDim==DIM_3DXY ) di3D.add(new Static3D(x,y, halfScreenHeight));
527
                       if( currentDim==DIM_3DXZ ) di3D.add(new Static3D(x, halfScreenHeight,y));
528
                       mAct.get().setNumRedPoints(len+1);
529
                       }
530
                     }
531
                   break; 
532
        }
533
      }
534
    
535
///////////////////////////////////////////////////////////////////////////////////////////////////
536

    
537
    private void addNewSpeedPoint(long time)
538
      {
539
      int prev = mPosition-1;
540
      if( prev<0 ) prev = NUM_POINTS-1;
541

    
542
      float xdiff = mPoints[3*prev  ]-mPoints[3*mPosition  ];
543
      float ydiff = mPoints[3*prev+1]-mPoints[3*mPosition+1];
544
      float zdiff = mPoints[3*prev+2]-mPoints[3*mPosition+2];
545

    
546
      float dist = (float)Math.sqrt( xdiff*xdiff + ydiff*ydiff + zdiff*zdiff );
547
      float speed= mDiffTime<=0 ? 0: dist / mDiffTime;
548
      float timepoint = ((float)(time-mStartTime))/mDuration;
549

    
550
      if( dist<1000.0f )   // otherwise this is a very first call; do not send it!
551
        {
552
        mAct.get().addPoint( timepoint - (int)timepoint, speed );
553
        }
554
      }
555

    
556
///////////////////////////////////////////////////////////////////////////////////////////////////
557

    
558
    @Override
559
    public boolean onTouchEvent(MotionEvent event)
560
      {
561
      int action = event.getAction();
562
      int xDown, yDown;
563

    
564
      switch(action)
565
        {
566
        case MotionEvent.ACTION_DOWN: xDown = (int)event.getX();
567
                                      yDown = (int)event.getY();
568
                                      
569
                                      addNewPoint(xDown,yDown);
570
                                    
571
                                      break;
572
        case MotionEvent.ACTION_MOVE: if( mMoving >=0 )
573
                                        {
574
                                        xDown = (int)event.getX();
575
                                        yDown = (int)event.getY();
576
                                        
577
                                        switch(currentDim)
578
                                          {
579
                                          case DIM_1D  : di1D.setPoint(mMoving, xDown);
580
                                                         break;
581
                                          case DIM_2D  : di2D.setPoint(mMoving, xDown, yDown);
582
                                                         break;
583
                                          case DIM_3DXY: di3D.setPoint(mMoving, xDown, yDown, (int)di3D.getPoint(mMoving).get3());
584
                                                         break;
585
                                          case DIM_3DXZ: di3D.setPoint(mMoving, xDown, (int)di3D.getPoint(mMoving).get2(), yDown);
586
                                                         break;
587
                                          }
588
                                        }                           
589
                                      break;
590
        case MotionEvent.ACTION_UP  : mMoving = -1;
591
                                      break;
592
        }
593
            
594
      return true;
595
      }
596
  }
(4-4/4)