Project

General

Profile

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

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

1 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 d40cfeb2 Leszek Koltunski
import android.view.Gravity;
26 08f92d82 Leszek Koltunski
import android.view.View;
27 d40cfeb2 Leszek Koltunski
import android.widget.Button;
28 08f92d82 Leszek Koltunski
import android.widget.LinearLayout;
29 d40cfeb2 Leszek Koltunski
import android.widget.NumberPicker;
30 08f92d82 Leszek Koltunski
import android.widget.SeekBar;
31
import android.widget.SeekBar.OnSeekBarChangeListener;
32 d40cfeb2 Leszek Koltunski
import android.widget.TableRow;
33 08f92d82 Leszek Koltunski
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 d40cfeb2 Leszek Koltunski
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 08f92d82 Leszek Koltunski
    
66 d40cfeb2 Leszek Koltunski
  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 08f92d82 Leszek Koltunski
    
73 d40cfeb2 Leszek Koltunski
  private EffectNames[] effects = new EffectNames[4];
74 08f92d82 Leszek Koltunski
    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76
    
77 d40cfeb2 Leszek Koltunski
  @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 08f92d82 Leszek Koltunski
      {
96 d40cfeb2 Leszek Koltunski
      @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 08f92d82 Leszek Koltunski
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124
125 d40cfeb2 Leszek Koltunski
  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 08f92d82 Leszek Koltunski
      {
166 d40cfeb2 Leszek Koltunski
      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 08f92d82 Leszek Koltunski
    
213 d40cfeb2 Leszek Koltunski
    deformX = 50;
214
    deformY = 50;
215
    deformZ = 50;
216 e2d8ea1d Leszek Koltunski
217 d40cfeb2 Leszek Koltunski
    distortX = 50;
218
    distortY = 50;
219
    distortZ = 50;
220 e2d8ea1d Leszek Koltunski
221 d40cfeb2 Leszek Koltunski
    sinkA =  50;
222
    swirlA = 50;
223 e2d8ea1d Leszek Koltunski
224 d40cfeb2 Leszek Koltunski
    addViews();
225
    }
226 08f92d82 Leszek Koltunski
    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228
    
229 d40cfeb2 Leszek Koltunski
  private void addViews()
230
    {
231
    LinearLayout layout = (LinearLayout)findViewById(R.id.vertex3dlayout);
232 08f92d82 Leszek Koltunski
    
233 d40cfeb2 Leszek Koltunski
    layout.removeAllViews();
234 08f92d82 Leszek Koltunski
      
235 d40cfeb2 Leszek Koltunski
    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 08f92d82 Leszek Koltunski
     
240 d40cfeb2 Leszek Koltunski
    for( int i=effects.length-1 ; i>=0 ; i-- )
241
      {
242
      switch(effects[i])
243 08f92d82 Leszek Koltunski
        {
244 d40cfeb2 Leszek Koltunski
        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 08f92d82 Leszek Koltunski
        }
249 d40cfeb2 Leszek Koltunski
      }
250 08f92d82 Leszek Koltunski
      
251 d40cfeb2 Leszek Koltunski
    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 08f92d82 Leszek Koltunski
     
256 d40cfeb2 Leszek Koltunski
    setDeformText();
257
    setDistortText();
258
    setSinkText();
259
    setSwirlText();
260 08f92d82 Leszek Koltunski
      
261 d40cfeb2 Leszek Koltunski
    setBar(R.id.vertex3ddeformBar1, deformX);
262
    setBar(R.id.vertex3ddeformBar2, deformY);
263
    setBar(R.id.vertex3ddeformBar3, deformZ);
264 08f92d82 Leszek Koltunski
265 d40cfeb2 Leszek Koltunski
    setBar(R.id.vertex3ddistortBar1, distortX);
266
    setBar(R.id.vertex3ddistortBar2, distortY);
267
    setBar(R.id.vertex3ddistortBar3, distortZ);
268 08f92d82 Leszek Koltunski
      
269 d40cfeb2 Leszek Koltunski
    setBar(R.id.vertex3dsinkBar1, sinkA);
270 e2d8ea1d Leszek Koltunski
271 d40cfeb2 Leszek Koltunski
    setBar(R.id.vertex3dswirlBar1, swirlA);
272 e2d8ea1d Leszek Koltunski
273 d40cfeb2 Leszek Koltunski
    Vertex3DRenderer.setOrder(effects);
274
    }
275 08f92d82 Leszek Koltunski
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277
278 d40cfeb2 Leszek Koltunski
  private void moveUp(EffectNames name)
279
    {
280
    int len = effects.length-1;
281
    int index = -1;
282 08f92d82 Leszek Koltunski
283 d40cfeb2 Leszek Koltunski
    for(int i=0; i<=len; i++)
284
      {
285
      if( effects[i]==name )
286 08f92d82 Leszek Koltunski
        {
287 d40cfeb2 Leszek Koltunski
        index=i;
288
        break;
289 08f92d82 Leszek Koltunski
        }
290 d40cfeb2 Leszek Koltunski
      }
291 08f92d82 Leszek Koltunski
292 d40cfeb2 Leszek Koltunski
    if( index==0 )
293
      {
294
      for(int i=0; i<len; i++)
295
        effects[i] = effects[i+1];
296 08f92d82 Leszek Koltunski
297 d40cfeb2 Leszek Koltunski
      effects[len] = name;
298 08f92d82 Leszek Koltunski
      }
299 d40cfeb2 Leszek Koltunski
    else if( index>0 )
300
      {
301
      effects[index]   = effects[index-1];
302
      effects[index-1] = name;
303
      }
304
305
    addViews();
306
    }
307 08f92d82 Leszek Koltunski
308
///////////////////////////////////////////////////////////////////////////////////////////////////
309
310 d40cfeb2 Leszek Koltunski
  public void ButtonDeform(View v)
311
    {
312
    moveUp(EffectNames.DEFORM);
313
    }
314 08f92d82 Leszek Koltunski
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316
317 d40cfeb2 Leszek Koltunski
  public void ButtonDistort(View v)
318
    {
319
    moveUp(EffectNames.DISTORT);
320
    }
321 08f92d82 Leszek Koltunski
322
///////////////////////////////////////////////////////////////////////////////////////////////////
323
324 d40cfeb2 Leszek Koltunski
  public void ButtonSink(View v)
325
    {
326
    moveUp(EffectNames.SINK);
327
    }
328 08f92d82 Leszek Koltunski
329
///////////////////////////////////////////////////////////////////////////////////////////////////
330
331 d40cfeb2 Leszek Koltunski
  public void ButtonSwirl(View v)
332
    {
333
    moveUp(EffectNames.SWIRL);
334
    }
335 08f92d82 Leszek Koltunski
336
///////////////////////////////////////////////////////////////////////////////////////////////////
337
338 d40cfeb2 Leszek Koltunski
  private void setBar(int id, int value)
339
    {
340
    bar = (SeekBar)findViewById(id);
341
    bar.setOnSeekBarChangeListener(this);
342
    bar.setProgress(value);
343
    }
344 08f92d82 Leszek Koltunski
345
///////////////////////////////////////////////////////////////////////////////////////////////////
346
347 d40cfeb2 Leszek Koltunski
  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 08f92d82 Leszek Koltunski
353 d40cfeb2 Leszek Koltunski
    Vertex3DRenderer.setDeform( fdeformX, fdeformY, fdeformZ );
354
    }
