Project

General

Profile

« Previous | Next » 

Revision bfcf419a

Added by Leszek Koltunski over 7 years ago

Improvements for the 'Cubes' and 'Effects3D' apps.

View differences:

src/main/java/org/distorted/examples/effects3d/Effects3DActivity.java
53 53
///////////////////////////////////////////////////////////////////////////////////////////////////
54 54

  
55 55
public class Effects3DActivity extends Activity
56
                              implements View.OnClickListener,
57
                                         AdapterView.OnItemSelectedListener
56
                               implements View.OnClickListener,
57
                                          AdapterView.OnItemSelectedListener
58 58
  {
59 59
  private static final int COLOR_OFF = 0xffffe81f;
60 60
  private static final int COLOR_ON  = 0xff0000ff;
61
  private static final int COLOR_INAC= 0xff00ff00;
61 62

  
62
  private boolean firstScreen;
63

  
64
  // fields needed for the first 'pick-a-shape' screen
65
  //
66 63
  private int mNumCols = 10;
67 64
  private int mNumRows = 10;
68 65
  private NumberPicker mColsPicker, mRowsPicker;
......
73 70
  private int mObjectType;
74 71
  private int mBitmapID;
75 72
  private Bitmap mBitmap;
73
  private LinearLayout mLay;
76 74

  
77 75
  private ArrayList<Effects3DEffect> mList;
78 76
  private int mEffectAdd;
......
97 95

  
98 96
    setContentView(R.layout.objectpickerlayout);
99 97

  
98
    mLay = (LinearLayout)findViewById(R.id.objectpicker_buttongrid);
99

  
100 100
    mColsPicker = (NumberPicker)findViewById(R.id.objectpicker_cols);
101 101
    mRowsPicker = (NumberPicker)findViewById(R.id.objectpicker_rows);
102 102

  
......
106 106
    mRowsPicker.setMinValue( 0);
107 107

  
108 108
    mColsPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener()
109
      {
110
      @Override
111
      public void onValueChange(NumberPicker picker, int oldVal, int newVal)
112
        {
113
        mNumCols = mColsPicker.getValue();
114
        }
115
      });
109
         {
110
         @Override
111
         public void onValueChange(NumberPicker picker, int oldVal, int newVal)
112
           {
113
           setGrid();
114
           }
115
         });
116 116

  
117 117
    mRowsPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener()
118
      {
119
      @Override
120
      public void onValueChange(NumberPicker picker, int oldVal, int newVal)
121
        {
122
        mNumRows = mRowsPicker.getValue();
123
        }
124
      });
125

  
126
    firstScreen = true;
118
         {
119
         @Override
120
         public void onValueChange(NumberPicker picker, int oldVal, int newVal)
121
           {
122
           setGrid();
123
           }
124
         });
127 125

  
128 126
    mObjectType = 0;
129 127

  
130 128
    Spinner typeSpinner  = (Spinner)findViewById(R.id.objectpicker_spinnerType);
131 129
    typeSpinner.setOnItemSelectedListener(this);
132 130

  
133
    String[] objectType = new String[] {"Cubes", "Flat"};
131
    String[] objectType = new String[] {"Mesh: Cubes", "Mesh: Flat"};
134 132

  
135 133
    ArrayAdapter<String> adapterType = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, objectType);
136 134
    adapterType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
......
139 137
    Spinner bitmapSpinner  = (Spinner)findViewById(R.id.objectpicker_spinnerBitmap);
140 138
    bitmapSpinner.setOnItemSelectedListener(this);
141 139

  
142
    String[] objectBitmap = new String[] { "Grid", "Girl", "Dog", "Cat", "Squares", "Bean", "Lisa"};
140
    String[] objectBitmap = new String[] { "Texture: Grid", "Texture: Girl", "Texture: Dog", "Texture: Cat",
141
                                           "Texture: Squares", "Texture: Bean", "Texture: Lisa" };
143 142

  
144 143
    ArrayAdapter<String> adapterBitmap = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, objectBitmap);
145 144
    adapterBitmap.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
