Project

General

Profile

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

examples / src / main / java / org / distorted / examples / fragment3d / Fragment3DActivity.java @ edafb4a7

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

    
45
import java.util.ArrayList;
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

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

    
56
  private boolean firstScreen;
57

    
58
  private int mNumCols = 3;
59
  private int mNumRows = 3;
60
  private NumberPicker mColsPicker, mRowsPicker;
61
  private LinearLayout mLay;
62
  private boolean[] mShape;
63
  private DistortedObject mObject;
64
  private int mObjectType;
65
  private int mBitmap;
66

    
67
  private ArrayList<Fragment3DEffect> mEffects = new ArrayList<>();
68
  private DistortedBitmap mCenter = null;
69

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71
    
72
  @Override
73
  protected void onCreate(Bundle savedState)
74
    {
75
    super.onCreate(savedState);
76

    
77
    setContentView(R.layout.objectpickerlayout);
78

    
79
    mColsPicker = (NumberPicker)findViewById(R.id.objectpicker_cols);
80
    mRowsPicker = (NumberPicker)findViewById(R.id.objectpicker_rows);
81

    
82
    mColsPicker.setMaxValue(10);
83
    mColsPicker.setMinValue( 0);
84
    mRowsPicker.setMaxValue(10);
85
    mRowsPicker.setMinValue( 0);
86

    
87
    mColsPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener()
88
      {
89
      @Override
90
      public void onValueChange(NumberPicker picker, int oldVal, int newVal)
91
        {
92
        mNumCols = mColsPicker.getValue();
93
        }
94
      });
95

    
96
    mRowsPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener()
97
      {
98
      @Override
99
      public void onValueChange(NumberPicker picker, int oldVal, int newVal)
100
        {
101
        mNumRows = mRowsPicker.getValue();
102
        }
103
      });
104

    
105
    firstScreen = true;
106

    
107
    mObjectType = 0;
108

    
109
    Spinner typeSpinner  = (Spinner)findViewById(R.id.objectpicker_spinnerType);
110
    typeSpinner.setOnItemSelectedListener(this);
111

    
112
    String[] objectType = new String[] {"DistortedCubes", "DistortedBitmap"};
113

    
114
    ArrayAdapter<String> adapterType = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, objectType);
115
    adapterType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
116
    typeSpinner.setAdapter(adapterType);
117

    
118
    Spinner bitmapSpinner  = (Spinner)findViewById(R.id.objectpicker_spinnerBitmap);
119
    bitmapSpinner.setOnItemSelectedListener(this);
120

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

    
134
    int width = mLay.getWidth();
135
    int height= mLay.getHeight();
136
    int w = mNumCols>0 ? (width / mNumCols) -10 : 0;
137
    int h = mNumRows>0 ? (height/ mNumRows) -10 : 0;
138
    int size= w<h ? w:h;
139
    int pad = size/20;
140

    
141
    mLay.removeAllViews();
142

    
143
    mShape = new boolean[mNumRows*mNumCols];
144

    
145
    TableRow.LayoutParams p = new android.widget.TableRow.LayoutParams();
146

    
147
    p.rightMargin  = pad;
148
    p.leftMargin   = pad;
149
    p.topMargin    = pad;
150
    p.bottomMargin = pad;
151
    p.height       = size;
152
    p.width        = size;
153

    
154
    for (int rows=0; rows<mNumRows; rows++)
155
      {
156
      TableRow tr = new TableRow(this);
157
      tr.setGravity(Gravity.CENTER);
158

    
159
      for(int cols=0; cols<mNumCols; cols++)
160
        {
161
        Button b = new Button(this);
162
        b.setOnClickListener(this);
163
        b.setId(rows*mNumCols+cols);
164
        b.setLayoutParams(p);
165
        b.setBackgroundColor(COLOR_ON);
166
        tr.addView(b, p);
167
        mShape[rows*mNumCols+cols] = true;
168
        }
169

    
170
      mLay.addView(tr);
171
      }
172
    }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175

    
176
  public DistortedObject getObject()
177
    {
178
    return mObject;
179
    }
180

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

    
183
  public void onClick(View view)
184
    {
185
    Button tmp = (Button)view;
186
    int id = tmp.getId();
187
    mShape[id] = !mShape[id];
188
    tmp.setBackgroundColor(mShape[id] ? COLOR_ON:COLOR_OFF);
189
    }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

    
193
  public int getBitmap()
194
    {
195
    return mBitmap;
196
    }
197

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

    
200
  public void Continue(View v)
