Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effects3d / Effects3DActivity.java @ bddd4b2d

1 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 76a81b6a Leszek Koltunski
package org.distorted.examples.effects3d;
21 08f92d82 Leszek Koltunski
22
import android.app.Activity;
23 9167cfd4 Leszek Koltunski
import android.graphics.Bitmap;
24
import android.graphics.BitmapFactory;
25
import android.graphics.Canvas;
26
import android.graphics.Paint;
27 08f92d82 Leszek Koltunski
import android.opengl.GLSurfaceView;
28
import android.os.Bundle;
29 d40cfeb2 Leszek Koltunski
import android.view.Gravity;
30 08f92d82 Leszek Koltunski
import android.view.View;
31 50ac40a6 Leszek Koltunski
import android.widget.AdapterView;
32
import android.widget.ArrayAdapter;
33 d40cfeb2 Leszek Koltunski
import android.widget.Button;
34 0ab55f0c Leszek Koltunski
import android.widget.CheckBox;
35 08f92d82 Leszek Koltunski
import android.widget.LinearLayout;
36 d40cfeb2 Leszek Koltunski
import android.widget.NumberPicker;
37 50ac40a6 Leszek Koltunski
import android.widget.Spinner;
38 d40cfeb2 Leszek Koltunski
import android.widget.TableRow;
39 08f92d82 Leszek Koltunski
40
import org.distorted.examples.R;
41 a418b421 Leszek Koltunski
import org.distorted.library.effect.EffectName;
42
import org.distorted.library.effect.EffectType;
43 01782e85 Leszek Koltunski
import org.distorted.library.main.Distorted;
44
import org.distorted.library.main.MeshCubes;
45
import org.distorted.library.main.MeshFlat;
46
import org.distorted.library.main.MeshObject;
47
import org.distorted.library.main.DistortedTexture;
48
import org.distorted.library.main.DistortedEffects;
49 56cbe1cf Leszek Koltunski
50 9167cfd4 Leszek Koltunski
import java.io.IOException;
51
import java.io.InputStream;
52 56cbe1cf Leszek Koltunski
import java.util.ArrayList;
53 08f92d82 Leszek Koltunski
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55
56 76a81b6a Leszek Koltunski
public class Effects3DActivity extends Activity
57 bfcf419a Leszek Koltunski
                               implements View.OnClickListener,
58
                                          AdapterView.OnItemSelectedListener
