Project

General

Profile

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

examples / src / main / java / org / distorted / examples / movingeffects / MovingEffectsSurfaceView.java @ 334c13fa

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

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

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

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

    
56
    private Dynamic3D di3D;
57
    private Static4D dr;
58
    private Dynamic4D mRegion;
59

    
60
    private Paint mPaint;
61
    private int moving;
62
    private long mTime = 0;
63
    
64
    private int mCurrEffect;
65
    private int mSize1, mSize2, mSizeR;
66
    private int mMax;
67

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

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

    
82
      if(!isInEditMode())
83
        {
84
        setFocusable(true);
85
        setFocusableInTouchMode(true);
86
        
87
        setEGLContextClientVersion(2);
88
        
89
        if( Build.FINGERPRINT.startsWith("generic") ) // when running on the emulator, insert a magic line that is
90
          {                                           // supposed to cure the 'no config chosen' crash on emulator startup
91
          setEGLConfigChooser(8, 8, 8, 8, 16, 0);   
92
          }
93
        
94
        MovingEffectsRenderer mRenderer = new MovingEffectsRenderer(this);
95
        setRenderer(mRenderer);
96
        }
97
      }
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

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

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

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

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

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

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

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

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

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

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

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

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

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

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

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

    
253
      int xDown, yDown;
254

    
255
      switch(event.getAction())
256
        {
257
        case MotionEvent.ACTION_DOWN: xDown = (int)event.getX();
258
                                      yDown = (int)event.getY();
259
                                    
260
                                      float gx, gy;
261
                                      Static2D dv;
262
                                      int len = di3D.getNumPoints();
263
                                 
264
                                      for(int g=0; g<len; g++)
265
                                        {
266
                                        dv = di3D.getPoint(g);
267
                                        gx = dv.getX();
268
                                        gy = dv.getY();
269
                                    
270
                                        if( (xDown-gx)*(xDown-gx) + (yDown-gy)*(yDown-gy) < (mMax*mMax)/100 )
271
                                          {
272
                                          moving = g;
273
                                          break;
274
                                          }
275
                                        }
276
                                      if( moving<0 )
277
                                        {
278
                                        synchronized(lock)
279
                                          {
280
                                          di3D.add(new Static3D(xDown,yDown,0));
281
                                          mRegion.add(new Static4D(xDown,yDown,mSizeR,mSizeR));
282
                                          }
283
                                        } 
284
                                      break;
285
        case MotionEvent.ACTION_MOVE: if( moving>=0 )
286
                                        {
287
                                        xDown = (int)event.getX();
288
                                        yDown = (int)event.getY();
289

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