Project

General

Profile

Download (15.3 KB) Statistics
| Branch: | Revision:

examples / src / main / java / org / distorted / examples / vertex3d / Vertex3DActivity.java @ d40cfeb2

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 int maxX, maxY, maxZ;
67

    
68
  private float fdeformX, fdeformY, fdeformZ;
69
  private float fdistortX, fdistortY, fdistortZ;
70
  private float fsinkA;
71
  private float fswirlA;
72
    
73
  private EffectNames[] effects = new EffectNames[4];
74
    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76
    
77
  @Override
78
  protected void onCreate(Bundle savedState)
79
    {
80
    super.onCreate(savedState);
81

    
82
    setContentView(R.layout.cubes1layout);
83

    
84
    mLay = (LinearLayout)findViewById(R.id.buttongrid);
85

    
86
    mColsPicker = (NumberPicker)findViewById(R.id.colsPicker);
87
    mRowsPicker = (NumberPicker)findViewById(R.id.rowsPicker);
88

    
89
    mColsPicker.setMaxValue(10);
90
    mColsPicker.setMinValue( 0);
91
    mRowsPicker.setMaxValue(10);
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
        setGrid();
100
        }
101
      });
102

    
103
    mRowsPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener()
104
      {
105
      @Override
106
      public void onValueChange(NumberPicker picker, int oldVal, int newVal)
107
        {
108
        setGrid();
109
        }
110
      });
111

    
112
    firstScreen = true;
113
    }
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116
// 'first screen' methods
117

    
118
  public static int getCols()
119
    {
120
    return mNumCols;
121
    }
122

    
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124

    
125
  public static String getShape()
126
    {
127
    String str = "";
128

    
129
    for(int i=0; i<mNumRows*mNumCols; i++)
130
      str += mShape[i] ? "1" : "0";
131

    
132
    //android.util.Log.d("CUBES", str);
133

    
134
    return str;
135
    }
136

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138

    
139
  public void setGrid()
140
    {
141
    mNumCols = mColsPicker.getValue();
142
    mNumRows = mRowsPicker.getValue();
143

    
144
    int width = mLay.getWidth();
145
    int height= mLay.getHeight();
146
    int w = mNumCols>0 ? (width / mNumCols) -10 : 0;
147
    int h = mNumRows>0 ? (height/ mNumRows) -10 : 0;
148
    int size= w<h ? w:h;
149
    int pad = size/20;
150

    
151
    mLay.removeAllViews();
152

    
153
    mShape = new boolean[mNumRows*mNumCols];
154

    
155
    TableRow.LayoutParams p = new android.widget.TableRow.LayoutParams();
156

    
157
    p.rightMargin  = pad;
158
    p.leftMargin   = pad;
159
    p.topMargin    = pad;
160
    p.bottomMargin = pad;
161
    p.height       = size;
162
    p.width        = size;
163

    
164
    for (int rows=0; rows<mNumRows; rows++)
165
      {
166
      TableRow tr = new TableRow(this);
167
      tr.setGravity(Gravity.CENTER);
168

    
169
      for(int cols=0; cols<mNumCols; cols++)
170
        {
171
        Button b = new Button(this);
172
        b.setOnClickListener(this);
173
        b.setId(rows*mNumCols+cols);
174
        b.setLayoutParams(p);
175
        b.setBackgroundColor(COLOR_OFF);
176
        tr.addView(b, p);
177
        mShape[rows*mNumCols+cols] = false;
178
        }
179

    
180
      mLay.addView(tr);
181
      }
182
    }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

    
186
  public void onClick(View view)
187
    {
188
    Button tmp = (Button)view;
189
    int id = tmp.getId();
190
    mShape[id] = !mShape[id];
191
    tmp.setBackgroundColor(mShape[id] ? COLOR_ON:COLOR_OFF);
192
    }
193

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

    
196
  public void Continue(View v)
197
    {
198
    firstScreen = false;
199
    setContentView(R.layout.vertex3dlayout);
200
    Default(null);
201
    }
202

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204
// 'second screen' methods
205

    
206
  public void Default(View view)
207
    {
208
    effects[0] = EffectNames.DEFORM;
209
    effects[1] = EffectNames.DISTORT;
210
    effects[2] = EffectNames.SINK;
211
    effects[3] = EffectNames.SWIRL;
212
    
213
    deformX = 50;
214
    deformY = 50;
215
    deformZ = 50;
216

    
217
    distortX = 50;
218
    distortY = 50;
219
    distortZ = 50;
220

    
221
    sinkA =  50;
222
    swirlA = 50;
223

    
224
    addViews();
225
    }
226
    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228
    
229
  private void addViews()