59 d40cfeb2 Leszek Koltunski
  {
60 50ac40a6 Leszek Koltunski
  private static final int COLOR_OFF = 0xffffe81f;
61
  private static final int COLOR_ON  = 0xff0000ff;
62 bfcf419a Leszek Koltunski
  private static final int COLOR_INAC= 0xff00ff00;
63 50ac40a6 Leszek Koltunski
64 56cbe1cf Leszek Koltunski
  private int mNumCols = 10;
65
  private int mNumRows = 10;
66 b3b2c6cf leszek
  private int mNumSlic =  1;
67 2e755580 Leszek Koltunski
  private boolean mGridInitialized;
68 b3b2c6cf leszek
  private NumberPicker mColsPicker, mRowsPicker, mSlicPicker;
69 50ac40a6 Leszek Koltunski
  private boolean[] mShape;
70 f6d884d5 Leszek Koltunski
  private DistortedTexture mTexture;
71 d04a4886 Leszek Koltunski
  private DistortedEffects mEffects;
72 b01acdaf Leszek Koltunski
  private MeshObject mMesh;
73 bcc8e016 Leszek Koltunski
  private int mObjectType;
74 cdc515b9 Leszek Koltunski
  private int mBitmapID;
75
  private Bitmap mBitmap;
76 bfcf419a Leszek Koltunski
  private LinearLayout mLay;
77 d40cfeb2 Leszek Koltunski
78 d04a4886 Leszek Koltunski
  private ArrayList<Effects3DEffect> mList;
79 56cbe1cf Leszek Koltunski
  private int mEffectAdd;
80 334c13fa Leszek Koltunski
  private float mCenterX, mCenterY, mCenterZ;
81 6f779cd4 Leszek Koltunski
  private float mRegionX, mRegionY, mRegionR;
82 50ac40a6 Leszek Koltunski
83 a418b421 Leszek Koltunski
  private EffectName[] mEffectNames;
84 24991bc2 Leszek Koltunski
85 47833473 Leszek Koltunski
  private static boolean mSupportsRegion;
86 4d5b37fe Leszek Koltunski
  private static boolean mSupportsCenter;
87 24991bc2 Leszek Koltunski
88 0ab55f0c Leszek Koltunski
  private static boolean mShowCenter = true;
89
  private static boolean mShowRegion = true;
90
  private static boolean mShowNormal = true;
91 bddd4b2d Leszek Koltunski
  private static boolean mUseOIT     = false;
92 0ab55f0c Leszek Koltunski
93 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
94 56cbe1cf Leszek Koltunski
95 d40cfeb2 Leszek Koltunski
  @Override
96
  protected void onCreate(Bundle savedState)
97
    {
98
    super.onCreate(savedState);
99
100 d04a4886 Leszek Koltunski
    mList = new ArrayList<>();
101 56cbe1cf Leszek Koltunski
102 8fd9f5fa Leszek Koltunski
    createEffectNames();
103
104 50ac40a6 Leszek Koltunski
    setContentView(R.layout.objectpickerlayout);
105 d40cfeb2 Leszek Koltunski
106 bddd4b2d Leszek Koltunski
    mLay = findViewById(R.id.objectpicker_buttongrid);
107 bfcf419a Leszek Koltunski
108 bddd4b2d Leszek Koltunski
    mColsPicker = findViewById(R.id.objectpicker_cols);
109
    mRowsPicker = findViewById(R.id.objectpicker_rows);
110
    mSlicPicker = findViewById(R.id.objectpicker_slices);
111 d40cfeb2 Leszek Koltunski
112 9758b1ec Leszek Koltunski
    mColsPicker.setMaxValue(40);
113 d40cfeb2 Leszek Koltunski
    mColsPicker.setMinValue( 0);
114 9758b1ec Leszek Koltunski
    mRowsPicker.setMaxValue(40);
115 d40cfeb2 Leszek Koltunski
    mRowsPicker.setMinValue( 0);
116 b3b2c6cf leszek
    mSlicPicker.setMaxValue(40);
117
    mSlicPicker.setMinValue( 0);
118 d40cfeb2 Leszek Koltunski
119
    mColsPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener()
120 bfcf419a Leszek Koltunski
         {
121
         @Override
122
         public void onValueChange(NumberPicker picker, int oldVal, int newVal)
123
           {
124
           setGrid();
125
           }
126
         });
127 d40cfeb2 Leszek Koltunski
128
    mRowsPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener()
129 bfcf419a Leszek Koltunski
         {
130
         @Override
131
         public void onValueChange(NumberPicker picker, int oldVal, int newVal)
132
           {
133
           setGrid();
134
           }
135
         });
136 d40cfeb2 Leszek Koltunski
137 b3b2c6cf leszek
    mSlicPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener()
138
         {
139
         @Override
140
         public void onValueChange(NumberPicker picker, int oldVal, int newVal)
141
           {
142
           mNumSlic = mSlicPicker.getValue();
143
           }
144
         });
145
146 50ac40a6 Leszek Koltunski
    mObjectType = 0;
147 2e755580 Leszek Koltunski
    mGridInitialized = false;
148 d40cfeb2 Leszek Koltunski
149 bddd4b2d Leszek Koltunski
    Spinner typeSpinner  = findViewById(R.id.objectpicker_spinnerType);
150 50ac40a6 Leszek Koltunski
    typeSpinner.setOnItemSelectedListener(this);
151 d40cfeb2 Leszek Koltunski
152 bfcf419a Leszek Koltunski
    String[] objectType = new String[] {"Mesh: Cubes", "Mesh: Flat"};
153 d40cfeb2 Leszek Koltunski
154 bcc8e016 Leszek Koltunski
    ArrayAdapter<String> adapterType = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, objectType);
155
    adapterType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
156
    typeSpinner.setAdapter(adapterType);
157
158 bddd4b2d Leszek Koltunski
    Spinner bitmapSpinner  = findViewById(R.id.objectpicker_spinnerBitmap);
159 bcc8e016 Leszek Koltunski
    bitmapSpinner.setOnItemSelectedListener(this);
160
161 bfcf419a Leszek Koltunski
    String[] objectBitmap = new String[] { "Texture: Grid", "Texture: Girl", "Texture: Dog", "Texture: Cat",
162
                                           "Texture: Squares", "Texture: Bean", "Texture: Lisa" };
163 bcc8e016 Leszek Koltunski
164
    ArrayAdapter<String> adapterBitmap = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, objectBitmap);
