Project

General

Profile

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

examples / src / main / java / org / distorted / examples / fragment3d / Fragment3DActivity.java @ 75c7def3

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.fragment3d;
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.SeekBar;
33
import android.widget.SeekBar.OnSeekBarChangeListener;
34
import android.widget.Spinner;
35
import android.widget.TableRow;
36
import android.widget.TextView;
37

    
38
import org.distorted.examples.R;
39
import org.distorted.library.Distorted;
40
import org.distorted.library.DistortedBitmap;
41
import org.distorted.library.DistortedCubes;
42
import org.distorted.library.DistortedObject;
43
import org.distorted.library.EffectNames;
44
import org.distorted.library.EffectTypes;
45
import org.distorted.library.type.Dynamic1D;
46
import org.distorted.library.type.Dynamic3D;
47
import org.distorted.library.type.Dynamic4D;
48

    
49
import java.util.ArrayList;
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

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

    
60
  private boolean firstScreen;
61

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

    
73
  private ArrayList<Fragment3DEffect> mEffects;
74

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

    
82
    mEffects = new ArrayList<>();
83

    
84
    setContentView(R.layout.objectpickerlayout);
85

    
86
    mColsPicker = (NumberPicker)findViewById(R.id.objectpicker_cols);
87
    mRowsPicker = (NumberPicker)findViewById(R.id.objectpicker_rows);
88

    
89
    mColsPicker.setMaxValue(10);
90
    mColsPicker.setMinValue( 0);
91
    mRowsPicker.setMaxValue(10);
92
    mRowsPicker.setMinValue( 0);
93

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

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

    
112
    firstScreen = true;
113

    
114
    mObjectType = 0;
115

    
116
    Spinner typeSpinner  = (Spinner)findViewById(R.id.objectpicker_spinnerType);
117
    typeSpinner.setOnItemSelectedListener(this);
118

    
119
    String[] objectType = new String[] {"DistortedCubes", "DistortedBitmap"};
120

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

    
125
    Spinner bitmapSpinner  = (Spinner)findViewById(R.id.objectpicker_spinnerBitmap);
126
    bitmapSpinner.setOnItemSelectedListener(this);
127

    
128
    String[] objectBitmap = new String[] {"Girl", "Dog", "Cat", "Grid"};
129

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

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136

    
137
  private void setGrid()
138
    {
139
    mLay = (LinearLayout)findViewById(R.id.objectpicker_buttongrid);
140

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

    
148
    mLay.removeAllViews();
149

    
150
    mShape = new boolean[mNumRows*mNumCols];
151

    
152
    TableRow.LayoutParams p = new android.widget.TableRow.LayoutParams();
153

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

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

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

    
177
      mLay.addView(tr);
178
      }
179
    }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

    
183
  public DistortedObject getObject()
184
    {
185
    return mObject;
186
    }
187

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189

    
190
  public int getBitmap()
191
    {
192
    return mBitmap;
193
    }
194

    
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196

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

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

    
207
  public void Continue(View v)
