Project

General

Profile

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

examples / src / main / java / org / distorted / examples / fragment3d / Fragment3DActivity.java @ df77c72c

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.fragment3d;
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.SeekBar;
33
import android.widget.SeekBar.OnSeekBarChangeListener;
34
import android.widget.Spinner;
35
import android.widget.TableRow;
36
import android.widget.TextView;
37

    
38
import org.distorted.examples.R;
39
import org.distorted.library.Distorted;
40
import org.distorted.library.DistortedBitmap;
41
import org.distorted.library.DistortedCubes;
42
import org.distorted.library.DistortedObject;
43
import org.distorted.library.EffectNames;
44

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

    
47
public class Fragment3DActivity extends Activity
48
                                implements OnSeekBarChangeListener,
49
                                           View.OnClickListener,
50
                                           AdapterView.OnItemSelectedListener
51
  {
52
  private static final int COLOR_OFF = 0xffffe81f;
53
  private static final int COLOR_ON  = 0xff0000ff;
54

    
55
  private boolean firstScreen;
56

    
57
  // fields needed for the first 'pick-a-shape' screen
58
  //
59
  private int mNumCols = 3;
60
  private int mNumRows = 3;
61
  private NumberPicker mColsPicker, mRowsPicker;
62
  private LinearLayout mLay;
63
  private boolean[] mShape;
64
  private DistortedObject mObject;
65
  private int mObjectType;
66
  private int mBitmap;
67

    
68
  // fields needed for the second 'apply fragment effects' screen
69
  //
70
  private SeekBar bar;
71
  private TextView textChroma, textAlpha, textBrightness, textSaturation, textCenter;
72
  private int chromaL, chromaR, chromaG, chromaB;
73
  private int alphaL;
74
  private int brightnessL;
75
  private int saturationL;
76
  private int centerX, centerY;
77

    
78
  private float fchromaL, fchromaR, fchromaG, fchromaB;
79
  private float falphaL;
80
  private float fbrightnessL;
81
  private float fsaturationL;
82
  private float fcenterX, fcenterY;
83
  private EffectNames[] effects = new EffectNames[4];
84

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86
    
87
  @Override
88
  protected void onCreate(Bundle savedState)
89
    {
90
    super.onCreate(savedState);
91

    
92
    setContentView(R.layout.objectpickerlayout);
93

    
94
    mColsPicker = (NumberPicker)findViewById(R.id.objectpicker_cols);
95
    mRowsPicker = (NumberPicker)findViewById(R.id.objectpicker_rows);
96

    
97
    mColsPicker.setMaxValue(10);
98
    mColsPicker.setMinValue( 0);
99
    mRowsPicker.setMaxValue(10);
100
    mRowsPicker.setMinValue( 0);
101

    
102
    mColsPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener()
103
      {
104
      @Override
105
      public void onValueChange(NumberPicker picker, int oldVal, int newVal)
106
        {
107
        mNumCols = mColsPicker.getValue();
108
        }
109
      });
110

    
111
    mRowsPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener()
112
      {
113
      @Override
114
      public void onValueChange(NumberPicker picker, int oldVal, int newVal)
115
        {
116
        mNumRows = mRowsPicker.getValue();
117
        }
118
      });
119

    
120
    firstScreen = true;
121

    
122
    mObjectType = 0;
123

    
124
    Spinner typeSpinner  = (Spinner)findViewById(R.id.objectpicker_spinnerType);
125
    typeSpinner.setOnItemSelectedListener(this);
126

    
127
    String[] objectType = new String[] {"DistortedCubes", "DistortedBitmap"};
128

    
129
    ArrayAdapter<String> adapterType = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, objectType);
130
    adapterType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
131
    typeSpinner.setAdapter(adapterType);
132

    
133
    Spinner bitmapSpinner  = (Spinner)findViewById(R.id.objectpicker_spinnerBitmap);
134
    bitmapSpinner.setOnItemSelectedListener(this);
135

    
136
    String[] objectBitmap = new String[] {"Girl", "Dog", "Cat", "Grid"};
137

    
138
    ArrayAdapter<String> adapterBitmap = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, objectBitmap);
139
    adapterBitmap.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