165
    adapterBitmap.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
166
    bitmapSpinner.setAdapter(adapterBitmap);
167 d40cfeb2 Leszek Koltunski
    }
168
169 8fd9f5fa Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
170
171
  private void createEffectNames()
172
    {
173 a418b421 Leszek Koltunski
    EffectType type1 = EffectType.FRAGMENT;
174
    EffectType type2 = EffectType.VERTEX;
175 76a81b6a Leszek Koltunski
176 a418b421 Leszek Koltunski
    EffectName[] names = EffectName.values();
177 8fd9f5fa Leszek Koltunski
178 76a81b6a Leszek Koltunski
    int numEffects=0;
179 8fd9f5fa Leszek Koltunski
180
    for(int i=0; i<names.length; i++)
181 76a81b6a Leszek Koltunski
      if( names[i].getType() == type1 || names[i].getType() == type2 ) numEffects++;
182 8fd9f5fa Leszek Koltunski
183 a418b421 Leszek Koltunski
    mEffectNames = new EffectName[numEffects];
184 8fd9f5fa Leszek Koltunski
185 76a81b6a Leszek Koltunski
    numEffects=0;
186 8fd9f5fa Leszek Koltunski
187
    for(int i=0; i<names.length; i++)
188 76a81b6a Leszek Koltunski
      if( names[i].getType() == type1 || names[i].getType() == type2 )
189 8fd9f5fa Leszek Koltunski
        {
190 76a81b6a Leszek Koltunski
        mEffectNames[numEffects++] = names[i];
191 8fd9f5fa Leszek Koltunski
        }
192
    }
193
194 d40cfeb2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
195
196 50ac40a6 Leszek Koltunski
  private void setGrid()
197 d40cfeb2 Leszek Koltunski
    {
198 2e755580 Leszek Koltunski
    mGridInitialized = true;
199
200 bfcf419a Leszek Koltunski
    mNumCols = mColsPicker.getValue();
201
    mNumRows = mRowsPicker.getValue();
202 d40cfeb2 Leszek Koltunski
203 bfcf419a Leszek Koltunski
    int width = mLay.getWidth();
204
    int height= mLay.getHeight();
205 57ba16f3 Leszek Koltunski
    int w = mNumCols>0 ? (int)( 0.9f*width / mNumCols) : 0;
206
    int h = mNumRows>0 ? (int)( 0.9f*height/ mNumRows) : 0;
207 d40cfeb2 Leszek Koltunski
    int size= w<h ? w:h;
208 57ba16f3 Leszek Koltunski
    int pad = size<20 ? 1 : size/20;
209 d40cfeb2 Leszek Koltunski
210 bfcf419a Leszek Koltunski
    mLay.removeAllViews();
211 d40cfeb2 Leszek Koltunski
212
    mShape = new boolean[mNumRows*mNumCols];
213
214 56cbe1cf Leszek Koltunski
    TableRow.LayoutParams p = new TableRow.LayoutParams();
215 d40cfeb2 Leszek Koltunski
216
    p.rightMargin  = pad;
217
    p.leftMargin   = pad;
218
    p.topMargin    = pad;
219
    p.bottomMargin = pad;
220
    p.height       = size;
221
    p.width        = size;
222
223
    for (int rows=0; rows<mNumRows; rows++)
224 08f92d82 Leszek Koltunski
      {
225 d40cfeb2 Leszek Koltunski
      TableRow tr = new TableRow(this);
226
      tr.setGravity(Gravity.CENTER);
227
228
      for(int cols=0; cols<mNumCols; cols++)
229
        {
230
        Button b = new Button(this);
231
        b.setOnClickListener(this);
232
        b.setId(rows*mNumCols+cols);
233
        b.setLayoutParams(p);
234 bfcf419a Leszek Koltunski
        b.setBackgroundColor(mObjectType==1 ? COLOR_INAC : COLOR_ON);
235 d40cfeb2 Leszek Koltunski
        tr.addView(b, p);
236 bcc8e016 Leszek Koltunski
        mShape[rows*mNumCols+cols] = true;
237 d40cfeb2 Leszek Koltunski
        }
238
239 bfcf419a Leszek Koltunski
      mLay.addView(tr);
240 d40cfeb2 Leszek Koltunski
      }
241
    }
242
243 50ac40a6 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
244
245 f6d884d5 Leszek Koltunski
  public DistortedTexture getTexture()
