Project

General

Profile

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

examples / src / main / java / org / distorted / examples / matrix3d / Matrix3DActivity.java @ 7451c98a

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

    
22
import android.app.Activity;
23
import android.graphics.Bitmap;
24
import android.graphics.BitmapFactory;
25
import android.opengl.GLSurfaceView;
26
import android.os.Bundle;
27
import android.view.View;
28
import android.widget.AdapterView;
29
import android.widget.ArrayAdapter;
30
import android.widget.LinearLayout;
31
import android.widget.Spinner;
32

    
33
import org.distorted.examples.R;
34
import org.distorted.library.Distorted;
35
import org.distorted.library.DistortedEffectQueues;
36
import org.distorted.library.GridObject;
37
import org.distorted.library.GridCubes;
38
import org.distorted.library.DistortedTexture;
39
import org.distorted.library.EffectNames;
40
import org.distorted.library.EffectTypes;
41

    
42
import java.io.IOException;
43
import java.io.InputStream;
44
import java.util.ArrayList;
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

    
48
public class Matrix3DActivity extends Activity implements AdapterView.OnItemSelectedListener
49
  {
50
  private DistortedTexture mTexture;
51
  private GridObject mGrid;
52
  private DistortedEffectQueues mQueues;
53

    
54
  private ArrayList<Matrix3DEffect> mEffects;
55
  private int mEffectAdd;
56

    
57
  private EffectNames[] mEffectNames;
58

    
59
  private static boolean mSupportsCenter;
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
  @Override
64
  protected void onCreate(Bundle savedState)
65
    {
66
    super.onCreate(savedState);
67

    
68
    mEffects = new ArrayList<>();
69

    
70
    createEffectNames();
71

    
72
    mQueues = new DistortedEffectQueues();
73
    mGrid   = new GridCubes(1,1,false);
74
    mTexture= new DistortedTexture(100,100);
75

    
76
    setEffectView();
77
    }
78

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80

    
81
  private void createEffectNames()
82
    {
83
    EffectTypes type = EffectTypes.MATRIX;
84

    
85
    EffectNames[] names = EffectNames.values();
86

    
87
    int numEffects=0;
88

    
89
    for(int i=0; i<names.length; i++)
90
      if( names[i].getType() == type ) numEffects++;
91

    
92
    mEffectNames = new EffectNames[numEffects];
93

    
94
    numEffects=0;
95

    
96
    for(int i=0; i<names.length; i++)
97
      if( names[i].getType() == type )
98
        {
99
        mEffectNames[numEffects++] = names[i];
100
        }
101
    }
102

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104

    
105
  public DistortedEffectQueues getQueues()
106
    {
107
    return mQueues;
108
    }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

    
112
  public DistortedTexture getTexture()
113
    {
114
    return mTexture;
115
    }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

    
119
  public GridObject getGrid()
120
    {
121
    return mGrid;
122
    }
123

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

    
126
  public void setRegion(float x, float y, float r)
127
    {
128

    
129
    }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132

    
133
  public void setCenter(float x, float y, float z)
134
    {
135

    
136
    }
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

    
140
  public static void setSupportsRegion(boolean supports)
141
    {
142

    
143
    }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

    
147
  public static boolean supportsRegion()
148
    {
149
    return false;
150
    }
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153

    
154
  public static void setSupportsCenter(boolean supports)
155
    {
156
    mSupportsCenter = supports;
157
    }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

    
161
  public static boolean supportsCenter()
162
    {
163
    return mSupportsCenter;
164
    }
165

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

    
168
  public Bitmap getBitmap()
169
    {
170
    Bitmap bmp;
171

    
172
    InputStream is = getResources().openRawResource(R.raw.grid);
173

    
174
    try
175
      {
176
      bmp = BitmapFactory.decodeStream(is);
177
      }
178
    finally
179
      {
180
      try
181
        {
182
        is.close();
183
        }
184
      catch(IOException e) { }
185
      }
186

    
187
    return bmp;
188
    }
189

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191

    
192
  public int getWidth()
193
    {
194
    return mTexture==null ? 0: mTexture.getWidth();
195
    }
196

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198

    
199
  public int getHeight()
200
    {
201
    return mTexture==null ? 0: mTexture.getHeight();
202
    }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

    
206
  public int getDepth()
207
    {
208
    return mTexture==null ? 0: mTexture.getDepth(mGrid);
209
    }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

    
213
  public int getScreenWidth()
214
    {
215
    Matrix3DRenderer r = ((Matrix3DSurfaceView)findViewById(R.id.matrix3dSurfaceView)).getRenderer();
216
    return r.getWidth();
217
    }
218

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

    
221
  public int getScreenHeight()
222
    {
223
    Matrix3DRenderer r = ((Matrix3DSurfaceView)findViewById(R.id.matrix3dSurfaceView)).getRenderer();
224
    return r.getHeight();
225
    }
226

    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228

    
229
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
230
    {
231
    switch(parent.getId())
232
      {
233
      case R.id.matrix3dspinner: mEffectAdd = pos;
234
                                 break;
235
      }
236
    }
237

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239

    
240
  public void onNothingSelected(AdapterView<?> parent)
241
    {
242
    }
243

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

    
246
  private void resetData()
247
    {
248
    mSupportsCenter=false;
249
    }
250

    
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252

    
253
  private void setEffectView()
254
    {
255
    resetData();
256

    
257
    final View view = getLayoutInflater().inflate(R.layout.matrix3dlayout, null);
258

    
259
    setContentView(view);
260

    
261
    String[] effects = new String[mEffectNames.length];
262

    
263
    for(int i=0; i<mEffectNames.length; i++) effects[i] = mEffectNames[i].name();
264

    
265
    Spinner effectSpinner = (Spinner)findViewById(R.id.matrix3dspinner );
266
    effectSpinner.setOnItemSelectedListener(this);
267

    
268
    ArrayAdapter<String> adapterEffect = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, effects);
269
    adapterEffect.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
270
    effectSpinner.setAdapter(adapterEffect);
271

    
272
    mEffectAdd = 0;
273
    }