140
    bitmapSpinner.setAdapter(adapterBitmap);
141
    }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

    
145
  private void setGrid()
146
    {
147
    mLay = (LinearLayout)findViewById(R.id.objectpicker_buttongrid);
148

    
149
    int width = mLay.getWidth();
150
    int height= mLay.getHeight();
151
    int w = mNumCols>0 ? (width / mNumCols) -10 : 0;
152
    int h = mNumRows>0 ? (height/ mNumRows) -10 : 0;
153
    int size= w<h ? w:h;
154
    int pad = size/20;
155

    
156
    mLay.removeAllViews();
157

    
158
    mShape = new boolean[mNumRows*mNumCols];
159

    
160
    TableRow.LayoutParams p = new android.widget.TableRow.LayoutParams();
161

    
162
    p.rightMargin  = pad;
163
    p.leftMargin   = pad;
164
    p.topMargin    = pad;
165
    p.bottomMargin = pad;
166
    p.height       = size;
167
    p.width        = size;
168

    
169
    for (int rows=0; rows<mNumRows; rows++)
170
      {
171
      TableRow tr = new TableRow(this);
172
      tr.setGravity(Gravity.CENTER);
173

    
174
      for(int cols=0; cols<mNumCols; cols++)
175
        {
176
        Button b = new Button(this);
177
        b.setOnClickListener(this);
178
        b.setId(rows*mNumCols+cols);
179
        b.setLayoutParams(p);
180
        b.setBackgroundColor(COLOR_ON);
181
        tr.addView(b, p);
182
        mShape[rows*mNumCols+cols] = true;
183
        }
184

    
185
      mLay.addView(tr);
186
      }
187
    }
188

    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190

    
191
  public DistortedObject getObject()
192
    {
193
    return mObject;
194
    }
195

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197

    
198
  public void onClick(View view)
199
    {
200
    Button tmp = (Button)view;
201
    int id = tmp.getId();
202
    mShape[id] = !mShape[id];
203
    tmp.setBackgroundColor(mShape[id] ? COLOR_ON:COLOR_OFF);
204
    }
205

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

    
208
  public int getBitmap()
209
    {
210
    return mBitmap;
211
    }
212

    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214

    
215
  public void Continue(View v)
216
    {
217
    if( mObjectType==1 )
218
      {
219
      firstScreen = false;
220
      mObject = new DistortedBitmap(100,100,mNumCols);
221
      setContentView(R.layout.fragment3dlayout);
222
      Default(null);
223
      }
224
    else
225
      {
226
      View view = getLayoutInflater().inflate(R.layout.objectpicker2layout, null);
227

    
228
      setContentView(view);
229

    
230
      view.post(new Runnable() {
231
            @Override
232
            public void run() {
233
              setGrid();
234
            }
235
        });
236
      }
237
    }
238

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

    
241
  public void Create(View v)
242
    {
243
    firstScreen = false;
244

    
245
    String str = "";
246

    
247
    for(int i=0; i<mNumRows*mNumCols; i++)
248
      str += mShape[i] ? "1" : "0";
249

    
250
    mObject = new DistortedCubes(mNumCols, str, 10);
251

    
252
    setContentView(R.layout.fragment3dlayout);
253
    Default(null);
254
    }
255

    
256

    
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258

    
259
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
260
    {
261
    switch(parent.getId())
262
      {
263
      case R.id.objectpicker_spinnerType  : mObjectType = pos; break;
264
      case R.id.objectpicker_spinnerBitmap: switch(pos)
265
                                              {
266
                                              case 0: mBitmap = R.raw.face; break;
267
                                              case 1: mBitmap = R.raw.dog;  break;
268
                                              case 2: mBitmap = R.raw.cat;  break;
269
                                              case 3: mBitmap = R.raw.grid; break;
270
                                              }
271
                                            break;
272
      }
273
    }
274

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

    
277
  public void onNothingSelected(AdapterView<?> parent)
278
    {
279
    }
280

    
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282
// 'second screen' methods
283

    
284
  public void Default(View view)