246 50ac40a6 Leszek Koltunski
    {
247 f6d884d5 Leszek Koltunski
    return mTexture;
248
    }
249
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251
252 d04a4886 Leszek Koltunski
  public DistortedEffects getEffects()
253 f6d884d5 Leszek Koltunski
    {
254 d04a4886 Leszek Koltunski
    return mEffects;
255 50ac40a6 Leszek Koltunski
    }
256
257 e8b6aa95 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
258
259 7fca6741 Leszek Koltunski
  public MeshObject getMesh()
260 e8b6aa95 Leszek Koltunski
    {
261 b01acdaf Leszek Koltunski
    return mMesh;
262 e8b6aa95 Leszek Koltunski
    }
263
264 24991bc2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
265
266 f338550a leszek
  public void setSupportsRegion(boolean supports)
267 24991bc2 Leszek Koltunski
    {
268 98c04ab8 leszek
    mSupportsRegion = supports;
269 24991bc2 Leszek Koltunski
    }
270
271 76a81b6a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
272
273 f338550a leszek
  public void setSupportsCenter(boolean supports)
274 76a81b6a Leszek Koltunski
    {
275 98c04ab8 leszek
    mSupportsCenter = supports;
276 76a81b6a Leszek Koltunski
    }
277
278 d40cfeb2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
279
280 9167cfd4 Leszek Koltunski
  public Bitmap getBitmap()
281 d40cfeb2 Leszek Koltunski
    {
282 cdc515b9 Leszek Koltunski
    if( mBitmap==null )
283 9167cfd4 Leszek Koltunski
      {
284 cdc515b9 Leszek Koltunski
      if( mBitmapID!=-1)
285 9167cfd4 Leszek Koltunski
        {
286 cdc515b9 Leszek Koltunski
        InputStream is = getResources().openRawResource(mBitmapID);
287
288 9167cfd4 Leszek Koltunski
        try
289
          {
290 cdc515b9 Leszek Koltunski
          mBitmap = BitmapFactory.decodeStream(is);
291
          }
292
        finally
293
          {
294
          try
295
            {
296
            is.close();
297
            }
298
          catch(IOException e) { }
299 9167cfd4 Leszek Koltunski
          }
300
        }
301 cdc515b9 Leszek Koltunski
      else
302
        {
303 0ab55f0c Leszek Koltunski
        final int W = 64*mNumCols;
304
        final int H = 64*mNumRows;
305 9167cfd4 Leszek Koltunski
306 cdc515b9 Leszek Koltunski
        Paint paint = new Paint();
307
        mBitmap = Bitmap.createBitmap(W,H, Bitmap.Config.ARGB_8888);
308
        Canvas canvas = new Canvas(mBitmap);
309 9167cfd4 Leszek Koltunski
310 cdc515b9 Leszek Koltunski
        paint.setAntiAlias(true);
311
        paint.setTextAlign(Paint.Align.CENTER);
312
        paint.setColor(0xff008800);
313
        paint.setStyle(Paint.Style.FILL);
314
        canvas.drawRect(0, 0, W, H, paint);
315
        paint.setColor(0xffffffff);
316 9167cfd4 Leszek Koltunski
317 0ab55f0c Leszek Koltunski
        for(int i=0; i<=mNumCols ; i++ ) canvas.drawRect(W*i/mNumCols-1, 0, W*i/mNumCols + 1, H, paint);
318
        for(int i=0; i<=mNumRows ; i++ ) canvas.drawRect( 0, H*i/mNumRows-1, W,  H*i/mNumRows+1, paint);
319 9167cfd4 Leszek Koltunski
        }
320
      }
321
322 cdc515b9 Leszek Koltunski
    return mBitmap;
323 d40cfeb2 Leszek Koltunski
    }
324
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326
327 56cbe1cf Leszek Koltunski
  public void onClick(View view)
328 d40cfeb2 Leszek Koltunski
    {
329 bfcf419a Leszek Koltunski
    if( mObjectType!=1 )
330
      {
331
      Button tmp = (Button)view;
332
      int id = tmp.getId();
333
      mShape[id] = !mShape[id];
334
      tmp.setBackgroundColor(mShape[id] ? COLOR_ON:COLOR_OFF);
335
      }
336
    }
337
338
///////////////////////////////////////////////////////////////////////////////////////////////////
339
340
  private void uncheckAll()