274

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

    
277
  public void newEffect(View v)
278
    {
279
    Matrix3DEffect eff = new Matrix3DEffect(mEffectNames[mEffectAdd], this);
280
    mEffects.add(eff);
281

    
282
    LinearLayout layout = (LinearLayout)findViewById(R.id.matrix3dlayout);
283
    View view = eff.createView();
284
    layout.addView(view);
285

    
286
    if( mSupportsCenter )
287
      {
288
      View center = eff.createCenter();
289
      layout.addView(center);
290
      }
291

    
292
    eff.apply(mQueues);
293
    }
294

    
295
///////////////////////////////////////////////////////////////////////////////////////////////////
296

    
297
  public void removeAll(View v)
298
    {
299
    mEffects.clear();
300
    LinearLayout layout = (LinearLayout)findViewById(R.id.matrix3dlayout);
301
    layout.removeAllViews();
302
    mQueues.abortEffects(EffectTypes.MATRIX);
303

    
304
    resetData();
305
    }
306

    
307
///////////////////////////////////////////////////////////////////////////////////////////////////
308

    
309
  public void remove(View v)
310
    {
311
    for(Matrix3DEffect effect: mEffects)
312
      {
313
      if( effect.thisView(v) )
314
        {
315
        LinearLayout layout = (LinearLayout)findViewById(R.id.matrix3dlayout);
316
        View view;
317

    
318
        view = effect.getEffect();
319
        if( view!=null ) layout.removeView(view);
320
        view = effect.getCenter();
321
        if( view!=null ) layout.removeView(view);
322
        view = effect.getRegion();
323
        if( view!=null ) layout.removeView(view);
324

    
325
        mQueues.abortEffect(effect.getId());
326
        mEffects.remove(effect);
327

    
328
        resetData();
329

    
330
        break;
331
        }
332
      }
333
    }
334

    
335
///////////////////////////////////////////////////////////////////////////////////////////////////
336
// Overrides
337

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

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

    
356
///////////////////////////////////////////////////////////////////////////////////////////////////
357
    
358
  @Override
359
  protected void onDestroy()
360
    {
361
    Distorted.onDestroy();
362
    super.onDestroy();
363
    }
364

    
365
  }
(1-1/4)