Project

General

Profile

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

examples / src / main / java / org / distorted / examples / movingeffects / MovingEffectsSurfaceView.java @ 6777d742

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.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.graphics.Canvas;
26
import android.graphics.Paint;
27
import android.graphics.Paint.Style;
28
import android.opengl.GLSurfaceView;
29
import android.view.MotionEvent;
30
import android.util.AttributeSet;
31

    
32
import org.distorted.library.effect.EffectType;
33
import org.distorted.library.effect.FragmentEffectAlpha;
34
import org.distorted.library.effect.FragmentEffectChroma;
35
import org.distorted.library.effect.VertexEffectDistort;
36
import org.distorted.library.effect.VertexEffectSink;
37
import org.distorted.library.effect.VertexEffectSwirl;
38
import org.distorted.library.main.DistortedEffects;
39
import org.distorted.library.type.Dynamic3D;
40
import org.distorted.library.type.Dynamic4D;
41
import org.distorted.library.type.Static1D;
42
import org.distorted.library.type.Static3D;
43
import org.distorted.library.type.Static4D;
44

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

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

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

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

    
61
    private Dynamic3D mCenterVertex;
62
    private Static4D mRegionVertex;
63
    private Dynamic4D mRegionFragment;
64

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

    
73
    private MovingEffectsRenderer mRenderer;
74

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

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

    
87
      mCurrEffect=EFFECT_POINTS;
88
      mPaint = new Paint();
89
      mPaint.setStyle(Style.FILL);
90
      moving = -1;
91
      
92
      mCenterVertex   = new Dynamic3D(LOOP_TIME,0.0f);
93
      mRegionFragment = new Dynamic4D(LOOP_TIME,0.0f);
94
      mRegionVertex   = new Static4D(0,0,0,0);
95
      mDistort        = new Static3D(0,0,0);
96

    
97
      if(!isInEditMode())
98
        {
99
        mRenderer = new MovingEffectsRenderer(this);
100

    
101
        setFocusable(true);
102
        setFocusableInTouchMode(true);
103
        final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
104
        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
105
        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
106
        setRenderer(mRenderer);
107

    
108
        mEffectDistort = new VertexEffectDistort( mDistort , mCenterVertex, mRegionVertex);
109
        mEffectSink    = new VertexEffectSink(new Static1D(10), mCenterVertex, mRegionVertex);
110
        mEffectSwirl   = new VertexEffectSwirl( new Static1D(30), mCenterVertex, mRegionVertex);
111
        mEffectAlpha   = new FragmentEffectAlpha(new Static1D(0.5f), mRegionFragment, true);
112
        mEffectChroma  = new FragmentEffectChroma(new Static1D(0.5f), new Static3D(1,0,0), mRegionFragment, true);
113
        }
114
      }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

    
118
    public void onSurfaceChanged(int width,int height)
119
      {
120
      int max = width>height ? width:height;
121

    
122
      mSize1 = max/200;
123
      mSize2 = max/80;
124
      mSizeR = max/6;
125

    
126
      mRegionVertex.set(0,0, mSizeR,mSizeR);
127
      mDistort.set3(max/10);
128
      }
129

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

    
132
    public int getCurrentEffect()
133
      {
134
      return mCurrEffect;
135
      }
136
    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138
    
139
    public void Bubble()
140
      {
141
      if( mCurrEffect==EFFECT_BUBBLE ) return;     
142
      
143
      synchronized(lock)
144
        {
145
        DistortedEffects q = mRenderer.getEffects();
146
        q.abortByType(EffectType.VERTEX);
147
        q.abortByType(EffectType.FRAGMENT);
148
        q.apply(mEffectDistort);
149
        mCurrEffect = EFFECT_BUBBLE;
150
        }
151
      }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

    
155
    public void Sink()
156
      {
157
      if( mCurrEffect==EFFECT_SINK ) return; 
158
         
159
      synchronized(lock)
160
        {
161
        DistortedEffects q = mRenderer.getEffects();
162
        q.abortByType(EffectType.VERTEX);
163
        q.abortByType(EffectType.FRAGMENT);
164
        q.apply(mEffectSink);
165
        mCurrEffect = EFFECT_SINK;
166
        }
167
      }
168

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170

    
171
    public void Chroma()
172
      {
173
      if( mCurrEffect==EFFECT_CHROMA ) return;
174
         
175
      synchronized(lock)
176
        {
177
        DistortedEffects q = mRenderer.getEffects();
178
        q.abortByType(EffectType.VERTEX);
179
        q.abortByType(EffectType.FRAGMENT);
180
        q.apply(mEffectChroma);
181
        mCurrEffect = EFFECT_CHROMA;
182
        }
183
      }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186

    
