Project

General

Profile

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

examples / src / main / java / org / distorted / examples / matrix3d / Matrix3DActivity.java @ 89c3e2bf

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.DistortedEffects;
36
import org.distorted.library.MeshCubes;
37
import org.distorted.library.MeshObject;
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 MeshObject mMesh;
52
  private DistortedEffects mEffects;
53

    
54
  private ArrayList<Matrix3DEffect> mList;
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
    mList = new ArrayList<>();
69

    
70
    createEffectNames();
71

    
72
    mEffects = new DistortedEffects();
73
    mMesh = new MeshCubes(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 DistortedEffects getEffects()
106
    {
107
    return mEffects;
108
    }
109

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

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

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

    
119
  public MeshObject getMesh()
120
    {
121
    return mMesh;
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 void setSupportsCenter(boolean supports)
148
    {
149
    mSupportsCenter = supports;
150
    }
151

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

    
154
  public Bitmap getBitmap()
155
    {
156
    Bitmap bmp;
157

    
158
    InputStream is = getResources().openRawResource(R.raw.grid);
159

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

    
173
    return bmp;
174
    }
175

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177

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

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184

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

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

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

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

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

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

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

    
216
///////////////////////////////////////////////////////////////////////////////////////////////////
217

    
218
  public void onNothingSelected(AdapterView<?> parent)
219
    {
220
    }
221

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

    
224
  private void resetData()
225
    {
226
    mSupportsCenter=false;
227
    }
228

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

    
231
  private void setEffectView()
232
    {
233
    resetData();
234

    
235
    final View view = getLayoutInflater().inflate(R.layout.matrix3dlayout, null);
236

    
237
    setContentView(view);
238

    
239
    String[] effects = new String[mEffectNames.length];
240

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

    
243
    Spinner effectSpinner = (Spinner)findViewById(R.id.matrix3dspinner );
244
    effectSpinner.setOnItemSelectedListener(this);
245

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

    
250
    mEffectAdd = 0;
251
    }
252

    
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254

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

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

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

    
270
    eff.apply(mEffects);
271
    }
272

    
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274

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

    
282
    resetData();
283
    }
284

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

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

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

    
303
        mEffects.abortEffect(effect.getId());
304
        mList.remove(effect);
305

    
306
        resetData();
307

    
308
        break;
309
        }
310
      }
311
    }
312

    
313
///////////////////////////////////////////////////////////////////////////////////////////////////
314
// Overrides
315

    
316
  @Override
317
  protected void onPause()
318
    {
319
    GLSurfaceView mView = (GLSurfaceView)findViewById(R.id.matrix3dSurfaceView);
320
    if( mView!=null ) mView.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)