Project

General

Profile

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

examples / src / main / java / org / distorted / examples / movingeffects / MovingEffectsSurfaceView.java @ 7bf107f7

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 Dynamic2D di2D;
61
    private static Static4D dr;
62
    private static Dynamic4D mRegion;
63

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

    
77
      mCurrEffect=EFFECT_POINTS;
78
      mPaint = new Paint();
79
      mPaint.setStyle(Style.FILL);
80
      moving = -1;
81
      
82
      di2D = new Dynamic2D();
83
      di2D.setCount(0.0f);
84
      di2D.setDuration(LOOP_TIME);
85

    
86
      dr = new Static4D(0,0,60,60);
87

    
88
      mRegion = new Dynamic4D();
89
      mRegion.setCount(0);
90
      mRegion.setDuration(LOOP_TIME);
91

    
92
      if(!isInEditMode())
93
        {
94
        setFocusable(true);
95
        setFocusableInTouchMode(true);
96
        
97
        setEGLContextClientVersion(2);
98
        
99
        if( Build.FINGERPRINT.startsWith("generic") ) // when running on the emulator, insert a magic line that is
100
          {                                           // supposed to cure the 'no config chosen' crash on emulator startup
101
          setEGLConfigChooser(8, 8, 8, 8, 16, 0);   
102
          }
103
        
104
        mRenderer = new MovingEffectsRenderer(this);
105
        setRenderer(mRenderer);
106
        }
107
      }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

    
111
    public static void setScreenSize(int width, int height)
112
      {
113
      mScrW = width;
114
      mScrH = height;
115
      }
116

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

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

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

    
141
    public static void Sink()
142
      {
143
      if( mCurrEffect==EFFECT_SINK ) return; 
144
         
145
      synchronized(lock)
146
        {
147
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
148
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
149
        MovingEffectsRenderer.mBackground.sink(new Static1D(10), di2D, dr);
150
        mCurrEffect = EFFECT_SINK;
151
        }
152
      }
153

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155

    
156
    public static void Macroblock()
157
      {
158
      if( mCurrEffect==EFFECT_MACRO ) return;   
159
         
160
      synchronized(lock)
161
        {
162
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
163
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
164
        MovingEffectsRenderer.mBackground.macroblock(new Static1D(3), mRegion);
165
        mCurrEffect = EFFECT_MACRO;
166
        }
167
      }
168

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170

    
171
    public static void Transparency()
172
      {
173
      if( mCurrEffect==EFFECT_TRANS ) return;   
174
      
175
      synchronized(lock)
176
        {
177
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
178
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
179
        MovingEffectsRenderer.mBackground.alpha(new Static1D(0.5f), mRegion, true);
180
        mCurrEffect = EFFECT_TRANS;
181
        }
182
      }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

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

    
201
    public static void Abort()
202
      {
203
      synchronized(lock)
204
        {
205
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
206
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
207
        di2D.removeAll();
208
        mRegion.removeAll();
209
        mCurrEffect = EFFECT_POINTS;
210
        }
211
      }
212

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