Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effects3d / Effects3DActivity.java @ 0579fe3a

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.effects3d;
21

    
22
import android.app.Activity;
23
import android.content.Intent;
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.Spinner;
33
import android.widget.TableRow;
34

    
35
import org.distorted.examples.R;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

    
39
public class Effects3DActivity extends Activity
40
                               implements View.OnClickListener, AdapterView.OnItemSelectedListener
41
  {
42
  private static final int COLOR_OFF = 0xffffe81f;
43
  private static final int COLOR_ON  = 0xff0000ff;
44
  private static final int COLOR_INAC= 0xff999999;
45

    
46
  private int mNumCols = 1;
47
  private int mNumRows = 1;
48
  private int mNumSlic = 1;
49
  private boolean mGridInitialized;
50
  private NumberPicker mColsPicker, mRowsPicker, mSlicPicker;
51
  private boolean[] mShape;
52
  private int mObjectType;
53
  private int mBitmapID;
54
  private LinearLayout mLay;
55

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

    
58
  @Override
59
  protected void onCreate(Bundle savedState)
60
    {
61
    super.onCreate(savedState);
62

    
63
    setContentView(R.layout.objectpickerlayout);
64

    
65
    mLay = findViewById(R.id.objectpicker_buttongrid);
66

    
67
    mColsPicker = findViewById(R.id.objectpicker_cols);
68
    mRowsPicker = findViewById(R.id.objectpicker_rows);
69
    mSlicPicker = findViewById(R.id.objectpicker_slices);
70

    
71
    mColsPicker.setMaxValue(40);
72
    mColsPicker.setMinValue( 0);
73
    mRowsPicker.setMaxValue(40);
74
    mRowsPicker.setMinValue( 0);
75
    mSlicPicker.setMaxValue(40);
76
    mSlicPicker.setMinValue( 0);
77

    
78
    mColsPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener()
79
         {
80
         @Override
81
         public void onValueChange(NumberPicker picker, int oldVal, int newVal)
82
           {
83
           setGrid();
84
           }
85
         });
86

    
87
    mRowsPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener()
88
         {
89
         @Override
90
         public void onValueChange(NumberPicker picker, int oldVal, int newVal)
91
           {
92
           setGrid();
93
           }
94
         });
95

    
96
    mSlicPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener()
97
         {
98
         @Override
99
         public void onValueChange(NumberPicker picker, int oldVal, int newVal)
100
           {
101
           mNumSlic = mSlicPicker.getValue();
102
           }
103
         });
104

    
105
    mObjectType = 0;
106
    mGridInitialized = false;
107

    
108
    Spinner typeSpinner  = findViewById(R.id.objectpicker_spinnerType);
109
    typeSpinner.setOnItemSelectedListener(this);
110

    
111
    String[] objectType = new String[] {"Mesh: Cubes", "Mesh: Flat", "Mesh: Sphere", "Mesh: Quad"};
112

    
113
    ArrayAdapter<String> adapterType = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, objectType);
114
    adapterType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
115
    typeSpinner.setAdapter(adapterType);
116

    
117
    Spinner bitmapSpinner  = findViewById(R.id.objectpicker_spinnerBitmap);
118
    bitmapSpinner.setOnItemSelectedListener(this);
119

    
120
    String[] objectBitmap = new String[] { "Texture: Grid", "Texture: Girl", "Texture: Dog", "Texture: Cat",
121
                                           "Texture: Squares", "Texture: Bean", "Texture: Lisa" };
122

    
123
    ArrayAdapter<String> adapterBitmap = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, objectBitmap);
124
    adapterBitmap.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
125
    bitmapSpinner.setAdapter(adapterBitmap);
126
    }
127

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

    
130
  private void setGrid()
131
    {
132
    mGridInitialized = true;
133

    
134
    mNumCols = mColsPicker.getValue();
135
    mNumRows = mRowsPicker.getValue();
136

    
137
    int width = mLay.getWidth();
138
    int height= mLay.getHeight();
139
    int w = mNumCols>0 ? (int)( 0.9f*width / mNumCols) : 0;
140
    int h = mNumRows>0 ? (int)( 0.9f*height/ mNumRows) : 0;
141
    int size= w<h ? w:h;
142
    int pad = size<20 ? 1 : size/20;
143

    
144
    mLay.removeAllViews();
145

    
146
    mShape = new boolean[mNumRows*mNumCols];
147

    
148
    TableRow.LayoutParams p = new TableRow.LayoutParams();
149

    
150
    p.rightMargin  = pad;
151
    p.leftMargin   = pad;
152
    p.topMargin    = pad;
153
    p.bottomMargin = pad;
154
    p.height       = size;
155
    p.width        = size;
156

    
157
    for (int rows=0; rows<mNumRows; rows++)
158
      {
159
      TableRow tr = new TableRow(this);
160
      tr.setGravity(Gravity.CENTER);
161

    
162
      for(int cols=0; cols<mNumCols; cols++)
163
        {
164
        Button b = new Button(this);
165
        b.setOnClickListener(this);
166
        b.setId(rows*mNumCols+cols);
167
        b.setLayoutParams(p);
168
        b.setBackgroundColor(mObjectType==0 ? COLOR_ON:COLOR_INAC);
169
        tr.addView(b, p);
170
        mShape[rows*mNumCols+cols] = true;
171
        }
172

    
173
      mLay.addView(tr);
174
      }
175
    }
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

    
179
  public void onClick(View view)