230
    {
231
    LinearLayout layout = (LinearLayout)findViewById(R.id.vertex3dlayout);
232
    
233
    layout.removeAllViews();
234
      
235
    View deform = getLayoutInflater().inflate(R.layout.vertex3ddeform , null);
236
    View distort= getLayoutInflater().inflate(R.layout.vertex3ddistort, null);
237
    View sink   = getLayoutInflater().inflate(R.layout.vertex3dsink   , null);
238
    View swirl  = getLayoutInflater().inflate(R.layout.vertex3dswirl  , null);
239
     
240
    for( int i=effects.length-1 ; i>=0 ; i-- )
241
      {
242
      switch(effects[i])
243
        {
244
        case DEFORM : layout.addView(deform , 0); break;
245
        case DISTORT: layout.addView(distort, 0); break;
246
        case SINK   : layout.addView(sink   , 0); break;
247
        case SWIRL  : layout.addView(swirl  , 0); break;
248
        }
249
      }
250
      
251
    textDeform = (TextView)findViewById(R.id.vertex3ddeformText);
252
    textDistort= (TextView)findViewById(R.id.vertex3ddistortText);
253
    textSink   = (TextView)findViewById(R.id.vertex3dsinkText);
254
    textSwirl  = (TextView)findViewById(R.id.vertex3dswirlText);
255
     
256
    setDeformText();
257
    setDistortText();
258
    setSinkText();
259
    setSwirlText();
260
      
261
    setBar(R.id.vertex3ddeformBar1, deformX);
262
    setBar(R.id.vertex3ddeformBar2, deformY);
263
    setBar(R.id.vertex3ddeformBar3, deformZ);
264

    
265
    setBar(R.id.vertex3ddistortBar1, distortX);
266
    setBar(R.id.vertex3ddistortBar2, distortY);
267
    setBar(R.id.vertex3ddistortBar3, distortZ);
268
      
269
    setBar(R.id.vertex3dsinkBar1, sinkA);
270

    
271
    setBar(R.id.vertex3dswirlBar1, swirlA);
272

    
273
    Vertex3DRenderer.setOrder(effects);
274
    }
275

    
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277

    
278
  private void moveUp(EffectNames name)
279
    {
280
    int len = effects.length-1;
281
    int index = -1;
282

    
283
    for(int i=0; i<=len; i++)
284
      {
285
      if( effects[i]==name )
286
        {
287
        index=i;
288
        break;
289
        }
290
      }
291

    
292
    if( index==0 )
293
      {
294
      for(int i=0; i<len; i++)
295
        effects[i] = effects[i+1];
296

    
297
      effects[len] = name;
298
      }
299
    else if( index>0 )
300
      {
301
      effects[index]   = effects[index-1];
302
      effects[index-1] = name;
303
      }
304

    
305
    addViews();
306
    }
307

    
308
///////////////////////////////////////////////////////////////////////////////////////////////////
309

    
310
  public void ButtonDeform(View v)
311
    {
312
    moveUp(EffectNames.DEFORM);
313
    }
314

    
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316

    
317
  public void ButtonDistort(View v)
318
    {
319
    moveUp(EffectNames.DISTORT);
320
    }
321

    
322
///////////////////////////////////////////////////////////////////////////////////////////////////
323

    
324
  public void ButtonSink(View v)
325
    {
326
    moveUp(EffectNames.SINK);
327
    }
328

    
329
///////////////////////////////////////////////////////////////////////////////////////////////////
330

    
331
  public void ButtonSwirl(View v)
332
    {
333
    moveUp(EffectNames.SWIRL);
334
    }
335

    
336
///////////////////////////////////////////////////////////////////////////////////////////////////
337

    
338
  private void setBar(int id, int value)
339
    {
340
    bar = (SeekBar)findViewById(id);
341
    bar.setOnSeekBarChangeListener(this);
342
    bar.setProgress(value);
343
    }
344

    
345
///////////////////////////////////////////////////////////////////////////////////////////////////
346

    
347
  private void computeDeform()
348
    {
349
    fdeformX = (deformX-50)*maxX/50.0f;
350
    fdeformY = (deformY-50)*maxY/50.0f;
351
    fdeformZ = (deformZ-50)*maxZ/50.0f;
352

    
353
    Vertex3DRenderer.setDeform( fdeformX, fdeformY, fdeformZ );
354
    }
355

    
356
///////////////////////////////////////////////////////////////////////////////////////////////////
357

    
358
  private void setDeformText()
359
    {
360
    fdeformX = ((int)(100*fdeformX))/100.0f;
361
    fdeformY = ((int)(100*fdeformY))/100.0f;
362
    fdeformZ = ((int)(100*fdeformZ))/100.0f;
363

    
364
    textDeform.setText("deform("+fdeformX+" , "+fdeformY+" , "+fdeformZ+")");
365
    }
366

    
367
///////////////////////////////////////////////////////////////////////////////////////////////////
368

    
369
  private void computeDistort()
370
    {
371
    fdistortX = (distortX-50)*maxX/50.0f;
372
    fdistortY = (distortY-50)*maxY/50.0f;
373
    fdistortZ = (distortZ-50)*maxZ/50.0f;
374

    
375
    Vertex3DRenderer.setDistort(fdistortX, fdistortY, fdistortZ);
376
    }
