Project

General

Profile

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

examples / src / main / java / org / distorted / examples / movingeffects / MovingEffectsSurfaceView.java @ 01782e85

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.view.MotionEvent;
28
import android.util.AttributeSet;
29
30 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
31 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
32 334c13fa Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
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 334c13fa Leszek Koltunski
    private Dynamic3D di3D;
56 4562f295 Leszek Koltunski
    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 f6d884d5 Leszek Koltunski
    private MovingEffectsRenderer mRenderer;
68
69 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
70 427ab7bf Leszek Koltunski
    
71 41a81a14 Leszek Koltunski
    public MovingEffectsSurfaceView(Context context, AttributeSet attrs)
72 427ab7bf Leszek Koltunski
      {
73 41a81a14 Leszek Koltunski
      super(context, attrs);
74 59759251 Leszek Koltunski
75 427ab7bf Leszek Koltunski
      mCurrEffect=EFFECT_POINTS;
76
      mPaint = new Paint();
77
      mPaint.setStyle(Style.FILL);
78
      moving = -1;
79
      
80 334c13fa Leszek Koltunski
      di3D    = new Dynamic3D(LOOP_TIME,0.0f);
81 f988589e Leszek Koltunski
      mRegion = new Dynamic4D(LOOP_TIME,0.0f);
82 59759251 Leszek Koltunski
83 f6d884d5 Leszek Koltunski
      mRenderer = new MovingEffectsRenderer(this);
84
85 427ab7bf Leszek Koltunski
      if(!isInEditMode())
86
        {
87
        setFocusable(true);
88
        setFocusableInTouchMode(true);
89 f6d884d5 Leszek Koltunski
        setRenderer(mRenderer);
90 427ab7bf Leszek Koltunski
        }
91
      }
92
93 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
94 427ab7bf Leszek Koltunski
95 4562f295 Leszek Koltunski
    public void onSurfaceChanged(int width,int height)
96 427ab7bf Leszek Koltunski
      {
97 97dadfe5 Leszek Koltunski
      mMax = width>height ? width:height;
98
99
      mSize1 = mMax/200;
100
      mSize2 = mMax/80;
101
      mSizeR = mMax/6;
102
103
      dr = new Static4D(0,0, mSizeR,mSizeR);
104 427ab7bf Leszek Koltunski
      }
105
106 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
107 427ab7bf Leszek Koltunski
108 4562f295 Leszek Koltunski
    public int getCurrentEffect()
109 427ab7bf Leszek Koltunski
      {
110
      return mCurrEffect;
111
      }
112
    
113 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
114 427ab7bf Leszek Koltunski
    
115 4562f295 Leszek Koltunski
    public void Bubble()
116 427ab7bf Leszek Koltunski
      {
117
      if( mCurrEffect==EFFECT_BUBBLE ) return;     
118
      
119
      synchronized(lock)
120
        {
121 d04a4886 Leszek Koltunski
        DistortedEffects q = mRenderer.getEffects();
122 f6d884d5 Leszek Koltunski
        q.abortEffects(EffectTypes.VERTEX);
123
        q.abortEffects(EffectTypes.FRAGMENT);
124
        q.distort( new Static3D(0,0,mMax/10) , di3D, dr);
125 427ab7bf Leszek Koltunski
        mCurrEffect = EFFECT_BUBBLE;
126
        }
127
      }
128
129 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
130 427ab7bf Leszek Koltunski
131 4562f295 Leszek Koltunski
    public void Sink()
132 427ab7bf Leszek Koltunski
      {
133
      if( mCurrEffect==EFFECT_SINK ) return; 
134
         
135
      synchronized(lock)
136
        {
137 d04a4886 Leszek Koltunski
        DistortedEffects q = mRenderer.getEffects();
138 f6d884d5 Leszek Koltunski
        q.abortEffects(EffectTypes.VERTEX);
139
        q.abortEffects(EffectTypes.FRAGMENT);
140
        q.sink(new Static1D(10), di3D, dr);
141 427ab7bf Leszek Koltunski
        mCurrEffect = EFFECT_SINK;
142
        }
143
      }
144
145 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
146 427ab7bf Leszek Koltunski
147 4562f295 Leszek Koltunski
    public void Chroma()
148 427ab7bf Leszek Koltunski
      {
149 4348b52e Leszek Koltunski
      if( mCurrEffect==EFFECT_CHROMA ) return;
150 427ab7bf Leszek Koltunski
         
151
      synchronized(lock)
152
        {
153 d04a4886 Leszek Koltunski
        DistortedEffects q = mRenderer.getEffects();
154 f6d884d5 Leszek Koltunski
        q.abortEffects(EffectTypes.VERTEX);
155
        q.abortEffects(EffectTypes.FRAGMENT);
156
        q.chroma(new Static1D(0.5f), new Static3D(1,0,0), mRegion, true);
157 4348b52e Leszek Koltunski
        mCurrEffect = EFFECT_CHROMA;
158 427ab7bf Leszek Koltunski
        }
159
      }
160
161 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
162 427ab7bf Leszek Koltunski
163 4562f295 Leszek Koltunski
    public void Transparency()
