Project

General

Profile

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

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

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 setNoise(float noise0, float noise1, float noise2)
208
      {
209
      mNoise0 = noise0;
210
      mNoise1 = noise1;
211
      mNoise2 = noise2;
212

    
213
      p1N.set(mNoise0);
214
      p2N.set(mNoise0,mNoise1);
215
      p3N.set(mNoise0,mNoise1,mNoise2);
216

    
217
      di1D.setNoise(p1N);
218
      di2D.setNoise(p2N);
219
      di3D.setNoise(p3N);
220
      }
221
    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

    
224
    public void setDimension(int dim)
225
      {
226
      if( currentDim != dim )
227
        {
228
        if( !(currentDim==DIM_3DXY && dim==DIM_3DXZ) && !(currentDim==DIM_3DXZ && dim==DIM_3DXY) )
229
          {
230
          resetPoints();
231
          }
232

    
233
        currentDim = dim;
234
        }
235
      }
236
    
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238
    
239
    public void drawCurve(Canvas c, long time)
240
      {
241
      if( mLastTime<0 )
242
        {
243
        mLastTime = time;
244
        }
245
      else
246
        {
247
        mDiffTime = time - mLastTime;
248
        }
249

    
250
      synchronized(lock)
251
        {
252
        switch(currentDim)
253
          {
254
          case DIM_1D: drawHorizontalAxis(c,"x");
255
                       drawPath(c,di1D,1,time);
256
                       drawRedPoints1D(c);
257
                       break;
258
          case DIM_2D: drawHorizontalAxis(c,"x");
259
                       drawVerticalAxis  (c,"y");
260
                       drawPath(c,di2D,1,time);
261
                       drawRedPoints2D(c);
262
                       break;
263
          default    : drawHorizontalAxis(c,"x");
264
                       drawVerticalAxis  (c, currentDim==DIM_3DXY ? "y" : "z" );
265
                       drawPath(c,di3D,(currentDim==DIM_3DXY ? 1:2),time);
266
                       drawRedPoints3D(c);
267
                       break;
268
          }
269
        }
270

    
271
      mLastTime = time;
272
      }
273

    
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275

    
276
    private void clearPoints()
277
      {
278
      for(int i=0; i<3*NUM_POINTS; i++)
279
         {
280
         mPoints[i] = -100000.0f;
281
         }
282
      }
283

    
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285

    
286
    public void resetPoints()
287
      {
288
      synchronized(lock)
289
        {
290
        clearPoints();
291

    
292
        switch(currentDim)
293
          {
294
          case DIM_1D  : di1D.removeAll(); break;
295
          case DIM_2D  : di2D.removeAll(); break;
296
          case DIM_3DXY:
297
          case DIM_3DXZ: di3D.removeAll(); break;
298
          }
299

    
300
        mAct.get().setNumRedPoints(0);
301
        }
302
      }
303

    
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305

    
306
    public void startDynamic()
307
      {
308
      mRunning = true;
309
      mLastTime= -1;
310
      mStartTime = System.currentTimeMillis();
311

    
312
      clearPoints();
313
      di1D.resetToBeginning();
314
      di2D.resetToBeginning();
315
      di3D.resetToBeginning();
316
      }
317

    
318
///////////////////////////////////////////////////////////////////////////////////////////////////
319

    
320
    public void stopDynamic()
321
      {
322
      mRunning = false;
323
      }
324

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326

    
327
    private void drawHorizontalAxis(Canvas c, String label)
328
      {
329
      mPaint.setColor(0xff000000);
330

    
331
      c.drawLine(0, halfScreenHeight, halfScreenWidth*2, halfScreenHeight, mPaint);
332
      c.drawText( label, 0.95f*halfScreenWidth*2, halfScreenHeight + mSizeT , mPaint);
333
      }
334

    
335

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

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

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

    
346
///////////////////////////////////////////////////////////////////////////////////////////////////
347

    
348
    private void drawPath(Canvas c, Dynamic dyn, int index, long time)
