Project

General

Profile

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

examples / src / main / java / org / distorted / examples / dynamic / DynamicSurfaceView.java @ 107e4b72

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

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

    
40
///////////////////////////////////////////////////////////////////
41

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

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

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

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

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

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

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

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

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

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

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

    
109
      if(!isInEditMode())
110
        {
111
        setFocusable(true);
112
        setFocusableInTouchMode(true);
113
        DynamicRenderer mRenderer = new DynamicRenderer(this);
114
        final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
115
        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
116
        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
117
        setRenderer(mRenderer);
118
        }
119
      }
120

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122

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

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

    
131
      mPaint.setTextSize(mSizeT);
132

    
133
      clearPoints();
134
      }
135

    
136
///////////////////////////////////////////////////////////////////
137

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

    
145
///////////////////////////////////////////////////////////////////
146

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

    
156
///////////////////////////////////////////////////////////////////
157

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

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

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

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

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

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

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

    
220
      mLastTime = time;
221
      }
222

    
223
///////////////////////////////////////////////////////////////////
224

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

    
233
///////////////////////////////////////////////////////////////////
234

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

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

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

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

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

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

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

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