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.Button;
|
28
|
import android.widget.LinearLayout;
|
29
|
import android.widget.NumberPicker;
|
30
|
import android.widget.SeekBar;
|
31
|
import android.widget.SeekBar.OnSeekBarChangeListener;
|
32
|
import android.widget.TableRow;
|
33
|
import android.widget.TextView;
|
34
|
|
35
|
import org.distorted.examples.R;
|
36
|
import org.distorted.library.Distorted;
|
37
|
import org.distorted.library.EffectNames;
|
38
|
|
39
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
40
|
|
41
|
public class Vertex3DActivity extends Activity implements OnSeekBarChangeListener, View.OnClickListener
|
42
|
{
|
43
|
private boolean firstScreen;
|
44
|
|
45
|
// fields needed for the first 'pick-a-shape' screen
|
46
|
//
|
47
|
private static int mNumCols = 3;
|
48
|
private static int mNumRows = 3;
|
49
|
|
50
|
private static final int COLOR_OFF = 0xffffe81f;
|
51
|
private static final int COLOR_ON = 0xff0000ff;
|
52
|
|
53
|
private NumberPicker mColsPicker, mRowsPicker;
|
54
|
private LinearLayout mLay;
|
55
|
private static boolean[] mShape;
|
56
|
|
57
|
// fields needed for the second 'apply vertex effects' screen
|
58
|
//
|
59
|
private SeekBar bar;
|
60
|
private TextView textDeform, textDistort, textSink, textSwirl;
|
61
|
private int deformX, deformY, deformZ;
|
62
|
private int distortX, distortY, distortZ;
|
63
|
private int sinkA;
|
64
|
private int swirlA;
|
65
|
|
66
|
private float fdeformX, fdeformY, fdeformZ;
|
67
|
private float fdistortX, fdistortY, fdistortZ;
|
68
|
private float fsinkA;
|
69
|
private float fswirlA;
|
70
|
|
71
|
private EffectNames[] effects = new EffectNames[4];
|
72
|
|
73
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
74
|
|
75
|
@Override
|
76
|
protected void onCreate(Bundle savedState)
|
77
|
{
|
78
|
super.onCreate(savedState);
|
79
|
|
80
|
setContentView(R.layout.cubes1layout);
|
81
|
|
82
|
mLay = (LinearLayout)findViewById(R.id.buttongrid);
|
83
|
|
84
|
mColsPicker = (NumberPicker)findViewById(R.id.colsPicker);
|
85
|
mRowsPicker = (NumberPicker)findViewById(R.id.rowsPicker);
|
86
|
|
87
|
mColsPicker.setMaxValue(10);
|
88
|
mColsPicker.setMinValue( 0);
|
89
|
mRowsPicker.setMaxValue(10);
|
90
|
mRowsPicker.setMinValue( 0);
|
91
|
|
92
|
mColsPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener()
|
93
|
{
|
94
|
@Override
|
95
|
public void onValueChange(NumberPicker picker, int oldVal, int newVal)
|
96
|
{
|
97
|
setGrid();
|
98
|
}
|
99
|
});
|
100
|
|
101
|
mRowsPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener()
|
102
|
{
|
103
|
@Override
|
104
|
public void onValueChange(NumberPicker picker, int oldVal, int newVal)
|
105
|
{
|
106
|
setGrid();
|
107
|
}
|
108
|
});
|
109
|
|
110
|
firstScreen = true;
|
111
|
}
|
112
|
|
113
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
114
|
// 'first screen' methods
|
115
|
|
116
|
public static int getCols()
|
117
|
{
|
118
|
return mNumCols;
|
119
|
}
|
120
|
|
121
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
122
|
|
123
|
public static String getShape()
|
124
|
{
|
125
|
String str = "";
|
126
|
|
127
|
for(int i=0; i<mNumRows*mNumCols; i++)
|
128
|
str += mShape[i] ? "1" : "0";
|
129
|
|
130
|
//android.util.Log.d("CUBES", str);
|
131
|
|
132
|
return str;
|
133
|
}
|
134
|
|
135
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
136
|
|
137
|
public void setGrid()
|
138
|
{
|
139
|
mNumCols = mColsPicker.getValue();
|
140
|
mNumRows = mRowsPicker.getValue();
|
141
|
|
142
|
int width = mLay.getWidth();
|
143
|
int height= mLay.getHeight();
|
144
|
int w = mNumCols>0 ? (width / mNumCols) -10 : 0;
|
145
|
int h = mNumRows>0 ? (height/ mNumRows) -10 : 0;
|
146
|
int size= w<h ? w:h;
|
147
|
int pad = size/20;
|
148
|
|
149
|
mLay.removeAllViews();
|
150
|
|
151
|
mShape = new boolean[mNumRows*mNumCols];
|
152
|
|
153
|
TableRow.LayoutParams p = new android.widget.TableRow.LayoutParams();
|
154
|
|
155
|
p.rightMargin = pad;
|
156
|
p.leftMargin = pad;
|
157
|
p.topMargin = pad;
|
158
|
p.bottomMargin = pad;
|
159
|
p.height = size;
|
160
|
p.width = size;
|
161
|
|
162
|
for (int rows=0; rows<mNumRows; rows++)
|
163
|
{
|
164
|
TableRow tr = new TableRow(this);
|
165
|
tr.setGravity(Gravity.CENTER);
|
166
|
|
167
|
for(int cols=0; cols<mNumCols; cols++)
|
168
|
{
|
169
|
Button b = new Button(this);
|
170
|
b.setOnClickListener(this);
|
171
|
b.setId(rows*mNumCols+cols);
|
172
|
b.setLayoutParams(p);
|
173
|
b.setBackgroundColor(COLOR_OFF);
|
174
|
tr.addView(b, p);
|
175
|
mShape[rows*mNumCols+cols] = false;
|
176
|
}
|
177
|
|
178
|
mLay.addView(tr);
|
179
|
}
|
180
|
}
|
181
|
|
182
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
183
|
|
184
|
public void onClick(View view)
|
185
|
{
|
186
|
Button tmp = (Button)view;
|
187
|
int id = tmp.getId();
|
188
|
mShape[id] = !mShape[id];
|
189
|
tmp.setBackgroundColor(mShape[id] ? COLOR_ON:COLOR_OFF);
|
190
|
}
|
191
|
|
192
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
193
|
|
194
|
public void Continue(View v)
|
195
|
{
|
196
|
firstScreen = false;
|
197
|
setContentView(R.layout.vertex3dlayout);
|
198
|
Default(null);
|
199
|
}
|
200
|
|
201
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
202
|
// 'second screen' methods
|
203
|
|
204
|
public void Default(View view)
|
205
|
{
|
206
|
effects[0] = EffectNames.DEFORM;
|
207
|
effects[1] = EffectNames.DISTORT;
|
208
|
effects[2] = EffectNames.SINK;
|
209
|
effects[3] = EffectNames.SWIRL;
|
210
|
|
211
|
deformX = 50;
|
212
|
deformY = 50;
|
213
|
deformZ = 50;
|
214
|
|
215
|
distortX = 50;
|
216
|
distortY = 50;
|
217
|
distortZ = 50;
|
218
|
|
219
|
sinkA = 50;
|
220
|
swirlA = 50;
|
221
|
|
222
|
addViews();
|
223
|
}
|
224
|
|
225
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
226
|
|
227
|
private void addViews()
|
228
|
{
|
229
|
LinearLayout layout = (LinearLayout)findViewById(R.id.vertex3dlayout);
|
230
|
|
231
|
layout.removeAllViews();
|
232
|
|
233
|
View deform = getLayoutInflater().inflate(R.layout.vertex3ddeform , null);
|
234
|
View distort= getLayoutInflater().inflate(R.layout.vertex3ddistort, null);
|
235
|
View sink = getLayoutInflater().inflate(R.layout.vertex3dsink , null);
|
236
|
View swirl = getLayoutInflater().inflate(R.layout.vertex3dswirl , null);
|
237
|
|
238
|
for( int i=effects.length-1 ; i>=0 ; i-- )
|
239
|
{
|
240
|
switch(effects[i])
|
241
|
{
|
242
|
case DEFORM : layout.addView(deform , 0); break;
|
243
|
case DISTORT: layout.addView(distort, 0); break;
|
244
|
case SINK : layout.addView(sink , 0); break;
|
245
|
case SWIRL : layout.addView(swirl , 0); break;
|
246
|
}
|
247
|
}
|
248
|
|
249
|
textDeform = (TextView)findViewById(R.id.vertex3ddeformText);
|
250
|
textDistort= (TextView)findViewById(R.id.vertex3ddistortText);
|
251
|
textSink = (TextView)findViewById(R.id.vertex3dsinkText);
|
252
|
textSwirl = (TextView)findViewById(R.id.vertex3dswirlText);
|
253
|
|
254
|
setDeformText();
|
255
|
setDistortText();
|
256
|
setSinkText();
|
257
|
setSwirlText();
|
258
|
|
259
|
setBar(R.id.vertex3ddeformBar1, deformX);
|
260
|
setBar(R.id.vertex3ddeformBar2, deformY);
|
261
|
setBar(R.id.vertex3ddeformBar3, deformZ);
|
262
|
|
263
|
setBar(R.id.vertex3ddistortBar1, distortX);
|
264
|
setBar(R.id.vertex3ddistortBar2, distortY);
|
265
|
setBar(R.id.vertex3ddistortBar3, distortZ);
|
266
|
|
267
|
setBar(R.id.vertex3dsinkBar1, sinkA);
|
268
|
|
269
|
setBar(R.id.vertex3dswirlBar1, swirlA);
|
270
|
|
271
|
Vertex3DRenderer.setOrder(effects);
|
272
|
}
|
273
|
|
274
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
275
|
|
276
|
private void moveUp(EffectNames name)
|
277
|
{
|
278
|
int len = effects.length-1;
|
279
|
int index = -1;
|
280
|
|
281
|
for(int i=0; i<=len; i++)
|
282
|
{
|
283
|
if( effects[i]==name )
|
284
|
{
|
285
|
index=i;
|
286
|
break;
|
287
|
}
|
288
|
}
|
289
|
|
290
|
if( index==0 )
|
291
|
{
|
292
|
for(int i=0; i<len; i++)
|
293
|
effects[i] = effects[i+1];
|
294
|
|
295
|
effects[len] = name;
|
296
|
}
|
297
|
else if( index>0 )
|
298
|
{
|
299
|
effects[index] = effects[index-1];
|
300
|
effects[index-1] = name;
|
301
|
}
|
302
|
|
303
|
addViews();
|
304
|
}
|
305
|
|
306
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
307
|
|
308
|
public void ButtonDeform(View v)
|
309
|
{
|
310
|
moveUp(EffectNames.DEFORM);
|
311
|
}
|
312
|
|
313
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
314
|
|
315
|
public void ButtonDistort(View v)
|
316
|
{
|
317
|
moveUp(EffectNames.DISTORT);
|
318
|
}
|
319
|
|
320
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
321
|
|
322
|
public void ButtonSink(View v)
|
323
|
{
|
324
|
moveUp(EffectNames.SINK);
|
325
|
}
|
326
|
|
327
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
328
|
|
329
|
public void ButtonSwirl(View v)
|
330
|
{
|
331
|
moveUp(EffectNames.SWIRL);
|
332
|
}
|
333
|
|
334
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
335
|
|
336
|
private void setBar(int id, int value)
|
337
|
{
|
338
|
bar = (SeekBar)findViewById(id);
|
339
|
bar.setOnSeekBarChangeListener(this);
|
340
|
bar.setProgress(value);
|
341
|
}
|
342
|
|
343
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
344
|
|
345
|
private void computeDeform()
|
346
|
{
|
347
|
fdeformX = (deformX-50)*Vertex3DRenderer.mScreenMin/50.0f;
|
348
|
fdeformY = (deformY-50)*Vertex3DRenderer.mScreenMin/50.0f;
|
349
|
fdeformZ = (deformZ-50)*Vertex3DRenderer.mScreenMin/50.0f;
|
350
|
|
351
|
Vertex3DRenderer.setDeform( fdeformX, fdeformY, fdeformZ );
|
352
|
}
|
353
|
|
354
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
355
|
|
356
|
private void setDeformText()
|
357
|
{
|
358
|
fdeformX = ((int)(100*fdeformX))/100.0f;
|
359
|
fdeformY = ((int)(100*fdeformY))/100.0f;
|
360
|
fdeformZ = ((int)(100*fdeformZ))/100.0f;
|
361
|
|
362
|
textDeform.setText("deform("+fdeformX+" , "+fdeformY+" , "+fdeformZ+")");
|
363
|
}
|
364
|
|
365
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
366
|
|
367
|
private void computeDistort()
|
368
|
{
|
369
|
fdistortX = (distortX-50)*Vertex3DRenderer.mScreenMin/50.0f;
|
370
|
fdistortY = (distortY-50)*Vertex3DRenderer.mScreenMin/50.0f;
|
371
|
fdistortZ = (distortZ-50)*Vertex3DRenderer.mScreenMin/50.0f;
|
372
|
|
373
|
Vertex3DRenderer.setDistort(fdistortX, fdistortY, fdistortZ);
|
374
|
}
|
375
|
|
376
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
377
|
|
378
|
private void setDistortText()
|
379
|
{
|
380
|
fdistortX = ((int)(100*fdistortX))/100.0f;
|
381
|
fdistortY = ((int)(100*fdistortY))/100.0f;
|
382
|
fdistortZ = ((int)(100*fdistortZ))/100.0f;
|
383
|
|
384
|
textDistort.setText("distort("+fdistortX+" , "+fdistortY+" , "+fdistortZ+")");
|
385
|
}
|
386
|
|
387
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
388
|
|
389
|
private void computeSink()
|
390
|
{
|
391
|
fsinkA = (sinkA>= 50 ? 0.18f*sinkA-8.0f : 0.018f*sinkA + 0.1f);
|
392
|
|
393
|
Vertex3DRenderer.setSink( fsinkA );
|
394
|
}
|
395
|
|
396
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
397
|
|
398
|
private void setSinkText()
|
399
|
{
|
400
|
fsinkA = ((int)(100*fsinkA))/100.0f;
|
401
|
|
402
|
textSink.setText("sink("+fsinkA+")");
|
403
|
}
|
404
|
|
405
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
406
|
|
407
|
private void computeSwirl()
|
408
|
{
|
409
|
fswirlA = (swirlA-50)*3.6f;
|
410
|
|
411
|
Vertex3DRenderer.setSwirl( fswirlA );
|
412
|
}
|
413
|
|
414
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
415
|
|
416
|
private void setSwirlText()
|
417
|
{
|
418
|
fswirlA = ((int)(100*fswirlA))/100.0f;
|
419
|
|
420
|
textSwirl.setText("swirl("+fswirlA+")");
|
421
|
}
|
422
|
|
423
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
424
|
// Overrides
|
425
|
|
426
|
@Override
|
427
|
protected void onPause()
|
428
|
{
|
429
|
GLSurfaceView mView = (GLSurfaceView)findViewById(R.id.vertex3dSurfaceView);
|
430
|
if( mView!=null ) mView.onPause();
|
431
|
super.onPause();
|
432
|
}
|
433
|
|
434
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
435
|
|
436
|
@Override
|
437
|
protected void onResume()
|
438
|
{
|
439
|
super.onResume();
|
440
|
GLSurfaceView mView = (GLSurfaceView)findViewById(R.id.vertex3dSurfaceView);
|
441
|
if( mView!=null ) mView.onResume();
|
442
|
}
|
443
|
|
444
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
445
|
|
446
|
@Override
|
447
|
public void onWindowFocusChanged(boolean hasFocus)
|
448
|
{
|
449
|
super.onWindowFocusChanged(hasFocus);
|
450
|
|
451
|
if( firstScreen )
|
452
|
{
|
453
|
mColsPicker.setValue(mNumCols);
|
454
|
mRowsPicker.setValue(mNumRows);
|
455
|
|
456
|
if( hasFocus ) setGrid();
|
457
|
}
|
458
|
}
|
459
|
|
460
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
461
|
|
462
|
@Override
|
463
|
protected void onDestroy()
|
464
|
{
|
465
|
Distorted.onDestroy();
|
466
|
super.onDestroy();
|
467
|
}
|
468
|
|
469
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
470
|
|
471
|
public void onProgressChanged(SeekBar bar, int progress, boolean fromUser)
|
472
|
{
|
473
|
switch (bar.getId())
|
474
|
{
|
475
|
case R.id.vertex3ddeformBar1 : deformX = progress; computeDeform() ; setDeformText() ; break;
|
476
|
case R.id.vertex3ddeformBar2 : deformY = progress; computeDeform() ; setDeformText() ; break;
|
477
|
case R.id.vertex3ddeformBar3 : deformZ = progress; computeDeform() ; setDeformText() ; break;
|
478
|
case R.id.vertex3ddistortBar1 : distortX= progress; computeDistort(); setDistortText() ; break;
|
479
|
case R.id.vertex3ddistortBar2 : distortY= progress; computeDistort(); setDistortText() ; break;
|
480
|
case R.id.vertex3ddistortBar3 : distortZ= progress; computeDistort(); setDistortText() ; break;
|
481
|
case R.id.vertex3dsinkBar1 : sinkA = progress; computeSink() ; setSinkText() ; break;
|
482
|
case R.id.vertex3dswirlBar1 : swirlA = progress; computeSwirl() ; setSwirlText() ; break;
|
483
|
}
|
484
|
}
|
485
|
|
486
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
487
|
|
488
|
public void onStartTrackingTouch(SeekBar bar) { }
|
489
|
|
490
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
491
|
|
492
|
public void onStopTrackingTouch(SeekBar bar) { }
|
493
|
}
|