Project

General

Profile

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

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

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 5068fa06 Leszek Koltunski
package org.distorted.examples.movingeffects;
21 427ab7bf Leszek Koltunski
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 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
32 59759251 Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
33 7589635e Leszek Koltunski
import org.distorted.library.type.Dynamic2D;
34
import org.distorted.library.type.Dynamic3D;
35 59759251 Leszek Koltunski
import org.distorted.library.type.Dynamic4D;
36
import org.distorted.library.type.Static1D;
37 7589635e Leszek Koltunski
import org.distorted.library.type.Static2D;
38
import org.distorted.library.type.Static3D;
39
import org.distorted.library.type.Static4D;
40 427ab7bf Leszek Koltunski
41 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
42 427ab7bf Leszek Koltunski
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 59759251 Leszek Koltunski
60 7589635e Leszek Koltunski
    private static Dynamic2D di2D;
61
    private static Static4D dr;
62 59759251 Leszek Koltunski
    private static Dynamic4D mRegion;
63
64 427ab7bf Leszek Koltunski
    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 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
72 427ab7bf Leszek Koltunski
    
73
    public MovingEffectsSurfaceView(Context c, AttributeSet attrs) 
74
      {
75
      super(c, attrs);
76 59759251 Leszek Koltunski
77 427ab7bf Leszek Koltunski
      mCurrEffect=EFFECT_POINTS;
78
      mPaint = new Paint();
79
      mPaint.setStyle(Style.FILL);
80
      moving = -1;
81
      
82 f988589e Leszek Koltunski
      di2D    = new Dynamic2D(LOOP_TIME,0.0f);
83
      mRegion = new Dynamic4D(LOOP_TIME,0.0f);
84
      dr      = new Static4D(0,0,60,60);
85 59759251 Leszek Koltunski
86 427ab7bf Leszek Koltunski
      if(!isInEditMode())
87
        {
88
        setFocusable(true);
89
        setFocusableInTouchMode(true);
90
        
91
        setEGLContextClientVersion(2);
92
        
93
        if( Build.FINGERPRINT.startsWith("generic") ) // when running on the emulator, insert a magic line that is
94
          {                                           // supposed to cure the 'no config chosen' crash on emulator startup
95
          setEGLConfigChooser(8, 8, 8, 8, 16, 0);   
96
          }
97
        
98
        mRenderer = new MovingEffectsRenderer(this);
99
        setRenderer(mRenderer);
100
        }
101
      }
102
103 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
104 427ab7bf Leszek Koltunski
105
    public static void setScreenSize(int width, int height)
106
      {
107
      mScrW = width;
108
      mScrH = height;
109
      }
110
111 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
112 427ab7bf Leszek Koltunski
113
    public static int getCurrentEffect()
114
      {
115
      return mCurrEffect;
116
      }
117
    
118 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
119 427ab7bf Leszek Koltunski
    
120
    public static void Bubble()
121
      {
122
      if( mCurrEffect==EFFECT_BUBBLE ) return;     
123
      
124
      synchronized(lock)
125
        {
126 a8c3ada7 Leszek Koltunski
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
127
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
128 7bf107f7 Leszek Koltunski
        MovingEffectsRenderer.mBackground.distort( new Static3D(0,0,30) , di2D, dr);
129 427ab7bf Leszek Koltunski
        mCurrEffect = EFFECT_BUBBLE;
130
        }
131
      }
132
133 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
134 427ab7bf Leszek Koltunski
135
    public static void Sink()
136
      {
137
      if( mCurrEffect==EFFECT_SINK ) return; 
138
         
139
      synchronized(lock)
140
        {
141 a8c3ada7 Leszek Koltunski
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
142
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
143 7bf107f7 Leszek Koltunski
        MovingEffectsRenderer.mBackground.sink(new Static1D(10), di2D, dr);
144 427ab7bf Leszek Koltunski
        mCurrEffect = EFFECT_SINK;
145
        }
146
      }
147
148 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
149 427ab7bf Leszek Koltunski
150
    public static void Macroblock()
151
      {
152
      if( mCurrEffect==EFFECT_MACRO ) return;   
153
         
154
      synchronized(lock)
155
        {
156 a8c3ada7 Leszek Koltunski
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
157
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
158 7bf107f7 Leszek Koltunski
        MovingEffectsRenderer.mBackground.macroblock(new Static1D(3), mRegion);
159 427ab7bf Leszek Koltunski
        mCurrEffect = EFFECT_MACRO;
160
        }
161
      }
162
163 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
164 427ab7bf Leszek Koltunski
165
    public static void Transparency()
166
      {
167
      if( mCurrEffect==EFFECT_TRANS ) return;   
168
      
169
      synchronized(lock)
170
        {
171 a8c3ada7 Leszek Koltunski
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
172
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
173 7bf107f7 Leszek Koltunski
        MovingEffectsRenderer.mBackground.alpha(new Static1D(0.5f), mRegion, true);
174 427ab7bf Leszek Koltunski
        mCurrEffect = EFFECT_TRANS;
175
        }
176
      }
