Project

General

Profile

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

examples / src / main / java / org / distorted / examples / movingeffects / MovingEffectsSurfaceView.java @ 525699f4

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
        setEGLContextClientVersion(2);
87
        setRenderer(new MovingEffectsRenderer(this));
88
        }
89
      }
90

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

    
93
    public void onSurfaceChanged(int width,int height)
94
      {
95
      mMax = width>height ? width:height;
96

    
97
      mSize1 = mMax/200;
98
      mSize2 = mMax/80;
99
      mSizeR = mMax/6;
100

    
101
      dr = new Static4D(0,0, mSizeR,mSizeR);
102
      }
103

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105

    
106
    public int getCurrentEffect()
107
      {
108
      return mCurrEffect;
109
      }
110
    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112
    
113
    public void Bubble()
114
      {
115
      if( mCurrEffect==EFFECT_BUBBLE ) return;     
116
      
117
      synchronized(lock)
118
        {
119
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
120
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
121
        MovingEffectsRenderer.mBackground.distort( new Static3D(0,0,mMax/10) , di3D, dr);
122
        mCurrEffect = EFFECT_BUBBLE;
123
        }
124
      }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127

    
128
    public void Sink()
129
      {
130
      if( mCurrEffect==EFFECT_SINK ) return; 
131
         
132
      synchronized(lock)
133
        {
134
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
135
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
136
        MovingEffectsRenderer.mBackground.sink(new Static1D(10), di3D, dr);
137
        mCurrEffect = EFFECT_SINK;
138
        }
139
      }
140

    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142

    
143
    public void Chroma()
144
      {
145
      if( mCurrEffect==EFFECT_CHROMA ) return;
146
         
147
      synchronized(lock)
148
        {
149
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
150
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
151
        MovingEffectsRenderer.mBackground.chroma(new Static1D(0.5f), new Static3D(1,0,0), mRegion, true);
152
        mCurrEffect = EFFECT_CHROMA;
153
        }
154
      }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

    
158
    public void Transparency()
159
      {
160
      if( mCurrEffect==EFFECT_TRANS ) return;   
161
      
162
      synchronized(lock)
163
        {
164
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
165
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
166
        MovingEffectsRenderer.mBackground.alpha(new Static1D(0.5f), mRegion, true);
167
        mCurrEffect = EFFECT_TRANS;
168
        }
169
      }
170

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172

    
173
    public void Swirl()
174
      {
175
      if( mCurrEffect==EFFECT_SWIRL ) return;   
176
      
177
      synchronized(lock)
178
        {
179
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
180
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
181
        MovingEffectsRenderer.mBackground.swirl( new Static1D(30), di3D, dr);
182
        mCurrEffect = EFFECT_SWIRL;
183
        }
184
      }
185
    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187

    
188
    public void Abort()
189
      {
190
      synchronized(lock)
191
        {
192
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
193
        MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
194
        di3D.removeAll();
195
        mRegion.removeAll();
196
        mCurrEffect = EFFECT_POINTS;
197
        }
198
      }
199

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201
 
202
    public void drawCurve(Canvas c, long time)
203
      {
204
      synchronized(lock)
205
        {  
206
        int len = di3D.getNumPoints();
207

    
208
        float[] drawCoord = new float[3];
209
        Static3D cu;
210
        int lit = mTime> 0 ? (int)((float)(time-mTime)*NUM_POINTS/LOOP_TIME)  : 0;
211
            
212
        if( len>=2 )
213
          {
214
          float step = (float)LOOP_TIME/(NUM_POINTS+1);
215

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

    
245
      int xDown, yDown;
246

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

    
282
                                        di3D.setPoint(moving, xDown, yDown, 0);
283
                                        mRegion.setPoint(moving, xDown, yDown, mSizeR, mSizeR);
284
                                        }                           
285
                                      break;
286
        case MotionEvent.ACTION_UP  : moving = -1;
287
                                      break;
288
        }
289
      return true;
290
      }
291
  }
(3-3/3)