Project

General

Profile

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

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

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 41a81a14 Leszek Koltunski
import android.app.ActivityManager;
23 427ab7bf Leszek Koltunski
import android.content.Context;
24 41a81a14 Leszek Koltunski
import android.content.pm.ConfigurationInfo;
25 427ab7bf Leszek Koltunski
import android.graphics.Canvas;
26
import android.graphics.Paint;
27
import android.graphics.Paint.Style;
28
import android.opengl.GLSurfaceView;
29
import android.view.MotionEvent;
30
import android.util.AttributeSet;
31
32 d04a4886 Leszek Koltunski
import org.distorted.library.DistortedEffects;
33 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
34 334c13fa Leszek Koltunski
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 97dadfe5 Leszek Koltunski
    private static final int NUM_POINTS= 100;
47 427ab7bf Leszek Koltunski
   
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 4348b52e Leszek Koltunski
    public static final int EFFECT_CHROMA=4;
53 427ab7bf Leszek Koltunski
    public static final int EFFECT_SWIRL =5;
54 59759251 Leszek Koltunski
55 97dadfe5 Leszek Koltunski
    private static final Object lock = new Object();
56 4562f295 Leszek Koltunski
57 334c13fa Leszek Koltunski
    private Dynamic3D di3D;
58 4562f295 Leszek Koltunski
    private Static4D dr;
59
    private Dynamic4D mRegion;
60
61
    private Paint mPaint;
62
    private int moving;
63
    private long mTime = 0;
64 427ab7bf Leszek Koltunski
    
65 4562f295 Leszek Koltunski
    private int mCurrEffect;
66
    private int mSize1, mSize2, mSizeR;
67
    private int mMax;
68 97dadfe5 Leszek Koltunski
69 f6d884d5 Leszek Koltunski
    private MovingEffectsRenderer mRenderer;
70
71 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
72 427ab7bf Leszek Koltunski
    
73 41a81a14 Leszek Koltunski
    public MovingEffectsSurfaceView(Context context, AttributeSet attrs)
