Project

General

Profile

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

examples / src / main / java / org / distorted / examples / movingeffects / MovingEffectsSurfaceView.java @ 89c3e2bf

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
        android.util.Log.e("View", "Using OpenGL ES "+configurationInfo.getGlEsVersion());
94
        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
95 f6d884d5 Leszek Koltunski
        setRenderer(mRenderer);
96 427ab7bf Leszek Koltunski
        }
97
      }
98
99 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
100 427ab7bf Leszek Koltunski
101 4562f295 Leszek Koltunski
    public void onSurfaceChanged(int width,int height)
102 427ab7bf Leszek Koltunski
      {
103 97dadfe5 Leszek Koltunski
      mMax = width>height ? width:height;
104
105
      mSize1 = mMax/200;
106
      mSize2 = mMax/80;
107
      mSizeR = mMax/6;
108
109
      dr = new Static4D(0,0, mSizeR,mSizeR);
110 427ab7bf Leszek Koltunski
      }
111
112 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
113 427ab7bf Leszek Koltunski
114 4562f295 Leszek Koltunski
    public int getCurrentEffect()
115 427ab7bf Leszek Koltunski
      {
116
      return mCurrEffect;
117
      }
118
    
119 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
120 427ab7bf Leszek Koltunski
    
121 4562f295 Leszek Koltunski
    public void Bubble()
122 427ab7bf Leszek Koltunski
      {
123
      if( mCurrEffect==EFFECT_BUBBLE ) return;     
124
      
125
      synchronized(lock)
126
        {
127 d04a4886 Leszek Koltunski
        DistortedEffects q = mRenderer.getEffects();
128 f6d884d5 Leszek Koltunski
        q.abortEffects(EffectTypes.VERTEX);
129
        q.abortEffects(EffectTypes.FRAGMENT);
130
        q.distort( new Static3D(0,0,mMax/10) , di3D, dr);
131 427ab7bf Leszek Koltunski
        mCurrEffect = EFFECT_BUBBLE;
132
        }
133
      }
134
135 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
136 427ab7bf Leszek Koltunski
137 4562f295 Leszek Koltunski
    public void Sink()
138 427ab7bf Leszek Koltunski
      {
139
      if( mCurrEffect==EFFECT_SINK ) return; 
140
         
141
      synchronized(lock)
142
        {
143 d04a4886 Leszek Koltunski
        DistortedEffects q = mRenderer.getEffects();
144 f6d884d5 Leszek Koltunski
        q.abortEffects(EffectTypes.VERTEX);
145
        q.abortEffects(EffectTypes.FRAGMENT);
146
        q.sink(new Static1D(10), di3D, dr);
147 427ab7bf Leszek Koltunski
        mCurrEffect = EFFECT_SINK;
148
        }
149
      }
150
151 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
152 427ab7bf Leszek Koltunski
153 4562f295 Leszek Koltunski
    public void Chroma()
154 427ab7bf Leszek Koltunski
      {
155 4348b52e Leszek Koltunski
      if( mCurrEffect==EFFECT_CHROMA ) return;
156 427ab7bf Leszek Koltunski
         
157
      synchronized(lock)
158
        {
159 d04a4886 Leszek Koltunski
        DistortedEffects q = mRenderer.getEffects();
160 f6d884d5 Leszek Koltunski
        q.abortEffects(EffectTypes.VERTEX);
161
        q.abortEffects(EffectTypes.FRAGMENT);
162
        q.chroma(new Static1D(0.5f), new Static3D(1,0,0), mRegion, true);
163 4348b52e Leszek Koltunski
        mCurrEffect = EFFECT_CHROMA;
164 427ab7bf Leszek Koltunski
        }
165
      }
166
167 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
168 427ab7bf Leszek Koltunski
169 4562f295 Leszek Koltunski
    public void Transparency()
170 427ab7bf Leszek Koltunski
      {
171
      if( mCurrEffect==EFFECT_TRANS ) return;   
172
      
173
      synchronized(lock)
174
        {
175 d04a4886 Leszek Koltunski
        DistortedEffects q = mRenderer.getEffects();
176 f6d884d5 Leszek Koltunski
        q.abortEffects(EffectTypes.VERTEX);
177
        q.abortEffects(EffectTypes.FRAGMENT);
178
        q.alpha(new Static1D(0.5f), mRegion, true);
179 427ab7bf Leszek Koltunski
        mCurrEffect = EFFECT_TRANS;
180
        }
181
      }
182
183 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
184 427ab7bf Leszek Koltunski
185 4562f295 Leszek Koltunski
    public void Swirl()
186 427ab7bf Leszek Koltunski
      {
187
      if( mCurrEffect==EFFECT_SWIRL ) return;   
188
      
189
      synchronized(lock)
190
        {
191 d04a4886 Leszek Koltunski
        DistortedEffects q = mRenderer.getEffects();
192 f6d884d5 Leszek Koltunski
        q.abortEffects(EffectTypes.VERTEX);
193
        q.abortEffects(EffectTypes.FRAGMENT);
194
        q.swirl( new Static1D(30), di3D, dr);
195 427ab7bf Leszek Koltunski
        mCurrEffect = EFFECT_SWIRL;
196
        }
197
      }
198
    
199 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
200 427ab7bf Leszek Koltunski
201 4562f295 Leszek Koltunski
    public void Abort()