177
178 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
179 427ab7bf Leszek Koltunski
180
    public static void Swirl()
181
      {
182
      if( mCurrEffect==EFFECT_SWIRL ) return;   
183
      
184
      synchronized(lock)
185
        {
186 a8c3ada7 Leszek Koltunski
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
187
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
188 7bf107f7 Leszek Koltunski
        MovingEffectsRenderer.mBackground.swirl( new Static1D(30), di2D, dr);
189 427ab7bf Leszek Koltunski
        mCurrEffect = EFFECT_SWIRL;
190
        }
191
      }
192
    
193 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
194 427ab7bf Leszek Koltunski
195
    public static void Abort()
196
      {
197
      synchronized(lock)
198
        {
199 a8c3ada7 Leszek Koltunski
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
200
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
201 427ab7bf Leszek Koltunski
        di2D.removeAll();
202 59759251 Leszek Koltunski
        mRegion.removeAll();
203 427ab7bf Leszek Koltunski
        mCurrEffect = EFFECT_POINTS;
204
        }
205
      }
206
207 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
208 427ab7bf Leszek Koltunski
 
209
    public static void drawCurve(Canvas c, long time)
210
      {
211
      synchronized(lock)
212
        {  
213
        int len = di2D.getNumPoints();
214
        float[] drawCoord = new float[2];
215 7589635e Leszek Koltunski
        Static2D cu;
216 427ab7bf Leszek Koltunski
        int lit = mTime> 0 ? (int)((float)(time-mTime)*NUM_INTERPOLATIONS/LOOP_TIME)  : 0; 
217
            
218
        if( len>=2 )
219
          {
220
          for(int i=0; i<NUM_INTERPOLATIONS; i++) 
221
            {
222
            int color = i<=lit ? 0xff - (lit                   -i)*0xff/(NUM_INTERPOLATIONS-1)  
223
                               : 0xff - (lit+NUM_INTERPOLATIONS-i)*0xff/(NUM_INTERPOLATIONS-1);
224
         
225
            mPaint.setColor( 0xffffff + ((color&0xff)<<24) );  
226
            di2D.interpolate( drawCoord, 0, (float)i/NUM_INTERPOLATIONS);
227
            c.drawCircle(drawCoord[0], drawCoord[1], 2.0f, mPaint );
228
            }
229
          }
230
     
231
        mPaint.setColor(0xffff0000);
232
      
233
        for(int curr=0; curr<len; curr++)
234
          {       
235
          cu = di2D.getPoint(curr);
236
          c.drawCircle(cu.getX(), cu.getY(), 5.0f, mPaint);
237
          }
238
        
239
        if( time-mTime > LOOP_TIME ) mTime = time;
240
        }
241
      }
242
       
243 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
244 427ab7bf Leszek Koltunski
    
245
    @Override public boolean onTouchEvent(MotionEvent event) 
246
      {
247
      if( mCurrEffect!=EFFECT_POINTS ) return true;   
248
      
249
      switch(event.getAction())
250
        {
251
        case MotionEvent.ACTION_DOWN: xDown = (int)event.getX()*MovingEffectsRenderer.BWID/mScrW; 
252
                                      yDown = (int)event.getY()*MovingEffectsRenderer.BHEI/mScrH;
253
                                    
254
                                      float gx, gy;
255 7589635e Leszek Koltunski
                                      Static2D dv;
256 427ab7bf Leszek Koltunski
                                      int len = di2D.getNumPoints();
257
                                 
258
                                      for(int g=0; g<len; g++)
259
                                        {
260
                                        dv = di2D.getPoint(g); 
261
                                        gx = dv.getX();
262
                                        gy = dv.getY();
263
                                    
264
                                        if( (xDown-gx)*(xDown-gx) + (yDown-gy)*(yDown-gy) < (MovingEffectsRenderer.BHEI*MovingEffectsRenderer.BHEI)/100 )
265
                                          {
266
                                          moving = g;
267
                                          break;
268
                                          }
269
                                        }
270
                                      if( moving<0 )
271
                                        {
272
                                        synchronized(lock)
273
                                          {
274 7589635e Leszek Koltunski
                                          di2D.add(new Static2D(xDown,yDown));
275 59759251 Leszek Koltunski
                                          mRegion.add(new Static4D(xDown,yDown,60,60));
276 427ab7bf Leszek Koltunski
                                          }
277
                                        } 
278
                                      break;
279
        case MotionEvent.ACTION_MOVE: if( moving>=0 )
280
                                        {
281
                                        xDown = (int)event.getX()*MovingEffectsRenderer.BWID/mScrW; 
282
                                        yDown = (int)event.getY()*MovingEffectsRenderer.BHEI/mScrH;
283
                                        di2D.setPoint(moving, xDown, yDown);
284 59759251 Leszek Koltunski
                                        mRegion.setPoint(moving, xDown, yDown, 60, 60);
285 427ab7bf Leszek Koltunski
                                        }                           
286
                                      break;
287
        case MotionEvent.ACTION_UP  : moving = -1;
288
                                      break;
289
        }
290
      return true;
291
      }
292
  }