Project

General

Profile

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

examples / src / main / java / org / distorted / examples / matrix3d / Matrix3DActivity.java @ fdddb0b2

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.main.Distorted;
35
import org.distorted.library.main.DistortedEffects;
36
import org.distorted.library.main.MeshCubes;
37
import org.distorted.library.main.MeshObject;
38
import org.distorted.library.main.DistortedTexture;
39

    
40
import java.io.IOException;
41
import java.io.InputStream;
42
import java.util.ArrayList;
43

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

    
46
public class Matrix3DActivity extends Activity implements AdapterView.OnItemSelectedListener
47
  {
48
  private DistortedTexture mTexture;
49
  private MeshObject mMesh;
50
  private DistortedEffects mEffects;
51

    
52
  private ArrayList<Matrix3DEffect> mList;
53
  private int mEffectAdd;
54

    
55
  private EffectNames[] mEffectNames;
56

    
57
  private static boolean mSupportsCenter;
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

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

    
66
    mList = new ArrayList<>();
67

    
68
    createEffectNames();
69

    
70
    mEffects = new DistortedEffects();
71
    mMesh    = new MeshCubes(1,1,1);
72
    mTexture = new DistortedTexture(100,100);
73

    
74
    setEffectView();
75
    }
76

    
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78

    
79
  private void createEffectNames()
80
    {
81
    EffectTypes type = EffectTypes.MATRIX;
82

    
83
    EffectNames[] names = EffectNames.values();
84

    
85
    int numEffects=0;
86

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

    
90
    mEffectNames = new EffectNames[numEffects];
91

    
92
    numEffects=0;
93

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

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102

    
103
  public DistortedEffects getEffects()
104
    {
105
    return mEffects;
106
    }
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109

    
110
  public DistortedTexture getTexture()
111
    {
112
    return mTexture;
113
    }
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116

    
117
  public MeshObject getMesh()
118
    {
119
    return mMesh;
120
    }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

    
124
  public void setRegion(float x, float y, float r)
125
    {
126

    
127
    }
128

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130

    
131
  public void setCenter(float x, float y, float z)
132
    {
133

    
134
    }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137

    
138
  public static void setSupportsRegion(boolean supports)
139
    {
140

    
141
    }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

    
145
  public static void setSupportsCenter(boolean supports)
146
    {
147
    mSupportsCenter = supports;
148
    }
149

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151

    
152
  public Bitmap getBitmap()
153
    {
154
    Bitmap bmp;
155

    
156
    InputStream is = getResources().openRawResource(R.raw.grid);
157

    
158
    try
159
      {
160
      bmp = BitmapFactory.decodeStream(is);
161
      }
162
    finally
163
      {
164
      try
165
        {
166
        is.close();
167
        }
168
      catch(IOException e) { }
169
      }
170

    
171
    return bmp;
172
    }
173

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

    
176
  public int getWidth()
177
    {
178
    return mTexture==null ? 0: mTexture.getWidth();
179
    }
180

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

    
183
  public int getHeight()
184
    {
185
    return mTexture==null ? 0: mTexture.getHeight();
186
    }
187

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

    
190
  public int getDepth()
191
    {
192
    return mTexture==null ? 0: mTexture.getDepth(mMesh);
193
    }
194

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

    
197
  public int getScreenWidth()
198
    {
199
    Matrix3DRenderer r = ((Matrix3DSurfaceView)findViewById(R.id.matrix3dSurfaceView)).getRenderer();
200
    return r.getWidth();
201
    }
202

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

    
205
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
206
    {
207
    switch(parent.getId())
208
      {
209
      case R.id.matrix3dspinner: mEffectAdd = pos;
210
                                 break;
211
      }
212
    }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

    
216
  public void onNothingSelected(AdapterView<?> parent)
217
    {
218
    }
219

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221

    
222
  private void resetData()
223
    {
224
    mSupportsCenter=false;
225
    }
226

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

    
229
  private void setEffectView()
230
    {
231
    resetData();
232

    
233
    final View view = getLayoutInflater().inflate(R.layout.matrix3dlayout, null);
234

    
235
    setContentView(view);
236

    
237
    String[] effects = new String[mEffectNames.length];
238

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

    
241
    Spinner effectSpinner = (Spinner)findViewById(R.id.matrix3dspinner );
242
    effectSpinner.setOnItemSelectedListener(this);
243

    
244
    ArrayAdapter<String> adapterEffect = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, effects);
245
    adapterEffect.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
246
    effectSpinner.setAdapter(adapterEffect);
247

    
248
    mEffectAdd = 0;
249
    }
250

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

    
253
  public void newEffect(View v)
254
    {
255
    Matrix3DEffect eff = new Matrix3DEffect(mEffectNames[mEffectAdd], this);
256
    mList.add(eff);
257

    
258
    LinearLayout layout = (LinearLayout)findViewById(R.id.matrix3dlayout);
259
    View view = eff.createView();
260
    layout.addView(view);
261

    
262
    if( mSupportsCenter )
263
      {
264
      View center = eff.createCenter();
265
      layout.addView(center);
266
      }
267

    
268
    eff.apply(mEffects);
269
    }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

    
273
  public void removeAll(View v)
274
    {
275
    mList.clear();
276
    LinearLayout layout = (LinearLayout)findViewById(R.id.matrix3dlayout);
277
    layout.removeAllViews();
278
    mEffects.abortEffects(EffectTypes.MATRIX);
279

    
280
    resetData();
281
    }
282

    
283
///////////////////////////////////////////////////////////////////////////////////////////////////
284

    
285
  public void remove(View v)
286
    {
287
    for(Matrix3DEffect effect: mList)
288
      {
289
      if( effect.thisView(v) )
290
        {
291
        LinearLayout layout = (LinearLayout)findViewById(R.id.matrix3dlayout);
292
        View view;
293

    
294
        view = effect.getEffect();
295
        if( view!=null ) layout.removeView(view);
296
        view = effect.getCenter();
297
        if( view!=null ) layout.removeView(view);
298
        view = effect.getRegion();
299
        if( view!=null ) layout.removeView(view);
300

    
301
        mEffects.abortEffect(effect.getId());
302
        mList.remove(effect);
303

    
304
        resetData();
305

    
306
        break;
307
        }
308
      }
309
    }
310

    
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312
// Overrides
313

    
314
  @Override
315
  protected void onPause()
316
    {
317
    GLSurfaceView mView = (GLSurfaceView)findViewById(R.id.matrix3dSurfaceView);
318
    if( mView!=null ) mView.onPause();
319

    
320
    Distorted.onPause();
321
    super.onPause();
322
    }
323

    
324
///////////////////////////////////////////////////////////////////////////////////////////////////
325
    
326
  @Override
327
  protected void onResume()
328
    {
329
    super.onResume();
330
    GLSurfaceView mView = (GLSurfaceView)findViewById(R.id.matrix3dSurfaceView);
331
    if( mView!=null ) mView.onResume();
332
    }
333

    
334
///////////////////////////////////////////////////////////////////////////////////////////////////
335
    
336
  @Override
337
  protected void onDestroy()
338
    {
339
    Distorted.onDestroy();
340
    super.onDestroy();
341
    }
342

    
343
  }
(1-1/4)