180
    {
181
    if( mObjectType==0 )  // cubes
182
      {
183
      Button tmp = (Button)view;
184
      int id = tmp.getId();
185
      mShape[id] = !mShape[id];
186
      tmp.setBackgroundColor(mShape[id] ? COLOR_ON:COLOR_OFF);
187
      }
188
    }
189

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191

    
192
  private void uncheckAll()
193
    {
194
    TableRow tr;
195
    Button butt;
196

    
197
    for (int row=0; row<mNumRows; row++)
198
      {
199
      tr = (TableRow)mLay.getChildAt(row);
200

    
201
      for(int col=0; col<mNumCols; col++)
202
        {
203
        butt = (Button)tr.getVirtualChildAt(col);
204
        butt.setBackgroundColor(mObjectType==0 ? COLOR_ON : COLOR_INAC);
205
        mShape[row*mNumCols+col] = true;
206
        }
207
      }
208
    }
209

    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

    
212
  public void Create(View v)
213
    {
214
    Intent mainInt = new Intent( getApplicationContext(), Effects3DActivity2.class);
215
    Bundle b = new Bundle();
216

    
217
    int rows=0, cols=0;
218

    
219
    switch(mObjectType)
220
      {
221
      case 0:
222
      case 1: rows = mNumRows;
223
              cols = mNumCols;
224
              break;
225
      case 2: rows = mNumRows;   // always make the sphere equal in X and Y
226
              cols = mNumRows;   //
227
              break;
228
      case 3: rows = 1;          // a quad is always 1x1
229
              cols = 1;
230
              break;
231
      }
232

    
233
    b.putInt("type", mObjectType);
234
    b.putInt("cols", cols);
235
    b.putInt("rows", rows);
236
    b.putInt("slices", mNumSlic);
237
    b.putInt("bitmap", mBitmapID);
238

    
239
    if( mObjectType==0 )   // cubes
240
      {
241
      String str = "";
242

    
243
      for(int i=0; i<mNumRows*mNumCols; i++)
244
        str += mShape[i] ? "1" : "0";
245

    
246
      b.putString("string", str);
247
      }
248
    else
249
      {
250
      b.putString("string", "");
251
      }
252

    
253
    mainInt.putExtras(b);
254
    startActivity(mainInt);
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  : if( mObjectType!=pos )
264
                                              {
265
                                              mObjectType = pos;
266
                                              uncheckAll();
267

    
268
                                              switch(mObjectType)
269
                                                {
270
                                                case 0: mColsPicker.setEnabled(true);
271
                                                        mRowsPicker.setEnabled(true);
272
                                                        mSlicPicker.setEnabled(true);
273
                                                        break;
274
                                                case 1: mColsPicker.setEnabled(true);
275
                                                        mRowsPicker.setEnabled(true);
276
                                                        mSlicPicker.setEnabled(false);
277
                                                        break;
278
                                                case 2: mColsPicker.setEnabled(false);
279
                                                        mRowsPicker.setEnabled(true);
280
                                                        mSlicPicker.setEnabled(false);
281
                                                        break;
282
                                                case 3: mColsPicker.setEnabled(false);
283
                                                        mRowsPicker.setEnabled(false);
284
                                                        mSlicPicker.setEnabled(false);
285
                                                        break;
286
                                                }
287
                                              }
288
                                            break;
289
      case R.id.objectpicker_spinnerBitmap: switch(pos)
290
                                              {
291
                                              case 0: mBitmapID = -1        ; break;
292
                                              case 1: mBitmapID = R.raw.face; break;
293
                                              case 2: mBitmapID = R.raw.dog ; break;
294
                                              case 3: mBitmapID = R.raw.cat ; break;
295
                                              case 4: mBitmapID = R.raw.grid; break;
296
                                              case 5: mBitmapID = R.raw.bean; break;
297
                                              case 6: mBitmapID = R.raw.monalisa; break;
298
                                              }
299
                                            break;
300
      }
301
    }
302

    
303
///////////////////////////////////////////////////////////////////////////////////////////////////
304

    
305
  public void onNothingSelected(AdapterView<?> parent)
306
    {
307
    }
308

    
309
///////////////////////////////////////////////////////////////////////////////////////////////////
310
// Overrides
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312

    
313
  @Override
314
  public void onWindowFocusChanged(boolean hasFocus)
315
    {
316
    super.onWindowFocusChanged(hasFocus);
317

    
318
    mColsPicker.setValue(mNumCols);
319
    mRowsPicker.setValue(mNumRows);
320
    mSlicPicker.setValue(mNumSlic);
321

    
322
    if( !mGridInitialized ) setGrid();
323
    }
324
  }
(1-1/7)