Project

General

Profile

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

examples / src / main / java / org / distorted / examples / movingeffects / MovingEffectsSurfaceView.java @ 525699f4

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