Project

General

Profile

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

examples / src / main / java / org / distorted / examples / movingeffects / MovingEffectsSurfaceView.java @ f6d884d5

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.view.MotionEvent;
28
import android.util.AttributeSet;
29

    
30
import org.distorted.library.DistortedEffectQueues;
31
import org.distorted.library.EffectTypes;
32
import org.distorted.library.type.Dynamic3D;
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 Dynamic3D di3D;
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
    private MovingEffectsRenderer mRenderer;
68

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

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

    
83
      mRenderer = new MovingEffectsRenderer(this);
84

    
85
      if(!isInEditMode())
86
        {
87
        setFocusable(true);
88
        setFocusableInTouchMode(true);
89
        setEGLContextClientVersion(2);
90
        setRenderer(mRenderer);
91
        }
92
      }
93

    
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95

    
96
    public void onSurfaceChanged(int width,int height)
97
      {
98
      mMax = width>height ? width:height;
99

    
100
      mSize1 = mMax/200;
101
      mSize2 = mMax/80;
102
      mSizeR = mMax/6;
103

    
104
      dr = new Static4D(0,0, mSizeR,mSizeR);
105
      }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108

    
109
    public int getCurrentEffect()
110
      {
111
      return mCurrEffect;
112
      }
113
    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115
    
116
    public void Bubble()
117
      {
118
      if( mCurrEffect==EFFECT_BUBBLE ) return;     
119
      
120
      synchronized(lock)
121
        {
122
        DistortedEffectQueues q = mRenderer.getQueues();
123
        q.abortEffects(EffectTypes.VERTEX);
124
        q.abortEffects(EffectTypes.FRAGMENT);
125
        q.distort( new Static3D(0,0,mMax/10) , di3D, dr);
126
        mCurrEffect = EFFECT_BUBBLE;
127
        }
128
      }
129

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131

    
132
    public void Sink()
133
      {
134
      if( mCurrEffect==EFFECT_SINK ) return; 
135
         
136
      synchronized(lock)
137
        {
138
        DistortedEffectQueues q = mRenderer.getQueues();
139
        q.abortEffects(EffectTypes.VERTEX);
140
        q.abortEffects(EffectTypes.FRAGMENT);
141
        q.sink(new Static1D(10), di3D, dr);
142
        mCurrEffect = EFFECT_SINK;
143
        }
144
      }
145

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

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

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163

    
164
    public void Transparency()
165
      {
166
      if( mCurrEffect==EFFECT_TRANS ) return;   
167
      
168
      synchronized(lock)
169
        {
170
        DistortedEffectQueues q = mRenderer.getQueues();
171
        q.abortEffects(EffectTypes.VERTEX);
172
        q.abortEffects(EffectTypes.FRAGMENT);
173
        q.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
        DistortedEffectQueues q = mRenderer.getQueues();
187
        q.abortEffects(EffectTypes.VERTEX);
188
        q.abortEffects(EffectTypes.FRAGMENT);
189
        q.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
        DistortedEffectQueues q = mRenderer.getQueues();
201
        q.abortEffects(EffectTypes.VERTEX);
202
        q.abortEffects(EffectTypes.FRAGMENT);
203
        di3D.removeAll();
204
        mRegion.removeAll();
205
        mCurrEffect = EFFECT_POINTS;
206
        }
207
      }
208

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

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

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

    
254
      int xDown, yDown;
255

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

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