Project

General

Profile

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

examples / src / main / java / org / distorted / examples / generic / GenericActivity.java @ 61420934

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.generic;
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 GenericActivity 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( 1);
73
    mRowsPicker.setMaxValue(40);
74
    mRowsPicker.setMinValue( 1);
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[GenericMeshList.LENGTH];
112

    
113
    for(int mesh=0; mesh<GenericMeshList.LENGTH; mesh++)
114
      {
115
      objectType[mesh] = "Mesh: "+GenericMeshList.getName(mesh);
116
      }
117

    
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
                                           "Texture: Squares", "Texture: Bean", "Texture: Lisa", "Texture: World" };
127

    
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
        b.setBackgroundColor(mObjectType==0 ? COLOR_ON:COLOR_INAC);
174
        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
    if( mObjectType==0 )  // cubes
187
      {
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
        butt.setBackgroundColor(mObjectType==0 ? COLOR_ON : COLOR_INAC);
210
        mShape[row*mNumCols+col] = true;
211
        }
212
      }
213
    }
214

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

    
217
  public void Create(View v)
218
    {
219
     Intent mainInt = new Intent( getApplicationContext(), GenericActivity2.class);
220
    Bundle b = new Bundle();
221

    
222
    b.putInt("type"  , mObjectType);
223
    b.putInt("cols"  , GenericMeshList.getCols(mObjectType, mNumCols, mNumRows, mNumSlic) );
224
    b.putInt("rows"  , GenericMeshList.getRows(mObjectType, mNumCols, mNumRows, mNumSlic) );
225
    b.putInt("slices", GenericMeshList.getSlic(mObjectType, mNumCols, mNumRows, mNumSlic) );
226
    b.putInt("bitmap", mBitmapID);
227

    
228
    b.putString("string", GenericMeshList.getString(mObjectType, mNumCols, mNumRows, mShape));
229

    
230
    mainInt.putExtras(b);
231
    startActivity(mainInt);
232
    }
233

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235

    
236
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
237
    {
238
    switch(parent.getId())
239
      {
240
      case R.id.objectpicker_spinnerType  : if( mObjectType!=pos )
241
                                              {
242
                                              mObjectType = pos;
243
                                              uncheckAll();
244

    
245
                                              int dim = GenericMeshList.getDimension(mObjectType);
246

    
247
                                              mRowsPicker.setEnabled(dim>=1);
248
                                              mColsPicker.setEnabled(dim>=2);
249
                                              mSlicPicker.setEnabled(dim>=3);
250
                                              }
251
                                            break;
252
      case R.id.objectpicker_spinnerBitmap: switch(pos)
253
                                              {
254
                                              case 0: mBitmapID = -1            ; break;
255
                                              case 1: mBitmapID = R.raw.face    ; break;
256
                                              case 2: mBitmapID = R.raw.dog     ; break;
257
                                              case 3: mBitmapID = R.raw.cat     ; break;
258
                                              case 4: mBitmapID = R.raw.grid    ; break;
259
                                              case 5: mBitmapID = R.raw.bean    ; break;
260
                                              case 6: mBitmapID = R.raw.monalisa; break;
261
                                              case 7: mBitmapID = R.raw.world   ; break;
262
                                              }
263
                                            break;
264
      }
265
    }
266

    
267
///////////////////////////////////////////////////////////////////////////////////////////////////
268

    
269
  public void onNothingSelected(AdapterView<?> parent)
270
    {
271
    }
272

    
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274
// Overrides
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

    
277
  @Override
278
  public void onWindowFocusChanged(boolean hasFocus)
279
    {
280
    super.onWindowFocusChanged(hasFocus);
281

    
282
    mColsPicker.setValue(mNumCols);
283
    mRowsPicker.setValue(mNumRows);
284
    mSlicPicker.setValue(mNumSlic);
285

    
286
    if( !mGridInitialized ) setGrid();
287
    }
288
  }
(1-1/8)