1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2018 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.effects3d;
|
21
|
|
22
|
import android.os.Bundle;
|
23
|
import android.support.v4.app.Fragment;
|
24
|
import android.view.LayoutInflater;
|
25
|
import android.view.View;
|
26
|
import android.view.ViewGroup;
|
27
|
import android.widget.AdapterView;
|
28
|
import android.widget.ArrayAdapter;
|
29
|
import android.widget.LinearLayout;
|
30
|
import android.widget.Spinner;
|
31
|
|
32
|
import org.distorted.examples.R;
|
33
|
import org.distorted.library.effect.EffectName;
|
34
|
import org.distorted.library.effect.EffectType;
|
35
|
import org.distorted.library.main.DistortedEffects;
|
36
|
|
37
|
import java.util.ArrayList;
|
38
|
|
39
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
40
|
|
41
|
public class Effects3DTab extends Fragment implements AdapterView.OnItemSelectedListener
|
42
|
{
|
43
|
private EffectType mType;
|
44
|
private ArrayList<Effects3DEffect> mList;
|
45
|
private int mEffectAdd;
|
46
|
private EffectName[] mEffectNames;
|
47
|
private DistortedEffects mEffects;
|
48
|
private String[] mEffectStrings;
|
49
|
private int mTab, mLayout, mSpinner;
|
50
|
private int mChildren;
|
51
|
|
52
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
53
|
|
54
|
public Effects3DTab()
|
55
|
{
|
56
|
mList = new ArrayList<>();
|
57
|
mEffectAdd = 0;
|
58
|
mChildren = 0;
|
59
|
}
|
60
|
|
61
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
62
|
|
63
|
@Override
|
64
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
|
65
|
{
|
66
|
Bundle args = getArguments();
|
67
|
|
68
|
int position = args.getInt("position");
|
69
|
|
70
|
switch(position)
|
71
|
{
|
72
|
case 0: mTab = R.layout.effects3dtab0;
|
73
|
mLayout = R.id.effects3dlayout0;
|
74
|
mSpinner = R.id.effects3dspinner0;
|
75
|
mType = EffectType.MATRIX;
|
76
|
break;
|
77
|
case 1: mTab = R.layout.effects3dtab1;
|
78
|
mLayout = R.id.effects3dlayout1;
|
79
|
mSpinner = R.id.effects3dspinner1;
|
80
|
mType = EffectType.VERTEX;
|
81
|
break;
|
82
|
case 2: mTab = R.layout.effects3dtab2;
|
83
|
mLayout = R.id.effects3dlayout2;
|
84
|
mSpinner = R.id.effects3dspinner2;
|
85
|
mType = EffectType.FRAGMENT;
|
86
|
break;
|
87
|
case 3: mTab = R.layout.effects3dtab3;
|
88
|
mLayout = R.id.effects3dlayout3;
|
89
|
mSpinner = R.id.effects3dspinner3;
|
90
|
mType = EffectType.POSTPROCESS;
|
91
|
break;
|
92
|
}
|
93
|
|
94
|
Effects3DActivity2 act = (Effects3DActivity2)getActivity();
|
95
|
|
96
|
if( act!=null )
|
97
|
{
|
98
|
act.setTab(position, this);
|
99
|
mEffects = act.getEffects();
|
100
|
}
|
101
|
|
102
|
createEffectNames(mType);
|
103
|
mEffectStrings = new String[mEffectNames.length];
|
104
|
|
105
|
for (int i = 0; i < mEffectNames.length; i++)
|
106
|
{
|
107
|
mEffectStrings[i] = mEffectNames[i].name();
|
108
|
}
|
109
|
|
110
|
return inflater.inflate( mTab, container, false);
|
111
|
}
|
112
|
|
113
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
114
|
|
115
|
@Override
|
116
|
public void onStart()
|
117
|
{
|
118
|
super.onStart();
|
119
|
|
120
|
Effects3DActivity2 act = (Effects3DActivity2)getActivity();
|
121
|
|
122
|
ArrayAdapter<String> adapterEffect = new ArrayAdapter<>( act, android.R.layout.simple_spinner_item, mEffectStrings);
|
123
|
adapterEffect.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
124
|
|
125
|
Spinner effectSpinner = act.findViewById(mSpinner);
|
126
|
|
127
|
effectSpinner.setOnItemSelectedListener(this);
|
128
|
effectSpinner.setAdapter(adapterEffect);
|
129
|
|
130
|
resetData();
|
131
|
}
|
132
|
|
133
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
134
|
|
135
|
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
|
136
|
{
|
137
|
if( parent.getId() == mSpinner )
|
138
|
{
|
139
|
mEffectAdd = pos;
|
140
|
}
|
141
|
}
|
142
|
|
143
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
144
|
|
145
|
public void onNothingSelected(AdapterView<?> parent)
|
146
|
{
|
147
|
}
|
148
|
|
149
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
150
|
|
151
|
private void resetData()
|
152
|
{
|
153
|
Effects3DActivity2 act = (Effects3DActivity2)getActivity();
|
154
|
Effects3DSurfaceView view = act.findViewById(R.id.effects3dSurfaceView);
|
155
|
view.getRenderer().showRegionAndCenter(false,false);
|
156
|
}
|
157
|
|
158
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
159
|
|
160
|
void newEffect()
|
161
|
{
|
162
|
Effects3DActivity2 act = (Effects3DActivity2)getActivity();
|
163
|
Effects3DEffect eff = new Effects3DEffect(mEffectNames[mEffectAdd], act );
|
164
|
mList.add(eff);
|
165
|
|
166
|
mChildren++;
|
167
|
|
168
|
LinearLayout layout = act.findViewById(mLayout);
|
169
|
View view = eff.createView(mChildren);
|
170
|
layout.addView(view);
|
171
|
|
172
|
if( mEffectNames[mEffectAdd].getCenterDimension() > 0 )
|
173
|
{
|
174
|
View center = eff.createCenter(mChildren);
|
175
|
layout.addView(center);
|
176
|
}
|
177
|
|
178
|
if( mEffectNames[mEffectAdd].getRegionDimension() > 0 )
|
179
|
{
|
180
|
View region = eff.createRegion(mChildren);
|
181
|
layout.addView(region);
|
182
|
}
|
183
|
|
184
|
eff.apply(mEffects);
|
185
|
|
186
|
boolean region = act.getShowRegion();
|
187
|
boolean center = act.getShowCenter();
|
188
|
|
189
|
boolean show = (mEffectNames[mEffectAdd].getType()==EffectType.VERTEX);
|
190
|
Effects3DSurfaceView sv = act.findViewById(R.id.effects3dSurfaceView);
|
191
|
sv.getRenderer().showRegionAndCenter( (show && region) , (show && center) );
|
192
|
}
|
193
|
|
194
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
195
|
|
196
|
private void createEffectNames(EffectType type)
|
197
|
{
|
198
|
EffectName[] names = EffectName.values();
|
199
|
|
200
|
int numEffects=0;
|
201
|
|
202
|
for(int i=0; i<names.length; i++)
|
203
|
if( names[i].getType() == type ) numEffects++;
|
204
|
|
205
|
mEffectNames = new EffectName[numEffects];
|
206
|
|
207
|
numEffects=0;
|
208
|
|
209
|
for(int i=0; i<names.length; i++)
|
210
|
if( names[i].getType() == type )
|
211
|
{
|
212
|
mEffectNames[numEffects++] = names[i];
|
213
|
}
|
214
|
}
|
215
|
|
216
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
217
|
|
218
|
void removeAll()
|
219
|
{
|
220
|
Effects3DActivity2 act = (Effects3DActivity2)getActivity();
|
221
|
|
222
|
mChildren = 0;
|
223
|
|
224
|
mList.clear();
|
225
|
LinearLayout layout = act.findViewById(mLayout);
|
226
|
layout.removeAllViews();
|
227
|
mEffects.abortByType(mType);
|
228
|
|
229
|
resetData();
|
230
|
act.resetData();
|
231
|
|
232
|
if( mType==EffectType.MATRIX )
|
233
|
{
|
234
|
act.resetMatrixEffects();
|
235
|
}
|
236
|
}
|
237
|
|
238
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
239
|
|
240
|
void remove(View v)
|
241
|
{
|
242
|
for(Effects3DEffect effect: mList)
|
243
|
{
|
244
|
if( effect.thisView(v) )
|
245
|
{
|
246
|
Effects3DActivity2 act = (Effects3DActivity2)getActivity();
|
247
|
LinearLayout layout = act.findViewById(mLayout);
|
248
|
View view;
|
249
|
|
250
|
view = effect.getEffect();
|
251
|
if( view!=null ) layout.removeView(view);
|
252
|
view = effect.getCenter();
|
253
|
if( view!=null ) layout.removeView(view);
|
254
|
view = effect.getRegion();
|
255
|
if( view!=null ) layout.removeView(view);
|
256
|
|
257
|
mEffects.abortById(effect.getId());
|
258
|
|
259
|
int index = mList.indexOf(effect);
|
260
|
int capac = mList.size();
|
261
|
|
262
|
for(int i=index+1; i<capac; i++)
|
263
|
{
|
264
|
mList.get(i).setBackground(i-1);
|
265
|
}
|
266
|
|
267
|
mList.remove(effect);
|
268
|
mChildren--;
|
269
|
|
270
|
resetData();
|
271
|
|
272
|
break;
|
273
|
}
|
274
|
}
|
275
|
}
|
276
|
|
277
|
}
|