Project

General

Profile

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

examples / src / main / java / org / distorted / examples / movingeffects / MovingEffectsSurfaceView.java @ 4562f295

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 97dadfe5 Leszek Koltunski
    private static final Object lock = new Object();
54 4562f295 Leszek Koltunski
55
    private Dynamic2D di2D;
56
    private Static4D dr;
57
    private Dynamic4D mRegion;
58
59
    private Paint mPaint;
60
    private int moving;
61
    private long mTime = 0;
62 427ab7bf Leszek Koltunski
    
63 4562f295 Leszek Koltunski
    private int mCurrEffect;
64
    private int mSize1, mSize2, mSizeR;
65
    private int mMax;
66 97dadfe5 Leszek Koltunski
67 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
68 427ab7bf Leszek Koltunski
    
69
    public MovingEffectsSurfaceView(Context c, AttributeSet attrs) 
70
      {
71
      super(c, attrs);
72 59759251 Leszek Koltunski
73 427ab7bf Leszek Koltunski
      mCurrEffect=EFFECT_POINTS;
74
      mPaint = new Paint();
75
      mPaint.setStyle(Style.FILL);
76
      moving = -1;
77
      
78 f988589e Leszek Koltunski
      di2D    = new Dynamic2D(LOOP_TIME,0.0f);
79
      mRegion = new Dynamic4D(LOOP_TIME,0.0f);
80 59759251 Leszek Koltunski
81 427ab7bf Leszek Koltunski
      if(!isInEditMode())
82
        {
83
        setFocusable(true);
84
        setFocusableInTouchMode(true);
85
        
86
        setEGLContextClientVersion(2);
87
        
88
        if( Build.FINGERPRINT.startsWith("generic") ) // when running on the emulator, insert a magic line that is
89
          {                                           // supposed to cure the 'no config chosen' crash on emulator startup
90
          setEGLConfigChooser(8, 8, 8, 8, 16, 0);   
91
          }
92
        
93 97dadfe5 Leszek Koltunski
        MovingEffectsRenderer mRenderer = new MovingEffectsRenderer(this);
94 427ab7bf Leszek Koltunski
        setRenderer(mRenderer);
95
        }
96
      }
97
98 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
99 427ab7bf Leszek Koltunski
100 4562f295 Leszek Koltunski
    public void onSurfaceChanged(int width,int height)
101 427ab7bf Leszek Koltunski
      {
102 97dadfe5 Leszek Koltunski
      mMax = width>height ? width:height;
103
104
      mSize1 = mMax/200;
105
      mSize2 = mMax/80;
106
      mSizeR = mMax/6;
107
108
      dr = new Static4D(0,0, mSizeR,mSizeR);
109 427ab7bf Leszek Koltunski
      }
110
111 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
112 427ab7bf Leszek Koltunski
113 4562f295 Leszek Koltunski
    public int getCurrentEffect()
114 427ab7bf Leszek Koltunski
      {
115
      return mCurrEffect;
116
      }
117
    
118 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
119 427ab7bf Leszek Koltunski
    
120 4562f295 Leszek Koltunski
    public void Bubble()
121 427ab7bf Leszek Koltunski
      {
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 97dadfe5 Leszek Koltunski
        MovingEffectsRenderer.mBackground.distort( new Static3D(0,0,mMax/10) , di2D, dr);
129 427ab7bf Leszek Koltunski
        mCurrEffect = EFFECT_BUBBLE;
130
        }
131
      }
132
133 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
134 427ab7bf Leszek Koltunski
135 4562f295 Leszek Koltunski
    public void Sink()
