Project

General

Profile

« Previous | Next » 

Revision e52efe17

Added by Leszek Koltunski almost 5 years ago

Improve the Dynamic app.

View differences:

src/main/java/org/distorted/examples/dynamic/DynamicSurfaceView.java
38 38
import org.distorted.library.type.Static2D;
39 39
import org.distorted.library.type.Static3D;
40 40

  
41
///////////////////////////////////////////////////////////////////
41
import java.lang.ref.WeakReference;
42

  
43
///////////////////////////////////////////////////////////////////////////////////////////////////
42 44

  
43 45
public class DynamicSurfaceView extends GLSurfaceView
44 46
    {
......
47 49
    public static final int DIM_3DXY = 2; 
48 50
    public static final int DIM_3DXZ = 3; 
49 51

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

  
55
    private WeakReference<DynamicActivity> mAct;
56

  
53 57
    private static int halfScreenHeight=0;
54 58
    private static int halfScreenWidth =0;
55 59

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

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

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

  
90 97
      mPaint = new Paint();
91 98
      mPaint.setStyle(Style.FILL);
92 99
      mPaint.setAntiAlias(true);
......
104 111

  
105 112
      mRunning = false;
106 113

  
114
      clearPoints();
115

  
107 116
      di1D = new Dynamic1D(mDuration,mCount);
108 117
      p1N  = new Static1D(mNoise0);
109 118
      di2D = new Dynamic2D(mDuration,mCount);
......
148 157
      clearPoints();
149 158
      }
150 159

  
151
///////////////////////////////////////////////////////////////////
160
///////////////////////////////////////////////////////////////////////////////////////////////////
152 161

  
153 162
    public static void setHalfWidth(int hw)
154 163
      {
155 164
      halfScreenWidth = hw;
156 165
      }
157 166

  
158
///////////////////////////////////////////////////////////////////
167
///////////////////////////////////////////////////////////////////////////////////////////////////
159 168

  
160 169
    public static void setHalfHeight(int hh)
161 170
      {
162 171
      halfScreenHeight = hh;
163 172
      }
164 173

  
165
///////////////////////////////////////////////////////////////////
174
///////////////////////////////////////////////////////////////////////////////////////////////////
166 175

  
167 176
    public void setMode(int mode)
168 177
      {
......
171 180
      di3D.setMode(mode);
172 181
      }
173 182

  
174
///////////////////////////////////////////////////////////////////
183
///////////////////////////////////////////////////////////////////////////////////////////////////
175 184

  
176 185
    public void setDuration(int duration)
177 186
      {
......
182 191
      di3D.setDuration(duration);
183 192
      }
184 193

  
185
///////////////////////////////////////////////////////////////////
194
///////////////////////////////////////////////////////////////////////////////////////////////////
186 195

  
187 196
    public void setCount(float count)
188 197
      {
......
193 202
      di3D.setCount(count);
194 203
      }
195 204

  
196
///////////////////////////////////////////////////////////////////
205
///////////////////////////////////////////////////////////////////////////////////////////////////
197 206

  
198 207
    public void setNoise(float noise0, float noise1, float noise2)
199 208
      {
......
210 219
      di3D.setNoise(p3N);
211 220
      }
212 221
    
213
///////////////////////////////////////////////////////////////////
222
///////////////////////////////////////////////////////////////////////////////////////////////////
214 223

  
215 224
    public void setDimension(int dim)
216 225
      {
......
225 234
        }
226 235
      }
227 236
    
228
///////////////////////////////////////////////////////////////////
237
///////////////////////////////////////////////////////////////////////////////////////////////////
229 238
    
230 239
    public void drawCurve(Canvas c, long time)
231 240
      {
......
262 271
      mLastTime = time;
263 272
      }
264 273

  
265
///////////////////////////////////////////////////////////////////
274
///////////////////////////////////////////////////////////////////////////////////////////////////
266 275

  
267 276
    private void clearPoints()
268 277
      {
269 278
      for(int i=0; i<3*NUM_POINTS; i++)
270 279
         {
271
         mPoints[i] = -10.0f;
280
         mPoints[i] = -100000.0f;
272 281
         }
273 282
      }
274 283

  
275
///////////////////////////////////////////////////////////////////
284
///////////////////////////////////////////////////////////////////////////////////////////////////
276 285

  
277 286
    public void resetPoints()
