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