Project

General

Profile

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

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

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 float[] mDrawCoord = new float[3];
73
      
74
///////////////////////////////////////////////////////////////////
75
    
76
    public DynamicSurfaceView(Context c, AttributeSet attrs)
77
      {
78
      super(c, attrs);
79
      
80
      mPaint = new Paint();
81
      mPaint.setStyle(Style.FILL);
82
      mPaint.setAntiAlias(true);
83
      
84
      moving    = -1;
85
      mDuration = 10000;
86
      mPosition = 0;
87
      mNoise    = 0.0f;
88
      
89
      di1D = new Dynamic1D(mDuration,0.5f);
90
      di1D.setNoise(mNoise);
91
      
92
      di2D = new Dynamic2D(mDuration,0.5f);
93
      di2D.setNoise(mNoise);
94
      
95
      di3D = new Dynamic3D(mDuration,0.5f);
96
      di3D.setNoise(mNoise);
97
        
98
      if(!isInEditMode())
99
        {
100
        setFocusable(true);
101
        setFocusableInTouchMode(true);
102
        
103
        setEGLContextClientVersion(2);
104
        
105
        if( Build.FINGERPRINT.startsWith("generic") ) // when running on the emulator, insert a magic line that is
106
          {                                           // supposed to cure the 'no config chosen' crash on emulator startup
107
          setEGLConfigChooser(8, 8, 8, 8, 16, 0);   
108
          }
109
        
110
        mRenderer = new DynamicRenderer(this);
111
        setRenderer(mRenderer);
112
        }
113
      }
114

    
115
///////////////////////////////////////////////////////////////////
116

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

    
132
///////////////////////////////////////////////////////////////////
133

    
134
    public static void setDuration(int duration)
135
      {
136
      mDuration = duration;
137
      
138
      di1D.setDuration(duration);
139
      di2D.setDuration(duration);
140
      di3D.setDuration(duration);
141
      }
142

    
143
///////////////////////////////////////////////////////////////////
144

    
145
    public static void setNoise(float noise)
146
      {
147
      mNoise = noise;
148
      
149
      di1D.setNoise(noise);
150
      di2D.setNoise(noise);
151
      di3D.setNoise(noise);
152
      }
153
    
154
///////////////////////////////////////////////////////////////////
155

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

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

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

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