Project

General

Profile

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

examples / src / main / java / org / distorted / examples / predeform / PredeformActivity.java @ 0dc8ffef

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

    
22
import android.app.Activity;
23
import android.content.Intent;
24
import android.graphics.Bitmap;
25
import android.graphics.BitmapFactory;
26
import android.graphics.Canvas;
27
import android.graphics.Paint;
28
import android.os.Bundle;
29
import android.view.View;
30
import android.widget.AdapterView;
31
import android.widget.ArrayAdapter;
32
import android.widget.NumberPicker;
33
import android.widget.Spinner;
34

    
35
import org.distorted.examples.R;
36
import org.distorted.library.mesh.MeshBase;
37

    
38
import java.io.IOException;
39
import java.io.InputStream;
40

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

    
43
public class PredeformActivity extends Activity implements AdapterView.OnItemSelectedListener
44
  {
45
  private int mNumCols;
46
  private int mNumRows;
47
  private int mNumSlic;
48
  private int mObjectType;
49
  private int mBitmapID;
50

    
51
  private NumberPicker mColsPicker, mRowsPicker, mSlicPicker;
52
  private boolean[] mShape;
53
  private MeshBase mMesh;
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

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

    
62
    mNumCols = 1;
63
    mNumRows = 1;
64
    mNumSlic = 1;
65
    mBitmapID=-1;
66
    resetShape();
67

    
68
    setContentView(R.layout.object_picker_layout);
69

    
70
    mColsPicker = findViewById(R.id.objectpicker_cols);
71
    mRowsPicker = findViewById(R.id.objectpicker_rows);
72
    mSlicPicker = findViewById(R.id.objectpicker_slices);
73

    
74
    mColsPicker.setMaxValue(40);
75
    mColsPicker.setMinValue( 1);
76
    mRowsPicker.setMaxValue(40);
77
    mRowsPicker.setMinValue( 1);
78
    mSlicPicker.setMaxValue(40);
79
    mSlicPicker.setMinValue( 0);
80

    
81
    mColsPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener()
82
         {
83
         @Override
84
         public void onValueChange(NumberPicker picker, int oldVal, int newVal)
85
           {
86
           mNumCols = newVal;
87
           resetShape();
88
           }
89
         });
90

    
91
    mRowsPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener()
92
         {
93
         @Override
94
         public void onValueChange(NumberPicker picker, int oldVal, int newVal)
95
           {
96
           mNumRows = newVal;
97
           resetShape();
98
           }
99
         });
100

    
101
    mSlicPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener()
102
         {
103
         @Override
104
         public void onValueChange(NumberPicker picker, int oldVal, int newVal)
105
           {
106
           mNumSlic = newVal;
107
           resetShape();
108
           }
109
         });
110

    
111
    mObjectType = 0;
112

    
113
    Spinner typeSpinner  = findViewById(R.id.objectpicker_spinnerType);
114
    typeSpinner.setOnItemSelectedListener(this);
115

    
116
    String[] objectType = new String[PredeformMeshList.LENGTH];
117

    
118
    for(int mesh = 0; mesh< PredeformMeshList.LENGTH; mesh++)
119
      {
120
      objectType[mesh] = "Mesh: "+ PredeformMeshList.getName(mesh);
121
      }
122

    
123
    ArrayAdapter<String> adapterType = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, objectType);
124
    adapterType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
125
    typeSpinner.setAdapter(adapterType);
126

    
127
    Spinner bitmapSpinner  = findViewById(R.id.objectpicker_spinnerBitmap);
128
    bitmapSpinner.setOnItemSelectedListener(this);
129

    
130
    String[] objectBitmap = new String[] { "Texture: Grid", "Texture: Girl", "Texture: Dog", "Texture: Cat",
131
                                           "Texture: Squares", "Texture: Bean", "Texture: Lisa", "Texture: World" };
132

    
133
    ArrayAdapter<String> adapterBitmap = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, objectBitmap);
134
    adapterBitmap.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
135
    bitmapSpinner.setAdapter(adapterBitmap);
136
    }
137

    
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

    
141
  private void resetShape()
142
    {
143
    mShape = new boolean[mNumRows*mNumCols];
144

    
145
    for (int rows=0; rows<mNumRows; rows++)
146
      {
147
      for(int cols=0; cols<mNumCols; cols++)
148
        {
149
        mShape[rows*mNumCols+cols] = true;
150
        }
151
      }
152

    
153
    recreateObject();
154
    }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

    
158
  MeshBase getMesh()
159
    {
160
    return mMesh;
161
    }
162

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164

    
165
  Bitmap getBitmap()
166
    {
167
    return createBitmap(mBitmapID);
168
    }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

    
172
  private void recreateObject()