341
    {
342
    TableRow tr;
343
    Button butt;
344
345
    for (int row=0; row<mNumRows; row++)
346
      {
347
      tr = (TableRow)mLay.getChildAt(row);
348
349
      for(int col=0; col<mNumCols; col++)
350
        {
351
        butt = (Button)tr.getVirtualChildAt(col);
352
        butt.setBackgroundColor(mObjectType==1 ? COLOR_INAC : COLOR_ON);
353
        mShape[row*mNumCols+col] = true;
354
        }
355
      }
356 bcc8e016 Leszek Koltunski
    }
357
358 6f779cd4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
359
360
  public void setRegion(float x, float y, float r)
361
    {
362
    mRegionX = x;
363 24991bc2 Leszek Koltunski
    mRegionY =-y;
364 6f779cd4 Leszek Koltunski
    mRegionR = r;
365
366 bddd4b2d Leszek Koltunski
    Effects3DSurfaceView view = findViewById(R.id.effects3dSurfaceView);
367 6f779cd4 Leszek Koltunski
    view.getRenderer().setRegion(mRegionX, mRegionY, mRegionR);
368
    }
369
370 950511ed Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
371
372 334c13fa Leszek Koltunski
  public void setCenter(float x, float y, float z)
373 950511ed Leszek Koltunski
    {
374
    mCenterX = x;
375
    mCenterY = y;
376 334c13fa Leszek Koltunski
    mCenterZ = z;
377 950511ed Leszek Koltunski
378 bddd4b2d Leszek Koltunski
    Effects3DSurfaceView view = findViewById(R.id.effects3dSurfaceView);
379 334c13fa Leszek Koltunski
    view.getRenderer().setCenter( mCenterX, mCenterY, mCenterZ );
380 950511ed Leszek Koltunski
    }
381
382
///////////////////////////////////////////////////////////////////////////////////////////////////
383
384
  public float getCenterX()
385
    {
386
    return mCenterX;
387
    }
388
389
///////////////////////////////////////////////////////////////////////////////////////////////////
390
391
  public float getCenterY()
392
    {
393
    return mCenterY;
394
    }
395
396 334c13fa Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
397
398
  public float getCenterZ()
399
    {
400
    return mCenterZ;
401
    }
402
403 6f779cd4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
404
405
  public float getRegionX()
406
    {
407
    return mRegionX;
408
    }
409
410
///////////////////////////////////////////////////////////////////////////////////////////////////
411
412
  public float getRegionY()
413
    {
414
    return mRegionY;
415
    }
416
417
///////////////////////////////////////////////////////////////////////////////////////////////////
418
419
  public float getRegionR()
420
    {
421
    return mRegionR;
422
    }
423
424 bcc8e016 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
425 50ac40a6 Leszek Koltunski
426 bfcf419a Leszek Koltunski
  public void Create(View v)
427 bcc8e016 Leszek Koltunski
    {
428 d76638f1 leszek
    DistortedEffects.setMax(EffectType.VERTEX ,20);    // those have to be called before
429
    DistortedEffects.setMax(EffectType.FRAGMENT,3);    // any DistortedEffect get created!
430 a98db446 Leszek Koltunski
431 50ac40a6 Leszek Koltunski
    if( mObjectType==1 )
432
      {
433 bfcf419a Leszek Koltunski
      mMesh = new MeshFlat(mNumCols,mNumRows);
434 50ac40a6 Leszek Koltunski
      }
435
    else
436
      {
437 bfcf419a Leszek Koltunski
      String str = "";
438 50ac40a6 Leszek Koltunski
439 bfcf419a Leszek Koltunski
      for(int i=0; i<mNumRows*mNumCols; i++)
440
        str += mShape[i] ? "1" : "0";
441 50ac40a6 Leszek Koltunski
442 b3b2c6cf leszek
      mMesh = new MeshCubes(mNumCols, str, mNumSlic);
443 50ac40a6 Leszek Koltunski
      }
444 bcc8e016 Leszek Koltunski
445 0ab55f0c Leszek Koltunski
    mMesh.setShowNormals(mShowNormal);
446
447 bfcf419a Leszek Koltunski
    mEffects= new DistortedEffects();
448
    mTexture= new DistortedTexture(mNumCols,mNumRows);
449 bcc8e016 Leszek Koltunski
450 bfcf419a Leszek Koltunski
    final View view = getLayoutInflater().inflate(R.layout.effects3dlayout, null);
451 bcc8e016 Leszek Koltunski
452 bfcf419a Leszek Koltunski
    setContentView(view);
453 bcc8e016 Leszek Koltunski
454 bfcf419a Leszek Koltunski
    String[] effects = new String[mEffectNames.length];
455 50ac40a6 Leszek Koltunski
456 bfcf419a Leszek Koltunski
    for(int i=0; i<mEffectNames.length; i++) effects[i] = mEffectNames[i].name();
457
458 bddd4b2d Leszek Koltunski
    Spinner effectSpinner = findViewById(R.id.effects3dspinner );
459 bfcf419a Leszek Koltunski
    effectSpinner.setOnItemSelectedListener(this);
460
461
    ArrayAdapter<String> adapterEffect = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, effects);
