Project

General

Profile

« Previous | Next » 

Revision bfcf419a

Added by Leszek Koltunski almost 8 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
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/examples/effects3d/Effects3DRenderer.java
75 75
      Effects3DActivity act = (Effects3DActivity)v.getContext();
76 76

  
77 77
      mObjectTexture     = act.getTexture();
78
      mObjectMesh = act.getMesh();
78
      mObjectMesh        = act.getMesh();
79 79
      mObjectEffects     = act.getEffects();
80 80
      mBackgroundTexture = new DistortedTexture(100,100);
81 81
      mCenterTexture     = new DistortedTexture(100,100);
src/main/res/layout/cubespickerlayout.xml
1 1
<?xml version="1.0" encoding="utf-8"?>
2 2

  
3 3
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
4
    android:orientation="vertical"
5
    android:layout_width="match_parent"
6
    android:layout_height="match_parent">
4
       android:orientation="vertical"
5
       android:layout_width="match_parent"
6
       android:layout_height="match_parent">
7 7

  
8 8
       <LinearLayout
9
        android:orientation="horizontal"
10
        android:layout_width="match_parent"
11
        android:layout_height="wrap_content">
9
           android:orientation="horizontal"
10
           android:layout_width="match_parent"
11
           android:layout_height="wrap_content">
12 12

  
13
        <NumberPicker
14
         android:id="@+id/cubespicker_rows"
15
         android:layout_width="wrap_content"
16
         android:layout_height="wrap_content"
17
         android:orientation="vertical"
18
         android:layout_span="1"
19
         />
13
           <NumberPicker
14
               android:id="@+id/cubespicker_rows"
15
               android:layout_width="wrap_content"
16
               android:layout_height="wrap_content"
17
               android:orientation="vertical"
18
               android:descendantFocusability="blocksDescendants"
19
           />
20 20

  
21
        <NumberPicker
22
         android:id="@+id/cubespicker_cols"
23
         android:layout_width="wrap_content"
24
         android:layout_height="wrap_content"
25
         android:orientation="vertical"
26
         android:layout_span="2"/>
21
           <NumberPicker
22
               android:id="@+id/cubespicker_cols"
23
               android:layout_width="wrap_content"
24
               android:layout_height="wrap_content"
25
               android:orientation="vertical"
26
               android:descendantFocusability="blocksDescendants"
27
           />
27 28

  
28
        <Button
29
            android:id="@+id/cubespicker_create"
30
            android:onClick="Create"
31
            android:text="@string/Create"
32
            android:layout_width="match_parent"
33
            android:layout_height="fill_parent"
34
            android:layout_span="3"
35
            android:layout_marginTop="5dp"/>
36
    </LinearLayout>
29
           <Button
30
               android:id="@+id/cubespicker_create"
31
               android:onClick="Create"
32
               android:text="@string/Create"
33
               android:layout_width="match_parent"
34
               android:layout_height="fill_parent"
35
           />
36
       </LinearLayout>
37 37

  
38 38
       <LinearLayout
39
        android:id="@+id/cubespicker_buttongrid"
40
        android:layout_width="match_parent"
41
        android:layout_height="fill_parent"
42
        android:gravity="center"
43
        android:orientation="vertical"
44
        android:layout_weight="0.8">
45
    </LinearLayout>
39
           android:id="@+id/cubespicker_buttongrid"
40
           android:layout_width="match_parent"
41
           android:layout_height="0dp"
42
           android:gravity="center"
43
           android:orientation="vertical"
44
           android:layout_weight="0.8">
45
       </LinearLayout>
46 46

  
47 47
   </LinearLayout>
src/main/res/layout/objectpicker2layout.xml
1
<?xml version="1.0" encoding="utf-8"?>
2

  
3
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
4
    android:orientation="vertical"
5
    android:layout_width="match_parent"
6
    android:layout_height="match_parent">
7

  
8
       <Button
9
        android:id="@+id/objectpicker_create"
10
        android:onClick="Create"
11
        android:text="@string/Create"
12
        android:layout_width="match_parent"
13
        android:layout_height="80dp"
14
        android:layout_span="3"
15
        android:layout_marginTop="5dp"/>
16

  
17
       <LinearLayout
18
        android:id="@+id/objectpicker_buttongrid"
19
        android:layout_width="match_parent"
20
        android:layout_height="fill_parent"
21
        android:gravity="center"
22
        android:orientation="vertical"
23
        android:layout_weight="0.8">
24
    </LinearLayout>
25

  
26
   </LinearLayout>
src/main/res/layout/objectpickerlayout.xml
1 1
<?xml version="1.0" encoding="utf-8"?>
2 2

  
3
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
4
                 android:orientation="vertical"
5
                 android:layout_width="match_parent"
6
                 android:layout_height="match_parent"
7
                 android:gravity="center_vertical">
3
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
4
     android:orientation="vertical"
5
     android:layout_width="match_parent"
6
     android:layout_height="match_parent">
8 7

  
9
       <TableLayout
8
     <LinearLayout
9
           android:orientation="horizontal"
10 10
           android:layout_width="match_parent"
11
           android:layout_height="wrap_content"
12
           android:stretchColumns="1,2,3,4"
13
           android:shrinkColumns="1,2,3,4"
14
           >
11
           android:layout_height="wrap_content">
15 12

  
16
        <TableRow