355 08f92d82 Leszek Koltunski
356
///////////////////////////////////////////////////////////////////////////////////////////////////
357
358 d40cfeb2 Leszek Koltunski
  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 08f92d82 Leszek Koltunski
364 d40cfeb2 Leszek Koltunski
    textDeform.setText("deform("+fdeformX+" , "+fdeformY+" , "+fdeformZ+")");
365
    }
366 08f92d82 Leszek Koltunski
367
///////////////////////////////////////////////////////////////////////////////////////////////////
368
369 d40cfeb2 Leszek Koltunski
  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 08f92d82 Leszek Koltunski
375 d40cfeb2 Leszek Koltunski
    Vertex3DRenderer.setDistort(fdistortX, fdistortY, fdistortZ);
376
    }
377 08f92d82 Leszek Koltunski
378
///////////////////////////////////////////////////////////////////////////////////////////////////
379
380 d40cfeb2 Leszek Koltunski
  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 08f92d82 Leszek Koltunski
386 d40cfeb2 Leszek Koltunski
    textDistort.setText("distort("+fdistortX+" , "+fdistortY+" , "+fdistortZ+")");
387
    }
388 08f92d82 Leszek Koltunski
389
///////////////////////////////////////////////////////////////////////////////////////////////////
390
391 d40cfeb2 Leszek Koltunski
  private void computeSink()
