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.effects2d;
|
21
|
|
22
|
import android.content.Context;
|
23
|
import android.opengl.GLSurfaceView;
|
24
|
import android.os.Build;
|
25
|
import android.view.MotionEvent;
|
26
|
import android.util.AttributeSet;
|
27
|
|
28
|
import org.distorted.library.EffectNames;
|
29
|
import org.distorted.library.EffectTypes;
|
30
|
import org.distorted.library.type.Dynamic1D;
|
31
|
import org.distorted.library.type.Static1D;
|
32
|
import org.distorted.library.type.Static2D;
|
33
|
import org.distorted.library.type.Static3D;
|
34
|
import org.distorted.library.type.Static4D;
|
35
|
import org.distorted.library.type.Dynamic3D;
|
36
|
|
37
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
38
|
|
39
|
public class Effects2DSurfaceView extends GLSurfaceView
|
40
|
{
|
41
|
private Effects2DRenderer mRenderer;
|
42
|
private int mCurrentEffect;
|
43
|
private int mScrW, mScrH;
|
44
|
|
45
|
private Static4D mRegionV, mRegionF;
|
46
|
private Static2D mPoint;
|
47
|
private Dynamic1D mInterA, mInterB, mInterC, mInterS;
|
48
|
private Dynamic3D mInterD;
|
49
|
|
50
|
private final static Static3D mRED = new Static3D(1,0,0);
|
51
|
|
52
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
53
|
|
54
|
public Effects2DSurfaceView(Context c, AttributeSet attrs)
|
55
|
{
|
56
|
super(c, attrs);
|
57
|
|
58
|
int duration = 10000;
|
59
|
float count = 1.0f;
|
60
|
int h = 30;
|
61
|
int r = 20;
|
62
|
|
63
|
mInterD = new Dynamic3D(duration,count);
|
64
|
|
65
|
mInterD.add(new Static3D( 0, r, h ));
|
66
|
mInterD.add(new Static3D(-r, 0, h ));
|
67
|
mInterD.add(new Static3D( 0,-r, h ));
|
68
|
mInterD.add(new Static3D( r, 0, h ));
|
69
|
|
70
|
mInterA = new Dynamic1D(duration,count);
|
71
|
mInterA.add(new Static1D(1));
|
72
|
mInterA.add(new Static1D(0));
|
73
|
|
74
|
mInterS = new Dynamic1D(duration,count);
|
75
|
mInterS.add(new Static1D(1.0f));
|
76
|
mInterS.add(new Static1D(0.3f));
|
77
|
|
78
|
mInterC = new Dynamic1D(duration,count);
|
79
|
mInterC.add(new Static1D(1));
|
80
|
mInterC.add(new Static1D(0));
|
81
|
|
82
|
mInterB = new Dynamic1D(duration,count);
|
83
|
mInterB.add(new Static1D(0));
|
84
|
mInterB.add(new Static1D(1));
|
85
|
|
86
|
if(!isInEditMode())
|
87
|
{
|
88
|
setFocusable(true);
|
89
|
setFocusableInTouchMode(true);
|
90
|
|
91
|
setEGLContextClientVersion(2);
|
92
|
|
93
|
if( Build.FINGERPRINT.startsWith("generic") ) // when running on the emulator, insert a magic line that is
|
94
|
{ // supposed to cure the 'no config chosen' crash on emulator startup
|
95
|
setEGLConfigChooser(8, 8, 8, 8, 16, 0);
|
96
|
}
|
97
|
|
98
|
mRenderer = new Effects2DRenderer(this);
|
99
|
setRenderer(mRenderer);
|
100
|
|
101
|
mPoint = new Static2D(0,0);
|
102
|
mRegionV= new Static4D(0,0,60,60);
|
103
|
mRegionF= new Static4D(0,0,60,60);
|
104
|
|
105
|
setEffect(0);
|
106
|
}
|
107
|
}
|
108
|
|
109
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
110
|
|
111
|
Effects2DRenderer getRenderer()
|
112
|
{
|
113
|
return mRenderer;
|
114
|
}
|
115
|
|
116
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
117
|
|
118
|
public void setScreenSize(int width, int height)
|
119
|
{
|
120
|
mScrW = width;
|
121
|
mScrH = height;
|
122
|
}
|
123
|
|
124
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
125
|
|
126
|
public void setEffect(int effect)
|
127
|
{
|
128
|
mCurrentEffect = effect;
|
129
|
}
|
130
|
|
131
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
132
|
|
133
|
@Override public boolean onTouchEvent(MotionEvent event)
|
134
|
{
|
135
|
int action = event.getAction();
|
136
|
int x, y;
|
137
|
long id;
|
138
|
|
139
|
switch(action)
|
140
|
{
|
141
|
case MotionEvent.ACTION_DOWN: x = (int)event.getX()* mRenderer.BWID/mScrW;
|
142
|
y = (int)event.getY()* mRenderer.BHEI/mScrH;
|
143
|
mPoint.set(x,y);
|
144
|
mRegionF.set(x,y,60,60);
|
145
|
Effects2DActivity act = (Effects2DActivity)getContext();
|
146
|
|
147
|
switch(mCurrentEffect)
|
148
|
{
|
149
|
case 0: id = mRenderer.mBackground.distort(mInterD, mPoint, mRegionV);
|
150
|
act.effectAdded(id, EffectNames.DISTORT, EffectTypes.VERTEX);
|
151
|
break;
|
152
|
case 1: id = mRenderer.mBackground.sink(mInterS, mPoint, mRegionV);
|
153
|
act.effectAdded(id, EffectNames.SINK, EffectTypes.VERTEX);
|
154
|
break;
|
155
|
case 2: id = mRenderer.mBackground.alpha(mInterA, mRegionF, true);
|
156
|
act.effectAdded(id, EffectNames.ALPHA, EffectTypes.FRAGMENT);
|
157
|
break;
|
158
|
case 3: id = mRenderer.mBackground.saturation(mInterB, mRegionF, false);
|
159
|
act.effectAdded(id, EffectNames.SATURATION, EffectTypes.FRAGMENT);
|
160
|
break;
|
161
|
case 4: id = mRenderer.mBackground.chroma(mInterC, mRED, mRegionF, true);
|
162
|
act.effectAdded(id, EffectNames.CHROMA, EffectTypes.FRAGMENT);
|
163
|
break;
|
164
|
}
|
165
|
|
166
|
break;
|
167
|
}
|
168
|
|
169
|
return true;
|
170
|
}
|
171
|
|
172
|
}
|