202 427ab7bf Leszek Koltunski
      {
203
      synchronized(lock)
204
        {
205 d04a4886 Leszek Koltunski
        DistortedEffects q = mRenderer.getEffects();
206 f6d884d5 Leszek Koltunski
        q.abortEffects(EffectTypes.VERTEX);
207
        q.abortEffects(EffectTypes.FRAGMENT);
208 334c13fa Leszek Koltunski
        di3D.removeAll();
209 59759251 Leszek Koltunski
        mRegion.removeAll();
210 427ab7bf Leszek Koltunski
        mCurrEffect = EFFECT_POINTS;
211 3c8b1903 Leszek Koltunski
        mRenderer.setRefresh();
212 427ab7bf Leszek Koltunski
        }
213
      }
214
215 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
216 427ab7bf Leszek Koltunski
 
217 4562f295 Leszek Koltunski
    public void drawCurve(Canvas c, long time)
218 427ab7bf Leszek Koltunski
      {
219
      synchronized(lock)
220
        {  
221 334c13fa Leszek Koltunski
        int len = di3D.getNumPoints();
222 97dadfe5 Leszek Koltunski
223 334c13fa Leszek Koltunski
        float[] drawCoord = new float[3];
224
        Static3D cu;
225 3c8b1903 Leszek Koltunski
226 427ab7bf Leszek Koltunski
        if( len>=2 )
227
          {
228 97dadfe5 Leszek Koltunski
          float step = (float)LOOP_TIME/(NUM_POINTS+1);
229
230
          for(int i=0; i<NUM_POINTS; i++)
231 427ab7bf Leszek Koltunski
            {
232 3c8b1903 Leszek Koltunski
            mPaint.setColor( 0xffffffff );
233 334c13fa Leszek Koltunski
            di3D.interpolateMain( drawCoord, 0, (long)(i*step) );
234 97dadfe5 Leszek Koltunski
            c.drawCircle(drawCoord[0], drawCoord[1], mSize1, mPaint );
235 427ab7bf Leszek Koltunski
            }
236
          }
237
     
238
        mPaint.setColor(0xffff0000);
239
      
240
        for(int curr=0; curr<len; curr++)
241
          {       
242 334c13fa Leszek Koltunski
          cu = di3D.getPoint(curr);
243 97dadfe5 Leszek Koltunski
          c.drawCircle(cu.getX(), cu.getY(), mSize2, mPaint);
244 427ab7bf Leszek Koltunski
          }
245
        
246
        if( time-mTime > LOOP_TIME ) mTime = time;
247
        }
248
      }
249
       
250 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
251 427ab7bf Leszek Koltunski
    
252
    @Override public boolean onTouchEvent(MotionEvent event) 
253
      {
254
      if( mCurrEffect!=EFFECT_POINTS ) return true;   
255 97dadfe5 Leszek Koltunski
256
      int xDown, yDown;
257
258 427ab7bf Leszek Koltunski
      switch(event.getAction())
259
        {
260 97dadfe5 Leszek Koltunski
        case MotionEvent.ACTION_DOWN: xDown = (int)event.getX();
261
                                      yDown = (int)event.getY();
262 427ab7bf Leszek Koltunski
                                    
263
                                      float gx, gy;
264 7589635e Leszek Koltunski
                                      Static2D dv;
265 334c13fa Leszek Koltunski
                                      int len = di3D.getNumPoints();
266 427ab7bf Leszek Koltunski
                                 
267
                                      for(int g=0; g<len; g++)
268
                                        {
269 334c13fa Leszek Koltunski
                                        dv = di3D.getPoint(g);
270 427ab7bf Leszek Koltunski
                                        gx = dv.getX();
271
                                        gy = dv.getY();
272
                                    
273 97dadfe5 Leszek Koltunski
                                        if( (xDown-gx)*(xDown-gx) + (yDown-gy)*(yDown-gy) < (mMax*mMax)/100 )
274 427ab7bf Leszek Koltunski
                                          {
275
                                          moving = g;
276
                                          break;
277
                                          }
278
                                        }
279
                                      if( moving<0 )
280
                                        {
281
                                        synchronized(lock)
282
                                          {
283 334c13fa Leszek Koltunski
                                          di3D.add(new Static3D(xDown,yDown,0));
284 97dadfe5 Leszek Koltunski
                                          mRegion.add(new Static4D(xDown,yDown,mSizeR,mSizeR));
285 427ab7bf Leszek Koltunski
                                          }
286 3c8b1903 Leszek Koltunski
                                        }
287
                                      mRenderer.setRefresh();
288 427ab7bf Leszek Koltunski
                                      break;
289
        case MotionEvent.ACTION_MOVE: if( moving>=0 )
290
                                        {
291 97dadfe5 Leszek Koltunski
                                        xDown = (int)event.getX();
292
                                        yDown = (int)event.getY();
293
294 334c13fa Leszek Koltunski
                                        di3D.setPoint(moving, xDown, yDown, 0);
295 97dadfe5 Leszek Koltunski
                                        mRegion.setPoint(moving, xDown, yDown, mSizeR, mSizeR);
296 3c8b1903 Leszek Koltunski
                                        }
297
                                      mRenderer.setRefresh();
298 427ab7bf Leszek Koltunski
                                      break;
299
        case MotionEvent.ACTION_UP  : moving = -1;
300
                                      break;
301
        }
302
      return true;
303
      }
304
  }