......
175 174

  
176 175
  private void setGrid()
177 176
    {
178
    LinearLayout lay = (LinearLayout)findViewById(R.id.objectpicker_buttongrid);
177
    mNumCols = mColsPicker.getValue();
178
    mNumRows = mRowsPicker.getValue();
179 179

  
180
    int width = lay.getWidth();
181
    int height= lay.getHeight();
180
    int width = mLay.getWidth();
181
    int height= mLay.getHeight();
182 182
    int w = mNumCols>0 ? (int)( 0.9f*width / mNumCols) : 0;
183 183
    int h = mNumRows>0 ? (int)( 0.9f*height/ mNumRows) : 0;
184 184
    int size= w<h ? w:h;
185 185
    int pad = size<20 ? 1 : size/20;
186 186

  
187
    lay.removeAllViews();
187
    mLay.removeAllViews();
188 188

  
189 189
    mShape = new boolean[mNumRows*mNumCols];
190 190

  
......
208 208
        b.setOnClickListener(this);
209 209
        b.setId(rows*mNumCols+cols);
210 210
        b.setLayoutParams(p);
211
        b.setBackgroundColor(COLOR_ON);
211
        b.setBackgroundColor(mObjectType==1 ? COLOR_INAC : COLOR_ON);
212 212
        tr.addView(b, p);
213 213
        mShape[rows*mNumCols+cols] = true;
214 214
        }
215 215

  
216
      lay.addView(tr);
216
      mLay.addView(tr);
217 217
      }
218 218
    }
219 219

  
......
320 320

  
321 321
  public void onClick(View view)
322 322
    {
323
    Button tmp = (Button)view;
324
    int id = tmp.getId();
325
    mShape[id] = !mShape[id];
326
    tmp.setBackgroundColor(mShape[id] ? COLOR_ON:COLOR_OFF);
323
    if( mObjectType!=1 )
324
      {
325
      Button tmp = (Button)view;
326
      int id = tmp.getId();
327
      mShape[id] = !mShape[id];
328
      tmp.setBackgroundColor(mShape[id] ? COLOR_ON:COLOR_OFF);
329
      }
330
    }
331

  
332
///////////////////////////////////////////////////////////////////////////////////////////////////
333

  
334
  private void uncheckAll()
335
    {
336
    TableRow tr;
337
    Button butt;
338

  
339
    for (int row=0; row<mNumRows; row++)
340
      {
341
      tr = (TableRow)mLay.getChildAt(row);
342

  
343
      for(int col=0; col<mNumCols; col++)
344
        {
345
        butt = (Button)tr.getVirtualChildAt(col);
346
        butt.setBackgroundColor(mObjectType==1 ? COLOR_INAC : COLOR_ON);
347
        mShape[row*mNumCols+col] = true;
348
        }
349
      }
327 350
    }
328 351

  
329 352
///////////////////////////////////////////////////////////////////////////////////////////////////
......
396 419

  
397 420
///////////////////////////////////////////////////////////////////////////////////////////////////
398 421

  
399
  public void Continue(View v)
422
  public void Create(View v)
400 423
    {
401
    firstScreen = false;
402

  
403 424
    DistortedEffects.setMaxVertex(20);    // those have to be called before
404 425
    DistortedEffects.setMaxFragment(3);   // any DistortedEffect get created!
405 426

  
406 427
    if( mObjectType==1 )
407 428
      {
408
      getBitmap();
409

  
410
      int w = mBitmap.getWidth();
411
      int h = mBitmap.getHeight();
412

  
413
      mEffects = new DistortedEffects();
414
      mTexture= new DistortedTexture(w,h);
415
      mMesh = new MeshFlat(mNumCols,mNumCols*h/w);
416
      setEffectView();
429
      mMesh = new MeshFlat(mNumCols,mNumRows);
417 430
      }
418 431
    else
419 432
      {
420
      View view = getLayoutInflater().inflate(R.layout.objectpicker2layout, null);
433
      String str = "";
421 434

  
422
      setContentView(view);
435
      for(int i=0; i<mNumRows*mNumCols; i++)
436
        str += mShape[i] ? "1" : "0";
423 437

  
424
      view.post(new Runnable() {
425
            @Override
426
            public void run() {
427
              setGrid();
428
            }
429
        });
438
      mMesh = new MeshCubes(mNumCols, str, false);
430 439
      }
431
    }
