Project

General

Profile

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

examples / src / main / java / org / distorted / examples / movingeffects / MovingEffectsSurfaceView.java @ 97dadfe5

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