Project

General

Profile

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

examples / src / main / java / org / distorted / examples / interpolator / InterpolatorSurfaceView.java @ 5068fa06

1
package org.distorted.examples.interpolator;
2

    
3
import android.content.Context;
4
import android.opengl.GLSurfaceView;
5
import android.os.Build;
6
import android.view.MotionEvent;
7
import android.util.AttributeSet;
8
import android.graphics.Canvas;
9
import android.graphics.Paint.Style;
10
import android.graphics.Paint;
11

    
12
import org.distorted.library.Interpolator1D;
13
import org.distorted.library.Interpolator2D;
14
import org.distorted.library.Interpolator3D;
15
import org.distorted.library.Float1D;
16
import org.distorted.library.Float2D;
17
import org.distorted.library.Float3D;
18

    
19
///////////////////////////////////////////////////////////////////
20

    
21
public class InterpolatorSurfaceView extends GLSurfaceView
22
    {
23
    public static final int DIM_1D   = 0; 
24
    public static final int DIM_2D   = 1; 
25
    public static final int DIM_3DXY = 2; 
26
    public static final int DIM_3DXZ = 3; 
27
   
28
    private static final int NUM_INTERPOLATIONS= 100;
29
    private static final int MAX_VECTORS       =   6;
30
   
31
    private InterpolatorRenderer mRenderer;
32
    private static int xDown,yDown;
33
    private static int mScrW, mScrH;
34
   
35
    private static Interpolator1D di1D;
36
    private static Interpolator2D di2D;
37
    private static Interpolator3D di3D;
38
    
39
    private static Paint mPaint;
40
    private static int moving;
41
    private static Object lock = new Object();
42
    private static long mTime = 0;
43
    private static int mDuration;
44
    private static float mPosition;
45
    private static float mNoise;
46
    
47
    private static int currentDim = DIM_2D;
48
    
49
    private static Float1D p1D;
50
    private static Float2D p2D;
51
    private static Float3D p3D;
52
    
53
    private static float[] mDrawCoord = new float[3];
54
      
55
///////////////////////////////////////////////////////////////////
56
    
57
    public InterpolatorSurfaceView(Context c, AttributeSet attrs) 
58
      {
59
      super(c, attrs);
60
      
61
      mPaint = new Paint();
62
      mPaint.setStyle(Style.FILL);
63
      mPaint.setAntiAlias(true);
64
      
65
      moving    = -1;
66
      mDuration = 10000;
67
      mPosition = 0;
68
      mNoise    = 0.0f;
69
      
70
      di1D = new Interpolator1D();
71
      di1D.setDuration(mDuration);
72
      di1D.setNoise(mNoise);
73
      
74
      di2D = new Interpolator2D();
75
      di2D.setDuration(mDuration);
76
      di2D.setNoise(mNoise);
77
      
78
      di3D = new Interpolator3D();
79
      di3D.setDuration(mDuration);
80
      di3D.setNoise(mNoise);
81
        
82
      if(!isInEditMode())
83
        {
84
        setFocusable(true);
85
        setFocusableInTouchMode(true);
86
        
87
        setEGLContextClientVersion(2);
88
        
89
        if( Build.FINGERPRINT.startsWith("generic") ) // when running on the emulator, insert a magic line that is
90
          {                                           // supposed to cure the 'no config chosen' crash on emulator startup
91
          setEGLConfigChooser(8, 8, 8, 8, 16, 0);   
92
          }
93
        
94
        mRenderer = new InterpolatorRenderer(this);
95
        setRenderer(mRenderer);
96
        }
97
      }
98

    
99
///////////////////////////////////////////////////////////////////
100

    
101
    public static void setMode(int mode)
102
      {
103
      di1D.setMode(mode);  
104
      di2D.setMode(mode);
105
      di3D.setMode(mode);
106
      }
107
      
108
///////////////////////////////////////////////////////////////////
109
    
110
    public static void setScreenSize(int width, int height)
111
      {
112
      mScrW = width;
113
      mScrH = height;
114
      }
115

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

    
118
    public static void setDuration(int duration)
119
      {
120
      mDuration = duration;
121
      
122
      di1D.setDuration(duration);
123
      di2D.setDuration(duration);
124
      di3D.setDuration(duration);
125
      }
126

    
127
///////////////////////////////////////////////////////////////////
128

    
129
    public static void setNoise(float noise)
130
      {
131
      mNoise = noise;
132
      
133
      di1D.setNoise(noise);
134
      di2D.setNoise(noise);
135
      di3D.setNoise(noise);
136
      }
137
    
138
///////////////////////////////////////////////////////////////////
139

    
140
    public static void setDimension(int dim)
141
      {
142
      if( currentDim != dim )
143
        {
144
        if( !(currentDim==DIM_3DXY && dim==DIM_3DXZ) && !(currentDim==DIM_3DXZ && dim==DIM_3DXY) )
145
          {
146
          synchronized(lock)
147
            {
148
            di1D.removeAll();
149
            di2D.removeAll();
150
            di3D.removeAll();
151
            }
152
          }
153
      
154
        currentDim = dim;
155
        }
156
      }
157
    
158
///////////////////////////////////////////////////////////////////
159
    
160
    public static void drawCurve(Canvas c, long time)
161
      {
162
      mPosition += (mTime>0 && mDuration>0) ? ((float)(time-mTime)*NUM_INTERPOLATIONS/mDuration) : 0; 
163
         
164
      synchronized(lock)
165
        {
166
        switch(currentDim)
167
          {
168
          case DIM_1D: drawCurve1D(c,time); break;
169
          case DIM_2D: drawCurve2D(c,time); break;
170
          default    : drawCurve3D(c,time); break;
171
          }
172
        }
173
      
174
      mTime = time;
175
      }
176

    
177
///////////////////////////////////////////////////////////////////
178
      
179
    private static void drawCurve1D(Canvas c, long time)
180
      {
181
      int len = di1D.getNumPoints();   
182
        
183
      mPaint.setColor(0xff000000);
184
      
185
      c.drawLine(0, InterpolatorRenderer.BHEI/2, InterpolatorRenderer.BWID, InterpolatorRenderer.BHEI/2, mPaint);
186
      c.drawText("x", 0.95f*InterpolatorRenderer.BWID, 0.55f*InterpolatorRenderer.BHEI , mPaint);
187
      
188
      if( len>=2 )
189
        {
190
        for(int i=0; i<NUM_INTERPOLATIONS; i++) 
191
          {
192
          int color = i<=mPosition ? 0xff - ((int)mPosition                   -i)*0xff/(NUM_INTERPOLATIONS-1)  
193
                                   : 0xff - ((int)mPosition+NUM_INTERPOLATIONS-i)*0xff/(NUM_INTERPOLATIONS-1);
194
         
195
          mPaint.setColor( 0xffffff + ((color&0xff)<<24) ); 
196
          di1D.interpolate(mDrawCoord,0, (float)i/NUM_INTERPOLATIONS);
197
          c.drawCircle(mDrawCoord[0], InterpolatorRenderer.BHEI/2 , 2.0f, mPaint );
198
          }
199
        }
200
     
201
      mPaint.setColor(0xffff0000);
202
      
203
      for(int curr=0; curr<len; curr++)
204
        {      
205
        p1D = di1D.getPoint(curr);
206
        c.drawCircle(p1D.getX(), InterpolatorRenderer.BHEI/2 , 5.0f, mPaint);
207
        }   
208
      }
209
    
210
///////////////////////////////////////////////////////////////////
211
      
212
    private static void drawCurve2D(Canvas c, long time)
213
      {
214
      int len = di2D.getNumPoints();   
215
        
216
      mPaint.setColor(0xff000000);
217
      
218
      c.drawLine(0, InterpolatorRenderer.BHEI/2, InterpolatorRenderer.BWID, InterpolatorRenderer.BHEI/2, mPaint);
219
      c.drawLine(InterpolatorRenderer.BWID/2, 0, InterpolatorRenderer.BWID/2, InterpolatorRenderer.BHEI, mPaint);
220
      
221
      c.drawText("x", 0.95f*InterpolatorRenderer.BWID, 0.55f*InterpolatorRenderer.BHEI , mPaint);
222
      c.drawText("y", 0.55f*InterpolatorRenderer.BWID, 0.05f*InterpolatorRenderer.BHEI , mPaint);
223
      
224
      if( len>=2 )
225
        {
226
        for(int i=0; i<NUM_INTERPOLATIONS; i++) 
227
          {
228
          int color = i<=mPosition ? 0xff - ((int)mPosition                   -i)*0xff/(NUM_INTERPOLATIONS-1)  
229
                                   : 0xff - ((int)mPosition+NUM_INTERPOLATIONS-i)*0xff/(NUM_INTERPOLATIONS-1);
230
         
231
          mPaint.setColor( 0xffffff + ((color&0xff)<<24) ); 
232
          di2D.interpolate(mDrawCoord,0, (float)i/NUM_INTERPOLATIONS);
233
          c.drawCircle(mDrawCoord[0], mDrawCoord[1], 2.0f, mPaint );
234
          }
235
        }
236
     
237
      mPaint.setColor(0xffff0000);
238
      
239
      for(int curr=0; curr<len; curr++)
240
        {      
241
        p2D = di2D.getPoint(curr);
242
        c.drawCircle(p2D.getX(),p2D.getY(), 5.0f, mPaint);
243
        }
244
      }
245

    
246
///////////////////////////////////////////////////////////////////
247
      
248
    private static void drawCurve3D(Canvas c, long time)
249
      {
250
      int len = di3D.getNumPoints();   
251
      
252
      mPaint.setColor(0xff000000);
253
      
254
      c.drawLine(0, InterpolatorRenderer.BHEI/2, InterpolatorRenderer.BWID, InterpolatorRenderer.BHEI/2, mPaint);
255
      c.drawLine(InterpolatorRenderer.BWID/2, 0, InterpolatorRenderer.BWID/2, InterpolatorRenderer.BHEI, mPaint);
256
      
257
      c.drawText("x", 0.95f*InterpolatorRenderer.BWID, 0.55f*InterpolatorRenderer.BHEI , mPaint);
258
      c.drawText( currentDim==DIM_3DXY ? "y" : "z", 0.55f*InterpolatorRenderer.BWID, 0.05f*InterpolatorRenderer.BHEI , mPaint);
259
      
260
      if( len>=2 )
261
        {
262
        for(int i=0; i<NUM_INTERPOLATIONS; i++) 
263
          {
264
          int color = i<=mPosition ? 0xff - ((int)mPosition                   -i)*0xff/(NUM_INTERPOLATIONS-1)  
265
                                   : 0xff - ((int)mPosition+NUM_INTERPOLATIONS-i)*0xff/(NUM_INTERPOLATIONS-1);
266
         
267
          mPaint.setColor( 0xffffff + ((color&0xff)<<24) ); 
268
          di3D.interpolate(mDrawCoord,0, (float)i/NUM_INTERPOLATIONS);
269
          c.drawCircle(mDrawCoord[0], mDrawCoord[currentDim==DIM_3DXY ? 1:2], 2.0f, mPaint );
270
          }
271
        }
272
     
273
      mPaint.setColor(0xffff0000);
274
      
275
      for(int curr=0; curr<len; curr++)
276
        {      
277
        p3D = di3D.getPoint(curr);
278
        c.drawCircle(p3D.getX(), currentDim==DIM_3DXY ? p3D.getY():p3D.getZ(), 5.0f, mPaint);
279
        }   
280
      }
281
    
282
///////////////////////////////////////////////////////////////////
283

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