349
      {
350
      int len = dyn.getNumPoints();
351

    
352
      if( len>=2 )
353
        {
354
        if( mRunning )
355
          {
356
          if ( ++mPosition >= NUM_POINTS ) mPosition=0;
357

    
358
          if( dyn.getDimension()==1 )
359
            {
360
            mPoints[3*mPosition+index] = halfScreenHeight;
361
            }
362

    
363
          if( dyn.get(mPoints,3*mPosition, time-mStartTime, mDiffTime) )
364
            {
365
            stopDynamic();
366
            }
367

    
368
          addNewSpeedPoint(time);
369
          }
370

    
371
        for(int i=0; i<NUM_POINTS; i++)
372
          {
373
          int color = i<=mPosition ? 0xff - (mPosition           -i)*0xff/(NUM_POINTS-1)
374
                                   : 0xff - (mPosition+NUM_POINTS-i)*0xff/(NUM_POINTS-1);
375

    
376
          mPaint.setColor( 0xffffff + ((color&0xff)<<24) );
377
          c.drawCircle(mPoints[3*i], mPoints[3*i+index] , mSize1, mPaint );
378
          }
379
        }
380
      }
381

    
382
///////////////////////////////////////////////////////////////////////////////////////////////////
383

    
384
    private void drawRedPoints1D(Canvas c)
385
      {
386
      int len = di1D.getNumPoints();
387

    
388
      for(int curr=0; curr<len; curr++)
389
        {
390
        p1D = di1D.getPoint(curr);
391
        drawRedPoint(c,curr+"", p1D.get1(), halfScreenHeight);
392
        }
393
      }
394

    
395
///////////////////////////////////////////////////////////////////////////////////////////////////
396

    
397
    private void drawRedPoints2D(Canvas c)
398
      {
399
      int len = di2D.getNumPoints();
400

    
401
      for(int curr=0; curr<len; curr++)
402
        {
403
        p2D = di2D.getPoint(curr);
404
        drawRedPoint(c,curr+"", p2D.get1(), p2D.get2());
405
        }
406
      }
407

    
408
///////////////////////////////////////////////////////////////////////////////////////////////////
409

    
410
    private void drawRedPoints3D(Canvas c)
411
      {
412
      int len = di3D.getNumPoints();
413

    
414
      for(int curr=0; curr<len; curr++)
415
        {
416
        p3D = di3D.getPoint(curr);
417
        drawRedPoint(c,curr+"", p3D.get1(), currentDim==DIM_3DXY ? p3D.get2():p3D.get3());
418
        }
419
      }
420

    
421
///////////////////////////////////////////////////////////////////////////////////////////////////
422

    
423
    private void drawRedPoint(Canvas c, String label, float width, float height)
424
      {
425
      mPaint.setColor(0xffff0000);
426
      c.drawCircle( width, height, mSize2, mPaint);
427
      mPaint.setColor(0xffffffff);
428
      c.drawText(label, width,height-mFontHeight, mPaint);
429
      }
430

    
431
///////////////////////////////////////////////////////////////////////////////////////////////////
432

    
433
    private void addNewPoint(int x, int y)
