Project

General

Profile

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

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

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 bc0a685b Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20 f988589e Leszek Koltunski
package org.distorted.examples.dynamic;
21 427ab7bf Leszek Koltunski
22 e4330c89 Leszek Koltunski
import android.app.ActivityManager;
23 427ab7bf Leszek Koltunski
import android.content.Context;
24 e4330c89 Leszek Koltunski
import android.content.pm.ConfigurationInfo;
25 2666a48c Leszek Koltunski
import android.graphics.Rect;
26 427ab7bf Leszek Koltunski
import android.opengl.GLSurfaceView;
27
import android.view.MotionEvent;
28
import android.util.AttributeSet;
29
import android.graphics.Canvas;
30
import android.graphics.Paint.Style;
31
import android.graphics.Paint;
32
33 27e12007 Leszek Koltunski
import org.distorted.library.type.Dynamic;
34 7589635e Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
35
import org.distorted.library.type.Dynamic2D;
36
import org.distorted.library.type.Dynamic3D;
37
import org.distorted.library.type.Static1D;
38
import org.distorted.library.type.Static2D;
39
import org.distorted.library.type.Static3D;
40 427ab7bf Leszek Koltunski
41 e52efe17 Leszek Koltunski
import java.lang.ref.WeakReference;
42
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44 427ab7bf Leszek Koltunski
45 f988589e Leszek Koltunski
public class DynamicSurfaceView extends GLSurfaceView
46 427ab7bf Leszek Koltunski
    {
47
    public static final int DIM_1D   = 0; 
48
    public static final int DIM_2D   = 1; 
49
    public static final int DIM_3DXY = 2; 
50
    public static final int DIM_3DXZ = 3; 
51 8b7c0ab3 Leszek Koltunski
52 e52efe17 Leszek Koltunski
    static final int NUM_POINTS = 250;
53 8b7c0ab3 Leszek Koltunski
    private static final Object lock = new Object();
54
55 e52efe17 Leszek Koltunski
    private WeakReference<DynamicActivity> mAct;
56
57 2666a48c Leszek Koltunski
    private static int halfScreenHeight=0;
58
    private static int halfScreenWidth =0;
59
60 fe3c72ce Leszek Koltunski
    private Dynamic1D di1D;
61
    private Dynamic2D di2D;
62
    private Dynamic3D di3D;
63 427ab7bf Leszek Koltunski
    
64 fe3c72ce Leszek Koltunski
    private Paint mPaint;
65 2666a48c Leszek Koltunski
    private int mMoving;
66 fe3c72ce Leszek Koltunski
    private int mDuration;
67
    private int mPosition;
68 2666a48c Leszek Koltunski
    private long mDiffTime, mLastTime, mStartTime;
69 fe3c72ce Leszek Koltunski
    private float mNoise0, mNoise1, mNoise2;
70 2666a48c Leszek Koltunski
    private float mCount;
71 508f22b5 Leszek Koltunski
72 fe3c72ce Leszek Koltunski
    private int mSize1, mSize2, mSizeT, mAvg;
73 2666a48c Leszek Koltunski
    private float mFontHeight;
74 508f22b5 Leszek Koltunski
75 fe3c72ce Leszek Koltunski
    private int currentDim= DIM_2D;
76 427ab7bf Leszek Koltunski
    
77 fe3c72ce Leszek Koltunski
    private Static1D p1D;
78
    private Static2D p2D;
79
    private Static3D p3D;
80 9ff0c8c3 Leszek Koltunski
81 fe3c72ce Leszek Koltunski
    private Static1D p1N;
82
    private Static2D p2N;
83
    private Static3D p3N;
84 9ff0c8c3 Leszek Koltunski
85 fe3c72ce Leszek Koltunski
    private float[] mPoints = new float[3*NUM_POINTS];
86 2666a48c Leszek Koltunski
    private boolean mRunning;
87
88 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
89 427ab7bf Leszek Koltunski
    
90 41a81a14 Leszek Koltunski
    public DynamicSurfaceView(Context context, AttributeSet attrs)
91 427ab7bf Leszek Koltunski
      {
92 41a81a14 Leszek Koltunski
      super(context, attrs);
93 e52efe17 Leszek Koltunski
94
      DynamicActivity act = (DynamicActivity)context;
95
      mAct = new WeakReference<>(act);
96
97 427ab7bf Leszek Koltunski
      mPaint = new Paint();
98
      mPaint.setStyle(Style.FILL);
99
      mPaint.setAntiAlias(true);
100 97dadfe5 Leszek Koltunski
101 2666a48c Leszek Koltunski
      mMoving = -1;
102 427ab7bf Leszek Koltunski
      mDuration = 10000;
103 2666a48c Leszek Koltunski
      mCount    = 0.0f;
104 427ab7bf Leszek Koltunski
      mPosition = 0;
105 8b7c0ab3 Leszek Koltunski
      mNoise0   = 0.0f;
106
      mNoise1   = 0.0f;
107
      mNoise2   = 0.0f;
108 b041d424 Leszek Koltunski
      mDiffTime = -1;
109
      mLastTime = -1;
110 2666a48c Leszek Koltunski
      mStartTime= -1;
111
112
      mRunning = false;
113 8b7c0ab3 Leszek Koltunski
114 e52efe17 Leszek Koltunski
      clearPoints();
115
116 2666a48c Leszek Koltunski
      di1D = new Dynamic1D(mDuration,mCount);
117 bf36cb6e Leszek Koltunski
      p1N  = new Static1D(mNoise0);
118 2666a48c Leszek Koltunski
      di2D = new Dynamic2D(mDuration,mCount);
119 bf36cb6e Leszek Koltunski
      p2N  = new Static2D(mNoise0,mNoise1);
120 2666a48c Leszek Koltunski
      di3D = new Dynamic3D(mDuration,mCount);
121 bf36cb6e Leszek Koltunski
      p3N  = new Static3D(mNoise0,mNoise1,mNoise2);
122
123 8db5b725 Leszek Koltunski
      di1D.setAccessType(Dynamic.ACCESS_TYPE_SEQUENTIAL);
124
      di2D.setAccessType(Dynamic.ACCESS_TYPE_SEQUENTIAL);
125
      di3D.setAccessType(Dynamic.ACCESS_TYPE_SEQUENTIAL);
126 27e12007 Leszek Koltunski
127 427ab7bf Leszek Koltunski
      if(!isInEditMode())
128
        {
129
        setFocusable(true);
130
        setFocusableInTouchMode(true);
131 8b7c0ab3 Leszek Koltunski
        DynamicRenderer mRenderer = new DynamicRenderer(this);
132 e4330c89 Leszek Koltunski
        final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
133
        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
134
        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
135 427ab7bf Leszek Koltunski
        setRenderer(mRenderer);
136
        }
137
      }
138
139 508f22b5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
140
141 fe3c72ce Leszek Koltunski
    public void onSurfaceChanged(int width,int height)
142 508f22b5 Leszek Koltunski
      {
143
      mAvg = (width+height)/2;
144
145
      mSize1 = mAvg/150;
146 2666a48c Leszek Koltunski
      mSize2 = mAvg/50;
147 508f22b5 Leszek Koltunski
      mSizeT = mAvg/30;
148
149
      mPaint.setTextSize(mSizeT);
150 2666a48c Leszek Koltunski
      mPaint.setTextAlign(Paint.Align.CENTER);
151
152
      final Rect textBounds = new Rect();
153
      String text = "1";
154
      mPaint.getTextBounds(text, 0, text.length(), textBounds);
155
      mFontHeight = textBounds.exactCenterY();
156 508f22b5 Leszek Koltunski
157
      clearPoints();
158
      }
159
160 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
161 2666a48c Leszek Koltunski
162
    public static void setHalfWidth(int hw)
163
      {
164
      halfScreenWidth = hw;
165
      }
166
167 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
168 2666a48c Leszek Koltunski
169
    public static void setHalfHeight(int hh)
170
      {
171
      halfScreenHeight = hh;
172
      }
173
174 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
175 427ab7bf Leszek Koltunski
176 fe3c72ce Leszek Koltunski
    public void setMode(int mode)
177 427ab7bf Leszek Koltunski
      {
178
      di1D.setMode(mode);  
179
      di2D.setMode(mode);
180
      di3D.setMode(mode);
181
      }
182
183 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
184 427ab7bf Leszek Koltunski
185 fe3c72ce Leszek Koltunski
    public void setDuration(int duration)
186 427ab7bf Leszek Koltunski
      {
187
      mDuration = duration;
188
      
189 2666a48c Leszek Koltunski
      di1D.setDuration(duration);
190
      di2D.setDuration(duration);
191
      di3D.setDuration(duration);
192
      }
193
194 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
195 2666a48c Leszek Koltunski
196
    public void setCount(float count)
197
      {
198
      mCount = count;
199
200
      di1D.setCount(count);
201
      di2D.setCount(count);
202
      di3D.setCount(count);
203 427ab7bf Leszek Koltunski
      }
204
205 dea555b9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
206
207
    public void setConvexity(float convexity)
208
      {
209
      di1D.setConvexity(convexity);
210
      di2D.setConvexity(convexity);
211
      di3D.setConvexity(convexity);
212
      }
213
214 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
215 427ab7bf Leszek Koltunski
216 fe3c72ce Leszek Koltunski
    public void setNoise(float noise0, float noise1, float noise2)
217 427ab7bf Leszek Koltunski
      {
218 8b7c0ab3 Leszek Koltunski
      mNoise0 = noise0;
219
      mNoise1 = noise1;
220
      mNoise2 = noise2;
221 9ff0c8c3 Leszek Koltunski
222 8b7c0ab3 Leszek Koltunski
      p1N.set(mNoise0);
223
      p2N.set(mNoise0,mNoise1);
224
      p3N.set(mNoise0,mNoise1,mNoise2);
225 9ff0c8c3 Leszek Koltunski
226
      di1D.setNoise(p1N);
227
      di2D.setNoise(p2N);
228
      di3D.setNoise(p3N);
229 427ab7bf Leszek Koltunski
      }
230
    
231 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
232 427ab7bf Leszek Koltunski
233 fe3c72ce Leszek Koltunski
    public void setDimension(int dim)
234 427ab7bf Leszek Koltunski
      {
235
      if( currentDim != dim )
236
        {
237
        if( !(currentDim==DIM_3DXY && dim==DIM_3DXZ) && !(currentDim==DIM_3DXZ && dim==DIM_3DXY) )
238
          {
239 2666a48c Leszek Koltunski
          resetPoints();
240 427ab7bf Leszek Koltunski
          }
241 2666a48c Leszek Koltunski
242 427ab7bf Leszek Koltunski
        currentDim = dim;
243
        }
244
      }
245
    
246 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
247 427ab7bf Leszek Koltunski
    
248 fe3c72ce Leszek Koltunski
    public void drawCurve(Canvas c, long time)
249 427ab7bf Leszek Koltunski
      {
250 b041d424 Leszek Koltunski
      if( mLastTime<0 )
251
        {
252
        mLastTime = time;
253
        }
254
      else
255
        {
256
        mDiffTime = time - mLastTime;
257
        }
258
259 427ab7bf Leszek Koltunski
      synchronized(lock)
260
        {
261
        switch(currentDim)
262
          {
263 2666a48c Leszek Koltunski
          case DIM_1D: drawHorizontalAxis(c,"x");
264
                       drawPath(c,di1D,1,time);
265
                       drawRedPoints1D(c);
266
                       break;
267
          case DIM_2D: drawHorizontalAxis(c,"x");
268
                       drawVerticalAxis  (c,"y");
269
                       drawPath(c,di2D,1,time);
270
                       drawRedPoints2D(c);
271
                       break;
272
          default    : drawHorizontalAxis(c,"x");
273
                       drawVerticalAxis  (c, currentDim==DIM_3DXY ? "y" : "z" );
274
                       drawPath(c,di3D,(currentDim==DIM_3DXY ? 1:2),time);
275
                       drawRedPoints3D(c);
276
                       break;
277 427ab7bf Leszek Koltunski
          }
278
        }
279 b041d424 Leszek Koltunski
280
      mLastTime = time;
281 427ab7bf Leszek Koltunski
      }
282
283 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
284 508f22b5 Leszek Koltunski
285 fe3c72ce Leszek Koltunski
    private void clearPoints()
286 508f22b5 Leszek Koltunski
      {
287
      for(int i=0; i<3*NUM_POINTS; i++)
288
         {
289 e52efe17 Leszek Koltunski
         mPoints[i] = -100000.0f;
290 508f22b5 Leszek Koltunski
         }
291
      }
292
293 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
294 508f22b5 Leszek Koltunski
295 2666a48c Leszek Koltunski
    public void resetPoints()
296 427ab7bf Leszek Koltunski
      {
297 2666a48c Leszek Koltunski
      synchronized(lock)
298 427ab7bf Leszek Koltunski
        {
299 2666a48c Leszek Koltunski
        clearPoints();
300 97dadfe5 Leszek Koltunski
301 2666a48c Leszek Koltunski
        switch(currentDim)
302 427ab7bf Leszek Koltunski
          {
303 2666a48c Leszek Koltunski
          case DIM_1D  : di1D.removeAll(); break;
304
          case DIM_2D  : di2D.removeAll(); break;
305
          case DIM_3DXY:
306
          case DIM_3DXZ: di3D.removeAll(); break;
307 427ab7bf Leszek Koltunski
          }
308 e52efe17 Leszek Koltunski
309 d84f5b73 Leszek Koltunski
        DynamicActivity act = mAct.get();
310
        act.setNumRedPoints(0);
311
        act.clearPoints();
312 427ab7bf Leszek Koltunski
        }
313
      }
314 2666a48c Leszek Koltunski
315 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
316 2666a48c Leszek Koltunski
317
    public void startDynamic()
318
      {
319
      mRunning = true;
320
      mLastTime= -1;
321
      mStartTime = System.currentTimeMillis();
322
323
      clearPoints();
324
      di1D.resetToBeginning();
325
      di2D.resetToBeginning();
326
      di3D.resetToBeginning();
327
      }
328
329 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
330 2666a48c Leszek Koltunski
331
    public void stopDynamic()
332
      {
333
      mRunning = false;
334
      }
335
336 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
337 2666a48c Leszek Koltunski
338
    private void drawHorizontalAxis(Canvas c, String label)
339 427ab7bf Leszek Koltunski
      {
340
      mPaint.setColor(0xff000000);
341 2666a48c Leszek Koltunski
342
      c.drawLine(0, halfScreenHeight, halfScreenWidth*2, halfScreenHeight, mPaint);
343
      c.drawText( label, 0.95f*halfScreenWidth*2, halfScreenHeight + mSizeT , mPaint);
344
      }
345
346
347 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
348 2666a48c Leszek Koltunski
349
    private void drawVerticalAxis(Canvas c, String label)
350
      {
351
      mPaint.setColor(0xff000000);
352
353
      c.drawLine(halfScreenWidth, 0, halfScreenWidth, halfScreenHeight*2, mPaint);
354
      c.drawText(label, halfScreenWidth + mSizeT,                mSizeT , mPaint);
355
      }
356
357 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
358 2666a48c Leszek Koltunski
359
    private void drawPath(Canvas c, Dynamic dyn, int index, long time)
360
      {
361
      int len = dyn.getNumPoints();
362
363 427ab7bf Leszek Koltunski
      if( len>=2 )
364
        {
365 2666a48c Leszek Koltunski
        if( mRunning )
366
          {
367
          if ( ++mPosition >= NUM_POINTS ) mPosition=0;
368
369
          if( dyn.getDimension()==1 )
370
            {
371
            mPoints[3*mPosition+index] = halfScreenHeight;
372
            }
373
374
          if( dyn.get(mPoints,3*mPosition, time-mStartTime, mDiffTime) )
375
            {
376
            stopDynamic();
377
            }
378 e52efe17 Leszek Koltunski
379
          addNewSpeedPoint(time);
380 2666a48c Leszek Koltunski
          }
381 97dadfe5 Leszek Koltunski
382
        for(int i=0; i<NUM_POINTS; i++)
383 427ab7bf Leszek Koltunski
          {
384 97dadfe5 Leszek Koltunski
          int color = i<=mPosition ? 0xff - (mPosition           -i)*0xff/(NUM_POINTS-1)
385
                                   : 0xff - (mPosition+NUM_POINTS-i)*0xff/(NUM_POINTS-1);
386 2666a48c Leszek Koltunski
387 97dadfe5 Leszek Koltunski
          mPaint.setColor( 0xffffff + ((color&0xff)<<24) );
388 2666a48c Leszek Koltunski
          c.drawCircle(mPoints[3*i], mPoints[3*i+index] , mSize1, mPaint );
389 427ab7bf Leszek Koltunski
          }
390
        }
391 2666a48c Leszek Koltunski
      }
392
393 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
394 2666a48c Leszek Koltunski
395
    private void drawRedPoints1D(Canvas c)
396
      {
397
      int len = di1D.getNumPoints();
398
399 427ab7bf Leszek Koltunski
      for(int curr=0; curr<len; curr++)
400 2666a48c Leszek Koltunski
        {
401
        p1D = di1D.getPoint(curr);
402
        drawRedPoint(c,curr+"", p1D.get1(), halfScreenHeight);
403 427ab7bf Leszek Koltunski
        }
404
      }
405
406 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
407 2666a48c Leszek Koltunski
408
    private void drawRedPoints2D(Canvas c)
409 427ab7bf Leszek Koltunski
      {
410 2666a48c Leszek Koltunski
      int len = di2D.getNumPoints();
411 97dadfe5 Leszek Koltunski
412 2666a48c Leszek Koltunski
      for(int curr=0; curr<len; curr++)
413
        {
414
        p2D = di2D.getPoint(curr);
415
        drawRedPoint(c,curr+"", p2D.get1(), p2D.get2());
416 427ab7bf Leszek Koltunski
        }
417 2666a48c Leszek Koltunski
      }
418
419 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
420 2666a48c Leszek Koltunski
421
    private void drawRedPoints3D(Canvas c)
422
      {
423
      int len = di3D.getNumPoints();
424
425 427ab7bf Leszek Koltunski
      for(int curr=0; curr<len; curr++)
426 2666a48c Leszek Koltunski
        {
427 427ab7bf Leszek Koltunski
        p3D = di3D.getPoint(curr);
428 2666a48c Leszek Koltunski
        drawRedPoint(c,curr+"", p3D.get1(), currentDim==DIM_3DXY ? p3D.get2():p3D.get3());
429
        }
430 427ab7bf Leszek Koltunski
      }
431 2666a48c Leszek Koltunski
432 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
433 2666a48c Leszek Koltunski
434
    private void drawRedPoint(Canvas c, String label, float width, float height)
435
      {
436
      mPaint.setColor(0xffff0000);
437
      c.drawCircle( width, height, mSize2, mPaint);
438
      mPaint.setColor(0xffffffff);
439
      c.drawText(label, width,height-mFontHeight, mPaint);
440
      }
441
442 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
443 427ab7bf Leszek Koltunski
444
    private void addNewPoint(int x, int y)
445
      {
446
      float gx,gy,gz;
447
      int len;
448 dea555b9 Leszek Koltunski
449 427ab7bf Leszek Koltunski
      switch(currentDim)
450
        {
451
        case DIM_1D: len = di1D.getNumPoints();
452 dea555b9 Leszek Koltunski
453 427ab7bf Leszek Koltunski
                     for(int g=0; g<len; g++)
454
                       {
455
                       p1D = di1D.getPoint(g);  
456 e3eab072 Leszek Koltunski
                       gx = p1D.get1();
457 427ab7bf Leszek Koltunski
                                    
458 508f22b5 Leszek Koltunski
                       if( (x-gx)*(x-gx) < (mAvg*mAvg/100) )
459 427ab7bf Leszek Koltunski
                         {
460 2666a48c Leszek Koltunski
                         mMoving = g;
461 427ab7bf Leszek Koltunski
                         break;
462
                         }
463
                       }
464 2666a48c Leszek Koltunski
                     if( mMoving <0 )
465 427ab7bf Leszek Koltunski
                       {
466
                       synchronized(lock)
467
                         {
468 97dadfe5 Leszek Koltunski
                         di1D.add(new Static1D(x));
469 e52efe17 Leszek Koltunski
                         mAct.get().setNumRedPoints(len+1);
470 427ab7bf Leszek Koltunski
                         }
471
                       }
472
                     break;
473
        case DIM_2D: len = di2D.getNumPoints();
474
                                 
475
                     for(int g=0; g<len; g++)
476
                       {
477
                       p2D = di2D.getPoint(g);  
478 e3eab072 Leszek Koltunski
                       gx = p2D.get1();
479
                       gy = p2D.get2();
480 427ab7bf Leszek Koltunski
                                    
481 508f22b5 Leszek Koltunski
                       if( (x-gx)*(x-gx) + (y-gy)*(y-gy) < (mAvg*mAvg/100) )
482 427ab7bf Leszek Koltunski
                         {
483 2666a48c Leszek Koltunski
                         mMoving = g;
484 427ab7bf Leszek Koltunski
                         break;
485
                         }
486
                       }
487 2666a48c Leszek Koltunski
                     if( mMoving <0 )
488 427ab7bf Leszek Koltunski
                       {
489
                       synchronized(lock)
490
                         {
491 97dadfe5 Leszek Koltunski
                         di2D.add(new Static2D(x,y));
492 e52efe17 Leszek Koltunski
                         mAct.get().setNumRedPoints(len+1);
493 427ab7bf Leszek Koltunski
                         }
494
                       }
495
                     break;
496
        default    : len = di3D.getNumPoints();
497
                                 
498
                     for(int g=0; g<len; g++)
499
                       {
500
                       p3D = di3D.getPoint(g);  
501 e3eab072 Leszek Koltunski
                       gx = p3D.get1();
502
                       gy = p3D.get2();
503
                       gz = p3D.get3();
504 427ab7bf Leszek Koltunski
                               
505
                     if( currentDim==DIM_3DXY )
506
                       {
507 508f22b5 Leszek Koltunski
                       if( (x-gx)*(x-gx) + (y-gy)*(y-gy) < (mAvg*mAvg/100) )
508 427ab7bf Leszek Koltunski
                         {
509 2666a48c Leszek Koltunski
                         mMoving = g;
510 427ab7bf Leszek Koltunski
                         break;
511
                         }
512
                       }
513
                     if( currentDim==DIM_3DXZ )
514
                       {
515 508f22b5 Leszek Koltunski
                       if( (x-gx)*(x-gx) + (y-gz)*(y-gz) < (mAvg*mAvg/100) )
516 427ab7bf Leszek Koltunski
                         {
517 2666a48c Leszek Koltunski
                         mMoving = g;
518 427ab7bf Leszek Koltunski
                         break;
519
                         }
520
                       }
521
                     }
522 2666a48c Leszek Koltunski
                   if( mMoving <0 )
523 427ab7bf Leszek Koltunski
                     { 
524
                     synchronized(lock)
525
                       {
526 e52efe17 Leszek Koltunski
                       if( currentDim==DIM_3DXY ) di3D.add(new Static3D(x,y, halfScreenHeight));
527
                       if( currentDim==DIM_3DXZ ) di3D.add(new Static3D(x, halfScreenHeight,y));
528
                       mAct.get().setNumRedPoints(len+1);
529 427ab7bf Leszek Koltunski
                       }
530
                     }
531
                   break; 
532
        }
533
      }
534
    
535 e52efe17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
536
537
    private void addNewSpeedPoint(long time)
538
      {
539
      int prev = mPosition-1;
540
      if( prev<0 ) prev = NUM_POINTS-1;
541
542
      float xdiff = mPoints[3*prev  ]-mPoints[3*mPosition  ];
543
      float ydiff = mPoints[3*prev+1]-mPoints[3*mPosition+1];
544
      float zdiff = mPoints[3*prev+2]-mPoints[3*mPosition+2];
545
546
      float dist = (float)Math.sqrt( xdiff*xdiff + ydiff*ydiff + zdiff*zdiff );
547
      float speed= mDiffTime<=0 ? 0: dist / mDiffTime;
548
      float timepoint = ((float)(time-mStartTime))/mDuration;
549
550
      if( dist<1000.0f )   // otherwise this is a very first call; do not send it!
551
        {
552
        mAct.get().addPoint( timepoint - (int)timepoint, speed );
553
        }
554
      }
555
556
///////////////////////////////////////////////////////////////////////////////////////////////////
557
558 2666a48c Leszek Koltunski
    @Override
559
    public boolean onTouchEvent(MotionEvent event)
560 427ab7bf Leszek Koltunski
      {
561
      int action = event.getAction();
562 97dadfe5 Leszek Koltunski
      int xDown, yDown;
563
564 427ab7bf Leszek Koltunski
      switch(action)
565
        {
566 97dadfe5 Leszek Koltunski
        case MotionEvent.ACTION_DOWN: xDown = (int)event.getX();
567
                                      yDown = (int)event.getY();
568 427ab7bf Leszek Koltunski
                                      
569
                                      addNewPoint(xDown,yDown);
570
                                    
571
                                      break;
572 2666a48c Leszek Koltunski
        case MotionEvent.ACTION_MOVE: if( mMoving >=0 )
573 427ab7bf Leszek Koltunski
                                        {
574 97dadfe5 Leszek Koltunski
                                        xDown = (int)event.getX();
575
                                        yDown = (int)event.getY();
576 427ab7bf Leszek Koltunski
                                        
577
                                        switch(currentDim)
578
                                          {
579 2666a48c Leszek Koltunski
                                          case DIM_1D  : di1D.setPoint(mMoving, xDown);
580 427ab7bf Leszek Koltunski
                                                         break;
581 2666a48c Leszek Koltunski
                                          case DIM_2D  : di2D.setPoint(mMoving, xDown, yDown);
582 427ab7bf Leszek Koltunski
                                                         break;
583 2666a48c Leszek Koltunski
                                          case DIM_3DXY: di3D.setPoint(mMoving, xDown, yDown, (int)di3D.getPoint(mMoving).get3());
584 427ab7bf Leszek Koltunski
                                                         break;
585 2666a48c Leszek Koltunski
                                          case DIM_3DXZ: di3D.setPoint(mMoving, xDown, (int)di3D.getPoint(mMoving).get2(), yDown);
586 427ab7bf Leszek Koltunski
                                                         break;
587
                                          }
588
                                        }                           
589
                                      break;
590 2666a48c Leszek Koltunski
        case MotionEvent.ACTION_UP  : mMoving = -1;
591 427ab7bf Leszek Koltunski
                                      break;
592
        }
593
            
594
      return true;
595
      }
596
  }