Project

General

Profile

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

examples / src / main / java / org / distorted / examples / vertex3d / Vertex3DActivity.java @ 56cbe1cf

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

    
22
import android.app.Activity;
23
import android.opengl.GLSurfaceView;
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
import org.distorted.library.Distorted;
37
import org.distorted.library.DistortedBitmap;
38
import org.distorted.library.DistortedCubes;
39
import org.distorted.library.DistortedObject;
40
import org.distorted.library.EffectNames;
41
import org.distorted.library.EffectTypes;
42
import org.distorted.library.type.Dynamic1D;
43
import org.distorted.library.type.Dynamic2D;
44
import org.distorted.library.type.Dynamic3D;
45
import org.distorted.library.type.Dynamic4D;
46

    
47
import java.util.ArrayList;
48

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

    
51
public class Vertex3DActivity extends Activity
52
                              implements View.OnClickListener,
53
                                         AdapterView.OnItemSelectedListener
54
  {
55
  private static final int COLOR_OFF = 0xffffe81f;
56
  private static final int COLOR_ON  = 0xff0000ff;
57

    
58
  private boolean firstScreen;
59

    
60
  // fields needed for the first 'pick-a-shape' screen
61
  //
62
  private int mNumCols = 10;
63
  private int mNumRows = 10;
64
  private NumberPicker mColsPicker, mRowsPicker;
65
  private boolean[] mShape;
66
  private DistortedObject mObject;
67
  private int mObjectType;
68
  private int mBitmap;
69

    
70
  private ArrayList<Vertex3DEffect> mEffects;
71
  private int mEffectAdd;
72

    
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74

    
75
  @Override
76
  protected void onCreate(Bundle savedState)
77
    {
78
    super.onCreate(savedState);
79

    
80
    mEffects = new ArrayList<>();
81

    
82
    setContentView(R.layout.objectpickerlayout);
83

    
84
    mColsPicker = (NumberPicker)findViewById(R.id.objectpicker_cols);
85
    mRowsPicker = (NumberPicker)findViewById(R.id.objectpicker_rows);
86

    
87
    mColsPicker.setMaxValue(15);
88
    mColsPicker.setMinValue( 0);
89
    mRowsPicker.setMaxValue(15);
90
    mRowsPicker.setMinValue( 0);
91

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

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

    
110
    firstScreen = true;
111

    
112
    mObjectType = 0;
113

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

    
117
    String[] objectType = new String[] {"DistortedCubes", "DistortedBitmap"};
118

    
119
    ArrayAdapter<String> adapterType = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, objectType);
120
    adapterType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
121
    typeSpinner.setAdapter(adapterType);
122

    
123
    Spinner bitmapSpinner  = (Spinner)findViewById(R.id.objectpicker_spinnerBitmap);
124
    bitmapSpinner.setOnItemSelectedListener(this);
125

    
126
    String[] objectBitmap = new String[] {"Girl", "Dog", "Cat", "Grid"};
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
    LinearLayout lay = (LinearLayout)findViewById(R.id.objectpicker_buttongrid);
138

    
139
    int width = lay.getWidth();
140
    int height= lay.getHeight();
141
    int w = mNumCols>0 ? (width / mNumCols) -10 : 0;
142
    int h = mNumRows>0 ? (height/ mNumRows) -10 : 0;
143
    int size= w<h ? w:h;
144
    int pad = size/20;
145

    
146
    lay.removeAllViews();
147

    
148
    mShape = new boolean[mNumRows*mNumCols];
149

    
150
    TableRow.LayoutParams p = new TableRow.LayoutParams();
151

    
152
    p.rightMargin  = pad;
153
    p.leftMargin   = pad;
154
    p.topMargin    = pad;
155
    p.bottomMargin = pad;
156
    p.height       = size;
157
    p.width        = size;
158

    
159
    for (int rows=0; rows<mNumRows; rows++)
160
      {
161
      TableRow tr = new TableRow(this);
162
      tr.setGravity(Gravity.CENTER);
163

    
164
      for(int cols=0; cols<mNumCols; cols++)
165
        {
166
        Button b = new Button(this);
167
        b.setOnClickListener(this);
168
        b.setId(rows*mNumCols+cols);
169
        b.setLayoutParams(p);
170
        b.setBackgroundColor(COLOR_ON);
171
        tr.addView(b, p);
172
        mShape[rows*mNumCols+cols] = true;
173
        }
174

    
175
      lay.addView(tr);
176
      }
177
    }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

    
181
  public DistortedObject getObject()
182
    {
183
    return mObject;
184
    }
185

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187

    
188
  public int getBitmap()
189
    {
190
    return mBitmap;
191
    }
192

    
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194

    
195
  public void onClick(View view)
196
    {
197
    Button tmp = (Button)view;
198
    int id = tmp.getId();
199
    mShape[id] = !mShape[id];
200
    tmp.setBackgroundColor(mShape[id] ? COLOR_ON:COLOR_OFF);
201
    }
202

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204

    
205
  public void Continue(View v)
206
    {
207
    firstScreen = false;
208

    
209
    if( mObjectType==1 )
210
      {
211
      mObject = new DistortedBitmap(100,100,mNumCols);
212
      setVertexView();
213
      }
214
    else
215
      {
216
      View view = getLayoutInflater().inflate(R.layout.objectpicker2layout, null);
217

    
218
      setContentView(view);
219

    
220
      view.post(new Runnable() {
221
            @Override
222
            public void run() {
223
              setGrid();
224
            }
225
        });
226
      }
227
    }