173
    {
174
    String str = PredeformMeshList.getString(mObjectType, mNumCols, mNumRows, mShape);
175
    mMesh      = PredeformMeshList.createMesh(mObjectType, mNumCols, mNumRows, mNumSlic, mBitmapID, str);
176
    }
177

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179

    
180
  private Bitmap createBitmap(int bitmapID)
181
    {
182
    int size = mNumCols > mNumRows ? (Math.max(mNumCols, mNumSlic)) : (Math.max(mNumRows, mNumSlic));
183

    
184
    if( bitmapID!=-1)
185
      {
186
      try (InputStream is = getResources().openRawResource(bitmapID))
187
        {
188
        return BitmapFactory.decodeStream(is);
189
        }
190
      catch( IOException ex ) { android.util.Log.e("act", "failed to open resource "+bitmapID); }
191
      }
192
    else
193
      {
194
      final int T = 64;
195
      final int S = T*size;
196

    
197
      Paint paint = new Paint();
198
      Bitmap bmp = Bitmap.createBitmap(S,S, Bitmap.Config.ARGB_8888);
199
      Canvas canvas = new Canvas(bmp);
200

    
201
      paint.setAntiAlias(true);
202
      paint.setTextAlign(Paint.Align.CENTER);
203
      paint.setColor(0xff008800);
204
      paint.setStyle(Paint.Style.FILL);
205
      canvas.drawRect(0, 0, S, S, paint);
206
      paint.setColor(0xffffffff);
207

    
208
      for(int i=0; i<=size ; i++ )
209
        {
210
        canvas.drawRect( T*i-1, 0, T*i+1, S, paint);
211
        canvas.drawRect( 0, T*i-1, S, T*i+1, paint);
212
        }
213

    
214
      return bmp;
215
      }
216

    
217
    return null;
218
    }
219

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221

    
222
  void click(int row, int col)
223
    {
224
    if( mObjectType==0 )  // cubes
225
      {
226
      int id = row*mNumCols+col;
227
      mShape[id] = !mShape[id];
228
      recreateObject();
229
      }
230
    }
231

    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233

    
234
  private void uncheckAll()
235
    {
236
    if( mObjectType==0 )  // cubes
237
      {
238
      for (int row=0; row<mNumRows; row++)
239
        for(int col=0; col<mNumCols; col++)
240
          mShape[row*mNumCols+col] = true;
241

    
242
      recreateObject();
243
      }
244
    }
245

    
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247

    
248
  public void Create(View v)
249
    {
250
    Intent mainInt = new Intent( getApplicationContext(), PredeformActivity2.class);
251
    Bundle b = new Bundle();
252

    
253
    b.putInt("type"  , mObjectType);
254
    b.putInt("cols"  , PredeformMeshList.getCols(mObjectType, mNumCols, mNumRows, mNumSlic) );
255
    b.putInt("rows"  , PredeformMeshList.getRows(mObjectType, mNumCols, mNumRows, mNumSlic) );
256
    b.putInt("slices", PredeformMeshList.getSlic(mObjectType, mNumCols, mNumRows, mNumSlic) );
257
    b.putInt("bitmap", mBitmapID);
258

    
259
    b.putString("string", PredeformMeshList.getString(mObjectType, mNumCols, mNumRows, mShape));
260

    
261
    mainInt.putExtras(b);
262
    startActivity(mainInt);
263
    }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266

    
267
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
268
    {
269
    switch(parent.getId())
270
      {
271
      case R.id.objectpicker_spinnerType  : if( mObjectType!=pos )
272
                                              {
273
                                              mObjectType = pos;
274
                                              uncheckAll();
275

    
276
                                              int dim = PredeformMeshList.getDimension(mObjectType);
277

    
278
                                              mRowsPicker.setEnabled(dim>=1);
279
                                              mColsPicker.setEnabled(dim>=2);
280
                                              mSlicPicker.setEnabled(dim>=3);
281

    
282
                                              recreateObject();
283
                                              }
284
                                            break;
285
      case R.id.objectpicker_spinnerBitmap: switch(pos)
286
                                              {
287
                                              case 0: mBitmapID = -1            ; break;
288
                                              case 1: mBitmapID = R.raw.face    ; break;
289
                                              case 2: mBitmapID = R.raw.dog     ; break;
290
                                              case 3: mBitmapID = R.raw.cat     ; break;
291
                                              case 4: mBitmapID = R.raw.grid    ; break;
292
                                              case 5: mBitmapID = R.raw.bean    ; break;
293
                                              case 6: mBitmapID = R.raw.monalisa; break;
294
                                              case 7: mBitmapID = R.raw.world   ; break;
295
                                              }
296

    
297
                                            PredeformSurfaceView v = findViewById(R.id.predeformSurfaceView);
298
                                            v.getRenderer().setTexture( createBitmap(mBitmapID) );
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
    resetShape();
323
    }
324
  }
(1-1/7)