136 427ab7bf Leszek Koltunski
      {
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 4562f295 Leszek Koltunski
    public void Chroma()
151 427ab7bf Leszek Koltunski
      {
152 4348b52e Leszek Koltunski
      if( mCurrEffect==EFFECT_CHROMA ) return;
153 427ab7bf Leszek Koltunski
         
154
      synchronized(lock)
155
        {
156 a8c3ada7 Leszek Koltunski
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
157
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
158 4348b52e Leszek Koltunski
        MovingEffectsRenderer.mBackground.chroma(new Static1D(0.5f), new Static3D(1,0,0), mRegion, true);
159
        mCurrEffect = EFFECT_CHROMA;
160 427ab7bf Leszek Koltunski
        }
161
      }
162
163 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
164 427ab7bf Leszek Koltunski
165 4562f295 Leszek Koltunski
    public void Transparency()
166 427ab7bf Leszek Koltunski
      {
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 4562f295 Leszek Koltunski
    public void Swirl()
181 427ab7bf Leszek Koltunski
      {
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 4562f295 Leszek Koltunski
    public void Abort()
196 427ab7bf Leszek Koltunski
      {
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 4562f295 Leszek Koltunski
    public void drawCurve(Canvas c, long time)
210 427ab7bf Leszek Koltunski
      {
211
      synchronized(lock)
212
        {  
213
        int len = di2D.getNumPoints();
214 97dadfe5 Leszek Koltunski
215 427ab7bf Leszek Koltunski
        float[] drawCoord = new float[2];
216 7589635e Leszek Koltunski
        Static2D cu;
217 97dadfe5 Leszek Koltunski
        int lit = mTime> 0 ? (int)((float)(time-mTime)*NUM_POINTS/LOOP_TIME)  : 0;
218 427ab7bf Leszek Koltunski
            
219
        if( len>=2 )
220
          {
221 97dadfe5 Leszek Koltunski
          float step = (float)LOOP_TIME/(NUM_POINTS+1);
222
223
          for(int i=0; i<NUM_POINTS; i++)
224 427ab7bf Leszek Koltunski
            {
225 97dadfe5 Leszek Koltunski
            int color = i<=lit ? 0xff - (lit           -i)*0xff/(NUM_POINTS-1)
226
                               : 0xff - (lit+NUM_POINTS-i)*0xff/(NUM_POINTS-1);
227 427ab7bf Leszek Koltunski
         
228
            mPaint.setColor( 0xffffff + ((color&0xff)<<24) );  
229 97dadfe5 Leszek Koltunski
            di2D.interpolateMain( drawCoord, 0, (long)(i*step) );
230
            c.drawCircle(drawCoord[0], drawCoord[1], mSize1, mPaint );
231 427ab7bf Leszek Koltunski
            }
232
          }
233
     
234
        mPaint.setColor(0xffff0000);
235
      
236
        for(int curr=0; curr<len; curr++)
237
          {       
238
          cu = di2D.getPoint(curr);
239 97dadfe5 Leszek Koltunski
          c.drawCircle(cu.getX(), cu.getY(), mSize2, mPaint);
240 427ab7bf Leszek Koltunski
          }
241
        
242
        if( time-mTime > LOOP_TIME ) mTime = time;
243
        }
244
      }
245
       
246 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
247 427ab7bf Leszek Koltunski
    
248
    @Override public boolean onTouchEvent(MotionEvent event) 
249
      {
250
      if( mCurrEffect!=EFFECT_POINTS ) return true;   
251 97dadfe5 Leszek Koltunski
252
      int xDown, yDown;
253
254 427ab7bf Leszek Koltunski
      switch(event.getAction())
255
        {
256 97dadfe5 Leszek Koltunski
        case MotionEvent.ACTION_DOWN: xDown = (int)event.getX();
257
                                      yDown = (int)event.getY();
258 427ab7bf Leszek Koltunski
                                    
259
                                      float gx, gy;
260 7589635e Leszek Koltunski
                                      Static2D dv;
261 427ab7bf Leszek Koltunski
                                      int len = di2D.getNumPoints();
262
                                 
263
                                      for(int g=0; g<len; g++)
264
                                        {
265
                                        dv = di2D.getPoint(g); 
266
                                        gx = dv.getX();
267
                                        gy = dv.getY();
268
                                    
269 97dadfe5 Leszek Koltunski
                                        if( (xDown-gx)*(xDown-gx) + (yDown-gy)*(yDown-gy) < (mMax*mMax)/100 )
270 427ab7bf Leszek Koltunski
                                          {
271
                                          moving = g;
272
                                          break;
273
                                          }
274
                                        }
275
                                      if( moving<0 )
276
                                        {
277
                                        synchronized(lock)
278
                                          {
279 7589635e Leszek Koltunski
                                          di2D.add(new Static2D(xDown,yDown));
280 97dadfe5 Leszek Koltunski
                                          mRegion.add(new Static4D(xDown,yDown,mSizeR,mSizeR));
281 427ab7bf Leszek Koltunski
                                          }
282
                                        } 
283
                                      break;
284
        case MotionEvent.ACTION_MOVE: if( moving>=0 )
285
                                        {
286 97dadfe5 Leszek Koltunski
                                        xDown = (int)event.getX();
287
                                        yDown = (int)event.getY();
288
289 427ab7bf Leszek Koltunski
                                        di2D.setPoint(moving, xDown, yDown);
290 97dadfe5 Leszek Koltunski
                                        mRegion.setPoint(moving, xDown, yDown, mSizeR, mSizeR);
291 427ab7bf Leszek Koltunski
                                        }                           
292
                                      break;
293
        case MotionEvent.ACTION_UP  : moving = -1;
294
                                      break;
295
        }
296
      return true;
297
      }
298
  }