Project

General

Profile

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

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

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.Dynamic;
32
import org.distorted.library.type.Dynamic1D;
33
import org.distorted.library.type.Dynamic2D;
34
import org.distorted.library.type.Dynamic3D;
35
import org.distorted.library.type.Static1D;
36
import org.distorted.library.type.Static2D;
37
import org.distorted.library.type.Static3D;
38

    
39
///////////////////////////////////////////////////////////////////
40

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

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

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

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

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

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

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

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

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

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

    
104
      di1D.setAccessMode(Dynamic.ACCESS_SEQUENTIAL);
105
      di2D.setAccessMode(Dynamic.ACCESS_SEQUENTIAL);
106
      di3D.setAccessMode(Dynamic.ACCESS_SEQUENTIAL);
107

    
108
      if(!isInEditMode())
109
        {
110
        setFocusable(true);
111
        setFocusableInTouchMode(true);
112
        setEGLContextClientVersion(2);
113
        DynamicRenderer mRenderer = new DynamicRenderer(this);
114
        setRenderer(mRenderer);
115
        }
116
      }
117

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

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

    
124
      mSize1 = mAvg/150;
125
      mSize2 = mAvg/60;
126
      mSizeT = mAvg/30;
127

    
128
      mPaint.setTextSize(mSizeT);
129

    
130
      clearPoints();
131
      }
132

    
133
///////////////////////////////////////////////////////////////////
134

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

    
142
///////////////////////////////////////////////////////////////////
143

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

    
153
///////////////////////////////////////////////////////////////////
154

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

    
161
      p1N.set(mNoise0);
162
      p2N.set(mNoise0,mNoise1);
163
      p3N.set(mNoise0,mNoise1,mNoise2);
164

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

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

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

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

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

    
217
      mLastTime = time;
218
      }
219

    
220
///////////////////////////////////////////////////////////////////
221

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

    
230
///////////////////////////////////////////////////////////////////
231

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

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

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

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

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

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

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

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