74 427ab7bf Leszek Koltunski
      {
75 41a81a14 Leszek Koltunski
      super(context, 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 334c13fa Leszek Koltunski
      di3D    = new Dynamic3D(LOOP_TIME,0.0f);
83 f988589e Leszek Koltunski
      mRegion = new Dynamic4D(LOOP_TIME,0.0f);
84 59759251 Leszek Koltunski
85 f6d884d5 Leszek Koltunski
      mRenderer = new MovingEffectsRenderer(this);
86
87 427ab7bf Leszek Koltunski
      if(!isInEditMode())
88
        {
89
        setFocusable(true);
90
        setFocusableInTouchMode(true);
91 41a81a14 Leszek Koltunski
        final ActivityManager activityManager     = (android.app.ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
92
        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
93
        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
94 f6d884d5 Leszek Koltunski
        setRenderer(mRenderer);
95 427ab7bf Leszek Koltunski
        }
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 d04a4886 Leszek Koltunski
        DistortedEffects q = mRenderer.getEffects();
127 f6d884d5 Leszek Koltunski
        q.abortEffects(EffectTypes.VERTEX);
128
        q.abortEffects(EffectTypes.FRAGMENT);
129
        q.distort( new Static3D(0,0,mMax/10) , di3D, dr);
130 427ab7bf Leszek Koltunski
        mCurrEffect = EFFECT_BUBBLE;
131
        }
132
      }
133
134 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
135 427ab7bf Leszek Koltunski
136 4562f295 Leszek Koltunski
    public void Sink()
137 427ab7bf Leszek Koltunski
      {
138
      if( mCurrEffect==EFFECT_SINK ) return; 
139
         
140
      synchronized(lock)
141
        {
142 d04a4886 Leszek Koltunski
        DistortedEffects q = mRenderer.getEffects();
143 f6d884d5 Leszek Koltunski
        q.abortEffects(EffectTypes.VERTEX);
144
        q.abortEffects(EffectTypes.FRAGMENT);
145
        q.sink(new Static1D(10), di3D, dr);
146 427ab7bf Leszek Koltunski
        mCurrEffect = EFFECT_SINK;
147
        }
148
      }
149
150 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
151 427ab7bf Leszek Koltunski
152 4562f295 Leszek Koltunski
    public void Chroma()
153 427ab7bf Leszek Koltunski
      {
154 4348b52e Leszek Koltunski
      if( mCurrEffect==EFFECT_CHROMA ) return;
155 427ab7bf Leszek Koltunski
         
156
      synchronized(lock)
157
        {
158 d04a4886 Leszek Koltunski
        DistortedEffects q = mRenderer.getEffects();
159 f6d884d5 Leszek Koltunski
        q.abortEffects(EffectTypes.VERTEX);
160
        q.abortEffects(EffectTypes.FRAGMENT);
161
        q.chroma(new Static1D(0.5f), new Static3D(1,0,0), mRegion, true);
162 4348b52e Leszek Koltunski
        mCurrEffect = EFFECT_CHROMA;
163 427ab7bf Leszek Koltunski
        }
164
      }
165
166 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
167 427ab7bf Leszek Koltunski
168 4562f295 Leszek Koltunski
    public void Transparency()
169 427ab7bf Leszek Koltunski
      {
170
      if( mCurrEffect==EFFECT_TRANS ) return;   
171
      
172
      synchronized(lock)
173
        {
174 d04a4886 Leszek Koltunski
        DistortedEffects q = mRenderer.getEffects();
175 f6d884d5 Leszek Koltunski
        q.abortEffects(EffectTypes.VERTEX);
176
        q.abortEffects(EffectTypes.FRAGMENT);
177
        q.alpha(new Static1D(0.5f), mRegion, true);
178 427ab7bf Leszek Koltunski
        mCurrEffect = EFFECT_TRANS;
179
        }
180
      }
181
182 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
183 427ab7bf Leszek Koltunski
184 4562f295 Leszek Koltunski
    public void Swirl()
185 427ab7bf Leszek Koltunski
      {
186
      if( mCurrEffect==EFFECT_SWIRL ) return;   
187
      
188
      synchronized(lock)
189
        {
190 d04a4886 Leszek Koltunski
        DistortedEffects q = mRenderer.getEffects();
191 f6d884d5 Leszek Koltunski
        q.abortEffects(EffectTypes.VERTEX);
192
        q.abortEffects(EffectTypes.FRAGMENT);
193
        q.swirl( new Static1D(30), di3D, dr);
194 427ab7bf Leszek Koltunski
        mCurrEffect = EFFECT_SWIRL;
195
        }
196
      }
197
    
198 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
199 427ab7bf Leszek Koltunski
200 4562f295 Leszek Koltunski
    public void Abort()
201 427ab7bf Leszek Koltunski
      {
202
      synchronized(lock)
203
        {
204 d04a4886 Leszek Koltunski
        DistortedEffects q = mRenderer.getEffects();
205 f6d884d5 Leszek Koltunski
        q.abortEffects(EffectTypes.VERTEX);
206
        q.abortEffects(EffectTypes.FRAGMENT);
207 334c13fa Leszek Koltunski
        di3D.removeAll();
208 59759251 Leszek Koltunski
        mRegion.removeAll();
209 427ab7bf Leszek Koltunski
        mCurrEffect = EFFECT_POINTS;
210 3c8b1903 Leszek Koltunski
        mRenderer.setRefresh();
211 427ab7bf Leszek Koltunski
        }
212
      }
213
214 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
215 427ab7bf Leszek Koltunski
 
216 4562f295 Leszek Koltunski
    public void drawCurve(Canvas c, long time)
217 427ab7bf Leszek Koltunski
      {
218
      synchronized(lock)
219
        {  
220 334c13fa Leszek Koltunski
        int len = di3D.getNumPoints();
221 97dadfe5 Leszek Koltunski
222 334c13fa Leszek Koltunski
        float[] drawCoord = new float[3];
223
        Static3D cu;
224 3c8b1903 Leszek Koltunski
225 427ab7bf Leszek Koltunski
        if( len>=2 )
226
          {
227 97dadfe5 Leszek Koltunski
          float step = (float)LOOP_TIME/(NUM_POINTS+1);
228
229
          for(int i=0; i<NUM_POINTS; i++)
230 427ab7bf Leszek Koltunski
            {
231 3c8b1903 Leszek Koltunski
            mPaint.setColor( 0xffffffff );
232 334c13fa Leszek Koltunski
            di3D.interpolateMain( drawCoord, 0, (long)(i*step) );
233 97dadfe5 Leszek Koltunski
            c.drawCircle(drawCoord[0], drawCoord[1], mSize1, mPaint );
234 427ab7bf Leszek Koltunski
            }
235
          }
236
     
237
        mPaint.setColor(0xffff0000);
238
      
239
        for(int curr=0; curr<len; curr++)
240
          {       
241 334c13fa Leszek Koltunski
          cu = di3D.getPoint(curr);
242 97dadfe5 Leszek Koltunski
          c.drawCircle(cu.getX(), cu.getY(), mSize2, mPaint);
243 427ab7bf Leszek Koltunski
          }
244
        
245
        if( time-mTime > LOOP_TIME ) mTime = time;
246
        }
247
      }
248
       
249 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
250 427ab7bf Leszek Koltunski
    
251
    @Override public boolean onTouchEvent(MotionEvent event) 
252
      {
253
      if( mCurrEffect!=EFFECT_POINTS ) return true;   
254 97dadfe5 Leszek Koltunski
255
      int xDown, yDown;
256
257 427ab7bf Leszek Koltunski
      switch(event.getAction())
258
        {
259 97dadfe5 Leszek Koltunski
        case MotionEvent.ACTION_DOWN: xDown = (int)event.getX();
260
                                      yDown = (int)event.getY();
261 427ab7bf Leszek Koltunski
                                    
262
                                      float gx, gy;
263 7589635e Leszek Koltunski
                                      Static2D dv;
264 334c13fa Leszek Koltunski
                                      int len = di3D.getNumPoints();
265 427ab7bf Leszek Koltunski
                                 
266
                                      for(int g=0; g<len; g++)
267
                                        {
268 334c13fa Leszek Koltunski
                                        dv = di3D.getPoint(g);
269 427ab7bf Leszek Koltunski
                                        gx = dv.getX();
270
                                        gy = dv.getY();
271
                                    
272 97dadfe5 Leszek Koltunski
                                        if( (xDown-gx)*(xDown-gx) + (yDown-gy)*(yDown-gy) < (mMax*mMax)/100 )
273 427ab7bf Leszek Koltunski
                                          {
274
                                          moving = g;
275
                                          break;
276
                                          }
277
                                        }
278
                                      if( moving<0 )
279
                                        {
280
                                        synchronized(lock)
281
                                          {
282 334c13fa Leszek Koltunski
                                          di3D.add(new Static3D(xDown,yDown,0));
283 97dadfe5 Leszek Koltunski
                                          mRegion.add(new Static4D(xDown,yDown,mSizeR,mSizeR));
284 427ab7bf Leszek Koltunski
                                          }
285 3c8b1903 Leszek Koltunski
                                        }
286
                                      mRenderer.setRefresh();
287 427ab7bf Leszek Koltunski
                                      break;
288
        case MotionEvent.ACTION_MOVE: if( moving>=0 )
289
                                        {
290 97dadfe5 Leszek Koltunski
                                        xDown = (int)event.getX();
291
                                        yDown = (int)event.getY();
292
293 334c13fa Leszek Koltunski
                                        di3D.setPoint(moving, xDown, yDown, 0);
294 97dadfe5 Leszek Koltunski
                                        mRegion.setPoint(moving, xDown, yDown, mSizeR, mSizeR);
295 3c8b1903 Leszek Koltunski
                                        }
296
                                      mRenderer.setRefresh();
297 427ab7bf Leszek Koltunski
                                      break;
298
        case MotionEvent.ACTION_UP  : moving = -1;
299
                                      break;
300
        }
301
      return true;
302
      }
303
  }