Project

General

Profile

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

examples / src / main / java / org / distorted / examples / movingeffects / MovingEffectsSurfaceView.java @ 89c3e2bf

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
        android.util.Log.e("View", "Using OpenGL ES "+configurationInfo.getGlEsVersion());
94
        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
95
        setRenderer(mRenderer);
96
        }
97
      }
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

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

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

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

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

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

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136

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

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

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

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168

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

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184

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

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

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

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

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

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

    
256
      int xDown, yDown;
257

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

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