17
            android:layout_width="match_parent"
18
            android:layout_height="match_parent"
19
            android:paddingTop="10dp">
20

  
21
            <TextView
22
                android:layout_width="wrap_content"
23
                android:layout_height="wrap_content"
24
                android:textAppearance="?android:attr/textAppearanceMedium"
25
                android:text="@string/Create"
26
                android:id="@+id/textView2"
27
                android:layout_gravity="center_vertical"
28
                android:layout_marginLeft="10dp"/>
29

  
30
            <Spinner
31
                android:layout_width="fill_parent"
13
           <Spinner
14
                android:layout_width="0dp"
32 15
                android:layout_height="50dp"
16
                android:layout_weight="0.5"
33 17
                android:id="@+id/objectpicker_spinnerType"
34
                android:layout_span="4"/>
35
        </TableRow>
36

  
37
           <TableRow
38
               android:layout_width="match_parent"
39
               android:layout_height="match_parent"
40
               android:paddingTop="10dp">
18
           />
41 19

  
42
               <TextView
43
                   android:layout_width="wrap_content"
44
                   android:layout_height="wrap_content"
45
                   android:textAppearance="?android:attr/textAppearanceMedium"
46
                   android:text="@string/Bitmap"
47
                   android:id="@+id/textView7"
48
                   android:layout_marginLeft="10dp"
49
                   android:layout_gravity="center_vertical"/>
50

  
51
               <Spinner
52
                   android:layout_width="wrap_content"
20
           <Spinner
21
                   android:layout_width="0dp"
53 22
                   android:layout_height="50dp"
23
                   android:layout_weight="0.5"
54 24
                   android:id="@+id/objectpicker_spinnerBitmap"
55
                   android:layout_span="4"/>
56
           </TableRow>
57

  
58
           <TableRow
59
            android:layout_width="match_parent"
60
            android:layout_height="match_parent"
61
            android:paddingTop="10dp"
62
            android:paddingBottom="10dp">
63

  
64
            <TextView
65
                android:layout_width="wrap_content"
66
                android:layout_height="wrap_content"
67
                android:textAppearance="?android:attr/textAppearanceMedium"
68
                android:text="@string/rows"
69
                android:id="@+id/textView8"
70
                android:layout_gravity="center_vertical"
71
                android:layout_marginLeft="10dp"
72
                />
25
           />
26
     </LinearLayout>
73 27

  
74
            <NumberPicker
75
                android:id="@+id/objectpicker_rows"
76
                android:layout_width="wrap_content"
77
                android:layout_height="wrap_content"
78
                android:orientation="vertical"
79
                />
80

  
81
            <TextView
82
                android:layout_width="wrap_content"
83
                android:layout_height="wrap_content"
84
                android:textAppearance="?android:attr/textAppearanceMedium"
85
                android:text="@string/cols"
86
                android:id="@+id/textView9"
87
                android:layout_marginLeft="10dp"
88
                android:layout_gravity="center_vertical"
89
                />
90

  
91
            <NumberPicker
92
                android:id="@+id/objectpicker_cols"
93
                android:layout_width="wrap_content"
94
                android:layout_height="wrap_content"
95
                android:orientation="vertical"
96
                />
97
        </TableRow>
98

  
99
       </TableLayout>
28
     <LinearLayout
29
           android:orientation="horizontal"
30
           android:layout_width="match_parent"
31
           android:layout_height="wrap_content">
32

  
33
           <NumberPicker
34
               android:id="@+id/objectpicker_rows"
35
               android:layout_width="wrap_content"
36
               android:layout_height="wrap_content"
37
               android:orientation="vertical"
38
               android:descendantFocusability="blocksDescendants"
39
           />
40

  
41
           <NumberPicker
42
               android:id="@+id/objectpicker_cols"
43
               android:layout_width="wrap_content"
44
               android:layout_height="wrap_content"
45
               android:orientation="vertical"
46
               android:descendantFocusability="blocksDescendants"
47
           />
48

  
49
           <Button
50
               android:id="@+id/objectpicker_create"
51
               android:onClick="Create"
52
               android:text="@string/Create"
53
               android:layout_width="match_parent"
54
               android:layout_height="fill_parent"
55
           />
56
       </LinearLayout>
100 57

  
101
    <Button
102
        android:id="@+id/objectpicker_create"
103
        android:onClick="Continue"
104
        android:text="@string/continu"
105
        android:layout_width="match_parent"
106
        android:layout_height="50dp"
107
        android:layout_span="4"
108
        android:layout_marginTop="5dp"/>
58
       <LinearLayout
59
           android:id="@+id/objectpicker_buttongrid"
60
           android:layout_width="match_parent"
61
           android:layout_height="0dp"
62
           android:gravity="center"
63
           android:orientation="vertical"
64
           android:layout_weight="0.8">
65
       </LinearLayout>
109 66

  
110 67
</LinearLayout>
src/main/res/values/strings.xml
60 60
    <string name="angleB">Beta</string>
61 61
    <string name="DepthYes">Depth</string>
62 62
    <string name="DepthNo">No Depth</string>
63
    <string name="mesh">Mesh</string>
63 64

  
64 65
    <string name="radius_placeholder">Radius: %1$s</string>
65 66
    <string name="noise_placeholder">Noise %1$s</string>

Also available in: Unified diff