Project

General

Profile

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

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

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 float mNoise0, mNoise1, mNoise2;
61

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

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

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

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

    
86
      moving    = -1;
87
      mDuration = 10000;
88
      mPosition = 0;
89
      mNoise0   = 0.0f;
90
      mNoise1   = 0.0f;
91
      mNoise2   = 0.0f;
92

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

    
100
      if(!isInEditMode())
101
        {
102
        setFocusable(true);
103
        setFocusableInTouchMode(true);
104
        
105
        setEGLContextClientVersion(2);
106
        
107
        if( Build.FINGERPRINT.startsWith("generic") ) // when running on the emulator, insert a magic line that is
108
          {                                           // supposed to cure the 'no config chosen' crash on emulator startup
109
          setEGLConfigChooser(8, 8, 8, 8, 16, 0);   
110
          }
111
        
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
      synchronized(lock)
198
        {
199
        switch(currentDim)
200
          {
201
          case DIM_1D: drawCurve1D(c,time); break;
202
          case DIM_2D: drawCurve2D(c,time); break;
203
          default    : drawCurve3D(c,time); break;
204
          }
205
        }
206
      }
207

    
208
///////////////////////////////////////////////////////////////////
209

    
210
    private void clearPoints()
211
      {
212
      for(int i=0; i<3*NUM_POINTS; i++)
213
         {
214
         mPoints[i] = -10.0f;
215
         }
216
      }
217

    
218
///////////////////////////////////////////////////////////////////
219

    
220
    private void drawCurve1D(Canvas c, long time)
221
      {
222
      int len = di1D.getNumPoints();   
223
      mPaint.setColor(0xff000000);
224
      
225
      c.drawLine(0, DynamicRenderer.texH/2, DynamicRenderer.texW, DynamicRenderer.texH/2, mPaint);
226
      c.drawText("x", 0.95f*DynamicRenderer.texW, DynamicRenderer.texH /2 + mSizeT , mPaint);
227

    
228
      if( len>=2 )
229
        {
230
        di1D.interpolateMain(mPoints,3*mPosition, time);
231

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

    
268
        for(int i=0; i<NUM_POINTS; i++)
269
          {
270
          int color = i<=mPosition ? 0xff - (mPosition           -i)*0xff/(NUM_POINTS-1)
271
                                   : 0xff - (mPosition+NUM_POINTS-i)*0xff/(NUM_POINTS-1);
272
         
273
          mPaint.setColor( 0xffffff + ((color&0xff)<<24) );
274
          c.drawCircle(mPoints[3*i], mPoints[3*i+1], mSize1, mPaint );
275
          }
276
        }
277
     
278
      mPaint.setColor(0xffff0000);
279
      
280
      for(int curr=0; curr<len; curr++)
281
        {      
282
        p2D = di2D.getPoint(curr);
283
        c.drawCircle(p2D.getX(),p2D.getY(), mSize2, mPaint);
284
        }
285
      }
286

    
287
///////////////////////////////////////////////////////////////////
288
      
289
    private void drawCurve3D(Canvas c, long time)
290
      {
291
      int len = di3D.getNumPoints();   
292
      mPaint.setColor(0xff000000);
293
      
294
      c.drawLine(0, DynamicRenderer.texH/2, DynamicRenderer.texW  , DynamicRenderer.texH/2, mPaint);
295
      c.drawLine(DynamicRenderer.texW/2, 0, DynamicRenderer.texW/2, DynamicRenderer.texH  , mPaint);
296
      
297
      c.drawText( "x"                             , 0.95f* DynamicRenderer.texW    , DynamicRenderer.texH/2 + mSizeT , mPaint);
298
      c.drawText( currentDim==DIM_3DXY ? "y" : "z", DynamicRenderer.texW/2 + mSizeT,                          mSizeT , mPaint);
299
      
300
      if( len>=2 )
301
        {
302
        di3D.interpolateMain(mPoints, 3*mPosition, time);
303

    
304
        for(int i=0; i<NUM_POINTS; i++)
305
          {
306
          int color = i<=mPosition ? 0xff - (mPosition           -i)*0xff/(NUM_POINTS-1)
307
                                   : 0xff - (mPosition+NUM_POINTS-i)*0xff/(NUM_POINTS-1);
308
         
309
          mPaint.setColor( 0xffffff + ((color&0xff)<<24) ); 
310
          c.drawCircle(mPoints[3*i], mPoints[3*i + (currentDim==DIM_3DXY ? 1:2) ], mSize1, mPaint );
311
          }
312
        }
313
     
314
      mPaint.setColor(0xffff0000);
315
      
316
      for(int curr=0; curr<len; curr++)
317
        {      
318
        p3D = di3D.getPoint(curr);
319
        c.drawCircle(p3D.getX(), currentDim==DIM_3DXY ? p3D.getY():p3D.getZ(), mSize2, mPaint);
320
        }   
321
      }
322
    
323
///////////////////////////////////////////////////////////////////
324

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

    
442
      switch(action)
443
        {
444
        case MotionEvent.ACTION_DOWN: xDown = (int)event.getX();
445
                                      yDown = (int)event.getY();
446
                                      
447
                                      addNewPoint(xDown,yDown);
448
                                    
449
                                      break;
450
        case MotionEvent.ACTION_MOVE: if( moving>=0 )
451
                                        {
452
                                        xDown = (int)event.getX();
453
                                        yDown = (int)event.getY();
454
                                        
455
                                        switch(currentDim)
456
                                          {
457
                                          case DIM_1D  : di1D.setPoint(moving, xDown); 
458
                                                         break;
459
                                          case DIM_2D  : di2D.setPoint(moving, xDown, yDown);
460
                                                         break;
461
                                          case DIM_3DXY: di3D.setPoint(moving, xDown, yDown, (int)di3D.getPoint(moving).getZ());
462
                                                         break;
463
                                          case DIM_3DXZ: di3D.setPoint(moving, xDown, (int)di3D.getPoint(moving).getY(), yDown);
464
                                                         break;
465
                                          }
466
                                        }                           
467
                                      break;
468
        case MotionEvent.ACTION_UP  : moving = -1;
469
                                      break;
470
        }
471
            
472
      return true;
473
      }
474
 
475
///////////////////////////////////////////////////////////////////
476
  }
(3-3/3)