462
    adapterEffect.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
463
    effectSpinner.setAdapter(adapterEffect);
464
465 98c04ab8 leszek
    resetData();
466
467 bfcf419a Leszek Koltunski
    mEffectAdd = 0;
468 d40cfeb2 Leszek Koltunski
    }
469
470 50ac40a6 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
471
472
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
473
    {
474
    switch(parent.getId())
475
      {
476 bfcf419a Leszek Koltunski
      case R.id.objectpicker_spinnerType  : if( mObjectType!=pos )
477
                                              {
478
                                              mObjectType = pos;
479
                                              uncheckAll();
480
                                              }
481 56cbe1cf Leszek Koltunski
                                            break;
482 bcc8e016 Leszek Koltunski
      case R.id.objectpicker_spinnerBitmap: switch(pos)
483
                                              {
484 cdc515b9 Leszek Koltunski
                                              case 0: mBitmapID = -1        ; break;
485
                                              case 1: mBitmapID = R.raw.face; break;
486
                                              case 2: mBitmapID = R.raw.dog ; break;
487
                                              case 3: mBitmapID = R.raw.cat ; break;
488
                                              case 4: mBitmapID = R.raw.grid; break;
489
                                              case 5: mBitmapID = R.raw.bean; break;
490
                                              case 6: mBitmapID = R.raw.monalisa; break;
491 bcc8e016 Leszek Koltunski
                                              }
492
                                            break;
493 76a81b6a Leszek Koltunski
      case R.id.effects3dspinner          : mEffectAdd = pos;
494 56cbe1cf Leszek Koltunski
                                            break;
495 50ac40a6 Leszek Koltunski
      }
496
    }
497
498
///////////////////////////////////////////////////////////////////////////////////////////////////
499
500
  public void onNothingSelected(AdapterView<?> parent)
501
    {
502
    }
503
504 d40cfeb2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
505
506 56cbe1cf Leszek Koltunski
  public int getWidth()
507 d40cfeb2 Leszek Koltunski
    {
508 f6d884d5 Leszek Koltunski
    return mTexture==null ? 0: mTexture.getWidth();
509 d40cfeb2 Leszek Koltunski
    }
510 08f92d82 Leszek Koltunski
511
///////////////////////////////////////////////////////////////////////////////////////////////////
512
513 56cbe1cf Leszek Koltunski
  public int getHeight()
514 d40cfeb2 Leszek Koltunski
    {
515 f6d884d5 Leszek Koltunski
    return mTexture==null ? 0: mTexture.getHeight();
516 d40cfeb2 Leszek Koltunski
    }
517 08f92d82 Leszek Koltunski
518 334c13fa Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
519
520
  public int getDepth()
521
    {
522 b01acdaf Leszek Koltunski
    return mTexture==null ? 0: mTexture.getDepth(mMesh);
523 334c13fa Leszek Koltunski
    }
524
525
///////////////////////////////////////////////////////////////////////////////////////////////////
526
527
  public int getScreenWidth()
528
    {
529
    return 0;
530
    }
531
532 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
533
534 6f779cd4 Leszek Koltunski
  private void resetData()
535 d40cfeb2 Leszek Koltunski
    {
536 950511ed Leszek Koltunski
    mCenterX = 0.5f*getWidth();
537
    mCenterY = 0.5f*getHeight();
538 6f779cd4 Leszek Koltunski
    mRegionX = 0;
539
    mRegionY = 0;
540
    mRegionR = getWidth()/2;
541 47833473 Leszek Koltunski
542 f338550a leszek
    mSupportsRegion =false;
543 4d5b37fe Leszek Koltunski
    mSupportsCenter =false;
544 98c04ab8 leszek
545 bddd4b2d Leszek Koltunski
    Effects3DSurfaceView view = findViewById(R.id.effects3dSurfaceView);
546 0ab55f0c Leszek Koltunski
    view.getRenderer().showRegionAndCenter(false,false);
547 6f779cd4 Leszek Koltunski
    }
