Project

General

Profile

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

examples / src / main / java / org / distorted / examples / dynamic / DynamicSurfaceView.java @ 9ff0c8c3

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 DynamicRenderer mRenderer;
51
    private static int xDown,yDown;
52
    private static int mScrW, mScrH;
53
   
54
    private static Dynamic1D di1D;
55
    private static Dynamic2D di2D;
56
    private static Dynamic3D di3D;
57
    
58
    private static Paint mPaint;
59
    private static int moving;
60
    private static Object lock = new Object();
61
    private static long mTime = 0;
62
    private static int mDuration;
63
    private static float mPosition;
64
    private static float mNoise;
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
      mNoise    = 0.0f;
92
      
93
      di1D = new Dynamic1D(mDuration,0.5f);
94
      p1N = new Static1D(mNoise);
95
      di1D.setNoise(p1N);
96
      
97
      di2D = new Dynamic2D(mDuration,0.5f);
98
      p2N = new Static2D(mNoise,mNoise);
99
      di2D.setNoise(p2N);
100
      
101
      di3D = new Dynamic3D(mDuration,0.5f);
102
      p3N = new Static3D(mNoise,mNoise,mNoise);
103
      di3D.setNoise(p3N);
104
        
105
      if(!isInEditMode())
106
        {
107
        setFocusable(true);
108
        setFocusableInTouchMode(true);
109
        
110
        setEGLContextClientVersion(2);
111
        
112
        if( Build.FINGERPRINT.startsWith("generic") ) // when running on the emulator, insert a magic line that is
113
          {                                           // supposed to cure the 'no config chosen' crash on emulator startup
114
          setEGLConfigChooser(8, 8, 8, 8, 16, 0);   
115
          }
116
        
117
        mRenderer = new DynamicRenderer(this);
118
        setRenderer(mRenderer);
119
        }
120
      }
121

    
122
///////////////////////////////////////////////////////////////////
123

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

    
139
///////////////////////////////////////////////////////////////////
140

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

    
150
///////////////////////////////////////////////////////////////////
151

    
152
    public static void setNoise(float noise)
153
      {
154
      mNoise = noise;
155

    
156
      p1N.set(mNoise);
157
      p2N.set(mNoise,mNoise);
158
      p3N.set(mNoise,mNoise,mNoise);
159

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

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

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

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

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