228

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

    
231
  public void Create(View v)
232
    {
233
    firstScreen = false;
234

    
235
    String str = "";
236

    
237
    for(int i=0; i<mNumRows*mNumCols; i++)
238
      str += mShape[i] ? "1" : "0";
239

    
240
    mObject = new DistortedCubes(mNumCols, str, 10);
241

    
242
    setVertexView();
243
    }
244

    
245

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

    
248
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
249
    {
250
    switch(parent.getId())
251
      {
252
      case R.id.objectpicker_spinnerType  : mObjectType = pos;
253
                                            break;
254
      case R.id.objectpicker_spinnerBitmap: switch(pos)
255
                                              {
256
                                              case 0: mBitmap = R.raw.face; break;
257
                                              case 1: mBitmap = R.raw.dog;  break;
258
                                              case 2: mBitmap = R.raw.cat;  break;
259
                                              case 3: mBitmap = R.raw.grid; break;
260
                                              }
261
                                            break;
262
      case R.id.vertex3dspinner           : mEffectAdd = pos;
263
                                            break;
264
      }
265
    }
266

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

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

    
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274

    
275
  public int getWidth()
276
    {
277
    return mObject==null ? 0: mObject.getWidth();
278
    }
279

    
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281

    
282
  public int getHeight()
283
    {
284
    return mObject==null ? 0: mObject.getHeight();
285
    }
286

    
287
///////////////////////////////////////////////////////////////////////////////////////////////////
288
// 'second screen' methods
289

    
290
  private void setVertexView()
291
    {
292
    final View view = getLayoutInflater().inflate(R.layout.vertex3dlayout, null);
293

    
294
    setContentView(view);
295

    
296
    String[] effects = new String[] { "DISTORT",
297
                                      "DEFORM",
298
                                      "SINK",
299
                                      "SWIRL" };
300

    
301
    Spinner effectSpinner = (Spinner)findViewById(R.id.vertex3dspinner );
302
    effectSpinner.setOnItemSelectedListener(this);
303

    
304
    ArrayAdapter<String> adapterEffect = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, effects);
305
    adapterEffect.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
306
    effectSpinner.setAdapter(adapterEffect);
307

    
308
    mEffectAdd = 0;
309
    }
310

    
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312

    
313
  public void newEffect(View v)
314
    {
315
    EffectNames name;
316

    
317
    switch(mEffectAdd)
318
      {
319
      case 0 : name = EffectNames.DISTORT; break;
320
      case 1 : name = EffectNames.DEFORM ; break;
321
      case 2 : name = EffectNames.SINK   ; break;
322
      case 3 : name = EffectNames.SWIRL  ; break;
323
      default: return;
324
      }
325

    
326
    Vertex3DEffect eff = new Vertex3DEffect(name, this);
327
    mEffects.add(eff);
328

    
329
    LinearLayout layout = (LinearLayout)findViewById(R.id.vertex3dlayout);
330
    View view = eff.createView();
331
    layout.addView(view);
332
    View center = eff.createCenter();
333
    layout.addView(center);
334
    View region = eff.createRegion();
335
    layout.addView(region);
336

    
337
    Dynamic1D dyn1 = eff.getDyn1();
338
    Dynamic2D cent = eff.getCenter();
339
    Dynamic3D dyn3 = eff.getDyn3();
340
    Dynamic4D regi = eff.getRegion();
341

    
342
    switch(mEffectAdd)
343
      {
344
      case 0: mObject.distort(dyn3, cent, regi); break;
345
      case 1: mObject.deform (dyn3, cent      ); break;
346
      case 2: mObject.sink   (dyn1, cent, regi); break;
347
      case 3: mObject.swirl  (dyn1, cent, regi); break;
348
      }
349
    }
350

    
351
///////////////////////////////////////////////////////////////////////////////////////////////////
352

    
353
  public void removeAll(View v)
354
    {
355
    mEffects.clear();
356
    LinearLayout layout = (LinearLayout)findViewById(R.id.vertex3dlayout);
357
    layout.removeAllViews();
358
    mObject.abortEffects(EffectTypes.VERTEX);
359
    }
360

    
361
///////////////////////////////////////////////////////////////////////////////////////////////////
362
// Overrides
363

    
364
  @Override
365
  protected void onPause()
366
    {
367
    GLSurfaceView mView = (GLSurfaceView)findViewById(R.id.vertex3dSurfaceView);
368
    if( mView!=null ) mView.onPause();
369
    super.onPause();
370
    }
371

    
372
///////////////////////////////////////////////////////////////////////////////////////////////////
373
    
374
  @Override
375
  protected void onResume()
376
    {
377
    super.onResume();
378
    GLSurfaceView mView = (GLSurfaceView)findViewById(R.id.vertex3dSurfaceView);
379
    if( mView!=null ) mView.onResume();
380
    }
381

    
382
///////////////////////////////////////////////////////////////////////////////////////////////////
383
    
384
  @Override
385
  public void onWindowFocusChanged(boolean hasFocus)
386
    {
387
    super.onWindowFocusChanged(hasFocus);
388

    
389
    if( firstScreen )
390
      {
391
      mColsPicker.setValue(mNumCols);
392
      mRowsPicker.setValue(mNumRows);
393
      }
394
    }
395

    
396
///////////////////////////////////////////////////////////////////////////////////////////////////
397
    
398
  @Override
399
  protected void onDestroy()
400
    {
401
    Distorted.onDestroy();
402
    super.onDestroy();
403
    }
404

    
405
  }
(1-1/4)