Project

General

Profile

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

examples / src / main / java / org / distorted / examples / dynamic / DynamicSurfaceView.java @ 6f3024ae

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.view.MotionEvent;
25
import android.util.AttributeSet;
26
import android.graphics.Canvas;
27
import android.graphics.Paint.Style;
28
import android.graphics.Paint;
29

    
30
import org.distorted.library.type.Dynamic;
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
      di1D.setAccessMode(Dynamic.ACCESS_SEQUENTIAL);
104
      di2D.setAccessMode(Dynamic.ACCESS_SEQUENTIAL);
105
      di3D.setAccessMode(Dynamic.ACCESS_SEQUENTIAL);
106

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

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

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

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

    
127
      mPaint.setTextSize(mSizeT);
128

    
129
      clearPoints();
130
      }
131

    
132
///////////////////////////////////////////////////////////////////
133

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

    
141
///////////////////////////////////////////////////////////////////
142

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

    
152
///////////////////////////////////////////////////////////////////
153

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

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

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

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

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

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

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

    
216
      mLastTime = time;
217
      }
218

    
219
///////////////////////////////////////////////////////////////////
220

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

    
229
///////////////////////////////////////////////////////////////////
230

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

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

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

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

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

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

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

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