Project

General

Profile

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

examples / src / main / java / org / distorted / examples / movingeffects / MovingEffectsSurfaceView.java @ 6f3a9b2a

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.DistortedEffects;
33
import org.distorted.library.EffectTypes;
34
import org.distorted.library.type.Dynamic3D;
35
import org.distorted.library.type.Dynamic4D;
36
import org.distorted.library.type.Static1D;
37
import org.distorted.library.type.Static2D;
38
import org.distorted.library.type.Static3D;
39
import org.distorted.library.type.Static4D;
40

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

    
43
public class MovingEffectsSurfaceView extends GLSurfaceView
44
    {
45
    private static final int LOOP_TIME = 5000;
46
    private static final int NUM_POINTS= 100;
47
   
48
    public static final int EFFECT_POINTS=0;
49
    public static final int EFFECT_BUBBLE=1;
50
    public static final int EFFECT_SINK  =2;
51
    public static final int EFFECT_TRANS =3;
52
    public static final int EFFECT_CHROMA=4;
53
    public static final int EFFECT_SWIRL =5;
54

    
55
    private static final Object lock = new Object();
56

    
57
    private Dynamic3D di3D;
58
    private Static4D dr;
59
    private Dynamic4D mRegion;
60

    
61
    private Paint mPaint;
62
    private int moving;
63
    private long mTime = 0;
64
    
65
    private int mCurrEffect;
66
    private int mSize1, mSize2, mSizeR;
67
    private int mMax;
68

    
69
    private MovingEffectsRenderer mRenderer;
70

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72
    
73
    public MovingEffectsSurfaceView(Context context, AttributeSet attrs)
74
      {
75
      super(context, attrs);
76

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

    
85
      mRenderer = new MovingEffectsRenderer(this);
86

    
87
      if(!isInEditMode())
88
        {
89
        setFocusable(true);
90
        setFocusableInTouchMode(true);
91
        final ActivityManager activityManager     = (android.app.ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
92
        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
93
        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
94
        setRenderer(mRenderer);
95
        }
96
      }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

    
100
    public void onSurfaceChanged(int width,int height)
101
      {
102
      mMax = width>height ? width:height;
103

    
104
      mSize1 = mMax/200;
105
      mSize2 = mMax/80;
106
      mSizeR = mMax/6;
107

    
108
      dr = new Static4D(0,0, mSizeR,mSizeR);
109
      }
110

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112

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

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

    
136
    public void Sink()
137
      {
138
      if( mCurrEffect==EFFECT_SINK ) return; 
139
         
140
      synchronized(lock)
141
        {
142
        DistortedEffects q = mRenderer.getEffects();
143
        q.abortEffects(EffectTypes.VERTEX);
144
        q.abortEffects(EffectTypes.FRAGMENT);
145
        q.sink(new Static1D(10), di3D, dr);
146
        mCurrEffect = EFFECT_SINK;
147
        }
148
      }
149

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151

    
152
    public void Chroma()
153
      {
154
      if( mCurrEffect==EFFECT_CHROMA ) return;
155
         
156
      synchronized(lock)
157
        {
158
        DistortedEffects q = mRenderer.getEffects();
159
        q.abortEffects(EffectTypes.VERTEX);
160
        q.abortEffects(EffectTypes.FRAGMENT);
161
        q.chroma(new Static1D(0.5f), new Static3D(1,0,0), mRegion, true);
162
        mCurrEffect = EFFECT_CHROMA;
163
        }
164
      }
165

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

    
168
    public void Transparency()
169
      {
170
      if( mCurrEffect==EFFECT_TRANS ) return;   
171
      
172
      synchronized(lock)
173
        {
174
        DistortedEffects q = mRenderer.getEffects();
175
        q.abortEffects(EffectTypes.VERTEX);
176
        q.abortEffects(EffectTypes.FRAGMENT);
177
        q.alpha(new Static1D(0.5f), mRegion, true);
178
        mCurrEffect = EFFECT_TRANS;
179
        }
180
      }
181

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

    
184
    public void Swirl()
185
      {
186
      if( mCurrEffect==EFFECT_SWIRL ) return;   
187
      
188
      synchronized(lock)
189
        {
190
        DistortedEffects q = mRenderer.getEffects();
191
        q.abortEffects(EffectTypes.VERTEX);
192
        q.abortEffects(EffectTypes.FRAGMENT);
193
        q.swirl( new Static1D(30), di3D, dr);
194
        mCurrEffect = EFFECT_SWIRL;
195
        }
196
      }
197
    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

    
200
    public void Abort()
201
      {
202
      synchronized(lock)
203
        {
204
        DistortedEffects q = mRenderer.getEffects();
205
        q.abortEffects(EffectTypes.VERTEX);
206
        q.abortEffects(EffectTypes.FRAGMENT);
207
        di3D.removeAll();
208
        mRegion.removeAll();
209
        mCurrEffect = EFFECT_POINTS;
210
        mRenderer.setRefresh();
211
        }
212
      }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215
 
216
    public void drawCurve(Canvas c, long time)
217
      {
218
      synchronized(lock)
219
        {  
220
        int len = di3D.getNumPoints();
221

    
222
        float[] drawCoord = new float[3];
223
        Static3D cu;
224

    
225
        if( len>=2 )
226
          {
227
          float step = (float)LOOP_TIME/(NUM_POINTS+1);
228

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

    
255
      int xDown, yDown;
256

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

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