Project

General

Profile

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

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

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.Dynamic1D;
33
import org.distorted.library.type.Dynamic2D;
34
import org.distorted.library.type.Dynamic3D;
35
import org.distorted.library.type.Dynamic4D;
36
import org.distorted.library.type.Static1D;
37
import org.distorted.library.type.Static2D;
38
import org.distorted.library.type.Static3D;
39
import org.distorted.library.type.Static4D;
40

    
41
///////////////////////////////////////////////////////////////////
42

    
43
public class MovingEffectsSurfaceView extends GLSurfaceView
44
    {
45
    private static final int LOOP_TIME = 5000;
46
    private static final int NUM_INTERPOLATIONS= 100;
47
   
48
    public static final int EFFECT_POINTS=0;
49
    public static final int EFFECT_BUBBLE=1;
50
    public static final int EFFECT_SINK  =2;
51
    public static final int EFFECT_TRANS =3;
52
    public static final int EFFECT_MACRO =4;
53
    public static final int EFFECT_SWIRL =5;
54
   
55
    private static MovingEffectsRenderer mRenderer;
56
    
57
    private static int xDown,yDown;
58
    private static int mScrW, mScrH;
59

    
60
    private static Dynamic1D macroDyn, alphaDyn;
61
    private static Dynamic2D di2D;
62
    private static Dynamic3D di3D;
63
    private static Static4D dr;
64

    
65
    private static Dynamic4D mRegion;
66

    
67
    private static Paint mPaint;
68
    private static int moving;
69
    private static Object lock = new Object();
70
    private static long mTime = 0;
71
    
72
    private static int mCurrEffect;
73
    
74
///////////////////////////////////////////////////////////////////
75
    
76
    public MovingEffectsSurfaceView(Context c, AttributeSet attrs) 
77
      {
78
      super(c, attrs);
79

    
80
      macroDyn = new Dynamic1D();
81
      macroDyn.setDuration(0);
82
      macroDyn.setCount(0.5f);
83
      macroDyn.add(new Static1D(1));
84
      macroDyn.add(new Static1D(3));
85

    
86
      alphaDyn = new Dynamic1D();
87
      alphaDyn.setDuration(0);
88
      alphaDyn.setCount(0.5f);
89
      alphaDyn.add(new Static1D(1.0f));
90
      alphaDyn.add(new Static1D(0.5f));
91

    
92
      mCurrEffect=EFFECT_POINTS;
93
      mPaint = new Paint();
94
      mPaint.setStyle(Style.FILL);
95
      moving = -1;
96
      
97
      di2D = new Dynamic2D();
98
      di2D.setCount(0.0f);
99
      di2D.setDuration(LOOP_TIME);
100
      
101
      di3D = new Dynamic3D();
102
      di3D.add(new Static3D(0,0, 0));
103
      di3D.add(new Static3D(0,0,30));
104
      
105
      dr = new Static4D(0,0,60,60);
106

    
107
      mRegion = new Dynamic4D();
108
      mRegion.setCount(0);
109
      mRegion.setDuration(LOOP_TIME);
110

    
111
      if(!isInEditMode())
112
        {
113
        setFocusable(true);
114
        setFocusableInTouchMode(true);
115
        
116
        setEGLContextClientVersion(2);
117
        
118
        if( Build.FINGERPRINT.startsWith("generic") ) // when running on the emulator, insert a magic line that is
119
          {                                           // supposed to cure the 'no config chosen' crash on emulator startup
120
          setEGLConfigChooser(8, 8, 8, 8, 16, 0);   
121
          }
122
        
123
        mRenderer = new MovingEffectsRenderer(this);
124
        setRenderer(mRenderer);
125
        }
126
      }
127

    
128
///////////////////////////////////////////////////////////////////
129

    
130
    public static void setScreenSize(int width, int height)
131
      {
132
      mScrW = width;
133
      mScrH = height;
134
      }
135

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

    
138
    public static int getCurrentEffect()
139
      {
140
      return mCurrEffect;
141
      }
142
    
143
///////////////////////////////////////////////////////////////////
144
    
145
    public static void Bubble()
146
      {
147
      if( mCurrEffect==EFFECT_BUBBLE ) return;     
148
      
149
      synchronized(lock)
150
        {
151
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
152
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
153
        MovingEffectsRenderer.mBackground.distort(di3D, dr, di2D);
154
        mCurrEffect = EFFECT_BUBBLE;
155
        }
156
      }
157

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

    
160
    public static void Sink()
161
      {
162
      if( mCurrEffect==EFFECT_SINK ) return; 
163
         
164
      synchronized(lock)
165
        {
166
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
167
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
168
        MovingEffectsRenderer.mBackground.sink(10.0f, dr, di2D, 0, 0.5f);
169
        mCurrEffect = EFFECT_SINK;
170
        }
171
      }
172

    
173
///////////////////////////////////////////////////////////////////
174

    
175
    public static void Macroblock()
176
      {
177
      if( mCurrEffect==EFFECT_MACRO ) return;   
178
         
179
      synchronized(lock)
180
        {
181
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
182
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
183
        MovingEffectsRenderer.mBackground.macroblock(macroDyn, mRegion);
184
        mCurrEffect = EFFECT_MACRO;
185
        }
186
      }
187

    
188
///////////////////////////////////////////////////////////////////
189

    
190
    public static void Transparency()
191
      {
192
      if( mCurrEffect==EFFECT_TRANS ) return;   
193
      
194
      synchronized(lock)
195
        {
196
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
197
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
198
        MovingEffectsRenderer.mBackground.alpha(alphaDyn, mRegion, true);
199
        mCurrEffect = EFFECT_TRANS;
200
        }
201
      }
202

    
203
///////////////////////////////////////////////////////////////////
204

    
205
    public static void Swirl()
206
      {
207
      if( mCurrEffect==EFFECT_SWIRL ) return;   
208
      
209
      synchronized(lock)
210
        {
211
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
212
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
213
        MovingEffectsRenderer.mBackground.swirl(30, dr, di2D);
214
        mCurrEffect = EFFECT_SWIRL;
215
        }
216
      }
217
    
218
///////////////////////////////////////////////////////////////////
219

    
220
    public static void Abort()
221
      {
222
      synchronized(lock)
223
        {
224
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
225
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
226
        di2D.removeAll();
227
        mRegion.removeAll();
228
        mCurrEffect = EFFECT_POINTS;
229
        }
230
      }
231

    
232
///////////////////////////////////////////////////////////////////
233
 
234
    public static void drawCurve(Canvas c, long time)
235
      {
236
      synchronized(lock)
237
        {  
238
        int len = di2D.getNumPoints();
239
        float[] drawCoord = new float[2];
240
        Static2D cu;
241
        int lit = mTime> 0 ? (int)((float)(time-mTime)*NUM_INTERPOLATIONS/LOOP_TIME)  : 0; 
242
            
243
        if( len>=2 )
244
          {
245
          for(int i=0; i<NUM_INTERPOLATIONS; i++) 
246
            {
247
            int color = i<=lit ? 0xff - (lit                   -i)*0xff/(NUM_INTERPOLATIONS-1)  
248
                               : 0xff - (lit+NUM_INTERPOLATIONS-i)*0xff/(NUM_INTERPOLATIONS-1);
249
         
250
            mPaint.setColor( 0xffffff + ((color&0xff)<<24) );  
251
            di2D.interpolate( drawCoord, 0, (float)i/NUM_INTERPOLATIONS);
252
            c.drawCircle(drawCoord[0], drawCoord[1], 2.0f, mPaint );
253
            }
254
          }
255
     
256
        mPaint.setColor(0xffff0000);
257
      
258
        for(int curr=0; curr<len; curr++)
259
          {       
260
          cu = di2D.getPoint(curr);
261
          c.drawCircle(cu.getX(), cu.getY(), 5.0f, mPaint);
262
          }
263
        
264
        if( time-mTime > LOOP_TIME ) mTime = time;
265
        }
266
      }
267
       
268
///////////////////////////////////////////////////////////////////
269
    
270
    @Override public boolean onTouchEvent(MotionEvent event) 
271
      {
272
      if( mCurrEffect!=EFFECT_POINTS ) return true;   
273
      
274
      switch(event.getAction())
275
        {
276
        case MotionEvent.ACTION_DOWN: xDown = (int)event.getX()*MovingEffectsRenderer.BWID/mScrW; 
277
                                      yDown = (int)event.getY()*MovingEffectsRenderer.BHEI/mScrH;
278
                                    
279
                                      float gx, gy;
280
                                      Static2D dv;
281
                                      int len = di2D.getNumPoints();
282
                                 
283
                                      for(int g=0; g<len; g++)
284
                                        {
285
                                        dv = di2D.getPoint(g); 
286
                                        gx = dv.getX();
287
                                        gy = dv.getY();
288
                                    
289
                                        if( (xDown-gx)*(xDown-gx) + (yDown-gy)*(yDown-gy) < (MovingEffectsRenderer.BHEI*MovingEffectsRenderer.BHEI)/100 )
290
                                          {
291
                                          moving = g;
292
                                          break;
293
                                          }
294
                                        }
295
                                      if( moving<0 )
296
                                        {
297
                                        synchronized(lock)
298
                                          {
299
                                          di2D.add(new Static2D(xDown,yDown));
300
                                          mRegion.add(new Static4D(xDown,yDown,60,60));
301
                                          }
302
                                        } 
303
                                      break;
304
        case MotionEvent.ACTION_MOVE: if( moving>=0 )
305
                                        {
306
                                        xDown = (int)event.getX()*MovingEffectsRenderer.BWID/mScrW; 
307
                                        yDown = (int)event.getY()*MovingEffectsRenderer.BHEI/mScrH;
308
                                        di2D.setPoint(moving, xDown, yDown);
309
                                        mRegion.setPoint(moving, xDown, yDown, 60, 60);
310
                                        }                           
311
                                      break;
312
        case MotionEvent.ACTION_UP  : moving = -1;
313
                                      break;
314
        }
315
      return true;
316
      }
317
 
318
///////////////////////////////////////////////////////////////////
319
// end of file
320

    
321
  }
(3-3/3)