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