278 287
      {
......
287 296
          case DIM_3DXY:
288 297
          case DIM_3DXZ: di3D.removeAll(); break;
289 298
          }
299

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

  
293
///////////////////////////////////////////////////////////////////
304
///////////////////////////////////////////////////////////////////////////////////////////////////
294 305

  
295 306
    public void startDynamic()
296 307
      {
......
304 315
      di3D.resetToBeginning();
305 316
      }
306 317

  
307
///////////////////////////////////////////////////////////////////
318
///////////////////////////////////////////////////////////////////////////////////////////////////
308 319

  
309 320
    public void stopDynamic()
310 321
      {
311 322
      mRunning = false;
312 323
      }
313 324

  
314
///////////////////////////////////////////////////////////////////
325
///////////////////////////////////////////////////////////////////////////////////////////////////
315 326

  
316 327
    private void drawHorizontalAxis(Canvas c, String label)
317 328
      {
......
322 333
      }
323 334

  
324 335

  
325
///////////////////////////////////////////////////////////////////
336
///////////////////////////////////////////////////////////////////////////////////////////////////
326 337

  
327 338
    private void drawVerticalAxis(Canvas c, String label)
328 339
      {
......
332 343
      c.drawText(label, halfScreenWidth + mSizeT,                mSizeT , mPaint);
333 344
      }
334 345

  
335
///////////////////////////////////////////////////////////////////
346
///////////////////////////////////////////////////////////////////////////////////////////////////
336 347

  
337 348
    private void drawPath(Canvas c, Dynamic dyn, int index, long time)
338 349
      {
......
353 364
            {
354 365
            stopDynamic();
355 366
            }
367

  
368
          addNewSpeedPoint(time);
356 369
          }
357 370

  
358 371
        for(int i=0; i<NUM_POINTS; i++)
......
366 379
        }
367 380
      }
368 381

  
369
///////////////////////////////////////////////////////////////////
382
///////////////////////////////////////////////////////////////////////////////////////////////////
370 383

  
371 384
    private void drawRedPoints1D(Canvas c)
372 385
      {
......
379 392
        }
380 393
      }
381 394

  
382
///////////////////////////////////////////////////////////////////
395
///////////////////////////////////////////////////////////////////////////////////////////////////
383 396

  
384 397
    private void drawRedPoints2D(Canvas c)
385 398
      {
......
392 405
        }
393 406
      }
394 407

  
395
///////////////////////////////////////////////////////////////////
408
///////////////////////////////////////////////////////////////////////////////////////////////////
396 409

  
397 410
    private void drawRedPoints3D(Canvas c)
398 411
      {
......
405 418
        }
406 419
      }
407 420

  
408
///////////////////////////////////////////////////////////////////
421
///////////////////////////////////////////////////////////////////////////////////////////////////
409 422

  
410 423
    private void drawRedPoint(Canvas c, String label, float width, float height)
411 424
      {
......
415 428
      c.drawText(label, width,height-mFontHeight, mPaint);
416 429
      }
417 430

  
418
///////////////////////////////////////////////////////////////////
431
///////////////////////////////////////////////////////////////////////////////////////////////////
419 432

  
420 433
    private void addNewPoint(int x, int y)
421 434
      {
......
442 455
                       synchronized(lock)
443 456
                         {
444 457
                         di1D.add(new Static1D(x));
458
                         mAct.get().setNumRedPoints(len+1);
445 459
                         }
446 460
                       }
447 461
                     break;
......
464 478
                       synchronized(lock)
465 479
                         {
466 480
                         di2D.add(new Static2D(x,y));
481
                         mAct.get().setNumRedPoints(len+1);
467 482
                         }
468 483
                       }
469 484
                     break;
......
497 512
                     { 
498 513
                     synchronized(lock)
499 514
                       {
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
                         }
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);
508 518
                       }
509 519
                     }
510 520
                   break; 
511 521
        }
512 522
      }
513 523
    
514
///////////////////////////////////////////////////////////////////
515
    
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

  
516 547
    @Override
517 548
    public boolean onTouchEvent(MotionEvent event)
518 549
      {
......
551 582
            
552 583
      return true;
553 584
      }
554
 
555
///////////////////////////////////////////////////////////////////
556 585
  }

Also available in: Unified diff