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.VertexEffectDeform;
|
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.Static1D;
|
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 mCenter;
|
61
|
private Static4D mRegionVertex;
|
62
|
private Static3D mRegionFragment;
|
63
|
|
64
|
private Paint mPaint;
|
65
|
private int moving;
|
66
|
private int mScrWidth, mScrHeight;
|
67
|
private long mTime = 0;
|
68
|
|
69
|
private int mCurrEffect;
|
70
|
private int mSize1, mSize2;
|
71
|
private Static3D mDeform;
|
72
|
|
73
|
private MovingEffectsRenderer mRenderer;
|
74
|
|
75
|
private VertexEffectDeform mEffectDeform;
|
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
|
mCenter = new Dynamic3D(LOOP_TIME,0.0f);
|
93
|
mRegionFragment = new Static3D(0,0,0);
|
94
|
mRegionVertex = new Static4D(0,0,0,0);
|
95
|
mDeform = 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
|
mEffectDeform = new VertexEffectDeform ( mDeform , mCenter, mRegionVertex);
|
109
|
mEffectSink = new VertexEffectSink (new Static1D(10) , mCenter, mRegionVertex);
|
110
|
mEffectSwirl = new VertexEffectSwirl (new Static1D(30) , mCenter, mRegionVertex);
|
111
|
mEffectAlpha = new FragmentEffectAlpha (new Static1D(0.5f), mCenter, mRegionFragment, true);
|
112
|
mEffectChroma = new FragmentEffectChroma(new Static1D(0.5f), new Static3D(1,0,0), mCenter, 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
|
mScrWidth = width;
|
123
|
mScrHeight = height;
|
124
|
|
125
|
mSize1 = max/200;
|
126
|
mSize2 = max/80;
|
127
|
int rad= max/6;
|
128
|
|
129
|
mRegionVertex.set(0,0,0,rad);
|
130
|
mRegionFragment.set(rad,rad,rad);
|
131
|
mDeform.set2(max/7);
|
132
|
}
|
133
|
|
134
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
135
|
|
136
|
public int getCurrentEffect()
|
137
|
{
|
138
|
return mCurrEffect;
|
139
|
}
|
140
|
|
141
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
142
|
|
143
|
public void Bubble()
|
144
|
{
|
145
|
if( mCurrEffect==EFFECT_BUBBLE ) return;
|
146
|
|
147
|
synchronized(lock)
|
148
|
{
|
149
|
DistortedEffects q = mRenderer.getEffects();
|
150
|
q.abortByType(EffectType.VERTEX);
|
151
|
q.abortByType(EffectType.FRAGMENT);
|
152
|
q.apply(mEffectDeform);
|
153
|
mCurrEffect = EFFECT_BUBBLE;
|
154
|
}
|
155
|
}
|
156
|
|
157
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
158
|
|
159
|
public void Sink()
|
160
|
{
|
161
|
if( mCurrEffect==EFFECT_SINK ) return;
|
162
|
|
163
|
synchronized(lock)
|
164
|
{
|
165
|
DistortedEffects q = mRenderer.getEffects();
|
166
|
q.abortByType(EffectType.VERTEX);
|
167
|
q.abortByType(EffectType.FRAGMENT);
|
168
|
q.apply(mEffectSink);
|
169
|
mCurrEffect = EFFECT_SINK;
|
170
|
}
|
171
|
}
|
172
|
|
173
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
174
|
|
175
|
public void Chroma()
|
176
|
{
|
177
|
if( mCurrEffect==EFFECT_CHROMA ) return;
|
178
|
|
179
|
synchronized(lock)
|
180
|
{
|
181
|
DistortedEffects q = mRenderer.getEffects();
|
182
|
q.abortByType(EffectType.VERTEX);
|
183
|
q.abortByType(EffectType.FRAGMENT);
|
184
|
q.apply(mEffectChroma);
|
185
|
mCurrEffect = EFFECT_CHROMA;
|
186
|
}
|
187
|
}
|
188
|
|
189
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
190
|
|
191
|
public void Transparency()
|
192
|
{
|
193
|
if( mCurrEffect==EFFECT_TRANS ) return;
|
194
|
|
195
|
synchronized(lock)
|
196
|
{
|
197
|
DistortedEffects q = mRenderer.getEffects();
|
198
|
q.abortByType(EffectType.VERTEX);
|
199
|
q.abortByType(EffectType.FRAGMENT);
|
200
|
q.apply(mEffectAlpha);
|
201
|
mCurrEffect = EFFECT_TRANS;
|
202
|
}
|
203
|
}
|
204
|
|
205
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
206
|
|
207
|
public void Swirl()
|
208
|
{
|
209
|
if( mCurrEffect==EFFECT_SWIRL ) return;
|
210
|
|
211
|
synchronized(lock)
|
212
|
{
|
213
|
DistortedEffects q = mRenderer.getEffects();
|
214
|
q.abortByType(EffectType.VERTEX);
|
215
|
q.abortByType(EffectType.FRAGMENT);
|
216
|
q.apply(mEffectSwirl);
|
217
|
mCurrEffect = EFFECT_SWIRL;
|
218
|
}
|
219
|
}
|
220
|
|
221
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
222
|
|
223
|
public void Abort()
|
224
|
{
|
225
|
synchronized(lock)
|
226
|
{
|
227
|
DistortedEffects q = mRenderer.getEffects();
|
228
|
q.abortByType(EffectType.VERTEX);
|
229
|
q.abortByType(EffectType.FRAGMENT);
|
230
|
mCenter.removeAll();
|
231
|
mCurrEffect = EFFECT_POINTS;
|
232
|
mRenderer.setRefresh();
|
233
|
}
|
234
|
}
|
235
|
|
236
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
237
|
|
238
|
public void drawCurve(Canvas c, long time)
|
239
|
{
|
240
|
synchronized(lock)
|
241
|
{
|
242
|
int len = mCenter.getNumPoints();
|
243
|
|
244
|
float[] drawCoord = new float[3];
|
245
|
Static3D cu;
|
246
|
|
247
|
if( len>=2 )
|
248
|
{
|
249
|
float step = (float)LOOP_TIME/(NUM_POINTS+1);
|
250
|
|
251
|
for(int i=0; i<NUM_POINTS; i++)
|
252
|
{
|
253
|
mPaint.setColor( 0xffffffff );
|
254
|
mCenter.get( drawCoord, 0, (long)(i*step) );
|
255
|
c.drawCircle(drawCoord[0] + mScrWidth*0.5f, mScrHeight*0.5f - drawCoord[1], mSize1, mPaint );
|
256
|
}
|
257
|
}
|
258
|
|
259
|
mPaint.setColor(0xffff0000);
|
260
|
|
261
|
for(int curr=0; curr<len; curr++)
|
262
|
{
|
263
|
cu = mCenter.getPoint(curr);
|
264
|
c.drawCircle(cu.get0() + mScrWidth*0.5f, mScrHeight*0.5f - cu.get1(), mSize2, mPaint);
|
265
|
}
|
266
|
|
267
|
if( time-mTime > LOOP_TIME ) mTime = time;
|
268
|
}
|
269
|
}
|
270
|
|
271
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
272
|
|
273
|
@Override
|
274
|
public boolean onTouchEvent(MotionEvent event)
|
275
|
{
|
276
|
if( mCurrEffect!=EFFECT_POINTS ) return true;
|
277
|
|
278
|
int xDown = (int)event.getX() - mScrWidth/2;
|
279
|
int yDown = mScrHeight/2 - (int)event.getY();
|
280
|
|
281
|
switch(event.getAction())
|
282
|
{
|
283
|
case MotionEvent.ACTION_DOWN: float gx, gy;
|
284
|
Static3D dv;
|
285
|
int len = mCenter.getNumPoints();
|
286
|
|
287
|
for(int g=0; g<len; g++)
|
288
|
{
|
289
|
dv = mCenter.getPoint(g);
|
290
|
gx = dv.get0();
|
291
|
gy = dv.get1();
|
292
|
|
293
|
float Z = mDeform.get2()/7;
|
294
|
|
295
|
if( (xDown-gx)*(xDown-gx) + (yDown-gy)*(yDown-gy) < Z*Z )
|
296
|
{
|
297
|
moving = g;
|
298
|
break;
|
299
|
}
|
300
|
}
|
301
|
if( moving<0 )
|
302
|
{
|
303
|
synchronized(lock)
|
304
|
{
|
305
|
mCenter.add(new Static3D(xDown,yDown,0));
|
306
|
}
|
307
|
}
|
308
|
mRenderer.setRefresh();
|
309
|
break;
|
310
|
case MotionEvent.ACTION_MOVE: if( moving>=0 )
|
311
|
{
|
312
|
mCenter.setPoint(moving, xDown, yDown, 0);
|
313
|
}
|
314
|
mRenderer.setRefresh();
|
315
|
break;
|
316
|
case MotionEvent.ACTION_UP : moving = -1;
|
317
|
break;
|
318
|
}
|
319
|
return true;
|
320
|
}
|
321
|
}
|