548
549
///////////////////////////////////////////////////////////////////////////////////////////////////
550
// 'second screen' methods
551
552 56cbe1cf Leszek Koltunski
  public void newEffect(View v)
553 794e6c4f Leszek Koltunski
    {
554 76a81b6a Leszek Koltunski
    Effects3DEffect eff = new Effects3DEffect(mEffectNames[mEffectAdd], this);
555 d04a4886 Leszek Koltunski
    mList.add(eff);
556 d2337a3a Leszek Koltunski
557 bddd4b2d Leszek Koltunski
    LinearLayout layout = findViewById(R.id.effects3dlayout);
558 56cbe1cf Leszek Koltunski
    View view = eff.createView();
559
    layout.addView(view);
560 950511ed Leszek Koltunski
561 4d5b37fe Leszek Koltunski
    if( mSupportsCenter )
562 76a81b6a Leszek Koltunski
      {
563
      View center = eff.createCenter();
564
      layout.addView(center);
565
      }
566
567
    if( mSupportsRegion )
568 950511ed Leszek Koltunski
      {
569
      View region = eff.createRegion();
570
      layout.addView(region);
571
      }
572
573 d04a4886 Leszek Koltunski
    eff.apply(mEffects);
574 98c04ab8 leszek
575 a418b421 Leszek Koltunski
    boolean show = (mEffectNames[mEffectAdd].getType()==EffectType.VERTEX);
576 bddd4b2d Leszek Koltunski
    Effects3DSurfaceView sv = findViewById(R.id.effects3dSurfaceView);
577 0ab55f0c Leszek Koltunski
    sv.getRenderer().showRegionAndCenter( (show && mShowRegion) , (show && mShowCenter) );
578 d2337a3a Leszek Koltunski
    }
579
580
///////////////////////////////////////////////////////////////////////////////////////////////////
581
582 56cbe1cf Leszek Koltunski
  public void removeAll(View v)
583 d2337a3a Leszek Koltunski
    {
584 d04a4886 Leszek Koltunski
    mList.clear();
585 bddd4b2d Leszek Koltunski
    LinearLayout layout = findViewById(R.id.effects3dlayout);
586 56cbe1cf Leszek Koltunski
    layout.removeAllViews();
587 a418b421 Leszek Koltunski
    mEffects.abortByType(EffectType.VERTEX);
588
    mEffects.abortByType(EffectType.FRAGMENT);
589 950511ed Leszek Koltunski
590 6f779cd4 Leszek Koltunski
    resetData();
591 950511ed Leszek Koltunski
592 bddd4b2d Leszek Koltunski
    Effects3DSurfaceView view = findViewById(R.id.effects3dSurfaceView);
593 76a81b6a Leszek Koltunski
    Effects3DRenderer renderer= view.getRenderer();
594 950511ed Leszek Koltunski
595 334c13fa Leszek Koltunski
    renderer.setCenter( mCenterX, mCenterY, mCenterZ );
596 6f779cd4 Leszek Koltunski
    renderer.setRegion( mRegionX, mRegionY, mRegionR );
597 950511ed Leszek Koltunski
    renderer.mQuat1.set(0,0,0,1);
598
    renderer.mQuat2.set(0,0,0,1);
599 d2337a3a Leszek Koltunski
    }
600
601 de82c8b7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
602
603
  public void remove(View v)
604
    {
605 d04a4886 Leszek Koltunski
    for(Effects3DEffect effect: mList)
606 fa9053f5 Leszek Koltunski
      {
607
      if( effect.thisView(v) )
608
        {
609 bddd4b2d Leszek Koltunski
        LinearLayout layout = findViewById(R.id.effects3dlayout);
610 fed00329 Leszek Koltunski
        View view;
611
612
        view = effect.getEffect();
613
        if( view!=null ) layout.removeView(view);
614
        view = effect.getCenter();
615
        if( view!=null ) layout.removeView(view);
616
        view = effect.getRegion();
617
        if( view!=null ) layout.removeView(view);
618
619 a418b421 Leszek Koltunski
        mEffects.abortById(effect.getId());
620 d04a4886 Leszek Koltunski
        mList.remove(effect);
621 adc117e0 Leszek Koltunski
622
        resetData();
623 fed00329 Leszek Koltunski
624
        break;
625 fa9053f5 Leszek Koltunski
        }
626
      }
627 de82c8b7 Leszek Koltunski
    }