285
    {
286
    effects[0] = EffectNames.CHROMA;
287
    effects[1] = EffectNames.ALPHA;
288
    effects[2] = EffectNames.BRIGHTNESS;
289
    effects[3] = EffectNames.SATURATION;
290
    
291
    chromaL = 0;
292
    chromaR = 0;
293
    chromaG = 0;
294
    chromaB = 0;
295

    
296
    alphaL     = 0;
297
    brightnessL= 0;
298
    saturationL= 0;
299

    
300
    centerX = 50;
301
    centerY = 50;
302

    
303
    textCenter = (TextView)findViewById(R.id.fragment3dcenterText);
304
    computeCenter();
305
    setCenterText();
306

    
307
    setBar(R.id.fragment3dcenterX, centerX);
308
    setBar(R.id.fragment3dcenterY, centerY);
309

    
310
    addViews();
311
    }
312
    
313
///////////////////////////////////////////////////////////////////////////////////////////////////
314
    
315
  private void addViews()
316
    {
317
    LinearLayout layout = (LinearLayout)findViewById(R.id.fragment3dlayout);
318
    
319
    layout.removeAllViews();
320
      
321
    View chroma    = getLayoutInflater().inflate(R.layout.fragment3dchroma    , null);
322
    View alpha     = getLayoutInflater().inflate(R.layout.fragment3dalpha     , null);
323
    View brightness= getLayoutInflater().inflate(R.layout.fragment3dbrightness, null);
324
    View saturation= getLayoutInflater().inflate(R.layout.fragment3dsaturation, null);
325
     
326
    for( int i=effects.length-1 ; i>=0 ; i-- )
327
      {
328
      switch(effects[i])
329
        {
330
        case CHROMA     : layout.addView(chroma    , 0); break;
331
        case ALPHA      : layout.addView(alpha     , 0); break;
332
        case BRIGHTNESS : layout.addView(brightness, 0); break;
333
        case SATURATION : layout.addView(saturation, 0); break;
334
        }
335
      }
336
      
337
    textChroma    = (TextView)findViewById(R.id.fragment3dchromaText);
338
    textAlpha     = (TextView)findViewById(R.id.fragment3dalphaText);
339
    textBrightness= (TextView)findViewById(R.id.fragment3dbrightnessText);
340
    textSaturation= (TextView)findViewById(R.id.fragment3dsaturationText);
341
     
342
    setChromaText();
343
    setAlphaText();
344
    setBrightnessText();
345
    setSaturationText();
346
      
347
    setBar(R.id.fragment3dchromaBar1, chromaL);
348
    setBar(R.id.fragment3dchromaBar2, chromaR);
349
    setBar(R.id.fragment3dchromaBar3, chromaG);
350
    setBar(R.id.fragment3dchromaBar4, chromaB);
351

    
352
    setBar(R.id.fragment3dalphaBar1     , alphaL);
353
    setBar(R.id.fragment3dbrightnessBar1, brightnessL);
354
    setBar(R.id.fragment3dsaturationBar1, saturationL);
355

    
356
    Fragment3DSurfaceView view = (Fragment3DSurfaceView)findViewById(R.id.fragment3dSurfaceView);
357
    view.getRenderer().setOrder(effects);
358
    }
359

    
360
///////////////////////////////////////////////////////////////////////////////////////////////////
361

    
362
  private void moveUp(EffectNames name)
363
    {
364
    int len = effects.length-1;
365
    int index = -1;
366

    
367
    for(int i=0; i<=len; i++)
368
      {
369
      if( effects[i]==name )
370
        {
371
        index=i;
372
        break;
373
        }
374
      }
375

    
376
    if( index==0 )
377
      {
378
      for(int i=0; i<len; i++)
379
        effects[i] = effects[i+1];
380

    
381
      effects[len] = name;
382
      }
383
    else if( index>0 )
384
      {
385
      effects[index]   = effects[index-1];
386
      effects[index-1] = name;
387
      }
388

    
389
    addViews();
390
    }
391

    
392
///////////////////////////////////////////////////////////////////////////////////////////////////
393

    
394
  public void ButtonChroma(View v)
395
    {
396
    moveUp(EffectNames.CHROMA);
397
    }
398

    
399
///////////////////////////////////////////////////////////////////////////////////////////////////
400

    
401
  public void ButtonAlpha(View v)