201
    {
202
    if( mObjectType==1 )
203
      {
204
      firstScreen = false;
205
      mObject = new DistortedBitmap(100,100,mNumCols);
206
      setContentView(R.layout.fragment3dlayout);
207
      }
208
    else
209
      {
210
      View view = getLayoutInflater().inflate(R.layout.objectpicker2layout, null);
211

    
212
      setContentView(view);
213

    
214
      view.post(new Runnable() {
215
            @Override
216
            public void run() {
217
              setGrid();
218
            }
219
        });
220
      }
221
    }
222

    
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224

    
225
  public void Create(View v)
226
    {
227
    firstScreen = false;
228

    
229
    String str = "";
230

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

    
234
    mObject = new DistortedCubes(mNumCols, str, 10);
235
    setContentView(R.layout.fragment3dlayout);
236
    }
237

    
238

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

    
241
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
242
    {
243
    switch(parent.getId())
244
      {
245
      case R.id.objectpicker_spinnerType  : mObjectType = pos; break;
246
      case R.id.objectpicker_spinnerBitmap: switch(pos)
247
                                              {
248
                                              case 0: mBitmap = R.raw.face; break;
249
                                              case 1: mBitmap = R.raw.dog;  break;
250
                                              case 2: mBitmap = R.raw.cat;  break;
251
                                              case 3: mBitmap = R.raw.grid; break;
252
                                              }
253
                                            break;
254
      }
255
    }
256

    
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258

    
259
  public void onNothingSelected(AdapterView<?> parent)
260
    {
261
    }
262

    
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264
// 'second screen' methods
265

    
266
  private void removeAllViews()
267
    {
268
    LinearLayout layout = (LinearLayout)findViewById(R.id.fragment3dlayout);
269

    
270
    layout.removeAllViews();
271
    mEffects.clear();
272
    mCenter = null;
273
    }
274

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

    
277
  private void addView(EffectNames name)
278
    {
279
    Fragment3DEffect newInfo = new Fragment3DEffect(name);
280
    LinearLayout layout = (LinearLayout)findViewById(R.id.fragment3dlayout);
281
    newInfo.addView(layout);
282
    mEffects.add(newInfo);
283
    }
284

    
285
///////////////////////////////////////////////////////////////////////////////////////////////////
286

    
287
  public void Remove(View v)
288
    {
289
    removeAllViews();
290
    }
291

    
292
///////////////////////////////////////////////////////////////////////////////////////////////////
293

    
294
  public void newChroma(View v)
295
    {
296
    addView(EffectNames.CHROMA);
297
    }
298

    
299
///////////////////////////////////////////////////////////////////////////////////////////////////
300

    
301
  public void newAlpha(View v)
302
    {
303
    addView(EffectNames.ALPHA);
304
    }
305

    
306
///////////////////////////////////////////////////////////////////////////////////////////////////
307

    
308
  public void newBrightness(View v)
309
    {
310
    addView(EffectNames.BRIGHTNESS);
311
    }
312

    
313
///////////////////////////////////////////////////////////////////////////////////////////////////
314

    
315
  public void newSaturation(View v)
316
    {
317
    addView(EffectNames.SATURATION);
318
    }
319

    
320
///////////////////////////////////////////////////////////////////////////////////////////////////
321

    
322
  public DistortedBitmap getCenter()
323
    {
324
    return mCenter;
325
    }
326

    
327
///////////////////////////////////////////////////////////////////////////////////////////////////
328

    
329
  public void setCenter(DistortedBitmap center  )
330
    {
331
    mCenter = center;
332
    }
333

    
334
///////////////////////////////////////////////////////////////////////////////////////////////////
335
// Overrides
336

    
337
  @Override
338
  protected void onPause()
339
    {
340
    GLSurfaceView mView = (GLSurfaceView)findViewById(R.id.fragment3dSurfaceView);
341
    if( mView!=null ) mView.onPause();
342
    super.onPause();
343
    }
344

    
345
///////////////////////////////////////////////////////////////////////////////////////////////////
346
    
347
  @Override
348
  protected void onResume()
349
    {
350
    super.onResume();
351
    GLSurfaceView mView = (GLSurfaceView)findViewById(R.id.fragment3dSurfaceView);
352
    if( mView!=null ) mView.onResume();
353
    }
354

    
355
///////////////////////////////////////////////////////////////////////////////////////////////////
356
    
357
  @Override
358
  public void onWindowFocusChanged(boolean hasFocus)
359
    {
360
    super.onWindowFocusChanged(hasFocus);
361

    
362
    if( firstScreen )
363
      {
364
      mColsPicker.setValue(mNumCols);
365
      mRowsPicker.setValue(mNumRows);
366
      }
367
    }
368

    
369
///////////////////////////////////////////////////////////////////////////////////////////////////
370
    
371
  @Override
372
  protected void onDestroy()
373
    {
374
    Distorted.onDestroy();
375
    super.onDestroy();
376
    }
377
  }
(1-1/4)