Project

General

Profile

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

examples / src / main / java / org / distorted / examples / movingeffects / MovingEffectsSurfaceView.java @ 5068fa06

1
package org.distorted.examples.movingeffects;
2

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

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

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

    
21
public class MovingEffectsSurfaceView extends GLSurfaceView
22
    {
23
    private static final int LOOP_TIME = 5000;
24
    private static final int NUM_INTERPOLATIONS= 100;
25
   
26
    public static final int EFFECT_POINTS=0;
27
    public static final int EFFECT_BUBBLE=1;
28
    public static final int EFFECT_SINK  =2;
29
    public static final int EFFECT_TRANS =3;
30
    public static final int EFFECT_MACRO =4;
31
    public static final int EFFECT_SWIRL =5;
32
   
33
    private static MovingEffectsRenderer mRenderer;
34
    
35
    private static int xDown,yDown;
36
    private static int mScrW, mScrH;
37
    
38
    private static Interpolator2D di2D;
39
    private static Interpolator3D di3D;
40
    private static Float4D dr;
41
    
42
    private static Paint mPaint;
43
    private static int moving;
44
    private static Object lock = new Object();
45
    private static long mTime = 0;
46
    
47
    private static int mCurrEffect;
48
    
49
///////////////////////////////////////////////////////////////////
50
    
51
    public MovingEffectsSurfaceView(Context c, AttributeSet attrs) 
52
      {
53
      super(c, attrs);
54
          
55
      mCurrEffect=EFFECT_POINTS;
56
      mPaint = new Paint();
57
      mPaint.setStyle(Style.FILL);
58
      moving = -1;
59
      
60
      di2D = new Interpolator2D();
61
      di2D.setCount(0.0f);
62
      di2D.setDuration(LOOP_TIME);
63
      
64
      di3D = new Interpolator3D();
65
      di3D.add(new Float3D(0,0, 0));
66
      di3D.add(new Float3D(0,0,30));
67
      
68
      dr = new Float4D(0,0,60,60);
69
      
70
      if(!isInEditMode())
71
        {
72
        setFocusable(true);
73
        setFocusableInTouchMode(true);
74
        
75
        setEGLContextClientVersion(2);
76
        
77
        if( Build.FINGERPRINT.startsWith("generic") ) // when running on the emulator, insert a magic line that is
78
          {                                           // supposed to cure the 'no config chosen' crash on emulator startup
79
          setEGLConfigChooser(8, 8, 8, 8, 16, 0);   
80
          }
81
        
82
        mRenderer = new MovingEffectsRenderer(this);
83
        setRenderer(mRenderer);
84
        }
85
      }
86

    
87
///////////////////////////////////////////////////////////////////
88

    
89
    public static void setScreenSize(int width, int height)
90
      {
91
      mScrW = width;
92
      mScrH = height;
93
      }
94

    
95
///////////////////////////////////////////////////////////////////
96

    
97
    public static int getCurrentEffect()
98
      {
99
      return mCurrEffect;
100
      }
101
    
102
///////////////////////////////////////////////////////////////////
103
    
104
    public static void Bubble()
105
      {
106
      if( mCurrEffect==EFFECT_BUBBLE ) return;     
107
      
108
      synchronized(lock)
109
        {
110
        MovingEffectsRenderer.mBackground.abortAllEffects(Distorted.TYPE_FRAG|Distorted.TYPE_VERT);
111
        MovingEffectsRenderer.mBackground.distort(di3D, dr, di2D);
112
        mCurrEffect = EFFECT_BUBBLE;
113
        }
114
      }
115

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

    
118
    public static void Sink()
119
      {
120
      if( mCurrEffect==EFFECT_SINK ) return; 
121
         
122
      synchronized(lock)
123
        {
124
        MovingEffectsRenderer.mBackground.abortAllEffects(Distorted.TYPE_FRAG|Distorted.TYPE_VERT);
125
        MovingEffectsRenderer.mBackground.sink(10.0f, dr, di2D, 0, 0.5f);  
126
        mCurrEffect = EFFECT_SINK;
127
        }
128
      }
129

    
130
///////////////////////////////////////////////////////////////////
131

    
132
    public static void Macroblock()
133
      {
134
      if( mCurrEffect==EFFECT_MACRO ) return;   
135
         
136
      synchronized(lock)
137
        {
138
        MovingEffectsRenderer.mBackground.abortAllEffects(Distorted.TYPE_FRAG|Distorted.TYPE_VERT);
139
        MovingEffectsRenderer.mBackground.macroblock(3, dr, di2D, 0, 0.5f);  
140
        mCurrEffect = EFFECT_MACRO;
141
        }
142
      }
143

    
144
///////////////////////////////////////////////////////////////////
145

    
146
    public static void Transparency()
147
      {
148
      if( mCurrEffect==EFFECT_TRANS ) return;   
149
      
150
      synchronized(lock)
151
        {
152
        MovingEffectsRenderer.mBackground.abortAllEffects(Distorted.TYPE_FRAG|Distorted.TYPE_VERT);
153
        MovingEffectsRenderer.mBackground.smooth_alpha(0.5f, dr, di2D, 0, 0.5f);  
154
        mCurrEffect = EFFECT_TRANS;
155
        }
156
      }
157

    
158
///////////////////////////////////////////////////////////////////
159

    
160
    public static void Swirl()
161
      {
162
      if( mCurrEffect==EFFECT_SWIRL ) return;   
163
      
164
      synchronized(lock)
165
        {
166
        MovingEffectsRenderer.mBackground.abortAllEffects(Distorted.TYPE_FRAG|Distorted.TYPE_VERT);
167
        MovingEffectsRenderer.mBackground.swirl(30, dr, di2D, 0, 0.5f);  
168
        mCurrEffect = EFFECT_SWIRL;
169
        }
170
      }
171
    
172
///////////////////////////////////////////////////////////////////
173

    
174
    public static void Abort()
175
      {
176
      synchronized(lock)
177
        {
178
        MovingEffectsRenderer.mBackground.abortAllEffects(Distorted.TYPE_FRAG|Distorted.TYPE_VERT);  
179
        di2D.removeAll();
180
        mCurrEffect = EFFECT_POINTS;
181
        }
182
      }
183

    
184
///////////////////////////////////////////////////////////////////
185
 
186
    public static void drawCurve(Canvas c, long time)
187
      {
188
      synchronized(lock)
189
        {  
190
        int len = di2D.getNumPoints();
191
        float[] drawCoord = new float[2];
192
        Float2D cu;
193
        int lit = mTime> 0 ? (int)((float)(time-mTime)*NUM_INTERPOLATIONS/LOOP_TIME)  : 0; 
194
            
195
        if( len>=2 )
196
          {
197
          for(int i=0; i<NUM_INTERPOLATIONS; i++) 
198
            {
199
            int color = i<=lit ? 0xff - (lit                   -i)*0xff/(NUM_INTERPOLATIONS-1)  
200
                               : 0xff - (lit+NUM_INTERPOLATIONS-i)*0xff/(NUM_INTERPOLATIONS-1);
201
         
202
            mPaint.setColor( 0xffffff + ((color&0xff)<<24) );  
203
            di2D.interpolate( drawCoord, 0, (float)i/NUM_INTERPOLATIONS);
204
            c.drawCircle(drawCoord[0], drawCoord[1], 2.0f, mPaint );
205
            }
206
          }
207
     
208
        mPaint.setColor(0xffff0000);
209
      
210
        for(int curr=0; curr<len; curr++)
211
          {       
212
          cu = di2D.getPoint(curr);
213
          c.drawCircle(cu.getX(), cu.getY(), 5.0f, mPaint);
214
          }
215
        
216
        if( time-mTime > LOOP_TIME ) mTime = time;
217
        }
218
      }
219
       
220
///////////////////////////////////////////////////////////////////
221
    
222
    @Override public boolean onTouchEvent(MotionEvent event) 
223
      {
224
      if( mCurrEffect!=EFFECT_POINTS ) return true;   
225
      
226
      switch(event.getAction())
227
        {
228
        case MotionEvent.ACTION_DOWN: xDown = (int)event.getX()*MovingEffectsRenderer.BWID/mScrW; 
229
                                      yDown = (int)event.getY()*MovingEffectsRenderer.BHEI/mScrH;
230
                                    
231
                                      float gx, gy;
232
                                      Float2D dv;
233
                                      int len = di2D.getNumPoints();
234
                                 
235
                                      for(int g=0; g<len; g++)
236
                                        {
237
                                        dv = di2D.getPoint(g); 
238
                                        gx = dv.getX();
239
                                        gy = dv.getY();
240
                                    
241
                                        if( (xDown-gx)*(xDown-gx) + (yDown-gy)*(yDown-gy) < (MovingEffectsRenderer.BHEI*MovingEffectsRenderer.BHEI)/100 )
242
                                          {
243
                                          moving = g;
244
                                          break;
245
                                          }
246
                                        }
247
                                      if( moving<0 )
248
                                        {
249
                                        synchronized(lock)
250
                                          {
251
                                          di2D.add(new Float2D(xDown,yDown)); 
252
                                          }
253
                                        } 
254
                                      break;
255
        case MotionEvent.ACTION_MOVE: if( moving>=0 )
256
                                        {
257
                                        xDown = (int)event.getX()*MovingEffectsRenderer.BWID/mScrW; 
258
                                        yDown = (int)event.getY()*MovingEffectsRenderer.BHEI/mScrH;
259
                                        di2D.setPoint(moving, xDown, yDown);
260
                                        }                           
261
                                      break;
262
        case MotionEvent.ACTION_UP  : moving = -1;
263
                                      break;
264
        }
265
      return true;
266
      }
267
 
268
///////////////////////////////////////////////////////////////////
269
// end of file
270

    
271
  }
(3-3/3)