Project

General

Profile

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

examples / src / main / java / org / distorted / examples / predeform / PredeformActivity.java @ 4b7c432e

1 59835a0a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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.predeform;
21
22
import android.app.Activity;
23
import android.content.Intent;
24
import android.os.Bundle;
25 4b7c432e Leszek Koltunski
import android.view.Gravity;
26 59835a0a Leszek Koltunski
import android.view.View;
27
import android.widget.AdapterView;
28
import android.widget.ArrayAdapter;
29 4b7c432e Leszek Koltunski
import android.widget.Button;
30
import android.widget.LinearLayout;
31 59835a0a Leszek Koltunski
import android.widget.NumberPicker;
32
import android.widget.Spinner;
33 4b7c432e Leszek Koltunski
import android.widget.TableRow;
34 59835a0a Leszek Koltunski
35
import org.distorted.examples.R;
36
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38
39 4b7c432e Leszek Koltunski
public class PredeformActivity extends Activity
40
                               implements View.OnClickListener, AdapterView.OnItemSelectedListener
41 59835a0a Leszek Koltunski
  {
42 4b7c432e Leszek Koltunski
  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 0dc8ffef Leszek Koltunski
  private NumberPicker mColsPicker, mRowsPicker, mSlicPicker;
51
  private boolean[] mShape;
52 4b7c432e Leszek Koltunski
  private int mObjectType;
53
  private int mBitmapID;
54
  private LinearLayout mLay;
55 59835a0a Leszek Koltunski
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57
58
  @Override
59
  protected void onCreate(Bundle savedState)
60
    {
61
    super.onCreate(savedState);
62
63 4b7c432e Leszek Koltunski
    setContentView(R.layout.objectpickerlayout);
64 59835a0a Leszek Koltunski
65 4b7c432e Leszek Koltunski
    mLay = findViewById(R.id.objectpicker_buttongrid);
66 59835a0a Leszek Koltunski
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 4b7c432e Leszek Koltunski
           setGrid();
84 59835a0a Leszek Koltunski
           }
85
         });
86
87
    mRowsPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener()
88
         {
89
         @Override
90
         public void onValueChange(NumberPicker picker, int oldVal, int newVal)
91
           {
92 4b7c432e Leszek Koltunski
           setGrid();
93 59835a0a Leszek Koltunski
           }
94
         });
95
96
    mSlicPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener()
97
         {
98
         @Override
99
         public void onValueChange(NumberPicker picker, int oldVal, int newVal)
100
           {
101 4b7c432e Leszek Koltunski
           mNumSlic = mSlicPicker.getValue();
102 59835a0a Leszek Koltunski
           }
103
         });
104
105
    mObjectType = 0;
106 4b7c432e Leszek Koltunski
    mGridInitialized = false;
107 59835a0a Leszek Koltunski
108
    Spinner typeSpinner  = findViewById(R.id.objectpicker_spinnerType);
109
    typeSpinner.setOnItemSelectedListener(this);
110
111
    String[] objectType = new String[PredeformMeshList.LENGTH];
112
113 4b7c432e Leszek Koltunski
    for(int mesh=0; mesh<PredeformMeshList.LENGTH; mesh++)
114 59835a0a Leszek Koltunski
      {
115 4b7c432e Leszek Koltunski
      objectType[mesh] = "Mesh: "+PredeformMeshList.getName(mesh);
116 59835a0a Leszek Koltunski
      }
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 0dc8ffef Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
134 59835a0a Leszek Koltunski
135 4b7c432e Leszek Koltunski
  private void setGrid()
136 0dc8ffef Leszek Koltunski
    {
137 4b7c432e Leszek Koltunski
    mGridInitialized = true;
138 59835a0a Leszek Koltunski
139 4b7c432e Leszek Koltunski
    mNumCols = mColsPicker.getValue();
140
    mNumRows = mRowsPicker.getValue();
141 59835a0a Leszek Koltunski
142 4b7c432e Leszek Koltunski
    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= Math.min(w, h);
147
    int pad = size<20 ? 1 : size/20;
148 59835a0a Leszek Koltunski
149 4b7c432e Leszek Koltunski
    mLay.removeAllViews();
150 0dc8ffef Leszek Koltunski
151 4b7c432e Leszek Koltunski
    mShape = new boolean[mNumRows*mNumCols];
152 0dc8ffef Leszek Koltunski
153 4b7c432e Leszek Koltunski
    TableRow.LayoutParams p = new TableRow.LayoutParams();
154 0dc8ffef Leszek Koltunski
155 4b7c432e Leszek Koltunski
    p.rightMargin  = pad;
156
    p.leftMargin   = pad;
157
    p.topMargin    = pad;
158
    p.bottomMargin = pad;
159
    p.height       = size;
160
    p.width        = size;
161 0dc8ffef Leszek Koltunski
162 4b7c432e Leszek Koltunski
    for (int rows=0; rows<mNumRows; rows++)
163 59835a0a Leszek Koltunski
      {
164 4b7c432e Leszek Koltunski
      TableRow tr = new TableRow(this);
165
      tr.setGravity(Gravity.CENTER);
166 0dc8ffef Leszek Koltunski
167 4b7c432e Leszek Koltunski
      for(int cols=0; cols<mNumCols; cols++)
168 59835a0a Leszek Koltunski
        {
169 4b7c432e Leszek Koltunski
        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 59835a0a Leszek Koltunski
        }
177
178 4b7c432e Leszek Koltunski
      mLay.addView(tr);
179 59835a0a Leszek Koltunski
      }
180
    }
181
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183
184 4b7c432e Leszek Koltunski
  public void onClick(View view)
185 59835a0a Leszek Koltunski
    {
186
    if( mObjectType==0 )  // cubes
187
      {
188 4b7c432e Leszek Koltunski
      Button tmp = (Button)view;
189
      int id = tmp.getId();
190 59835a0a Leszek Koltunski
      mShape[id] = !mShape[id];
191 4b7c432e Leszek Koltunski
      tmp.setBackgroundColor(mShape[id] ? COLOR_ON:COLOR_OFF);
192 59835a0a Leszek Koltunski
      }
193
    }
194
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196
197
  private void uncheckAll()
198
    {
199 4b7c432e Leszek Koltunski
    TableRow tr;
200
    Button butt;
201
202
    for (int row=0; row<mNumRows; row++)
203 59835a0a Leszek Koltunski
      {
204 4b7c432e Leszek Koltunski
      tr = (TableRow)mLay.getChildAt(row);
205 59835a0a Leszek Koltunski
206 4b7c432e Leszek Koltunski
      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 59835a0a Leszek Koltunski
      }
213
    }
214
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216
217
  public void Create(View v)
218
    {
219
    Intent mainInt = new Intent( getApplicationContext(), PredeformActivity2.class);
220
    Bundle b = new Bundle();
221
222
    b.putInt("type"  , mObjectType);
223
    b.putInt("cols"  , PredeformMeshList.getCols(mObjectType, mNumCols, mNumRows, mNumSlic) );
224
    b.putInt("rows"  , PredeformMeshList.getRows(mObjectType, mNumCols, mNumRows, mNumSlic) );
225
    b.putInt("slices", PredeformMeshList.getSlic(mObjectType, mNumCols, mNumRows, mNumSlic) );
226
    b.putInt("bitmap", mBitmapID);
227
228
    b.putString("string", PredeformMeshList.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 = PredeformMeshList.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 4b7c432e Leszek Koltunski
    if( !mGridInitialized ) setGrid();
287 59835a0a Leszek Koltunski
    }
288
  }