392
    {
393
    fsinkA = (sinkA>= 50 ? sinkA-49 : 1/(51-sinkA));
394 08f92d82 Leszek Koltunski
395 d40cfeb2 Leszek Koltunski
    Vertex3DRenderer.setSink( fsinkA );
396
    }
397 08f92d82 Leszek Koltunski
398
///////////////////////////////////////////////////////////////////////////////////////////////////
399
400 d40cfeb2 Leszek Koltunski
  private void setSinkText()
401
    {
402
    fsinkA = ((int)(100*fsinkA))/100.0f;
403 08f92d82 Leszek Koltunski
404 d40cfeb2 Leszek Koltunski
    textSink.setText("sink("+fsinkA+")");
405
    }
406 08f92d82 Leszek Koltunski
407
///////////////////////////////////////////////////////////////////////////////////////////////////
408
409 d40cfeb2 Leszek Koltunski
  private void computeSwirl()
410
    {
411
    fswirlA = (swirlA-50)*3.6f;
412 08f92d82 Leszek Koltunski
413 d40cfeb2 Leszek Koltunski
    Vertex3DRenderer.setSwirl( fswirlA );
414
    }
415 08f92d82 Leszek Koltunski
    
416
///////////////////////////////////////////////////////////////////////////////////////////////////
417
418 d40cfeb2 Leszek Koltunski
  private void setSwirlText()
419
    {
420
    fswirlA = ((int)(100*fswirlA))/100.0f;
421 e2d8ea1d Leszek Koltunski
422 d40cfeb2 Leszek Koltunski
    textSwirl.setText("swirl("+fswirlA+")");
423
    }
424 08f92d82 Leszek Koltunski
   
425
///////////////////////////////////////////////////////////////////////////////////////////////////
426 d40cfeb2 Leszek Koltunski
// 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 08f92d82 Leszek Koltunski
436
///////////////////////////////////////////////////////////////////////////////////////////////////
437
    
438 d40cfeb2 Leszek Koltunski
  @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 08f92d82 Leszek Koltunski
446
///////////////////////////////////////////////////////////////////////////////////////////////////
447
    
448 d40cfeb2 Leszek Koltunski
  @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 08f92d82 Leszek Koltunski
      {
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 d40cfeb2 Leszek Koltunski
    }
469 08f92d82 Leszek Koltunski
470
///////////////////////////////////////////////////////////////////////////////////////////////////
471
    
472 d40cfeb2 Leszek Koltunski
  @Override
473
  protected void onDestroy()
474
    {
475
    Distorted.onDestroy();
476
    super.onDestroy();
477
    }
478 08f92d82 Leszek Koltunski
    
479
///////////////////////////////////////////////////////////////////////////////////////////////////
480
    
481 d40cfeb2 Leszek Koltunski
  public void onProgressChanged(SeekBar bar, int progress, boolean fromUser)
482
    {
483
    switch (bar.getId())
484 08f92d82 Leszek Koltunski
      {
485 d40cfeb2 Leszek Koltunski
      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 08f92d82 Leszek Koltunski
      }
494 d40cfeb2 Leszek Koltunski
    }
495 08f92d82 Leszek Koltunski
496
///////////////////////////////////////////////////////////////////////////////////////////////////
497
498 d40cfeb2 Leszek Koltunski
  public void onStartTrackingTouch(SeekBar bar) { }
499 08f92d82 Leszek Koltunski
    
500
///////////////////////////////////////////////////////////////////////////////////////////////////
501
502 d40cfeb2 Leszek Koltunski
  public void onStopTrackingTouch(SeekBar bar)  { }
503
  }