Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
package org.distorted.examples.movingeffects;
21

    
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
import org.distorted.library.EffectTypes;
32
import org.distorted.library.type.Dynamic2D;
33
import org.distorted.library.type.Dynamic4D;
34
import org.distorted.library.type.Static1D;
35
import org.distorted.library.type.Static2D;
36
import org.distorted.library.type.Static3D;
37
import org.distorted.library.type.Static4D;
38

    
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40

    
41
public class MovingEffectsSurfaceView extends GLSurfaceView
42
    {
43
    private static final int LOOP_TIME = 5000;
44
    private static final int NUM_POINTS= 100;
45
   
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
    public static final int EFFECT_CHROMA=4;
51
    public static final int EFFECT_SWIRL =5;
52

    
53
    private static final Object lock = new Object();
54

    
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
    
63
    private int mCurrEffect;
64
    private int mSize1, mSize2, mSizeR;
65
    private int mMax;
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68
    
69
    public MovingEffectsSurfaceView(Context c, AttributeSet attrs) 
70
      {
71
      super(c, attrs);
72

    
73
      mCurrEffect=EFFECT_POINTS;
74
      mPaint = new Paint();
75
      mPaint.setStyle(Style.FILL);
76
      moving = -1;
77
      
78
      di2D    = new Dynamic2D(LOOP_TIME,0.0f);
79
      mRegion = new Dynamic4D(LOOP_TIME,0.0f);
80

    
81
      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
        MovingEffectsRenderer mRenderer = new MovingEffectsRenderer(this);
94
        setRenderer(mRenderer);
95
        }
96
      }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

    
100
    public void onSurfaceChanged(int width,int height)
101
      {
102
      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
      }
110

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112

    
113
    public int getCurrentEffect()
114
      {
115
      return mCurrEffect;
116
      }
117
    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119
    
120
    public void Bubble()
121
      {
122
      if( mCurrEffect==EFFECT_BUBBLE ) return;     
123
      
124
      synchronized(lock)
125
        {
126
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
127
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
128
        MovingEffectsRenderer.mBackground.distort( new Static3D(0,0,mMax/10) , di2D, dr);
129
        mCurrEffect = EFFECT_BUBBLE;
130
        }
131
      }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

    
135
    public void Sink()
136
      {
137
      if( mCurrEffect==EFFECT_SINK ) return; 
138
         
139
      synchronized(lock)
140
        {
141
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
142
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
143
        MovingEffectsRenderer.mBackground.sink(new Static1D(10), di2D, dr);
144
        mCurrEffect = EFFECT_SINK;
145
        }
146
      }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

    
150
    public void Chroma()
151
      {
152
      if( mCurrEffect==EFFECT_CHROMA ) return;
153
         
154
      synchronized(lock)
155
        {
156
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
157
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
158
        MovingEffectsRenderer.mBackground.chroma(new Static1D(0.5f), new Static3D(1,0,0), mRegion, true);
159
        mCurrEffect = EFFECT_CHROMA;
160
        }
161
      }
162

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164

    
165
    public void Transparency()
166
      {
167
      if( mCurrEffect==EFFECT_TRANS ) return;   
168
      
169
      synchronized(lock)
170
        {
171
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
172
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
173
        MovingEffectsRenderer.mBackground.alpha(new Static1D(0.5f), mRegion, true);
174
        mCurrEffect = EFFECT_TRANS;
175
        }
176
      }
177

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179

    
180
    public void Swirl()
181
      {
182
      if( mCurrEffect==EFFECT_SWIRL ) return;   
183
      
184
      synchronized(lock)
185
        {
186
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
187
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
188
        MovingEffectsRenderer.mBackground.swirl( new Static1D(30), di2D, dr);
189
        mCurrEffect = EFFECT_SWIRL;
190
        }
191
      }
192
    
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194

    
195
    public void Abort()
196
      {
197
      synchronized(lock)
198
        {
199
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
200
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
201
        di2D.removeAll();
202
        mRegion.removeAll();
203
        mCurrEffect = EFFECT_POINTS;
204
        }
205
      }
206

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208
 
209
    public void drawCurve(Canvas c, long time)
210
      {
211
      synchronized(lock)
212
        {  
213
        int len = di2D.getNumPoints();
214

    
215
        float[] drawCoord = new float[2];
216
        Static2D cu;
217
        int lit = mTime> 0 ? (int)((float)(time-mTime)*NUM_POINTS/LOOP_TIME)  : 0;
218
            
219
        if( len>=2 )
220
          {
221
          float step = (float)LOOP_TIME/(NUM_POINTS+1);
222

    
223
          for(int i=0; i<NUM_POINTS; i++)
224
            {
225
            int color = i<=lit ? 0xff - (lit           -i)*0xff/(NUM_POINTS-1)
226
                               : 0xff - (lit+NUM_POINTS-i)*0xff/(NUM_POINTS-1);
227
         
228
            mPaint.setColor( 0xffffff + ((color&0xff)<<24) );  
229
            di2D.interpolateMain( drawCoord, 0, (long)(i*step) );
230
            c.drawCircle(drawCoord[0], drawCoord[1], mSize1, mPaint );
231
            }
232
          }
233
     
234
        mPaint.setColor(0xffff0000);
235
      
236
        for(int curr=0; curr<len; curr++)
237
          {       
238
          cu = di2D.getPoint(curr);
239
          c.drawCircle(cu.getX(), cu.getY(), mSize2, mPaint);
240
          }
241
        
242
        if( time-mTime > LOOP_TIME ) mTime = time;
243
        }
244
      }
245
       
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247
    
248
    @Override public boolean onTouchEvent(MotionEvent event) 
249
      {
250
      if( mCurrEffect!=EFFECT_POINTS ) return true;   
251

    
252
      int xDown, yDown;
253

    
254
      switch(event.getAction())
255
        {
256
        case MotionEvent.ACTION_DOWN: xDown = (int)event.getX();
257
                                      yDown = (int)event.getY();
258
                                    
259
                                      float gx, gy;
260
                                      Static2D dv;
261
                                      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
                                        if( (xDown-gx)*(xDown-gx) + (yDown-gy)*(yDown-gy) < (mMax*mMax)/100 )
270
                                          {
271
                                          moving = g;
272
                                          break;
273
                                          }
274
                                        }
275
                                      if( moving<0 )
276
                                        {
277
                                        synchronized(lock)
278
                                          {
279
                                          di2D.add(new Static2D(xDown,yDown));
280
                                          mRegion.add(new Static4D(xDown,yDown,mSizeR,mSizeR));
281
                                          }
282
                                        } 
283
                                      break;
284
        case MotionEvent.ACTION_MOVE: if( moving>=0 )
285
                                        {
286
                                        xDown = (int)event.getX();
287
                                        yDown = (int)event.getY();
288

    
289
                                        di2D.setPoint(moving, xDown, yDown);
290
                                        mRegion.setPoint(moving, xDown, yDown, mSizeR, mSizeR);
291
                                        }                           
292
                                      break;
293
        case MotionEvent.ACTION_UP  : moving = -1;
294
                                      break;
295
        }
296
      return true;
297
      }
298
  }
(3-3/3)