Project

General

Profile

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

examples / src / main / java / org / distorted / examples / movingeffects / MovingEffectsSurfaceView.java @ 08eabc44

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.movingeffects;
21

    
22
import android.content.Context;
23
import android.graphics.Canvas;
24
import android.graphics.Paint;
25
import android.graphics.Paint.Style;
26
import android.opengl.GLSurfaceView;
27
import android.os.Build;
28
import android.view.MotionEvent;
29
import android.util.AttributeSet;
30

    
31
import org.distorted.library.EffectTypes;
32
import org.distorted.library.type.Float2D;
33
import org.distorted.library.type.Float3D;
34
import org.distorted.library.type.Float4D;
35
import org.distorted.library.type.Interpolator2D;
36
import org.distorted.library.type.Interpolator3D;
37

    
38
///////////////////////////////////////////////////////////////////
39

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

    
106
///////////////////////////////////////////////////////////////////
107

    
108
    public static void setScreenSize(int width, int height)
109
      {
110
      mScrW = width;
111
      mScrH = height;
112
      }
113

    
114
///////////////////////////////////////////////////////////////////
115

    
116
    public static int getCurrentEffect()
117
      {
118
      return mCurrEffect;
119
      }
120
    
121
///////////////////////////////////////////////////////////////////
122
    
123
    public static void Bubble()
124
      {
125
      if( mCurrEffect==EFFECT_BUBBLE ) return;     
126
      
127
      synchronized(lock)
128
        {
129
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
130
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
131
        MovingEffectsRenderer.mBackground.distort(di3D, dr, di2D);
132
        mCurrEffect = EFFECT_BUBBLE;
133
        }
134
      }
135

    
136
///////////////////////////////////////////////////////////////////
137

    
138
    public static void Sink()
139
      {
140
      if( mCurrEffect==EFFECT_SINK ) return; 
141
         
142
      synchronized(lock)
143
        {
144
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
145
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
146
        MovingEffectsRenderer.mBackground.sink(10.0f, dr, di2D, 0, 0.5f);
147
        mCurrEffect = EFFECT_SINK;
148
        }
149
      }
150

    
151
///////////////////////////////////////////////////////////////////
152

    
153
    public static void Macroblock()
154
      {
155
      if( mCurrEffect==EFFECT_MACRO ) return;   
156
         
157
      synchronized(lock)
158
        {
159
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
160
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
161
        MovingEffectsRenderer.mBackground.macroblock(3, dr, di2D, 0, 0.5f);  
162
        mCurrEffect = EFFECT_MACRO;
163
        }
164
      }
165

    
166
///////////////////////////////////////////////////////////////////
167

    
168
    public static void Transparency()
169
      {
170
      if( mCurrEffect==EFFECT_TRANS ) return;   
171
      
172
      synchronized(lock)
173
        {
174
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
175
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
176
        MovingEffectsRenderer.mBackground.smooth_alpha(0.5f, dr, di2D, 0, 0.5f);  
177
        mCurrEffect = EFFECT_TRANS;
178
        }
179
      }
180

    
181
///////////////////////////////////////////////////////////////////
182

    
183
    public static void Swirl()
184
      {
185
      if( mCurrEffect==EFFECT_SWIRL ) return;   
186
      
187
      synchronized(lock)
188
        {
189
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
190
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
191
        MovingEffectsRenderer.mBackground.swirl(30, dr, di2D);
192
        mCurrEffect = EFFECT_SWIRL;
193
        }
194
      }
195
    
196
///////////////////////////////////////////////////////////////////
197

    
198
    public static void Abort()
199
      {
200
      synchronized(lock)
201
        {
202
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
203
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
204
        di2D.removeAll();
205
        mCurrEffect = EFFECT_POINTS;
206
        }
207
      }
208

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

    
296
  }
(3-3/3)