402
    {
403
    moveUp(EffectNames.ALPHA);
404
    }
405

    
406
///////////////////////////////////////////////////////////////////////////////////////////////////
407

    
408
  public void ButtonBrightness(View v)
409
    {
410
    moveUp(EffectNames.BRIGHTNESS);
411
    }
412

    
413
///////////////////////////////////////////////////////////////////////////////////////////////////
414

    
415
  public void ButtonSaturation(View v)
416
    {
417
    moveUp(EffectNames.SATURATION);
418
    }
419

    
420
///////////////////////////////////////////////////////////////////////////////////////////////////
421

    
422
  private void setBar(int id, int value)
423
    {
424
    bar = (SeekBar)findViewById(id);
425
    bar.setOnSeekBarChangeListener(this);
426
    bar.setProgress(value);
427
    }
428

    
429
///////////////////////////////////////////////////////////////////////////////////////////////////
430

    
431
  private void computeChroma()
432
    {
433
    Fragment3DSurfaceView view = (Fragment3DSurfaceView)findViewById(R.id.fragment3dSurfaceView);
434
    Fragment3DRenderer renderer = view.getRenderer();
435

    
436
    fchromaL = chromaL/100.0f;
437
    fchromaR = chromaR*255/100.0f;
438
    fchromaG = chromaG*255/100.0f;
439
    fchromaB = chromaB*255/100.0f;
440

    
441
    renderer.setChroma( fchromaL, fchromaR, fchromaG, fchromaB );
442
    }
443

    
444
///////////////////////////////////////////////////////////////////////////////////////////////////
445

    
446
  private void setChromaText()
447
    {
448
    fchromaL = ((int)(100*fchromaL))/100.0f;
449
    fchromaR = (float)((int)fchromaR);
450
    fchromaG = (float)((int)fchromaG);
451
    fchromaB = (float)((int)fchromaB);
452

    
453
    textChroma.setText("chroma("+fchromaL+"( "+fchromaR+" , "+fchromaG+" , "+fchromaB+" )");
454
    }
455

    
456
///////////////////////////////////////////////////////////////////////////////////////////////////
457

    
458
  private void computeAlpha()
459
    {
460
    Fragment3DSurfaceView view = (Fragment3DSurfaceView)findViewById(R.id.fragment3dSurfaceView);
461

    
462
    falphaL = alphaL/100.0f;
463

    
464
    view.getRenderer().setAlpha(falphaL);
465
    }
466

    
467
///////////////////////////////////////////////////////////////////////////////////////////////////
468

    
469
  private void setAlphaText()
470
    {
471
    falphaL = ((int)(100*falphaL))/100.0f;
472

    
473
    textAlpha.setText("alpha("+falphaL+")");
474
    }
475

    
476
///////////////////////////////////////////////////////////////////////////////////////////////////
477

    
478
  private void computeBrightness()
479
    {
480
    Fragment3DSurfaceView view = (Fragment3DSurfaceView)findViewById(R.id.fragment3dSurfaceView);
481

    
482
    fbrightnessL = brightnessL/100.0f;
483

    
484
    view.getRenderer().setBrightness( fbrightnessL );
485
    }
486

    
487
///////////////////////////////////////////////////////////////////////////////////////////////////
488

    
489
  private void setBrightnessText()
490
    {
491
    fbrightnessL = ((int)(100*fbrightnessL))/100.0f;
492

    
493
    textBrightness.setText("brightness("+fbrightnessL+")");
494
    }
495

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

    
498
  private void computeSaturation()
499
    {
500
    Fragment3DSurfaceView view = (Fragment3DSurfaceView)findViewById(R.id.fragment3dSurfaceView);
501

    
502
    fsaturationL = saturationL/100.0f;
503

    
504
    view.getRenderer().setSaturation( fsaturationL );
505
    }
506
    
507
///////////////////////////////////////////////////////////////////////////////////////////////////
508

    
509
  private void setSaturationText()
510
    {
511
    fsaturationL = ((int)(100*fsaturationL))/100.0f;
512

    
513
    textSaturation.setText("saturation("+fsaturationL+")");
514
    }
515

    
516
///////////////////////////////////////////////////////////////////////////////////////////////////
517

    
518
  private void computeCenter()