164 427ab7bf Leszek Koltunski
      {
165
      if( mCurrEffect==EFFECT_TRANS ) return;   
166
      
167
      synchronized(lock)
168
        {
169 d04a4886 Leszek Koltunski
        DistortedEffects q = mRenderer.getEffects();
170 f6d884d5 Leszek Koltunski
        q.abortEffects(EffectTypes.VERTEX);
171
        q.abortEffects(EffectTypes.FRAGMENT);
172
        q.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 4562f295 Leszek Koltunski
    public void Swirl()
180 427ab7bf Leszek Koltunski
      {
181
      if( mCurrEffect==EFFECT_SWIRL ) return;   
182
      
183
      synchronized(lock)
184
        {
185 d04a4886 Leszek Koltunski
        DistortedEffects q = mRenderer.getEffects();
186 f6d884d5 Leszek Koltunski
        q.abortEffects(EffectTypes.VERTEX);
187
        q.abortEffects(EffectTypes.FRAGMENT);
188
        q.swirl( new Static1D(30), di3D, 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 d04a4886 Leszek Koltunski
        DistortedEffects q = mRenderer.getEffects();
200 f6d884d5 Leszek Koltunski
        q.abortEffects(EffectTypes.VERTEX);
201
        q.abortEffects(EffectTypes.FRAGMENT);
202 334c13fa Leszek Koltunski
        di3D.removeAll();
203 59759251 Leszek Koltunski
        mRegion.removeAll();
204 427ab7bf Leszek Koltunski
        mCurrEffect = EFFECT_POINTS;
205 3c8b1903 Leszek Koltunski
        mRenderer.setRefresh();
206 427ab7bf Leszek Koltunski
        }
207
      }
208
209 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
210 427ab7bf Leszek Koltunski
 
211 4562f295 Leszek Koltunski
    public void drawCurve(Canvas c, long time)
212 427ab7bf Leszek Koltunski
      {
213
      synchronized(lock)
214
        {  
215 334c13fa Leszek Koltunski
        int len = di3D.getNumPoints();
216 97dadfe5 Leszek Koltunski
217 334c13fa Leszek Koltunski
        float[] drawCoord = new float[3];
218
        Static3D cu;
219 3c8b1903 Leszek Koltunski
220 427ab7bf Leszek Koltunski
        if( len>=2 )
221
          {
222 97dadfe5 Leszek Koltunski
          float step = (float)LOOP_TIME/(NUM_POINTS+1);
223
224
          for(int i=0; i<NUM_POINTS; i++)
225 427ab7bf Leszek Koltunski
            {
226 3c8b1903 Leszek Koltunski
            mPaint.setColor( 0xffffffff );
227 334c13fa Leszek Koltunski
            di3D.interpolateMain( drawCoord, 0, (long)(i*step) );
228 97dadfe5 Leszek Koltunski
            c.drawCircle(drawCoord[0], drawCoord[1], mSize1, mPaint );
229 427ab7bf Leszek Koltunski
            }
230
          }
231
     
232
        mPaint.setColor(0xffff0000);
233
      
234
        for(int curr=0; curr<len; curr++)
235
          {       
236 334c13fa Leszek Koltunski
          cu = di3D.getPoint(curr);
237 97dadfe5 Leszek Koltunski
          c.drawCircle(cu.getX(), cu.getY(), mSize2, mPaint);
238 427ab7bf Leszek Koltunski
          }
239
        
240
        if( time-mTime > LOOP_TIME ) mTime = time;
241
        }
242
      }
243
       
244 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
245 427ab7bf Leszek Koltunski
    
246
    @Override public boolean onTouchEvent(MotionEvent event) 
247
      {
248
      if( mCurrEffect!=EFFECT_POINTS ) return true;   
249 97dadfe5 Leszek Koltunski
250
      int xDown, yDown;
251
252 427ab7bf Leszek Koltunski
      switch(event.getAction())
253
        {
254 97dadfe5 Leszek Koltunski
        case MotionEvent.ACTION_DOWN: xDown = (int)event.getX();
255
                                      yDown = (int)event.getY();
256 427ab7bf Leszek Koltunski
                                    
257
                                      float gx, gy;
258 7589635e Leszek Koltunski
                                      Static2D dv;
259 334c13fa Leszek Koltunski
                                      int len = di3D.getNumPoints();
260 427ab7bf Leszek Koltunski
                                 
261
                                      for(int g=0; g<len; g++)
262
                                        {
263 334c13fa Leszek Koltunski
                                        dv = di3D.getPoint(g);
264 427ab7bf Leszek Koltunski
                                        gx = dv.getX();
265
                                        gy = dv.getY();
266
                                    
267 97dadfe5 Leszek Koltunski
                                        if( (xDown-gx)*(xDown-gx) + (yDown-gy)*(yDown-gy) < (mMax*mMax)/100 )
268 427ab7bf Leszek Koltunski
                                          {
269
                                          moving = g;
270
                                          break;
271
                                          }
272
                                        }
273
                                      if( moving<0 )
274
                                        {
275
                                        synchronized(lock)
276
                                          {
277 334c13fa Leszek Koltunski
                                          di3D.add(new Static3D(xDown,yDown,0));
278 97dadfe5 Leszek Koltunski
                                          mRegion.add(new Static4D(xDown,yDown,mSizeR,mSizeR));
279 427ab7bf Leszek Koltunski
                                          }
280 3c8b1903 Leszek Koltunski
                                        }
281
                                      mRenderer.setRefresh();
282 427ab7bf Leszek Koltunski
                                      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 334c13fa Leszek Koltunski
                                        di3D.setPoint(moving, xDown, yDown, 0);
289 97dadfe5 Leszek Koltunski
                                        mRegion.setPoint(moving, xDown, yDown, mSizeR, mSizeR);
290 3c8b1903 Leszek Koltunski
                                        }
291
                                      mRenderer.setRefresh();
292 427ab7bf Leszek Koltunski
                                      break;
293
        case MotionEvent.ACTION_UP  : moving = -1;
294
                                      break;
295
        }
296
      return true;
297
      }
298
  }