Project

General

Profile

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

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

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_INTERPOLATIONS= 100;
48
    private static final int MAX_VECTORS       =   6;
49

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

    
52
    private static int xDown,yDown;
53
    private static int mScrW, mScrH;
54
   
55
    private static Dynamic1D di1D;
56
    private static Dynamic2D di2D;
57
    private static Dynamic3D di3D;
58
    
59
    private static Paint mPaint;
60
    private static int moving;
61
    private static long mTime = 0;
62
    private static int mDuration;
63
    private static float mPosition;
64
    private static float mNoise0, mNoise1, mNoise2;
65
    
66
    private static int currentDim = DIM_2D;
67
    
68
    private static Static1D p1D;
69
    private static Static2D p2D;
70
    private static Static3D p3D;
71

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

    
76
    private static float[] mDrawCoord = new float[3];
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

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

    
102
      if(!isInEditMode())
103
        {
104
        setFocusable(true);
105
        setFocusableInTouchMode(true);
106
        
107
        setEGLContextClientVersion(2);
108
        
109
        if( Build.FINGERPRINT.startsWith("generic") ) // when running on the emulator, insert a magic line that is
110
          {                                           // supposed to cure the 'no config chosen' crash on emulator startup
111
          setEGLConfigChooser(8, 8, 8, 8, 16, 0);   
112
          }
113
        
114
        DynamicRenderer mRenderer = new DynamicRenderer(this);
115
        setRenderer(mRenderer);
116
        }
117
      }
118

    
119
///////////////////////////////////////////////////////////////////
120

    
121
    public static void setMode(int mode)
122
      {
123
      di1D.setMode(mode);  
124
      di2D.setMode(mode);
125
      di3D.setMode(mode);
126
      }
127
      
128
///////////////////////////////////////////////////////////////////
129
    
130
    public static void setScreenSize(int width, int height)
131
      {
132
      mScrW = width;
133
      mScrH = height;
134
      }
135

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

    
138
    public static void setDuration(int duration)
139
      {
140
      mDuration = duration;
141
      
142
      di1D.setDuration(duration);
143
      di2D.setDuration(duration);
144
      di3D.setDuration(duration);
145
      }
146

    
147
///////////////////////////////////////////////////////////////////
148

    
149
    public static void setNoise(float noise0, float noise1, float noise2)
150
      {
151
      mNoise0 = noise0;
152
      mNoise1 = noise1;
153
      mNoise2 = noise2;
154

    
155
      p1N.set(mNoise0);
156
      p2N.set(mNoise0,mNoise1);
157
      p3N.set(mNoise0,mNoise1,mNoise2);
158

    
159
      di1D.setNoise(p1N);
160
      di2D.setNoise(p2N);
161
      di3D.setNoise(p3N);
162
      }
163
    
164
///////////////////////////////////////////////////////////////////
165

    
166
    public static void setDimension(int dim)
167
      {
168
      if( currentDim != dim )
169
        {
170
        if( !(currentDim==DIM_3DXY && dim==DIM_3DXZ) && !(currentDim==DIM_3DXZ && dim==DIM_3DXY) )
171
          {
172
          synchronized(lock)
173
            {
174
            di1D.removeAll();
175
            di2D.removeAll();
176
            di3D.removeAll();
177
            }
178
          }
179
      
180
        currentDim = dim;
181
        }
182
      }
183
    
184
///////////////////////////////////////////////////////////////////
185
    
186
    public static void drawCurve(Canvas c, long time)
187
      {
188
      mPosition += (mTime>0 && mDuration>0) ? ((float)(time-mTime)*NUM_INTERPOLATIONS/mDuration) : 0; 
189
         
190
      synchronized(lock)
191
        {
192
        switch(currentDim)
193
          {
194
          case DIM_1D: drawCurve1D(c,time); break;
195
          case DIM_2D: drawCurve2D(c,time); break;
196
          default    : drawCurve3D(c,time); break;
197
          }
198
        }
199
      
200
      mTime = time;
201
      }
202

    
203
///////////////////////////////////////////////////////////////////
204
      
205
    private static void drawCurve1D(Canvas c, long time)
206
      {
207
      int len = di1D.getNumPoints();   
208
        
209
      mPaint.setColor(0xff000000);
210
      
211
      c.drawLine(0, DynamicRenderer.BHEI/2, DynamicRenderer.BWID, DynamicRenderer.BHEI/2, mPaint);
212
      c.drawText("x", 0.95f* DynamicRenderer.BWID, 0.55f* DynamicRenderer.BHEI , mPaint);
213
      
214
      if( len>=2 )
215
        {
216
        for(int i=0; i<NUM_INTERPOLATIONS; i++) 
217
          {
218
          int color = i<=mPosition ? 0xff - ((int)mPosition                   -i)*0xff/(NUM_INTERPOLATIONS-1)  
219
                                   : 0xff - ((int)mPosition+NUM_INTERPOLATIONS-i)*0xff/(NUM_INTERPOLATIONS-1);
220
         
221
          mPaint.setColor( 0xffffff + ((color&0xff)<<24) ); 
222
          di1D.interpolate(mDrawCoord,0, (float)i/NUM_INTERPOLATIONS);
223
          c.drawCircle(mDrawCoord[0], DynamicRenderer.BHEI/2 , 2.0f, mPaint );
224
          }
225
        }
226
     
227
      mPaint.setColor(0xffff0000);
228
      
229
      for(int curr=0; curr<len; curr++)
230
        {      
231
        p1D = di1D.getPoint(curr);
232
        c.drawCircle(p1D.getX(), DynamicRenderer.BHEI/2 , 5.0f, mPaint);
233
        }   
234
      }