432 440

  
433
///////////////////////////////////////////////////////////////////////////////////////////////////
441
    mEffects= new DistortedEffects();
442
    mTexture= new DistortedTexture(mNumCols,mNumRows);
434 443

  
435
  public void Create(View v)
436
    {
437
    firstScreen = false;
444
    resetData();
438 445

  
439
    String str = "";
446
    final View view = getLayoutInflater().inflate(R.layout.effects3dlayout, null);
440 447

  
441
    for(int i=0; i<mNumRows*mNumCols; i++)
442
      str += mShape[i] ? "1" : "0";
448
    setContentView(view);
443 449

  
444
    mEffects = new DistortedEffects();
445
    mTexture= new DistortedTexture(mNumCols,mNumRows);
446
    mMesh = new MeshCubes(mNumCols, str, false);
450
    String[] effects = new String[mEffectNames.length];
447 451

  
448
    setEffectView();
452
    for(int i=0; i<mEffectNames.length; i++) effects[i] = mEffectNames[i].name();
453

  
454
    Spinner effectSpinner = (Spinner)findViewById(R.id.effects3dspinner );
455
    effectSpinner.setOnItemSelectedListener(this);
456

  
457
    ArrayAdapter<String> adapterEffect = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, effects);
458
    adapterEffect.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
459
    effectSpinner.setAdapter(adapterEffect);
460

  
461
    mEffectAdd = 0;
449 462
    }
450 463

  
451 464
///////////////////////////////////////////////////////////////////////////////////////////////////
......
454 467
    {
455 468
    switch(parent.getId())
456 469
      {
457
      case R.id.objectpicker_spinnerType  : mObjectType = pos;
470
      case R.id.objectpicker_spinnerType  : if( mObjectType!=pos )
471
                                              {
472
                                              mObjectType = pos;
473
                                              uncheckAll();
474
                                              }
458 475
                                            break;
459 476
      case R.id.objectpicker_spinnerBitmap: switch(pos)
460 477
                                              {
......
523 540
///////////////////////////////////////////////////////////////////////////////////////////////////
524 541
// 'second screen' methods
525 542

  
526
  private void setEffectView()
527
    {
528
    resetData();
529

  
530
    final View view = getLayoutInflater().inflate(R.layout.effects3dlayout, null);
531

  
532
    setContentView(view);
533

  
534
    String[] effects = new String[mEffectNames.length];
535

  
536
    for(int i=0; i<mEffectNames.length; i++) effects[i] = mEffectNames[i].name();
537

  
538
    Spinner effectSpinner = (Spinner)findViewById(R.id.effects3dspinner );
539
    effectSpinner.setOnItemSelectedListener(this);
540

  
541
    ArrayAdapter<String> adapterEffect = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, effects);
542
    adapterEffect.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
543
    effectSpinner.setAdapter(adapterEffect);
544

  
545
    mEffectAdd = 0;
546
    }
547

  
548
///////////////////////////////////////////////////////////////////////////////////////////////////
549

  
550 543
  public void newEffect(View v)
551 544
    {
552 545
    Effects3DEffect eff = new Effects3DEffect(mEffectNames[mEffectAdd], this);
......
648 641
    {
649 642
    super.onWindowFocusChanged(hasFocus);
650 643

  
651
    if( firstScreen )
652
      {
653
      mColsPicker.setValue(mNumCols);
654
      mRowsPicker.setValue(mNumRows);
655
      }
644
    mColsPicker.setValue(mNumCols);
645
    mRowsPicker.setValue(mNumRows);
646

  
647
    if( hasFocus ) setGrid();
656 648
    }
657 649

  
658 650
///////////////////////////////////////////////////////////////////////////////////////////////////

Also available in: Unified diff