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.fragment3d;
|
21
|
|
22
|
import android.app.Activity;
|
23
|
import android.opengl.GLSurfaceView;
|
24
|
import android.os.Bundle;
|
25
|
import android.view.Gravity;
|
26
|
import android.view.View;
|
27
|
import android.widget.AdapterView;
|
28
|
import android.widget.ArrayAdapter;
|
29
|
import android.widget.Button;
|
30
|
import android.widget.LinearLayout;
|
31
|
import android.widget.NumberPicker;
|
32
|
import android.widget.SeekBar;
|
33
|
import android.widget.SeekBar.OnSeekBarChangeListener;
|
34
|
import android.widget.Spinner;
|
35
|
import android.widget.TableRow;
|
36
|
import android.widget.TextView;
|
37
|
|
38
|
import org.distorted.examples.R;
|
39
|
import org.distorted.library.Distorted;
|
40
|
import org.distorted.library.DistortedBitmap;
|
41
|
import org.distorted.library.DistortedCubes;
|
42
|
import org.distorted.library.DistortedObject;
|
43
|
import org.distorted.library.EffectNames;
|
44
|
import org.distorted.library.EffectTypes;
|
45
|
import org.distorted.library.type.Dynamic1D;
|
46
|
import org.distorted.library.type.Dynamic3D;
|
47
|
|
48
|
import java.util.ArrayList;
|
49
|
|
50
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
51
|
|
52
|
public class Fragment3DActivity extends Activity
|
53
|
implements OnSeekBarChangeListener,
|
54
|
View.OnClickListener,
|
55
|
AdapterView.OnItemSelectedListener
|
56
|
{
|
57
|
private static final int COLOR_OFF = 0xffffe81f;
|
58
|
private static final int COLOR_ON = 0xff0000ff;
|
59
|
|
60
|
private boolean firstScreen;
|
61
|
|
62
|
// fields needed for the first 'pick-a-shape' screen
|
63
|
//
|
64
|
private int mNumCols = 3;
|
65
|
private int mNumRows = 3;
|
66
|
private NumberPicker mColsPicker, mRowsPicker;
|
67
|
private LinearLayout mLay;
|
68
|
private boolean[] mShape;
|
69
|
private DistortedObject mObject;
|
70
|
private int mObjectType;
|
71
|
private int mBitmap;
|
72
|
|
73
|
// fields needed for the second 'apply fragment effects' screen
|
74
|
//
|
75
|
private TextView textCenter;
|
76
|
private int centerX, centerY;
|
77
|
private float fcenterX, fcenterY;
|
78
|
private ArrayList<Fragment3DEffect> mEffects;
|
79
|
|
80
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
81
|
|
82
|
@Override
|
83
|
protected void onCreate(Bundle savedState)
|
84
|
{
|
85
|
super.onCreate(savedState);
|
86
|
|
87
|
mEffects = new ArrayList<>();
|
88
|
|
89
|
setContentView(R.layout.objectpickerlayout);
|
90
|
|
91
|
mColsPicker = (NumberPicker)findViewById(R.id.objectpicker_cols);
|
92
|
mRowsPicker = (NumberPicker)findViewById(R.id.objectpicker_rows);
|
93
|
|
94
|
mColsPicker.setMaxValue(10);
|
95
|
mColsPicker.setMinValue( 0);
|
96
|
mRowsPicker.setMaxValue(10);
|
97
|
mRowsPicker.setMinValue( 0);
|
98
|
|
99
|
mColsPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener()
|
100
|
{
|
101
|
@Override
|
102
|
public void onValueChange(NumberPicker picker, int oldVal, int newVal)
|
103
|
{
|
104
|
mNumCols = mColsPicker.getValue();
|
105
|
}
|
106
|
});
|
107
|
|
108
|
mRowsPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener()
|
109
|
{
|
110
|
@Override
|
111
|
public void onValueChange(NumberPicker picker, int oldVal, int newVal)
|
112
|
{
|
113
|
mNumRows = mRowsPicker.getValue();
|
114
|
}
|
115
|
});
|
116
|
|
117
|
firstScreen = true;
|
118
|
|
119
|
mObjectType = 0;
|
120
|
|
121
|
Spinner typeSpinner = (Spinner)findViewById(R.id.objectpicker_spinnerType);
|
122
|
typeSpinner.setOnItemSelectedListener(this);
|
123
|
|
124
|
String[] objectType = new String[] {"DistortedCubes", "DistortedBitmap"};
|
125
|
|
126
|
ArrayAdapter<String> adapterType = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, objectType);
|
127
|
adapterType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
128
|
typeSpinner.setAdapter(adapterType);
|
129
|
|
130
|
Spinner bitmapSpinner = (Spinner)findViewById(R.id.objectpicker_spinnerBitmap);
|
131
|
bitmapSpinner.setOnItemSelectedListener(this);
|
132
|
|
133
|
String[] objectBitmap = new String[] {"Girl", "Dog", "Cat", "Grid"};
|
134
|
|
135
|
ArrayAdapter<String> adapterBitmap = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, objectBitmap);
|
136
|
adapterBitmap.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
137
|
bitmapSpinner.setAdapter(adapterBitmap);
|
138
|
}
|
139
|
|
140
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
141
|
|
142
|
private void setGrid()
|
143
|
{
|
144
|
mLay = (LinearLayout)findViewById(R.id.objectpicker_buttongrid);
|
145
|
|
146
|
int width = mLay.getWidth();
|
147
|
int height= mLay.getHeight();
|
148
|
int w = mNumCols>0 ? (width / mNumCols) -10 : 0;
|
149
|
int h = mNumRows>0 ? (height/ mNumRows) -10 : 0;
|
150
|
int size= w<h ? w:h;
|
151
|
int pad = size/20;
|
152
|
|
153
|
mLay.removeAllViews();
|
154
|
|
155
|
mShape = new boolean[mNumRows*mNumCols];
|
156
|
|
157
|
TableRow.LayoutParams p = new android.widget.TableRow.LayoutParams();
|
158
|
|
159
|
p.rightMargin = pad;
|
160
|
p.leftMargin = pad;
|
161
|
p.topMargin = pad;
|
162
|
p.bottomMargin = pad;
|
163
|
p.height = size;
|
164
|
p.width = size;
|
165
|
|
166
|
for (int rows=0; rows<mNumRows; rows++)
|
167
|
{
|
168
|
TableRow tr = new TableRow(this);
|
169
|
tr.setGravity(Gravity.CENTER);
|
170
|
|
171
|
for(int cols=0; cols<mNumCols; cols++)
|
172
|
{
|
173
|
Button b = new Button(this);
|
174
|
b.setOnClickListener(this);
|
175
|
b.setId(rows*mNumCols+cols);
|
176
|
b.setLayoutParams(p);
|
177
|
b.setBackgroundColor(COLOR_ON);
|
178
|
tr.addView(b, p);
|
179
|
mShape[rows*mNumCols+cols] = true;
|
180
|
}
|
181
|
|
182
|
mLay.addView(tr);
|
183
|
}
|
184
|
}
|
185
|
|
186
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
187
|
|
188
|
public DistortedObject getObject()
|
189
|
{
|
190
|
return mObject;
|
191
|
}
|
192
|
|
193
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
194
|
|
195
|
public int getBitmap()
|
196
|
{
|
197
|
return mBitmap;
|
198
|
}
|
199
|
|
200
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
201
|
|
202
|
public void onClick(View view)
|
203
|
{
|
204
|
Button tmp = (Button)view;
|
205
|
int id = tmp.getId();
|
206
|
mShape[id] = !mShape[id];
|
207
|
tmp.setBackgroundColor(mShape[id] ? COLOR_ON:COLOR_OFF);
|
208
|
}
|
209
|
|
210
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
211
|
|
212
|
public void Continue(View v)
|
213
|
{
|
214
|
firstScreen = false;
|
215
|
|
216
|
if( mObjectType==1 )
|
217
|
{
|
218
|
mObject = new DistortedBitmap(100,100,mNumCols);
|
219
|
setFragmentView();
|
220
|
}
|
221
|
else
|
222
|
{
|
223
|
View view = getLayoutInflater().inflate(R.layout.objectpicker2layout, null);
|
224
|
|
225
|
setContentView(view);
|
226
|
|
227
|
view.post(new Runnable() {
|
228
|
@Override
|
229
|
public void run() {
|
230
|
setGrid();
|
231
|
}
|
232
|
});
|
233
|
}
|
234
|
}
|
235
|
|
236
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
237
|
|
238
|
public void Create(View v)
|
239
|
{
|
240
|
firstScreen = false;
|
241
|
|
242
|
String str = "";
|
243
|
|
244
|
for(int i=0; i<mNumRows*mNumCols; i++)
|
245
|
str += mShape[i] ? "1" : "0";
|
246
|
|
247
|
mObject = new DistortedCubes(mNumCols, str, 10);
|
248
|
|
249
|
setFragmentView();
|
250
|
}
|
251
|
|
252
|
|
253
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
254
|
|
255
|
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
|
256
|
{
|
257
|
switch(parent.getId())
|
258
|
{
|
259
|
case R.id.objectpicker_spinnerType : mObjectType = pos; break;
|
260
|
case R.id.objectpicker_spinnerBitmap: switch(pos)
|
261
|
{
|
262
|
case 0: mBitmap = R.raw.face; break;
|
263
|
case 1: mBitmap = R.raw.dog; break;
|
264
|
case 2: mBitmap = R.raw.cat; break;
|
265
|
case 3: mBitmap = R.raw.grid; break;
|
266
|
}
|
267
|
break;
|
268
|
}
|
269
|
}
|
270
|
|
271
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
272
|
|
273
|
public void onNothingSelected(AdapterView<?> parent)
|
274
|
{
|
275
|
}
|
276
|
|
277
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
278
|
// 'second screen' methods
|
279
|
|
280
|
private void setFragmentView()
|
281
|
{
|
282
|
final View view = getLayoutInflater().inflate(R.layout.fragment3dlayout, null);
|
283
|
|
284
|
setContentView(view);
|
285
|
|
286
|
centerX = 50;
|
287
|
centerY = 50;
|
288
|
|
289
|
textCenter = (TextView)view.findViewById(R.id.fragment3dcenterText);
|
290
|
computeCenter();
|
291
|
setCenterText();
|
292
|
|
293
|
SeekBar bar;
|
294
|
|
295
|
bar = (SeekBar)view.findViewById(R.id.fragment3dcenterX);
|
296
|
bar.setOnSeekBarChangeListener(this);
|
297
|
bar.setProgress(centerX);
|
298
|
|
299
|
bar = (SeekBar)view.findViewById(R.id.fragment3dcenterY);
|
300
|
bar.setOnSeekBarChangeListener(this);
|
301
|
bar.setProgress(centerY);
|
302
|
}
|
303
|
|
304
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
305
|
|
306
|
public void newChroma(View v)
|
307
|
{
|
308
|
Fragment3DEffect chroma = new Fragment3DEffect(EffectNames.CHROMA);
|
309
|
mEffects.add(chroma);
|
310
|
|
311
|
View view = chroma.createView(this);
|
312
|
LinearLayout layout = (LinearLayout)findViewById(R.id.fragment3dlayout);
|
313
|
layout.addView(view);
|
314
|
|
315
|
Dynamic1D dyn1 = chroma.getDyn1();
|
316
|
Dynamic3D dyn3 = chroma.getDyn3();
|
317
|
|
318
|
mObject.chroma(dyn1,dyn3);
|
319
|
}
|
320
|
|
321
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
322
|
|
323
|
public void newAlpha(View v)
|
324
|
{
|
325
|
Fragment3DEffect alpha = new Fragment3DEffect(EffectNames.ALPHA);
|
326
|
mEffects.add(alpha);
|
327
|
|
328
|
View view = alpha.createView(this);
|
329
|
LinearLayout layout = (LinearLayout)findViewById(R.id.fragment3dlayout);
|
330
|
layout.addView(view);
|
331
|
|
332
|
Dynamic1D dyn1 = alpha.getDyn1();
|
333
|
|
334
|
mObject.alpha(dyn1);
|
335
|
}
|
336
|
|
337
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
338
|
|
339
|
public void newBrightness(View v)
|
340
|
{
|
341
|
Fragment3DEffect brightness = new Fragment3DEffect(EffectNames.BRIGHTNESS);
|
342
|
mEffects.add(brightness);
|
343
|
|
344
|
View view = brightness.createView(this);
|
345
|
LinearLayout layout = (LinearLayout)findViewById(R.id.fragment3dlayout);
|
346
|
layout.addView(view);
|
347
|
|
348
|
Dynamic1D dyn1 = brightness.getDyn1();
|
349
|
|
350
|
mObject.brightness(dyn1);
|
351
|
}
|
352
|
|
353
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
354
|
|
355
|
public void newSaturation(View v)
|
356
|
{
|
357
|
Fragment3DEffect saturation = new Fragment3DEffect(EffectNames.SATURATION);
|
358
|
mEffects.add(saturation);
|
359
|
|
360
|
View view = saturation.createView(this);
|
361
|
LinearLayout layout = (LinearLayout)findViewById(R.id.fragment3dlayout);
|
362
|
layout.addView(view);
|
363
|
|
364
|
Dynamic1D dyn1 = saturation.getDyn1();
|
365
|
|
366
|
mObject.saturation(dyn1);
|
367
|
}
|
368
|
|
369
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
370
|
|
371
|
public void removeAll(View v)
|
372
|
{
|
373
|
mEffects.clear();
|
374
|
LinearLayout layout = (LinearLayout)findViewById(R.id.fragment3dlayout);
|
375
|
layout.removeAllViews();
|
376
|
mObject.abortEffects(EffectTypes.FRAGMENT);
|
377
|
}
|
378
|
|
379
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
380
|
|
381
|
private void computeCenter()
|
382
|
{
|
383
|
Fragment3DSurfaceView view = (Fragment3DSurfaceView)findViewById(R.id.fragment3dSurfaceView);
|
384
|
|
385
|
fcenterX = centerX;
|
386
|
fcenterY = centerY;
|
387
|
|
388
|
view.getRenderer().setCenter( fcenterX, fcenterY );
|
389
|
}
|
390
|
|
391
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
392
|
|
393
|
private void setCenterText()
|
394
|
{
|
395
|
fcenterX = ((int)(100*fcenterX))/100.0f;
|
396
|
fcenterY = ((int)(100*fcenterY))/100.0f;
|
397
|
|
398
|
textCenter.setText("center("+fcenterX+","+fcenterY+")");
|
399
|
}
|
400
|
|
401
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
402
|
|
403
|
public float getCenterX()
|
404
|
{
|
405
|
return fcenterX;
|
406
|
}
|
407
|
|
408
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
409
|
|
410
|
public float getCenterY()
|
411
|
{
|
412
|
return fcenterY;
|
413
|
}
|
414
|
|
415
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
416
|
// Overrides
|
417
|
|
418
|
@Override
|
419
|
protected void onPause()
|
420
|
{
|
421
|
GLSurfaceView mView = (GLSurfaceView)findViewById(R.id.fragment3dSurfaceView);
|
422
|
if( mView!=null ) mView.onPause();
|
423
|
super.onPause();
|
424
|
}
|
425
|
|
426
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
427
|
|
428
|
@Override
|
429
|
protected void onResume()
|
430
|
{
|
431
|
super.onResume();
|
432
|
GLSurfaceView mView = (GLSurfaceView)findViewById(R.id.fragment3dSurfaceView);
|
433
|
if( mView!=null ) mView.onResume();
|
434
|
}
|
435
|
|
436
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
437
|
|
438
|
@Override
|
439
|
public void onWindowFocusChanged(boolean hasFocus)
|
440
|
{
|
441
|
super.onWindowFocusChanged(hasFocus);
|
442
|
|
443
|
if( firstScreen )
|
444
|
{
|
445
|
mColsPicker.setValue(mNumCols);
|
446
|
mRowsPicker.setValue(mNumRows);
|
447
|
}
|
448
|
}
|
449
|
|
450
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
451
|
|
452
|
@Override
|
453
|
protected void onDestroy()
|
454
|
{
|
455
|
Distorted.onDestroy();
|
456
|
super.onDestroy();
|
457
|
}
|
458
|
|
459
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
460
|
|
461
|
public void onProgressChanged(SeekBar bar, int progress, boolean fromUser)
|
462
|
{
|
463
|
switch (bar.getId())
|
464
|
{
|
465
|
case R.id.fragment3dcenterX: centerX = progress; computeCenter() ; setCenterText() ; break;
|
466
|
case R.id.fragment3dcenterY: centerY = progress; computeCenter() ; setCenterText() ; break;
|
467
|
}
|
468
|
}
|
469
|
|
470
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
471
|
|
472
|
public void onStartTrackingTouch(SeekBar bar) { }
|
473
|
|
474
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
475
|
|
476
|
public void onStopTrackingTouch(SeekBar bar) { }
|
477
|
}
|