434
      {
435
      float gx,gy,gz;
436
      int len;
437
      
438
      switch(currentDim)
439
        {
440
        case DIM_1D: len = di1D.getNumPoints();
441
                
442
                     for(int g=0; g<len; g++)
443
                       {
444
                       p1D = di1D.getPoint(g);  
445
                       gx = p1D.get1();
446
                                    
447
                       if( (x-gx)*(x-gx) < (mAvg*mAvg/100) )
448
                         {
449
                         mMoving = g;
450
                         break;
451
                         }
452
                       }
453
                     if( mMoving <0 )
454
                       {
455
                       synchronized(lock)
456
                         {
457
                         di1D.add(new Static1D(x));
458
                         mAct.get().setNumRedPoints(len+1);
459
                         }
460
                       }
461
                     break;
462
        case DIM_2D: len = di2D.getNumPoints();
463
                                 
464
                     for(int g=0; g<len; g++)
465
                       {
466
                       p2D = di2D.getPoint(g);  
467
                       gx = p2D.get1();
468
                       gy = p2D.get2();
469
                                    
470
                       if( (x-gx)*(x-gx) + (y-gy)*(y-gy) < (mAvg*mAvg/100) )
471
                         {
472
                         mMoving = g;
473
                         break;
474
                         }
475
                       }
476
                     if( mMoving <0 )
477
                       {
478
                       synchronized(lock)
479
                         {
480
                         di2D.add(new Static2D(x,y));
481
                         mAct.get().setNumRedPoints(len+1);
482
                         }
483
                       }
484
                     break;
485
        default    : len = di3D.getNumPoints();
486
                                 
487
                     for(int g=0; g<len; g++)
488
                       {
489
                       p3D = di3D.getPoint(g);  
490
                       gx = p3D.get1();
491
                       gy = p3D.get2();
492
                       gz = p3D.get3();
493
                               
494
                     if( currentDim==DIM_3DXY )
495
                       {
496
                       if( (x-gx)*(x-gx) + (y-gy)*(y-gy) < (mAvg*mAvg/100) )
497
                         {
498
                         mMoving = g;
499
                         break;
500
                         }
501
                       }
502
                     if( currentDim==DIM_3DXZ )
503
                       {
504
                       if( (x-gx)*(x-gx) + (y-gz)*(y-gz) < (mAvg*mAvg/100) )
505
                         {
506
                         mMoving = g;
507
                         break;
508
                         }
509
                       }
510
                     }
511
                   if( mMoving <0 )
512
                     { 
513
                     synchronized(lock)
514
                       {
515
                       if( currentDim==DIM_3DXY ) di3D.add(new Static3D(x,y, halfScreenHeight));
516
                       if( currentDim==DIM_3DXZ ) di3D.add(new Static3D(x, halfScreenHeight,y));
517
                       mAct.get().setNumRedPoints(len+1);
518
                       }
519
                     }
520
                   break; 
521
        }
522
      }
523
    
524
///////////////////////////////////////////////////////////////////////////////////////////////////
525

    
526
    private void addNewSpeedPoint(long time)
527
      {
528
      int prev = mPosition-1;
529
      if( prev<0 ) prev = NUM_POINTS-1;
530

    
531
      float xdiff = mPoints[3*prev  ]-mPoints[3*mPosition  ];
532
      float ydiff = mPoints[3*prev+1]-mPoints[3*mPosition+1];
533
      float zdiff = mPoints[3*prev+2]-mPoints[3*mPosition+2];
534

    
535
      float dist = (float)Math.sqrt( xdiff*xdiff + ydiff*ydiff + zdiff*zdiff );
536
      float speed= mDiffTime<=0 ? 0: dist / mDiffTime;
537
      float timepoint = ((float)(time-mStartTime))/mDuration;
538

    
539
      if( dist<1000.0f )   // otherwise this is a very first call; do not send it!
540
        {
541
        mAct.get().addPoint( timepoint - (int)timepoint, speed );
542
        }
543
      }
544

    
545
///////////////////////////////////////////////////////////////////////////////////////////////////
546

    
547
    @Override
548
    public boolean onTouchEvent(MotionEvent event)
549
      {
550
      int action = event.getAction();
551
      int xDown, yDown;
552

    
553
      switch(action)
554
        {
555
        case MotionEvent.ACTION_DOWN: xDown = (int)event.getX();
556
                                      yDown = (int)event.getY();
557
                                      
558
                                      addNewPoint(xDown,yDown);
559
                                    
560
                                      break;
561
        case MotionEvent.ACTION_MOVE: if( mMoving >=0 )
562
                                        {
563
                                        xDown = (int)event.getX();
564
                                        yDown = (int)event.getY();
565
                                        
566
                                        switch(currentDim)
567
                                          {
568
                                          case DIM_1D  : di1D.setPoint(mMoving, xDown);
569
                                                         break;
570
                                          case DIM_2D  : di2D.setPoint(mMoving, xDown, yDown);
571
                                                         break;
572
                                          case DIM_3DXY: di3D.setPoint(mMoving, xDown, yDown, (int)di3D.getPoint(mMoving).get3());
573
                                                         break;
574
                                          case DIM_3DXZ: di3D.setPoint(mMoving, xDown, (int)di3D.getPoint(mMoving).get2(), yDown);
575
                                                         break;
576
                                          }
577
                                        }                           
578
                                      break;
579
        case MotionEvent.ACTION_UP  : mMoving = -1;
580
                                      break;
581
        }
582
            
583
      return true;
584
      }
585
  }
(4-4/4)