Project

General

Profile

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

examples / src / main / java / org / distorted / examples / inflate / InflateActivity.java @ 83e0ca5e

1 77a500b3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 77a500b3 Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 77a500b3 Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 77a500b3 Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 77a500b3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20
package org.distorted.examples.inflate;
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 InflateActivity 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 24624a1a Leszek Koltunski
  private static final int COLOR_INAC= 0xff999999;
45 77a500b3 Leszek Koltunski
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 a886f1fc Leszek Koltunski
    mColsPicker.setMinValue( 1);
73 77a500b3 Leszek Koltunski
    mRowsPicker.setMaxValue(40);
74 a886f1fc Leszek Koltunski
    mRowsPicker.setMinValue( 1);
75 77a500b3 Leszek Koltunski
    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 83e0ca5e Leszek Koltunski
    String[] objectType = new String[InflateMeshList.LENGTH];
112
113
    for(int mesh=0; mesh<InflateMeshList.LENGTH; mesh++)
114
      {
115
      objectType[mesh] = "Mesh: "+InflateMeshList.getName(mesh);
116
      }
117 77a500b3 Leszek Koltunski
118
    ArrayAdapter<String> adapterType = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, objectType);
119
    adapterType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
120
    typeSpinner.setAdapter(adapterType);
121
122
    Spinner bitmapSpinner  = findViewById(R.id.objectpicker_spinnerBitmap);
123
    bitmapSpinner.setOnItemSelectedListener(this);
124
125
    String[] objectBitmap = new String[] { "Texture: Grid", "Texture: Girl", "Texture: Dog", "Texture: Cat",
126 222ea32f Leszek Koltunski
                                           "Texture: Squares", "Texture: Bean", "Texture: Lisa", "Texture: World" };
127 77a500b3 Leszek Koltunski
128
    ArrayAdapter<String> adapterBitmap = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, objectBitmap);
129
    adapterBitmap.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
130
    bitmapSpinner.setAdapter(adapterBitmap);
131
    }
132
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134
135
  private void setGrid()
136
    {
137
    mGridInitialized = true;
138
139
    mNumCols = mColsPicker.getValue();
140
    mNumRows = mRowsPicker.getValue();
141
142
    int width = mLay.getWidth();
143
    int height= mLay.getHeight();
144
    int w = mNumCols>0 ? (int)( 0.9f*width / mNumCols) : 0;
145
    int h = mNumRows>0 ? (int)( 0.9f*height/ mNumRows) : 0;
146
    int size= w<h ? w:h;
147
    int pad = size<20 ? 1 : size/20;
148
149
    mLay.removeAllViews();
150
151
    mShape = new boolean[mNumRows*mNumCols];
152
153
    TableRow.LayoutParams p = new TableRow.LayoutParams();
154
155
    p.rightMargin  = pad;
156
    p.leftMargin   = pad;
157
    p.topMargin    = pad;
158
    p.bottomMargin = pad;
159
    p.height       = size;
160
    p.width        = size;
161
162
    for (int rows=0; rows<mNumRows; rows++)
163
      {
164
      TableRow tr = new TableRow(this);
165
      tr.setGravity(Gravity.CENTER);
166
167
      for(int cols=0; cols<mNumCols; cols++)
168
        {
169
        Button b = new Button(this);
170
        b.setOnClickListener(this);
171
        b.setId(rows*mNumCols+cols);
172
        b.setLayoutParams(p);
173 0579fe3a Leszek Koltunski
        b.setBackgroundColor(mObjectType==0 ? COLOR_ON:COLOR_INAC);
174 77a500b3 Leszek Koltunski
        tr.addView(b, p);
175
        mShape[rows*mNumCols+cols] = true;
176
        }
177
178
      mLay.addView(tr);
179
      }
180
    }
181
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183
184
  public void onClick(View view)
