Project

General

Profile

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

examples / src / main / java / org / distorted / examples / movingeffects / MovingEffectsSurfaceView.java @ 6173632c

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.effect.EffectType;
31
import org.distorted.library.effect.FragmentEffectAlpha;
32
import org.distorted.library.effect.FragmentEffectChroma;
33
import org.distorted.library.effect.VertexEffectDistort;
34
import org.distorted.library.effect.VertexEffectSink;
35
import org.distorted.library.effect.VertexEffectSwirl;
36
import org.distorted.library.main.DistortedEffects;
37
import org.distorted.library.type.Dynamic3D;
38
import org.distorted.library.type.Dynamic4D;
39
import org.distorted.library.type.Static1D;
40
import org.distorted.library.type.Static2D;
41
import org.distorted.library.type.Static3D;
42
import org.distorted.library.type.Static4D;
43

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

    
46
public class MovingEffectsSurfaceView extends GLSurfaceView
47
    {
48
    private static final int LOOP_TIME = 5000;
49
    private static final int NUM_POINTS= 100;
50

    
51
    public static final int EFFECT_POINTS=0;
52
    public static final int EFFECT_BUBBLE=1;
53
    public static final int EFFECT_SINK  =2;
54
    public static final int EFFECT_TRANS =3;
55
    public static final int EFFECT_CHROMA=4;
56
    public static final int EFFECT_SWIRL =5;
57

    
58
    private static final Object lock = new Object();
59

    
60
    private Dynamic3D di3D;
61
    private Static4D dr;
62
    private Dynamic4D mRegion;
63

    
64
    private Paint mPaint;
65
    private int moving;
66
    private long mTime = 0;
67
    
68
    private int mCurrEffect;
69
    private int mSize1, mSize2, mSizeR;
70
    private int mMax;
71

    
72
    private MovingEffectsRenderer mRenderer;
73

    
74
    private VertexEffectDistort mEffectDistort;
75
    private VertexEffectSink mEffectSink;
76
    private VertexEffectSwirl mEffectSwirl;
77
    private FragmentEffectChroma mEffectChroma;
78
    private FragmentEffectAlpha mEffectAlpha;
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81
    
82
    public MovingEffectsSurfaceView(Context context, AttributeSet attrs)
83
      {
84
      super(context, attrs);
85

    
86
      mCurrEffect=EFFECT_POINTS;
87
      mPaint = new Paint();
88
      mPaint.setStyle(Style.FILL);
89
      moving = -1;
90
      
91
      di3D    = new Dynamic3D(LOOP_TIME,0.0f);
92
      mRegion = new Dynamic4D(LOOP_TIME,0.0f);
93

    
94
      mRenderer = new MovingEffectsRenderer(this);
95

    
96
      if(!isInEditMode())
97
        {
98
        setFocusable(true);
99
        setFocusableInTouchMode(true);
100
        setRenderer(mRenderer);
101
        }
102

    
103
      mEffectDistort = new VertexEffectDistort(new Static3D(0,0,mMax/10) , di3D, dr);
104
      mEffectSink    = new VertexEffectSink(new Static1D(10), di3D, dr);
105
      mEffectSwirl   = new VertexEffectSwirl( new Static1D(30), di3D, dr);
106
      mEffectAlpha   = new FragmentEffectAlpha(new Static1D(0.5f), mRegion, true);
107
      mEffectChroma  = new FragmentEffectChroma(new Static1D(0.5f), new Static3D(1,0,0), mRegion, true);
108
      }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

    
112
    public void onSurfaceChanged(int width,int height)
113
      {
114
      mMax = width>height ? width:height;
115

    
116
      mSize1 = mMax/200;
117
      mSize2 = mMax/80;
118
      mSizeR = mMax/6;
119

    
120
      dr = new Static4D(0,0, mSizeR,mSizeR);
121
      }
122

    
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124

    
125
    public int getCurrentEffect()
126
      {
127
      return mCurrEffect;
128
      }
129
    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131
    
132
    public void Bubble()
133
      {
134
      if( mCurrEffect==EFFECT_BUBBLE ) return;     
135
      
136
      synchronized(lock)
137
        {
138
        DistortedEffects q = mRenderer.getEffects();
139
        q.abortByType(EffectType.VERTEX);
140
        q.abortByType(EffectType.FRAGMENT);
141
        q.apply(mEffectDistort);
142
        mCurrEffect = EFFECT_BUBBLE;
143
        }
144
      }
145

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

    
148
    public void Sink()
149
      {
150
      if( mCurrEffect==EFFECT_SINK ) return; 
151
         
152
      synchronized(lock)
153
        {
154
        DistortedEffects q = mRenderer.getEffects();
155
        q.abortByType(EffectType.VERTEX);
156
        q.abortByType(EffectType.FRAGMENT);
157
        q.apply(mEffectSink);
158
        mCurrEffect = EFFECT_SINK;
159
        }
160
      }
161

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

    
164
    public void Chroma()
165
      {
166
      if( mCurrEffect==EFFECT_CHROMA ) return;
167
         
168
      synchronized(lock)
169
        {
170
        DistortedEffects q = mRenderer.getEffects();
171
        q.abortByType(EffectType.VERTEX);
172
        q.abortByType(EffectType.FRAGMENT);
173
        q.apply(mEffectChroma);
174
        mCurrEffect = EFFECT_CHROMA;
175
        }
176
      }
177

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

    
180
    public void Transparency()
181
      {
182
      if( mCurrEffect==EFFECT_TRANS ) return;   
183
      
184
      synchronized(lock)
185
        {
186
        DistortedEffects q = mRenderer.getEffects();
187
        q.abortByType(EffectType.VERTEX);
188
        q.abortByType(EffectType.FRAGMENT);
189
        q.apply(mEffectAlpha);
190
        mCurrEffect = EFFECT_TRANS;
191
        }
192
      }
193

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

    
196
    public void Swirl()
197
      {
198
      if( mCurrEffect==EFFECT_SWIRL ) return;   
199
      
200
      synchronized(lock)
201
        {
202
        DistortedEffects q = mRenderer.getEffects();
203
        q.abortByType(EffectType.VERTEX);
204
        q.abortByType(EffectType.FRAGMENT);
205
        q.apply(mEffectSwirl);
206
        mCurrEffect = EFFECT_SWIRL;
207
        }
208
      }
209
    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

    
212
    public void Abort()
213
      {
214
      synchronized(lock)
215
        {
216
        DistortedEffects q = mRenderer.getEffects();
217
        q.abortByType(EffectType.VERTEX);
218
        q.abortByType(EffectType.FRAGMENT);
219
        di3D.removeAll();
220
        mRegion.removeAll();
221
        mCurrEffect = EFFECT_POINTS;
222
        mRenderer.setRefresh();
223
        }
224
      }
225

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227
 
228
    public void drawCurve(Canvas c, long time)
229
      {
230
      synchronized(lock)
231
        {  
232
        int len = di3D.getNumPoints();
233

    
234
        float[] drawCoord = new float[3];
235
        Static3D cu;
236

    
237
        if( len>=2 )
238
          {
239
          float step = (float)LOOP_TIME/(NUM_POINTS+1);
240

    
241
          for(int i=0; i<NUM_POINTS; i++)
242
            {
243
            mPaint.setColor( 0xffffffff );
244
            di3D.interpolateMain( drawCoord, 0, (long)(i*step) );
245
            c.drawCircle(drawCoord[0], drawCoord[1], mSize1, mPaint );
246
            }
247
          }
248
     
249
        mPaint.setColor(0xffff0000);
250
      
251
        for(int curr=0; curr<len; curr++)
252
          {       
253
          cu = di3D.getPoint(curr);
254
          c.drawCircle(cu.getX(), cu.getY(), mSize2, mPaint);
255
          }
256
        
257
        if( time-mTime > LOOP_TIME ) mTime = time;
258
        }
259
      }
260
       
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262
    
263
    @Override public boolean onTouchEvent(MotionEvent event) 
264
      {
265
      if( mCurrEffect!=EFFECT_POINTS ) return true;   
266

    
267
      int xDown, yDown;
268

    
269
      switch(event.getAction())
270
        {
271
        case MotionEvent.ACTION_DOWN: xDown = (int)event.getX();
272
                                      yDown = (int)event.getY();
273
                                    
274
                                      float gx, gy;
275
                                      Static2D dv;
276
                                      int len = di3D.getNumPoints();
277
                                 
278
                                      for(int g=0; g<len; g++)
279
                                        {
280
                                        dv = di3D.getPoint(g);
281
                                        gx = dv.getX();
282
                                        gy = dv.getY();
283
                                    
284
                                        if( (xDown-gx)*(xDown-gx) + (yDown-gy)*(yDown-gy) < (mMax*mMax)/100 )
285
                                          {
286
                                          moving = g;
287
                                          break;
288
                                          }
289
                                        }
290
                                      if( moving<0 )
291
                                        {
292
                                        synchronized(lock)
293
                                          {
294
                                          di3D.add(new Static3D(xDown,yDown,0));
295
                                          mRegion.add(new Static4D(xDown,yDown,mSizeR,mSizeR));
296
                                          }
297
                                        }
298
                                      mRenderer.setRefresh();
299
                                      break;
300
        case MotionEvent.ACTION_MOVE: if( moving>=0 )
301
                                        {
302
                                        xDown = (int)event.getX();
303
                                        yDown = (int)event.getY();
304

    
305
                                        di3D.setPoint(moving, xDown, yDown, 0);
306
                                        mRegion.setPoint(moving, xDown, yDown, mSizeR, mSizeR);
307
                                        }
308
                                      mRenderer.setRefresh();
309
                                      break;
310
        case MotionEvent.ACTION_UP  : moving = -1;
311
                                      break;
312
        }
313
      return true;
314
      }
315
  }
(3-3/3)