519
    {
520
    Fragment3DSurfaceView view = (Fragment3DSurfaceView)findViewById(R.id.fragment3dSurfaceView);
521

    
522
    fcenterX = centerX;
523
    fcenterY = centerY;
524

    
525
    view.getRenderer().setCenter( fcenterX, fcenterY );
526
    }
527

    
528
///////////////////////////////////////////////////////////////////////////////////////////////////
529

    
530
  private void setCenterText()
531
    {
532
    fcenterX = ((int)(100*fcenterX))/100.0f;
533
    fcenterY = ((int)(100*fcenterY))/100.0f;
534

    
535
    textCenter.setText("center("+fcenterX+","+fcenterY+")");
536
    }
537

    
538
///////////////////////////////////////////////////////////////////////////////////////////////////
539

    
540
  public float getCenterX()
541
    {
542
    return fcenterX;
543
    }
544

    
545
///////////////////////////////////////////////////////////////////////////////////////////////////
546

    
547
  public float getCenterY()
548
    {
549
    return fcenterY;
550
    }
551

    
552
///////////////////////////////////////////////////////////////////////////////////////////////////
553
// Overrides
554

    
555
  @Override
556
  protected void onPause()
557
    {
558
    GLSurfaceView mView = (GLSurfaceView)findViewById(R.id.fragment3dSurfaceView);
559
    if( mView!=null ) mView.onPause();
560
    super.onPause();
561
    }
562

    
563
///////////////////////////////////////////////////////////////////////////////////////////////////
564
    
565
  @Override
566
  protected void onResume()
567
    {
568
    super.onResume();
569
    GLSurfaceView mView = (GLSurfaceView)findViewById(R.id.fragment3dSurfaceView);
570
    if( mView!=null ) mView.onResume();
571
    }
572

    
573
///////////////////////////////////////////////////////////////////////////////////////////////////
574
    
575
  @Override
576
  public void onWindowFocusChanged(boolean hasFocus)
577
    {
578
    super.onWindowFocusChanged(hasFocus);
579

    
580
    if( firstScreen )
581
      {
582
      mColsPicker.setValue(mNumCols);
583
      mRowsPicker.setValue(mNumRows);
584
      }
585
    }
586

    
587
///////////////////////////////////////////////////////////////////////////////////////////////////
588
    
589
  @Override
590
  protected void onDestroy()
591
    {
592
    Distorted.onDestroy();
593
    super.onDestroy();
594
    }
595
    
596
///////////////////////////////////////////////////////////////////////////////////////////////////
597
    
598
  public void onProgressChanged(SeekBar bar, int progress, boolean fromUser)
599
    {
600
    switch (bar.getId())
601
      {
602
      case R.id.fragment3dcenterX       : centerX    = progress; computeCenter()    ; setCenterText()    ; break;
603
      case R.id.fragment3dcenterY       : centerY    = progress; computeCenter()    ; setCenterText()    ; break;
604
      case R.id.fragment3dchromaBar1    : chromaL    = progress; computeChroma()    ; setChromaText()    ; break;
605
      case R.id.fragment3dchromaBar2    : chromaR    = progress; computeChroma()    ; setChromaText()    ; break;
606
      case R.id.fragment3dchromaBar3    : chromaG    = progress; computeChroma()    ; setChromaText()    ; break;
607
      case R.id.fragment3dchromaBar4    : chromaB    = progress; computeChroma()    ; setChromaText()    ; break;
608
      case R.id.fragment3dalphaBar1     : alphaL     = progress; computeAlpha()     ; setAlphaText()     ; break;
609
      case R.id.fragment3dbrightnessBar1: brightnessL= progress; computeBrightness(); setBrightnessText(); break;
610
      case R.id.fragment3dsaturationBar1: saturationL= progress; computeSaturation(); setSaturationText(); break;
611
      }
612
    }
613

    
614
///////////////////////////////////////////////////////////////////////////////////////////////////
615

    
616
  public void onStartTrackingTouch(SeekBar bar) { }
617
    
618
///////////////////////////////////////////////////////////////////////////////////////////////////
619

    
620
  public void onStopTrackingTouch(SeekBar bar)  { }
621
  }
(1-1/3)