187
    public void Transparency()
188
      {
189
      if( mCurrEffect==EFFECT_TRANS ) return;   
190
      
191
      synchronized(lock)
192
        {
193
        DistortedEffects q = mRenderer.getEffects();
194
        q.abortByType(EffectType.VERTEX);
195
        q.abortByType(EffectType.FRAGMENT);
196
        q.apply(mEffectAlpha);
197
        mCurrEffect = EFFECT_TRANS;
198
        }
199
      }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

    
203
    public void Swirl()
204
      {
205
      if( mCurrEffect==EFFECT_SWIRL ) return;   
206
      
207
      synchronized(lock)
208
        {
209
        DistortedEffects q = mRenderer.getEffects();
210
        q.abortByType(EffectType.VERTEX);
211
        q.abortByType(EffectType.FRAGMENT);
212
        q.apply(mEffectSwirl);
213
        mCurrEffect = EFFECT_SWIRL;
214
        }
215
      }
216
    
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218

    
219
    public void Abort()
220
      {
221
      synchronized(lock)
222
        {
223
        DistortedEffects q = mRenderer.getEffects();
224
        q.abortByType(EffectType.VERTEX);
225
        q.abortByType(EffectType.FRAGMENT);
226
        mCenterVertex.removeAll();
227
        mRegionFragment.removeAll();
228
        mCurrEffect = EFFECT_POINTS;
229
        mRenderer.setRefresh();
230
        }
231
      }
232

    
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234
 
235
    public void drawCurve(Canvas c, long time)
236
      {
237
      synchronized(lock)
238
        {  
239
        int len = mCenterVertex.getNumPoints();
240

    
241
        float[] drawCoord = new float[3];
242
        Static3D cu;
243

    
244
        if( len>=2 )
245
          {
246
          float step = (float)LOOP_TIME/(NUM_POINTS+1);
247

    
248
          for(int i=0; i<NUM_POINTS; i++)
249
            {
250
            mPaint.setColor( 0xffffffff );
251
            mCenterVertex.get( drawCoord, 0, (long)(i*step) );
252
            c.drawCircle(drawCoord[0], drawCoord[1], mSize1, mPaint );
253
            }
254
          }
255
     
256
        mPaint.setColor(0xffff0000);
257
      
258
        for(int curr=0; curr<len; curr++)
259
          {       
260
          cu = mCenterVertex.getPoint(curr);
261
          c.drawCircle(cu.get1(), cu.get2(), mSize2, mPaint);
262
          }
263
        
264
        if( time-mTime > LOOP_TIME ) mTime = time;
265
        }
266
      }
267
       
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269
    
270
    @Override public boolean onTouchEvent(MotionEvent event) 
271
      {
272
      if( mCurrEffect!=EFFECT_POINTS ) return true;   
273

    
274
      int xDown, yDown;
275

    
276
      switch(event.getAction())
277
        {
278
        case MotionEvent.ACTION_DOWN: xDown = (int)event.getX();
279
                                      yDown = (int)event.getY();
280
                                    
281
                                      float gx, gy;
282
                                      Static3D dv;
283
                                      int len = mCenterVertex.getNumPoints();
284
                                 
285
                                      for(int g=0; g<len; g++)
286
                                        {
287
                                        dv = mCenterVertex.getPoint(g);
288
                                        gx = dv.get1();
289
                                        gy = dv.get2();
290

    
291
                                        float Z = mDistort.get3()/10;
292

    
293
                                        if( (xDown-gx)*(xDown-gx) + (yDown-gy)*(yDown-gy) < Z*Z )
294
                                          {
295
                                          moving = g;
296
                                          break;
297
                                          }
298
                                        }
299
                                      if( moving<0 )
300
                                        {
301
                                        synchronized(lock)
302
                                          {
303
                                          mCenterVertex.add(new Static3D(xDown,yDown,0));
304
                                          mRegionFragment.add(new Static4D(xDown,yDown,mSizeR,mSizeR));
305
                                          }
306
                                        }
307
                                      mRenderer.setRefresh();
308
                                      break;
309
        case MotionEvent.ACTION_MOVE: if( moving>=0 )
310
                                        {
311
                                        xDown = (int)event.getX();
312
                                        yDown = (int)event.getY();
313

    
314
                                        mCenterVertex.setPoint(moving, xDown, yDown, 0);
315
                                        mRegionFragment.setPoint(moving, xDown, yDown, mSizeR, mSizeR);
316
                                        }
317
                                      mRenderer.setRefresh();
318
                                      break;
319
        case MotionEvent.ACTION_UP  : moving = -1;
320
                                      break;
321
        }
322
      return true;
323
      }
324
  }
(3-3/3)