Project

General

Profile

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

examples / src / main / java / org / distorted / examples / inflate / InflateActivity.java @ 77a500b3

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.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
  private static final int COLOR_INAC= 0xff00ff00;
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"};
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==1 ? COLOR_INAC : COLOR_ON);
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!=1 )
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==1 ? COLOR_INAC : COLOR_ON);
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(), InflateActivity2.class);
215
    Bundle b = new Bundle();
216

    
217
    b.putInt("type", mObjectType);
218
    b.putInt("cols", mNumCols);
219
    b.putInt("rows", mNumRows);
220
    b.putInt("slices", mNumSlic);
221
    b.putInt("bitmap", mBitmapID);
222

    
223
    if( mObjectType==1 )
224
      {
225
      b.putString("string", "");
226
      }
227
    else
228
      {
229
      String str = "";
230

    
231
      for(int i=0; i<mNumRows*mNumCols; i++)
232
        str += mShape[i] ? "1" : "0";
233

    
234
      b.putString("string", str);
235
      }
236

    
237
    mainInt.putExtras(b);
238
    startActivity(mainInt);
239
    }
240

    
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242

    
243
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
244
    {
245
    switch(parent.getId())
246
      {
247
      case R.id.objectpicker_spinnerType  : if( mObjectType!=pos )
248
                                              {
249
                                              mObjectType = pos;
250
                                              uncheckAll();
251
                                              }
252
                                            break;
253
      case R.id.objectpicker_spinnerBitmap: switch(pos)
254
                                              {
255
                                              case 0: mBitmapID = -1        ; break;
256
                                              case 1: mBitmapID = R.raw.face; break;
257
                                              case 2: mBitmapID = R.raw.dog ; break;
258
                                              case 3: mBitmapID = R.raw.cat ; break;
259
                                              case 4: mBitmapID = R.raw.grid; break;
260
                                              case 5: mBitmapID = R.raw.bean; break;
261
                                              case 6: mBitmapID = R.raw.monalisa; 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/4)