Project

General

Profile

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

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

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.content.Context;
23
import android.opengl.GLSurfaceView;
24
import android.os.Build;
25
import android.view.MotionEvent;
26
import android.util.AttributeSet;
27
import android.graphics.Canvas;
28
import android.graphics.Paint.Style;
29
import android.graphics.Paint;
30

    
31
import org.distorted.library.type.Dynamic1D;
32
import org.distorted.library.type.Dynamic2D;
33
import org.distorted.library.type.Dynamic3D;
34
import org.distorted.library.type.Static1D;
35
import org.distorted.library.type.Static2D;
36
import org.distorted.library.type.Static3D;
37

    
38
///////////////////////////////////////////////////////////////////
39

    
40
public class DynamicSurfaceView extends GLSurfaceView
41
    {
42
    public static final int DIM_1D   = 0; 
43
    public static final int DIM_2D   = 1; 
44
    public static final int DIM_3DXY = 2; 
45
    public static final int DIM_3DXZ = 3; 
46

    
47
    private static final int NUM_POINTS = 250;
48
    private static final int MAX_POINTS =   6;
49

    
50
    private static final Object lock = new Object();
51

    
52
    private Dynamic1D di1D;
53
    private Dynamic2D di2D;
54
    private Dynamic3D di3D;
55
    
56
    private Paint mPaint;
57
    private int moving;
58
    private int mDuration;
59
    private int mPosition;
60
    private long mDiffTime, mLastTime;
61
    private float mNoise0, mNoise1, mNoise2;
62

    
63
    private int mSize1, mSize2, mSizeT, mAvg;
64

    
65
    private int currentDim= DIM_2D;
66
    
67
    private Static1D p1D;
68
    private Static2D p2D;
69
    private Static3D p3D;
70

    
71
    private Static1D p1N;
72
    private Static2D p2N;
73
    private Static3D p3N;
74

    
75
    private float[] mPoints = new float[3*NUM_POINTS];
76
      
77
///////////////////////////////////////////////////////////////////
78
    
79
    public DynamicSurfaceView(Context c, AttributeSet attrs)
80
      {
81
      super(c, attrs);
82
      
83
      mPaint = new Paint();
84
      mPaint.setStyle(Style.FILL);
85
      mPaint.setAntiAlias(true);
86

    
87
      moving    = -1;
88
      mDuration = 10000;
89
      mPosition = 0;
90
      mNoise0   = 0.0f;
91
      mNoise1   = 0.0f;
92
      mNoise2   = 0.0f;
93
      mDiffTime = -1;
94
      mLastTime = -1;
95

    
96
      di1D = new Dynamic1D(mDuration,0.0f);
97
      p1N  = new Static1D(mNoise0);
98
      di2D = new Dynamic2D(mDuration,0.0f);
99
      p2N  = new Static2D(mNoise0,mNoise1);
100
      di3D = new Dynamic3D(mDuration,0.0f);
101
      p3N  = new Static3D(mNoise0,mNoise1,mNoise2);
102

    
103
      if(!isInEditMode())
104
        {
105
        setFocusable(true);
106
        setFocusableInTouchMode(true);
107
        
108
        setEGLContextClientVersion(2);
109
        
110
        if( Build.FINGERPRINT.startsWith("generic") ) // when running on the emulator, insert a magic line that is
111
          {                                           // supposed to cure the 'no config chosen' crash on emulator startup
112
          setEGLConfigChooser(8, 8, 8, 8, 16, 0);   
113
          }
114
        
115
        DynamicRenderer mRenderer = new DynamicRenderer(this);
116
        setRenderer(mRenderer);
117
        }
118
      }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

    
122
    public void onSurfaceChanged(int width,int height)
123
      {
124
      mAvg = (width+height)/2;
125

    
126
      mSize1 = mAvg/150;
127
      mSize2 = mAvg/60;
128
      mSizeT = mAvg/30;
129

    
130
      mPaint.setTextSize(mSizeT);
131

    
132
      clearPoints();
133
      }
134

    
135
///////////////////////////////////////////////////////////////////
136

    
137
    public void setMode(int mode)
138
      {
139
      di1D.setMode(mode);  
140
      di2D.setMode(mode);
141
      di3D.setMode(mode);
142
      }
143

    
144
///////////////////////////////////////////////////////////////////
145

    
146
    public void setDuration(int duration)
147
      {
148
      mDuration = duration;
149
      
150
      di1D.setDuration(duration);
151
      di2D.setDuration(duration);
152
      di3D.setDuration(duration);
153
      }
154

    
155
///////////////////////////////////////////////////////////////////
156

    
157
    public void setNoise(float noise0, float noise1, float noise2)
158
      {
159
      mNoise0 = noise0;
160
      mNoise1 = noise1;
161
      mNoise2 = noise2;
162

    
163
      p1N.set(mNoise0);
164
      p2N.set(mNoise0,mNoise1);
165
      p3N.set(mNoise0,mNoise1,mNoise2);
166

    
167
      di1D.setNoise(p1N);
168
      di2D.setNoise(p2N);
169
      di3D.setNoise(p3N);
170
      }
171
    
172
///////////////////////////////////////////////////////////////////
173

    
174
    public void setDimension(int dim)
175
      {
176
      if( currentDim != dim )
177
        {
178
        if( !(currentDim==DIM_3DXY && dim==DIM_3DXZ) && !(currentDim==DIM_3DXZ && dim==DIM_3DXY) )
179
          {
180
          synchronized(lock)
181
            {
182
            di1D.removeAll();
183
            di2D.removeAll();
184
            di3D.removeAll();
185

    
186
            clearPoints();
187
            }
188
          }
189
      
190
        currentDim = dim;
191
        }
192
      }
193
    
194
///////////////////////////////////////////////////////////////////
195
    
196
    public void drawCurve(Canvas c, long time)
197
      {
198
      if ( ++mPosition >= NUM_POINTS ) mPosition=0;
199

    
200
      if( mLastTime<0 )
201
        {
202
        mLastTime = time;
203
        }
204
      else
205
        {
206
        mDiffTime = time - mLastTime;
207
        }
208

    
209
      synchronized(lock)
210
        {
211
        switch(currentDim)
212
          {
213
          case DIM_1D: drawCurve1D(c,time); break;
214
          case DIM_2D: drawCurve2D(c,time); break;
215
          default    : drawCurve3D(c,time); break;
216
          }
217
        }
218

    
219
      mLastTime = time;
220
      }
221

    
222
///////////////////////////////////////////////////////////////////
223

    
224
    private void clearPoints()
225
      {
226
      for(int i=0; i<3*NUM_POINTS; i++)
227
         {
228
         mPoints[i] = -10.0f;
229
         }
230
      }
231

    
232
///////////////////////////////////////////////////////////////////
233

    
234
    private void drawCurve1D(Canvas c, long time)
235
      {
236
      int len = di1D.getNumPoints();   
237
      mPaint.setColor(0xff000000);
238
      
239
      c.drawLine(0, DynamicRenderer.texH/2, DynamicRenderer.texW, DynamicRenderer.texH/2, mPaint);
240
      c.drawText("x", 0.95f*DynamicRenderer.texW, DynamicRenderer.texH /2 + mSizeT , mPaint);
241

    
242
      if( len>=2 )
243
        {
244
        di1D.interpolateMain(mPoints,3*mPosition, time, mDiffTime);
245

    
246
        for(int i=0; i<NUM_POINTS; i++)
247
          {
248
          int color = i<=mPosition ? 0xff - (mPosition           -i)*0xff/(NUM_POINTS-1)
249
                                   : 0xff - (mPosition+NUM_POINTS-i)*0xff/(NUM_POINTS-1);
250
         
251
          mPaint.setColor( 0xffffff + ((color&0xff)<<24) ); 
252
          c.drawCircle(mPoints[3*i], DynamicRenderer.texH/2 , mSize1, mPaint );
253
          }
254
        }
255
     
256
      mPaint.setColor(0xffff0000);
257
      
258
      for(int curr=0; curr<len; curr++)
259
        {      
260
        p1D = di1D.getPoint(curr);
261
        c.drawCircle(p1D.getX(), DynamicRenderer.texH/2 , mSize2, mPaint);
262
        }   
263
      }
264
    
265
///////////////////////////////////////////////////////////////////
266
      
267
    private void drawCurve2D(Canvas c, long time)
268
      {
269
      int len = di2D.getNumPoints();   
270
      mPaint.setColor(0xff000000);
271
      
272
      c.drawLine(0, DynamicRenderer.texH/2, DynamicRenderer.texW, DynamicRenderer.texH/2, mPaint);
273
      c.drawLine(DynamicRenderer.texW/2, 0, DynamicRenderer.texW/2, DynamicRenderer.texH, mPaint);
274
      
275
      c.drawText("x", 0.95f* DynamicRenderer.texW    , DynamicRenderer.texH/2+mSizeT , mPaint);
276
      c.drawText("y", DynamicRenderer.texW/2 + mSizeT,                        mSizeT , mPaint);
277
      
278
      if( len>=2 )
279
        {
280
        di2D.interpolateMain(mPoints,3*mPosition, time, mDiffTime);
281

    
282
        for(int i=0; i<NUM_POINTS; i++)
283
          {
284
          int color = i<=mPosition ? 0xff - (mPosition           -i)*0xff/(NUM_POINTS-1)
285
                                   : 0xff - (mPosition+NUM_POINTS-i)*0xff/(NUM_POINTS-1);
286
         
287
          mPaint.setColor( 0xffffff + ((color&0xff)<<24) );
288
          c.drawCircle(mPoints[3*i], mPoints[3*i+1], mSize1, mPaint );
289
          }
290
        }
291
     
292
      mPaint.setColor(0xffff0000);
293
      
294
      for(int curr=0; curr<len; curr++)
295
        {      
296
        p2D = di2D.getPoint(curr);
297
        c.drawCircle(p2D.getX(),p2D.getY(), mSize2, mPaint);
298
        }
299
      }
300

    
301
///////////////////////////////////////////////////////////////////
302
      
303
    private void drawCurve3D(Canvas c, long time)
304
      {
305
      int len = di3D.getNumPoints();   
306
      mPaint.setColor(0xff000000);
307
      
308
      c.drawLine(0, DynamicRenderer.texH/2, DynamicRenderer.texW  , DynamicRenderer.texH/2, mPaint);
309
      c.drawLine(DynamicRenderer.texW/2, 0, DynamicRenderer.texW/2, DynamicRenderer.texH  , mPaint);
310
      
311
      c.drawText( "x"                             , 0.95f* DynamicRenderer.texW    , DynamicRenderer.texH/2 + mSizeT , mPaint);
312
      c.drawText( currentDim==DIM_3DXY ? "y" : "z", DynamicRenderer.texW/2 + mSizeT,                          mSizeT , mPaint);
313
      
314
      if( len>=2 )
315
        {
316
        di3D.interpolateMain(mPoints, 3*mPosition, time, mDiffTime);
317

    
318
        for(int i=0; i<NUM_POINTS; i++)
319
          {
320
          int color = i<=mPosition ? 0xff - (mPosition           -i)*0xff/(NUM_POINTS-1)
321
                                   : 0xff - (mPosition+NUM_POINTS-i)*0xff/(NUM_POINTS-1);
322
         
323
          mPaint.setColor( 0xffffff + ((color&0xff)<<24) ); 
324
          c.drawCircle(mPoints[3*i], mPoints[3*i + (currentDim==DIM_3DXY ? 1:2) ], mSize1, mPaint );
325
          }
326
        }
327
     
328
      mPaint.setColor(0xffff0000);
329
      
330
      for(int curr=0; curr<len; curr++)
331
        {      
332
        p3D = di3D.getPoint(curr);
333
        c.drawCircle(p3D.getX(), currentDim==DIM_3DXY ? p3D.getY():p3D.getZ(), mSize2, mPaint);
334
        }   
335
      }
336
    
337
///////////////////////////////////////////////////////////////////
338

    
339
    private void addNewPoint(int x, int y)
340
      {
341
      float gx,gy,gz;
342
      int len;
343
      
344
      switch(currentDim)
345
        {
346
        case DIM_1D: len = di1D.getNumPoints();
347
                
348
                     for(int g=0; g<len; g++)
349
                       {
350
                       p1D = di1D.getPoint(g);  
351
                       gx = p1D.getX();
352
                                    
353
                       if( (x-gx)*(x-gx) < (mAvg*mAvg/100) )
354
                         {
355
                         moving = g;
356
                         break;
357
                         }
358
                       }
359
                     if( moving<0 )
360
                       {
361
                       synchronized(lock)
362
                         {
363
                         if( len>=MAX_POINTS )
364
                           {
365
                           di1D.removeAll();
366
                           clearPoints();
367
                           }
368
                         di1D.add(new Static1D(x));
369
                         }
370
                       }
371
                     break;
372
        case DIM_2D: len = di2D.getNumPoints();
373
                                 
374
                     for(int g=0; g<len; g++)
375
                       {
376
                       p2D = di2D.getPoint(g);  
377
                       gx = p2D.getX();
378
                       gy = p2D.getY();
379
                                    
380
                       if( (x-gx)*(x-gx) + (y-gy)*(y-gy) < (mAvg*mAvg/100) )
381
                         {
382
                         moving = g;
383
                         break;
384
                         }
385
                       }
386
                     if( moving<0 )
387
                       {
388
                       synchronized(lock)
389
                         {
390
                         if( len>=MAX_POINTS )
391
                           {
392
                           di2D.removeAll();
393
                           clearPoints();
394
                           }
395
                         di2D.add(new Static2D(x,y));
396
                         }
397
                       }
398
                     break;
399
        default    : len = di3D.getNumPoints();
400
                                 
401
                     for(int g=0; g<len; g++)
402
                       {
403
                       p3D = di3D.getPoint(g);  
404
                       gx = p3D.getX();
405
                       gy = p3D.getY();
406
                       gz = p3D.getZ();
407
                               
408
                     if( currentDim==DIM_3DXY )
409
                       {
410
                       if( (x-gx)*(x-gx) + (y-gy)*(y-gy) < (mAvg*mAvg/100) )
411
                         {
412
                         moving = g;
413
                         break;
414
                         }
415
                       }
416
                     if( currentDim==DIM_3DXZ )
417
                       {
418
                       if( (x-gx)*(x-gx) + (y-gz)*(y-gz) < (mAvg*mAvg/100) )
419
                         {
420
                         moving = g;
421
                         break;
422
                         }
423
                       }
424
                     }
425
                   if( moving<0 )
426
                     { 
427
                     synchronized(lock)
428
                       {
429
                       if( len>=MAX_POINTS )
430
                         {
431
                         di3D.removeAll();
432
                         clearPoints();
433
                         }
434
                    
435
                       if( currentDim==DIM_3DXY )
436
                         {
437
                         di3D.add(new Static3D(x,y, DynamicRenderer.texH/2));
438
                         }
439
                       if( currentDim==DIM_3DXZ )
440
                         {
441
                         di3D.add(new Static3D(x, DynamicRenderer.texH/2,y));
442
                         }
443
                       }
444
                     }
445
                   break; 
446
        }
447
      }
448
    
449
///////////////////////////////////////////////////////////////////
450
    
451
    @Override public boolean onTouchEvent(MotionEvent event) 
452
      {
453
      int action = event.getAction();
454
      int xDown, yDown;
455

    
456
      switch(action)
457
        {
458
        case MotionEvent.ACTION_DOWN: xDown = (int)event.getX();
459
                                      yDown = (int)event.getY();
460
                                      
461
                                      addNewPoint(xDown,yDown);
462
                                    
463
                                      break;
464
        case MotionEvent.ACTION_MOVE: if( moving>=0 )
465
                                        {
466
                                        xDown = (int)event.getX();
467
                                        yDown = (int)event.getY();
468
                                        
469
                                        switch(currentDim)
470
                                          {
471
                                          case DIM_1D  : di1D.setPoint(moving, xDown); 
472
                                                         break;
473
                                          case DIM_2D  : di2D.setPoint(moving, xDown, yDown);
474
                                                         break;
475
                                          case DIM_3DXY: di3D.setPoint(moving, xDown, yDown, (int)di3D.getPoint(moving).getZ());
476
                                                         break;
477
                                          case DIM_3DXZ: di3D.setPoint(moving, xDown, (int)di3D.getPoint(moving).getY(), yDown);
478
                                                         break;
479
                                          }
480
                                        }                           
481
                                      break;
482
        case MotionEvent.ACTION_UP  : moving = -1;
483
                                      break;
484
        }
485
            
486
      return true;
487
      }
488
 
489
///////////////////////////////////////////////////////////////////
490
  }
(3-3/3)