235
    
236
///////////////////////////////////////////////////////////////////
237
      
238
    private static void drawCurve2D(Canvas c, long time)
239
      {
240
      int len = di2D.getNumPoints();   
241
        
242
      mPaint.setColor(0xff000000);
243
      
244
      c.drawLine(0, DynamicRenderer.BHEI/2, DynamicRenderer.BWID, DynamicRenderer.BHEI/2, mPaint);
245
      c.drawLine(DynamicRenderer.BWID/2, 0, DynamicRenderer.BWID/2, DynamicRenderer.BHEI, mPaint);
246
      
247
      c.drawText("x", 0.95f* DynamicRenderer.BWID, 0.55f* DynamicRenderer.BHEI , mPaint);
248
      c.drawText("y", 0.55f* DynamicRenderer.BWID, 0.05f* DynamicRenderer.BHEI , mPaint);
249
      
250
      if( len>=2 )
251
        {
252
        for(int i=0; i<NUM_INTERPOLATIONS; i++) 
253
          {
254
          int color = i<=mPosition ? 0xff - ((int)mPosition                   -i)*0xff/(NUM_INTERPOLATIONS-1)  
255
                                   : 0xff - ((int)mPosition+NUM_INTERPOLATIONS-i)*0xff/(NUM_INTERPOLATIONS-1);
256
         
257
          mPaint.setColor( 0xffffff + ((color&0xff)<<24) ); 
258
          di2D.interpolate(mDrawCoord,0, (float)i/NUM_INTERPOLATIONS);
259
          c.drawCircle(mDrawCoord[0], mDrawCoord[1], 2.0f, mPaint );
260
          }
261
        }
262
     
263
      mPaint.setColor(0xffff0000);
264
      
265
      for(int curr=0; curr<len; curr++)
266
        {      
267
        p2D = di2D.getPoint(curr);
268
        c.drawCircle(p2D.getX(),p2D.getY(), 5.0f, mPaint);
269
        }
270
      }
271

    
272
///////////////////////////////////////////////////////////////////
273
      
274
    private static void drawCurve3D(Canvas c, long time)
275
      {
276
      int len = di3D.getNumPoints();   
277
      
278
      mPaint.setColor(0xff000000);
279
      
280
      c.drawLine(0, DynamicRenderer.BHEI/2, DynamicRenderer.BWID, DynamicRenderer.BHEI/2, mPaint);
281
      c.drawLine(DynamicRenderer.BWID/2, 0, DynamicRenderer.BWID/2, DynamicRenderer.BHEI, mPaint);
282
      
283
      c.drawText("x", 0.95f* DynamicRenderer.BWID, 0.55f* DynamicRenderer.BHEI , mPaint);
284
      c.drawText( currentDim==DIM_3DXY ? "y" : "z", 0.55f* DynamicRenderer.BWID, 0.05f* DynamicRenderer.BHEI , mPaint);
285
      
286
      if( len>=2 )
287
        {
288
        for(int i=0; i<NUM_INTERPOLATIONS; i++) 
289
          {
290
          int color = i<=mPosition ? 0xff - ((int)mPosition                   -i)*0xff/(NUM_INTERPOLATIONS-1)  
291
                                   : 0xff - ((int)mPosition+NUM_INTERPOLATIONS-i)*0xff/(NUM_INTERPOLATIONS-1);
292
         
293
          mPaint.setColor( 0xffffff + ((color&0xff)<<24) ); 
294
          di3D.interpolate(mDrawCoord,0, (float)i/NUM_INTERPOLATIONS);
295
          c.drawCircle(mDrawCoord[0], mDrawCoord[currentDim==DIM_3DXY ? 1:2], 2.0f, mPaint );
296
          }
297
        }
298
     
299
      mPaint.setColor(0xffff0000);
300
      
301
      for(int curr=0; curr<len; curr++)
302
        {      
303
        p3D = di3D.getPoint(curr);
304
        c.drawCircle(p3D.getX(), currentDim==DIM_3DXY ? p3D.getY():p3D.getZ(), 5.0f, mPaint);
305
        }   
306
      }
307
    
308
///////////////////////////////////////////////////////////////////
309

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