208
    {
209
    firstScreen = false;
210

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

    
220
      setContentView(view);
221

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

    
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232

    
233
  public void Create(View v)
234
    {
235
    firstScreen = false;
236

    
237
    String str = "";
238

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

    
242
    mObject = new DistortedCubes(mNumCols, str, 10);
243

    
244
    setFragmentView();
245
    }
246

    
247

    
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249

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

    
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267

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

    
272
///////////////////////////////////////////////////////////////////////////////////////////////////
273
// 'second screen' methods
274

    
275
  private void setFragmentView()
276
    {
277
    final View view = getLayoutInflater().inflate(R.layout.fragment3dlayout, null);
278

    
279
    setContentView(view);
280
    }
281

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283

    
284
  public void newChroma(View v)
285
    {
286
    Fragment3DEffect chroma = new Fragment3DEffect(EffectNames.CHROMA);
287
    mEffects.add(chroma);
288

    
289
    LinearLayout layout = (LinearLayout)findViewById(R.id.fragment3dlayout);
290
    View view = chroma.createView(this);
291
    layout.addView(view);
292
    View region = chroma.createRegion(this);
293
    layout.addView(region);
294

    
295
    Dynamic1D dyn1 = chroma.getDyn1();
296
    Dynamic3D dyn3 = chroma.getDyn3();
297
    Dynamic4D regi = chroma.getRegion();
298

    
299
    mObject.chroma(dyn1, dyn3, regi, false);
300
    }
301

    
302
///////////////////////////////////////////////////////////////////////////////////////////////////
303

    
304
  public void newAlpha(View v)
305
    {
306
    Fragment3DEffect alpha = new Fragment3DEffect(EffectNames.ALPHA);
307
    mEffects.add(alpha);
308

    
309
    LinearLayout layout = (LinearLayout)findViewById(R.id.fragment3dlayout);
310
    View view = alpha.createView(this);
311
    layout.addView(view);
312
    View region = alpha.createRegion(this);
313
    layout.addView(region);
314

    
315
    Dynamic1D dyn1 = alpha.getDyn1();
316
    Dynamic4D regi = alpha.getRegion();
317

    
318
    mObject.alpha(dyn1, regi, false);
319
    }
320

    
321
///////////////////////////////////////////////////////////////////////////////////////////////////
322

    
323
  public void newBrightness(View v)
324
    {
325
    Fragment3DEffect brightness = new Fragment3DEffect(EffectNames.BRIGHTNESS);
326
    mEffects.add(brightness);
327

    
328
    LinearLayout layout = (LinearLayout)findViewById(R.id.fragment3dlayout);
329
    View view = brightness.createView(this);
330
    layout.addView(view);
331
    View region = brightness.createRegion(this);
332
    layout.addView(region);
333

    
334
    Dynamic1D dyn1 = brightness.getDyn1();
335
    Dynamic4D regi = brightness.getRegion();
336

    
337
    mObject.brightness(dyn1, regi, false);
338
    }
339

    
340
///////////////////////////////////////////////////////////////////////////////////////////////////
341

    
342
  public void newSaturation(View v)
343
    {
344
    Fragment3DEffect saturation = new Fragment3DEffect(EffectNames.SATURATION);
345
    mEffects.add(saturation);
346

    
347
    LinearLayout layout = (LinearLayout)findViewById(R.id.fragment3dlayout);
348
    View view = saturation.createView(this);
349
    layout.addView(view);
350
    View region = saturation.createRegion(this);
351
    layout.addView(region);
352

    
353
    Dynamic1D dyn1 = saturation.getDyn1();
354
    Dynamic4D regi = saturation.getRegion();
355

    
356
    mObject.saturation(dyn1, regi, false);
357
    }
358

    
359
///////////////////////////////////////////////////////////////////////////////////////////////////
360

    
361
  public void removeAll(View v)
362
    {
363
    mEffects.clear();
364
    LinearLayout layout = (LinearLayout)findViewById(R.id.fragment3dlayout);
365
    layout.removeAllViews();
366
    mObject.abortEffects(EffectTypes.FRAGMENT);
367
    }
368

    
369
///////////////////////////////////////////////////////////////////////////////////////////////////
370
// Overrides
371

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

    
380
///////////////////////////////////////////////////////////////////////////////////////////////////
381
    
382
  @Override
383
  protected void onResume()
384
    {
385
    super.onResume();
386
    GLSurfaceView mView = (GLSurfaceView)findViewById(R.id.fragment3dSurfaceView);
387
    if( mView!=null ) mView.onResume();
388
    }
389

    
390
///////////////////////////////////////////////////////////////////////////////////////////////////
391
    
392
  @Override
393
  public void onWindowFocusChanged(boolean hasFocus)
394
    {
395
    super.onWindowFocusChanged(hasFocus);
396

    
397
    if( firstScreen )
398
      {
399
      mColsPicker.setValue(mNumCols);
400
      mRowsPicker.setValue(mNumRows);
401
      }
402
    }
403

    
404
///////////////////////////////////////////////////////////////////////////////////////////////////
405
    
406
  @Override
407
  protected void onDestroy()
408
    {
409
    Distorted.onDestroy();
410
    super.onDestroy();
411
    }
412

    
413
  }
(1-1/4)