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