Project

General

Profile

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

examples / src / main / java / org / distorted / examples / dynamic / DynamicSurfaceView.java @ 5e3cd4e1

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 context, AttributeSet attrs)
80
      {
81
      super(context, 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
        DynamicRenderer mRenderer = new DynamicRenderer(this);
112
        setRenderer(mRenderer);
113
        }
114
      }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

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

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

    
126
      mPaint.setTextSize(mSizeT);
127

    
128
      clearPoints();
129
      }
130

    
131
///////////////////////////////////////////////////////////////////
132

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

    
140
///////////////////////////////////////////////////////////////////
141

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

    
151
///////////////////////////////////////////////////////////////////
152

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

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

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

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

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

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

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

    
215
      mLastTime = time;
216
      }
217

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

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

    
228
///////////////////////////////////////////////////////////////////
229

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

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

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

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

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

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

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

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