Project

General

Profile

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

examples / src / main / java / org / distorted / examples / dynamic / DynamicSurfaceView.java @ 8b7c0ab3

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
      di1D.setNoise(p1N);
98
      
99
      di2D = new Dynamic2D(mDuration,0.5f);
100
      p2N = new Static2D(mNoise0,mNoise1);
101
      di2D.setNoise(p2N);
102
      
103
      di3D = new Dynamic3D(mDuration,0.5f);
104
      p3N = new Static3D(mNoise0,mNoise1,mNoise2);
105
      di3D.setNoise(p3N);
106
        
107
      if(!isInEditMode())
108
        {
109
        setFocusable(true);
110
        setFocusableInTouchMode(true);
111
        
112
        setEGLContextClientVersion(2);
113
        
114
        if( Build.FINGERPRINT.startsWith("generic") ) // when running on the emulator, insert a magic line that is
115
          {                                           // supposed to cure the 'no config chosen' crash on emulator startup
116
          setEGLConfigChooser(8, 8, 8, 8, 16, 0);   
117
          }
118
        
119
        DynamicRenderer mRenderer = new DynamicRenderer(this);
120
        setRenderer(mRenderer);
121
        }
122
      }
123

    
124
///////////////////////////////////////////////////////////////////
125

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

    
141
///////////////////////////////////////////////////////////////////
142

    
143
    public static 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 static 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 static 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
          }
184
      
185
        currentDim = dim;
186
        }
187
      }
188
    
189
///////////////////////////////////////////////////////////////////
190
    
191
    public static void drawCurve(Canvas c, long time)
192
      {
193
      mPosition += (mTime>0 && mDuration>0) ? ((float)(time-mTime)*NUM_INTERPOLATIONS/mDuration) : 0; 
194
         
195
      synchronized(lock)
196
        {
197
        switch(currentDim)
198
          {
199
          case DIM_1D: drawCurve1D(c,time); break;
200
          case DIM_2D: drawCurve2D(c,time); break;
201
          default    : drawCurve3D(c,time); break;
202
          }
203
        }
204
      
205
      mTime = time;
206
      }
207

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

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

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