628
629 0ab55f0c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
630
631
    boolean getShowCenter()
632
      {
633
      return mShowCenter;
634
      }
635
636
///////////////////////////////////////////////////////////////////////////////////////////////////
637
638
    boolean getShowRegion()
639
      {
640
      return mShowRegion;
641
      }
642
643
///////////////////////////////////////////////////////////////////////////////////////////////////
644
645
    public void showCenter(View view)
646
      {
647
      CheckBox box = (CheckBox)view;
648
      mShowCenter = box.isChecked();
649
650 bddd4b2d Leszek Koltunski
      Effects3DSurfaceView sv = findViewById(R.id.effects3dSurfaceView);
651 0ab55f0c Leszek Koltunski
      sv.getRenderer().showRegionAndCenter(mShowRegion,mShowCenter);
652
      }
653
654
///////////////////////////////////////////////////////////////////////////////////////////////////
655
656
    public void showRegion(View view)
657
      {
658
      CheckBox box = (CheckBox)view;
659
      mShowRegion = box.isChecked();
660
661 bddd4b2d Leszek Koltunski
      Effects3DSurfaceView sv = findViewById(R.id.effects3dSurfaceView);
662 0ab55f0c Leszek Koltunski
      sv.getRenderer().showRegionAndCenter(mShowRegion,mShowCenter);
663
      }
664
665
///////////////////////////////////////////////////////////////////////////////////////////////////
666
667
    public void showNormal(View view)
668
      {
669
      CheckBox box = (CheckBox)view;
670
      mShowNormal = box.isChecked();
671
672
      if ( mMesh!=null )
673
        {
674
        mMesh.setShowNormals(mShowNormal);
675
        }
676
      }
677
678 bddd4b2d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
679
680
    public void triggerOIT(View view)
681
      {
682
      CheckBox box = (CheckBox)view;
683
      mUseOIT = box.isChecked();
684
685
      Effects3DSurfaceView sv = findViewById(R.id.effects3dSurfaceView);
686
      sv.getRenderer().useOIT(mUseOIT);
687
      }
688
689 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
690 d40cfeb2 Leszek Koltunski
// Overrides
691
692
  @Override
693
  protected void onPause()
694
    {
695 bddd4b2d Leszek Koltunski
    GLSurfaceView mView = findViewById(R.id.effects3dSurfaceView);
696 d40cfeb2 Leszek Koltunski
    if( mView!=null ) mView.onPause();
697 b0ebdf5e Leszek Koltunski
698
    Distorted.onPause();
699 d40cfeb2 Leszek Koltunski
    super.onPause();
700
    }
701 08f92d82 Leszek Koltunski
702
///////////////////////////////////////////////////////////////////////////////////////////////////
703
    
704 d40cfeb2 Leszek Koltunski
  @Override
705
  protected void onResume()
706
    {
707
    super.onResume();
708 bddd4b2d Leszek Koltunski
    GLSurfaceView mView = findViewById(R.id.effects3dSurfaceView);
709 d40cfeb2 Leszek Koltunski
    if( mView!=null ) mView.onResume();
710
    }
711 08f92d82 Leszek Koltunski
712
///////////////////////////////////////////////////////////////////////////////////////////////////
713
    
714 d40cfeb2 Leszek Koltunski
  @Override
715
  public void onWindowFocusChanged(boolean hasFocus)
716
    {
717
    super.onWindowFocusChanged(hasFocus);
718
719 bfcf419a Leszek Koltunski
    mColsPicker.setValue(mNumCols);
720
    mRowsPicker.setValue(mNumRows);
721 b3b2c6cf leszek
    mSlicPicker.setValue(mNumSlic);
722 bfcf419a Leszek Koltunski
723 2e755580 Leszek Koltunski
    if( !mGridInitialized ) setGrid();
724 d40cfeb2 Leszek Koltunski
    }
725 08f92d82 Leszek Koltunski
726
///////////////////////////////////////////////////////////////////////////////////////////////////
727
    
728 d40cfeb2 Leszek Koltunski
  @Override
729
  protected void onDestroy()
730
    {
731
    Distorted.onDestroy();
732
    super.onDestroy();
733
    }
734 08f92d82 Leszek Koltunski
735 d40cfeb2 Leszek Koltunski
  }