185
    {
186 24624a1a Leszek Koltunski
    if( mObjectType==0 )  // cubes
187 77a500b3 Leszek Koltunski
      {
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
197
  private void uncheckAll()
198
    {
199
    TableRow tr;
200
    Button butt;
201
202
    for (int row=0; row<mNumRows; row++)
203
      {
204
      tr = (TableRow)mLay.getChildAt(row);
205
206
      for(int col=0; col<mNumCols; col++)
207
        {
208
        butt = (Button)tr.getVirtualChildAt(col);
209 24624a1a Leszek Koltunski
        butt.setBackgroundColor(mObjectType==0 ? COLOR_ON : COLOR_INAC);
210 77a500b3 Leszek Koltunski
        mShape[row*mNumCols+col] = true;
211
        }
212
      }
213
    }
214
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216
217
  public void Create(View v)
218
    {
219
    Intent mainInt = new Intent( getApplicationContext(), InflateActivity2.class);
220
    Bundle b = new Bundle();
221
222 83e0ca5e Leszek Koltunski
    int rows,cols;
223 24624a1a Leszek Koltunski
224 83e0ca5e Leszek Koltunski
    switch( InflateMeshList.getDimension(mObjectType) )
225 24624a1a Leszek Koltunski
      {
226 83e0ca5e Leszek Koltunski
      case 0: rows = 1;          // a quad
227
              cols = 1;
228 24624a1a Leszek Koltunski
              break;
229 83e0ca5e Leszek Koltunski
      case 1: rows = mNumRows;   // Triangles, Sphere
230
              cols = mNumRows;
231 24624a1a Leszek Koltunski
              break;
232 83e0ca5e Leszek Koltunski
      default:rows = mNumRows;
233
              cols = mNumCols;
234 24624a1a Leszek Koltunski
              break;
235
      }
236
237 77a500b3 Leszek Koltunski
    b.putInt("type", mObjectType);
238 24624a1a Leszek Koltunski
    b.putInt("cols", cols);
239
    b.putInt("rows", rows);
240 77a500b3 Leszek Koltunski
    b.putInt("slices", mNumSlic);
241
    b.putInt("bitmap", mBitmapID);
242
243 24624a1a Leszek Koltunski
    if( mObjectType==0 )   // cubes
244 77a500b3 Leszek Koltunski
      {
245
      String str = "";
246
247
      for(int i=0; i<mNumRows*mNumCols; i++)
248
        str += mShape[i] ? "1" : "0";
249
250
      b.putString("string", str);
251
      }
252 24624a1a Leszek Koltunski
    else
253
      {
254
      b.putString("string", "");
255
      }
256 77a500b3 Leszek Koltunski
257
    mainInt.putExtras(b);
258
    startActivity(mainInt);
259
    }
260
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262
263
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
264
    {
265
    switch(parent.getId())
266
      {
267
      case R.id.objectpicker_spinnerType  : if( mObjectType!=pos )
268
                                              {
269
                                              mObjectType = pos;
270
                                              uncheckAll();
271 24624a1a Leszek Koltunski
272 83e0ca5e Leszek Koltunski
                                              int dim = InflateMeshList.getDimension(mObjectType);
273
274
                                              mRowsPicker.setEnabled(dim>=1);
275
                                              mColsPicker.setEnabled(dim>=2);
276
                                              mSlicPicker.setEnabled(dim>=3);
277 77a500b3 Leszek Koltunski
                                              }
278
                                            break;
279
      case R.id.objectpicker_spinnerBitmap: switch(pos)
280
                                              {
281 222ea32f Leszek Koltunski
                                              case 0: mBitmapID = -1            ; break;
282
                                              case 1: mBitmapID = R.raw.face    ; break;
283
                                              case 2: mBitmapID = R.raw.dog     ; break;
284
                                              case 3: mBitmapID = R.raw.cat     ; break;
285
                                              case 4: mBitmapID = R.raw.grid    ; break;
286
                                              case 5: mBitmapID = R.raw.bean    ; break;
287 77a500b3 Leszek Koltunski
                                              case 6: mBitmapID = R.raw.monalisa; break;
288 222ea32f Leszek Koltunski
                                              case 7: mBitmapID = R.raw.world   ; break;
289 77a500b3 Leszek Koltunski
                                              }
290
                                            break;
291
      }
292
    }
293
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295
296
  public void onNothingSelected(AdapterView<?> parent)
297
    {
298
    }
299
300
///////////////////////////////////////////////////////////////////////////////////////////////////
301
// Overrides
302
///////////////////////////////////////////////////////////////////////////////////////////////////
303
304
  @Override
305
  public void onWindowFocusChanged(boolean hasFocus)
306
    {
307
    super.onWindowFocusChanged(hasFocus);
308
309
    mColsPicker.setValue(mNumCols);
310
    mRowsPicker.setValue(mNumRows);
311
    mSlicPicker.setValue(mNumSlic);
312
313
    if( !mGridInitialized ) setGrid();
314
    }
315
  }