Project

General

Profile

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

examples / src / main / java / org / distorted / examples / movingeffects / MovingEffectsSurfaceView.java @ a8c3ada7

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.EffectTypes;
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.abortEffects(EffectTypes.VERTEX);
111
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
112
        MovingEffectsRenderer.mBackground.distort(di3D, dr, di2D);
113
        mCurrEffect = EFFECT_BUBBLE;
114
        }
115
      }
116

    
117
///////////////////////////////////////////////////////////////////
118

    
119
    public static void Sink()
120
      {
121
      if( mCurrEffect==EFFECT_SINK ) return; 
122
         
123
      synchronized(lock)
124
        {
125
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
126
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
127
        MovingEffectsRenderer.mBackground.sink(10.0f, dr, di2D, 0, 0.5f);
128
        mCurrEffect = EFFECT_SINK;
129
        }
130
      }
131

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

    
134
    public static void Macroblock()
135
      {
136
      if( mCurrEffect==EFFECT_MACRO ) return;   
137
         
138
      synchronized(lock)
139
        {
140
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
141
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
142
        MovingEffectsRenderer.mBackground.macroblock(3, dr, di2D, 0, 0.5f);  
143
        mCurrEffect = EFFECT_MACRO;
144
        }
145
      }
146

    
147
///////////////////////////////////////////////////////////////////
148

    
149
    public static void Transparency()
150
      {
151
      if( mCurrEffect==EFFECT_TRANS ) return;   
152
      
153
      synchronized(lock)
154
        {
155
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
156
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
157
        MovingEffectsRenderer.mBackground.smooth_alpha(0.5f, dr, di2D, 0, 0.5f);  
158
        mCurrEffect = EFFECT_TRANS;
159
        }
160
      }
161

    
162
///////////////////////////////////////////////////////////////////
163

    
164
    public static void Swirl()
165
      {
166
      if( mCurrEffect==EFFECT_SWIRL ) return;   
167
      
168
      synchronized(lock)
169
        {
170
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
171
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
172
        MovingEffectsRenderer.mBackground.swirl(30, dr, di2D, 0, 0.5f);  
173
        mCurrEffect = EFFECT_SWIRL;
174
        }
175
      }
176
    
177
///////////////////////////////////////////////////////////////////
178

    
179
    public static void Abort()
180
      {
181
      synchronized(lock)
182
        {
183
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
184
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
185
        di2D.removeAll();
186
        mCurrEffect = EFFECT_POINTS;
187
        }
188
      }
189

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

    
277
  }
(3-3/3)