Project

General

Profile

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

examples / src / main / java / org / distorted / examples / movingeffects / MovingEffectsSurfaceView.java @ 5e3cd4e1

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 mCenterVertex;
61
    private Static4D mRegionVertex;
62
    private Dynamic4D mRegionFragment;
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 Static3D mDistort;
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
      mCenterVertex   = new Dynamic3D(LOOP_TIME,0.0f);
92
      mRegionFragment = new Dynamic4D(LOOP_TIME,0.0f);
93
      mRegionVertex   = new Static4D(0,0,0,0);
94
      mDistort        = new Static3D(0,0,0);
95

    
96
      mRenderer = new MovingEffectsRenderer(this);
97

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

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

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

    
114
    public void onSurfaceChanged(int width,int height)
115
      {
116
      int max = width>height ? width:height;
117

    
118
      mSize1 = max/200;
119
      mSize2 = max/80;
120
      mSizeR = max/6;
121

    
122
      mRegionVertex.set(0,0, mSizeR,mSizeR);
123
      mDistort.set3(max/10);
124
      }
125

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

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

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

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

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166

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

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

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

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198

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

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

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230
 
231
    public void drawCurve(Canvas c, long time)
232
      {
233
      synchronized(lock)
234
        {  
235
        int len = mCenterVertex.getNumPoints();
236

    
237
        float[] drawCoord = new float[3];
238
        Static3D cu;
239

    
240
        if( len>=2 )
241
          {
242
          float step = (float)LOOP_TIME/(NUM_POINTS+1);
243

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

    
270
      int xDown, yDown;
271

    
272
      switch(event.getAction())
273
        {
274
        case MotionEvent.ACTION_DOWN: xDown = (int)event.getX();
275
                                      yDown = (int)event.getY();
276
                                    
277
                                      float gx, gy;
278
                                      Static2D dv;
279
                                      int len = mCenterVertex.getNumPoints();
280
                                 
281
                                      for(int g=0; g<len; g++)
282
                                        {
283
                                        dv = mCenterVertex.getPoint(g);
284
                                        gx = dv.getX();
285
                                        gy = dv.getY();
286

    
287
                                        float Z = mDistort.getZ()/10;
288

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

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