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.os.Build;
|
28
|
import android.view.MotionEvent;
|
29
|
import android.util.AttributeSet;
|
30
|
|
31
|
import org.distorted.library.EffectTypes;
|
32
|
import org.distorted.library.type.Dynamic2D;
|
33
|
import org.distorted.library.type.Dynamic4D;
|
34
|
import org.distorted.library.type.Static1D;
|
35
|
import org.distorted.library.type.Static2D;
|
36
|
import org.distorted.library.type.Static3D;
|
37
|
import org.distorted.library.type.Static4D;
|
38
|
|
39
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
40
|
|
41
|
public class MovingEffectsSurfaceView extends GLSurfaceView
|
42
|
{
|
43
|
private static final int LOOP_TIME = 5000;
|
44
|
private static final int NUM_POINTS= 100;
|
45
|
|
46
|
public static final int EFFECT_POINTS=0;
|
47
|
public static final int EFFECT_BUBBLE=1;
|
48
|
public static final int EFFECT_SINK =2;
|
49
|
public static final int EFFECT_TRANS =3;
|
50
|
public static final int EFFECT_CHROMA=4;
|
51
|
public static final int EFFECT_SWIRL =5;
|
52
|
|
53
|
private static Dynamic2D di2D;
|
54
|
private static Static4D dr;
|
55
|
private static Dynamic4D mRegion;
|
56
|
|
57
|
private static Paint mPaint;
|
58
|
private static int moving;
|
59
|
private static final Object lock = new Object();
|
60
|
private static long mTime = 0;
|
61
|
|
62
|
private static int mCurrEffect;
|
63
|
private static int mSize1, mSize2, mSizeR;
|
64
|
private static int mMax;
|
65
|
|
66
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
67
|
|
68
|
public MovingEffectsSurfaceView(Context c, AttributeSet attrs)
|
69
|
{
|
70
|
super(c, attrs);
|
71
|
|
72
|
mCurrEffect=EFFECT_POINTS;
|
73
|
mPaint = new Paint();
|
74
|
mPaint.setStyle(Style.FILL);
|
75
|
moving = -1;
|
76
|
|
77
|
di2D = new Dynamic2D(LOOP_TIME,0.0f);
|
78
|
mRegion = new Dynamic4D(LOOP_TIME,0.0f);
|
79
|
|
80
|
if(!isInEditMode())
|
81
|
{
|
82
|
setFocusable(true);
|
83
|
setFocusableInTouchMode(true);
|
84
|
|
85
|
setEGLContextClientVersion(2);
|
86
|
|
87
|
if( Build.FINGERPRINT.startsWith("generic") ) // when running on the emulator, insert a magic line that is
|
88
|
{ // supposed to cure the 'no config chosen' crash on emulator startup
|
89
|
setEGLConfigChooser(8, 8, 8, 8, 16, 0);
|
90
|
}
|
91
|
|
92
|
MovingEffectsRenderer mRenderer = new MovingEffectsRenderer(this);
|
93
|
setRenderer(mRenderer);
|
94
|
}
|
95
|
}
|
96
|
|
97
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
98
|
|
99
|
public static void onSurfaceChanged(int width,int height)
|
100
|
{
|
101
|
mMax = width>height ? width:height;
|
102
|
|
103
|
mSize1 = mMax/200;
|
104
|
mSize2 = mMax/80;
|
105
|
mSizeR = mMax/6;
|
106
|
|
107
|
dr = new Static4D(0,0, mSizeR,mSizeR);
|
108
|
}
|
109
|
|
110
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
111
|
|
112
|
public static int getCurrentEffect()
|
113
|
{
|
114
|
return mCurrEffect;
|
115
|
}
|
116
|
|
117
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
118
|
|
119
|
public static void Bubble()
|
120
|
{
|
121
|
if( mCurrEffect==EFFECT_BUBBLE ) return;
|
122
|
|
123
|
synchronized(lock)
|
124
|
{
|
125
|
MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
|
126
|
MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
|
127
|
MovingEffectsRenderer.mBackground.distort( new Static3D(0,0,mMax/10) , di2D, dr);
|
128
|
mCurrEffect = EFFECT_BUBBLE;
|
129
|
}
|
130
|
}
|
131
|
|
132
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
133
|
|
134
|
public static void Sink()
|
135
|
{
|
136
|
if( mCurrEffect==EFFECT_SINK ) return;
|
137
|
|
138
|
synchronized(lock)
|
139
|
{
|
140
|
MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
|
141
|
MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
|
142
|
MovingEffectsRenderer.mBackground.sink(new Static1D(10), di2D, dr);
|
143
|
mCurrEffect = EFFECT_SINK;
|
144
|
}
|
145
|
}
|
146
|
|
147
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
148
|
|
149
|
public static void Chroma()
|
150
|
{
|
151
|
if( mCurrEffect==EFFECT_CHROMA ) return;
|
152
|
|
153
|
synchronized(lock)
|
154
|
{
|
155
|
MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
|
156
|
MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
|
157
|
MovingEffectsRenderer.mBackground.chroma(new Static1D(0.5f), new Static3D(1,0,0), mRegion, true);
|
158
|
mCurrEffect = EFFECT_CHROMA;
|
159
|
}
|
160
|
}
|
161
|
|
162
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
163
|
|
164
|
public static void Transparency()
|
165
|
{
|
166
|
if( mCurrEffect==EFFECT_TRANS ) return;
|
167
|
|
168
|
synchronized(lock)
|
169
|
{
|
170
|
MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
|
171
|
MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
|
172
|
MovingEffectsRenderer.mBackground.alpha(new Static1D(0.5f), mRegion, true);
|
173
|
mCurrEffect = EFFECT_TRANS;
|
174
|
}
|
175
|
}
|
176
|
|
177
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
178
|
|
179
|
public static void Swirl()
|
180
|
{
|
181
|
if( mCurrEffect==EFFECT_SWIRL ) return;
|
182
|
|
183
|
synchronized(lock)
|
184
|
{
|
185
|
MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
|
186
|
MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
|
187
|
MovingEffectsRenderer.mBackground.swirl( new Static1D(30), di2D, dr);
|
188
|
mCurrEffect = EFFECT_SWIRL;
|
189
|
}
|
190
|
}
|
191
|
|
192
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
193
|
|
194
|
public static void Abort()
|
195
|
{
|
196
|
synchronized(lock)
|
197
|
{
|
198
|
MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.VERTEX);
|
199
|
MovingEffectsRenderer.mBackground.abortEffects(EffectTypes.FRAGMENT);
|
200
|
di2D.removeAll();
|
201
|
mRegion.removeAll();
|
202
|
mCurrEffect = EFFECT_POINTS;
|
203
|
}
|
204
|
}
|
205
|
|
206
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
207
|
|
208
|
public static void drawCurve(Canvas c, long time)
|
209
|
{
|
210
|
synchronized(lock)
|
211
|
{
|
212
|
int len = di2D.getNumPoints();
|
213
|
|
214
|
float[] drawCoord = new float[2];
|
215
|
Static2D cu;
|
216
|
int lit = mTime> 0 ? (int)((float)(time-mTime)*NUM_POINTS/LOOP_TIME) : 0;
|
217
|
|
218
|
if( len>=2 )
|
219
|
{
|
220
|
float step = (float)LOOP_TIME/(NUM_POINTS+1);
|
221
|
|
222
|
for(int i=0; i<NUM_POINTS; i++)
|
223
|
{
|
224
|
int color = i<=lit ? 0xff - (lit -i)*0xff/(NUM_POINTS-1)
|
225
|
: 0xff - (lit+NUM_POINTS-i)*0xff/(NUM_POINTS-1);
|
226
|
|
227
|
mPaint.setColor( 0xffffff + ((color&0xff)<<24) );
|
228
|
di2D.interpolateMain( drawCoord, 0, (long)(i*step) );
|
229
|
c.drawCircle(drawCoord[0], drawCoord[1], mSize1, mPaint );
|
230
|
}
|
231
|
}
|
232
|
|
233
|
mPaint.setColor(0xffff0000);
|
234
|
|
235
|
for(int curr=0; curr<len; curr++)
|
236
|
{
|
237
|
cu = di2D.getPoint(curr);
|
238
|
c.drawCircle(cu.getX(), cu.getY(), mSize2, mPaint);
|
239
|
}
|
240
|
|
241
|
if( time-mTime > LOOP_TIME ) mTime = time;
|
242
|
}
|
243
|
}
|
244
|
|
245
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
246
|
|
247
|
@Override public boolean onTouchEvent(MotionEvent event)
|
248
|
{
|
249
|
if( mCurrEffect!=EFFECT_POINTS ) return true;
|
250
|
|
251
|
int xDown, yDown;
|
252
|
|
253
|
switch(event.getAction())
|
254
|
{
|
255
|
case MotionEvent.ACTION_DOWN: xDown = (int)event.getX();
|
256
|
yDown = (int)event.getY();
|
257
|
|
258
|
float gx, gy;
|
259
|
Static2D dv;
|
260
|
int len = di2D.getNumPoints();
|
261
|
|
262
|
for(int g=0; g<len; g++)
|
263
|
{
|
264
|
dv = di2D.getPoint(g);
|
265
|
gx = dv.getX();
|
266
|
gy = dv.getY();
|
267
|
|
268
|
if( (xDown-gx)*(xDown-gx) + (yDown-gy)*(yDown-gy) < (mMax*mMax)/100 )
|
269
|
{
|
270
|
moving = g;
|
271
|
break;
|
272
|
}
|
273
|
}
|
274
|
if( moving<0 )
|
275
|
{
|
276
|
synchronized(lock)
|
277
|
{
|
278
|
di2D.add(new Static2D(xDown,yDown));
|
279
|
mRegion.add(new Static4D(xDown,yDown,mSizeR,mSizeR));
|
280
|
}
|
281
|
}
|
282
|
break;
|
283
|
case MotionEvent.ACTION_MOVE: if( moving>=0 )
|
284
|
{
|
285
|
xDown = (int)event.getX();
|
286
|
yDown = (int)event.getY();
|
287
|
|
288
|
di2D.setPoint(moving, xDown, yDown);
|
289
|
mRegion.setPoint(moving, xDown, yDown, mSizeR, mSizeR);
|
290
|
}
|
291
|
break;
|
292
|
case MotionEvent.ACTION_UP : moving = -1;
|
293
|
break;
|
294
|
}
|
295
|
return true;
|
296
|
}
|
297
|
}
|