377

    
378
///////////////////////////////////////////////////////////////////////////////////////////////////
379

    
380
  private void setDistortText()
381
    {
382
    fdistortX = ((int)(100*fdistortX))/100.0f;
383
    fdistortY = ((int)(100*fdistortY))/100.0f;
384
    fdistortZ = ((int)(100*fdistortZ))/100.0f;
385

    
386
    textDistort.setText("distort("+fdistortX+" , "+fdistortY+" , "+fdistortZ+")");
387
    }
388

    
389
///////////////////////////////////////////////////////////////////////////////////////////////////
390

    
391
  private void computeSink()
392
    {
393
    fsinkA = (sinkA>= 50 ? sinkA-49 : 1/(51-sinkA));
394

    
395
    Vertex3DRenderer.setSink( fsinkA );
396
    }
397

    
398
///////////////////////////////////////////////////////////////////////////////////////////////////
399

    
400
  private void setSinkText()
401
    {
402
    fsinkA = ((int)(100*fsinkA))/100.0f;
403

    
404
    textSink.setText("sink("+fsinkA+")");
405
    }
406

    
407
///////////////////////////////////////////////////////////////////////////////////////////////////
408

    
409
  private void computeSwirl()
410
    {
411
    fswirlA = (swirlA-50)*3.6f;
412

    
413
    Vertex3DRenderer.setSwirl( fswirlA );
414
    }
415
    
416
///////////////////////////////////////////////////////////////////////////////////////////////////
417

    
418
  private void setSwirlText()
419
    {
420
    fswirlA = ((int)(100*fswirlA))/100.0f;
421

    
422
    textSwirl.setText("swirl("+fswirlA+")");
423
    }
424
   
425
///////////////////////////////////////////////////////////////////////////////////////////////////
426
// Overrides
427

    
428
  @Override
429
  protected void onPause()
430
    {
431
    GLSurfaceView mView = (GLSurfaceView)findViewById(R.id.vertex3dSurfaceView);
432
    if( mView!=null ) mView.onPause();
433
    super.onPause();
434
    }
435

    
436
///////////////////////////////////////////////////////////////////////////////////////////////////
437
    
438
  @Override
439
  protected void onResume()
440
    {
441
    super.onResume();
442
    GLSurfaceView mView = (GLSurfaceView)findViewById(R.id.vertex3dSurfaceView);
443
    if( mView!=null ) mView.onResume();
444
    }
445

    
446
///////////////////////////////////////////////////////////////////////////////////////////////////
447
    
448
  @Override
449
  public void onWindowFocusChanged(boolean hasFocus)
450
    {
451
    super.onWindowFocusChanged(hasFocus);
452

    
453
    if( firstScreen )
454
      {
455
      mColsPicker.setValue(mNumCols);
456
      mRowsPicker.setValue(mNumRows);
457

    
458
      if( hasFocus ) setGrid();
459
      }
460
    else
461
      {
462
      GLSurfaceView mView = (GLSurfaceView) this.findViewById(R.id.vertex3dSurfaceView);
463
      
464
      maxX = mView.getWidth();
465
      maxY = mView.getHeight();
466
      maxZ = (maxX+maxY)/2;
467
      }
468
    }
469

    
470
///////////////////////////////////////////////////////////////////////////////////////////////////
471
    
472
  @Override
473
  protected void onDestroy()
474
    {
475
    Distorted.onDestroy();
476
    super.onDestroy();
477
    }
478
    
479
///////////////////////////////////////////////////////////////////////////////////////////////////
480
    
481
  public void onProgressChanged(SeekBar bar, int progress, boolean fromUser)
482
    {
483
    switch (bar.getId())
484
      {
485
      case R.id.vertex3ddeformBar1  : deformX = progress; computeDeform() ; setDeformText()  ; break;
486
      case R.id.vertex3ddeformBar2  : deformY = progress; computeDeform() ; setDeformText()  ; break;
487
      case R.id.vertex3ddeformBar3  : deformZ = progress; computeDeform() ; setDeformText()  ; break;
488
      case R.id.vertex3ddistortBar1 : distortX= progress; computeDistort(); setDistortText() ; break;
489
      case R.id.vertex3ddistortBar2 : distortY= progress; computeDistort(); setDistortText() ; break;
490
      case R.id.vertex3ddistortBar3 : distortZ= progress; computeDistort(); setDistortText() ; break;
491
      case R.id.vertex3dsinkBar1    : sinkA   = progress; computeSink()   ; setSinkText()    ; break;
492
      case R.id.vertex3dswirlBar1   : swirlA  = progress; computeSwirl()  ; setSwirlText()   ; break;
493
      }
494
    }
495

    
496
///////////////////////////////////////////////////////////////////////////////////////////////////
497

    
498
  public void onStartTrackingTouch(SeekBar bar) { }
499
    
500
///////////////////////////////////////////////////////////////////////////////////////////////////
501

    
502
  public void onStopTrackingTouch(SeekBar bar)  { }
503
  }
(1-1/3)