Project

General

Profile

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

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

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.DistortedObjectGrid;
36
import org.distorted.library.DistortedCubesGrid;
37
import org.distorted.library.DistortedObject;
38
import org.distorted.library.EffectNames;
39
import org.distorted.library.EffectTypes;
40

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

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

    
47
public class Matrix3DActivity extends Activity implements AdapterView.OnItemSelectedListener
48
  {
49
  private DistortedObject mObject;
50
  private DistortedObjectGrid mGrid;
51

    
52
  private ArrayList<Matrix3DEffect> mEffects;
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
    mEffects = new ArrayList<>();
67

    
68
    createEffectNames();
69

    
70
    mGrid   = new DistortedCubesGrid(1,1,false);
71
    mObject = new DistortedObject(100,100,100);
72

    
73
    setEffectView();
74
    }
75

    
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77

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

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

    
84
    int numEffects=0;
85

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

    
89
    mEffectNames = new EffectNames[numEffects];
90

    
91
    numEffects=0;
92

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

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101

    
102
  public DistortedObject getObject()
103
    {
104
    return mObject;
105
    }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108

    
109
  public DistortedObjectGrid getGrid()
110
    {
111
    return mGrid;
112
    }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

    
116
  public void setRegion(float x, float y, float r)
117
    {
118

    
119
    }
120

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122

    
123
  public void setCenter(float x, float y, float z)
124
    {
125

    
126
    }
127

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

    
130
  public static void setSupportsRegion(boolean supports)
131
    {
132

    
133
    }
134

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136

    
137
  public static boolean supportsRegion()
138
    {
139
    return false;
140
    }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143

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

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

    
151
  public static boolean supportsCenter()
152
    {
153
    return mSupportsCenter;
154
    }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

    
158
  public Bitmap getBitmap()
159
    {
160
    Bitmap bmp;
161

    
162
    InputStream is = getResources().openRawResource(R.raw.grid);
163

    
164
    try
165
      {
166
      bmp = BitmapFactory.decodeStream(is);
167
      }
168
    finally
169
      {
170
      try
171
        {
172
        is.close();
173
        }
174
      catch(IOException e) { }
175
      }
176

    
177
    return bmp;
178
    }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

    
182
  public int getWidth()
183
    {
184
    return mObject==null ? 0: mObject.getWidth();
185
    }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

    
189
  public int getHeight()
190
    {
191
    return mObject==null ? 0: mObject.getHeight();
192
    }
193

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

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

    
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203

    
204
  public int getScreenHeight()
205
    {
206
    Matrix3DRenderer r = ((Matrix3DSurfaceView)findViewById(R.id.matrix3dSurfaceView)).getRenderer();
207
    return r.getHeight();
208
    }
209

    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

    
212
  public int getDepth()
213
    {
214
    return mObject==null ? 0: mObject.getDepth();
215
    }
216

    
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218

    
219
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
220
    {
221
    switch(parent.getId())
222
      {
223
      case R.id.matrix3dspinner          : mEffectAdd = pos;
224
                                           break;
225
      }
226
    }
227

    
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229

    
230
  public void onNothingSelected(AdapterView<?> parent)
231
    {
232
    }
233

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235

    
236
  private void resetData()
237
    {
238
    mSupportsCenter=false;
239
    }
240

    
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242

    
243
  private void setEffectView()
244
    {
245
    resetData();
246

    
247
    final View view = getLayoutInflater().inflate(R.layout.matrix3dlayout, null);
248

    
249
    setContentView(view);
250

    
251
    String[] effects = new String[mEffectNames.length];
252

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

    
255
    Spinner effectSpinner = (Spinner)findViewById(R.id.matrix3dspinner );
256
    effectSpinner.setOnItemSelectedListener(this);
257

    
258
    ArrayAdapter<String> adapterEffect = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, effects);
259
    adapterEffect.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
260
    effectSpinner.setAdapter(adapterEffect);
261

    
262
    mEffectAdd = 0;
263
    }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266

    
267
  public void newEffect(View v)
268
    {
269
    Matrix3DEffect eff = new Matrix3DEffect(mEffectNames[mEffectAdd], this);
270
    mEffects.add(eff);
271

    
272
    LinearLayout layout = (LinearLayout)findViewById(R.id.matrix3dlayout);
273
    View view = eff.createView();
274
    layout.addView(view);
275

    
276
    if( mSupportsCenter )
277
      {
278
      View center = eff.createCenter();
279
      layout.addView(center);
280
      }
281

    
282
    eff.apply(mObject);
283
    }
284

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

    
287
  public void removeAll(View v)
288
    {
289
    mEffects.clear();
290
    LinearLayout layout = (LinearLayout)findViewById(R.id.matrix3dlayout);
291
    layout.removeAllViews();
292
    mObject.abortEffects(EffectTypes.MATRIX);
293

    
294
    resetData();
295
    }
296

    
297
///////////////////////////////////////////////////////////////////////////////////////////////////
298

    
299
  public void remove(View v)
300
    {
301
    for(Matrix3DEffect effect: mEffects)
302
      {
303
      if( effect.thisView(v) )
304
        {
305
        LinearLayout layout = (LinearLayout)findViewById(R.id.matrix3dlayout);
306
        View view;
307

    
308
        view = effect.getEffect();
309
        if( view!=null ) layout.removeView(view);
310
        view = effect.getCenter();
311
        if( view!=null ) layout.removeView(view);
312
        view = effect.getRegion();
313
        if( view!=null ) layout.removeView(view);
314

    
315
        long id = effect.getId();
316
        mObject.abortEffect(id);
317
        mEffects.remove(effect);
318

    
319
        resetData();
320

    
321
        break;
322
        }
323
      }
324
    }
325

    
326
///////////////////////////////////////////////////////////////////////////////////////////////////
327
// Overrides
328

    
329
  @Override
330
  protected void onPause()
331
    {
332
    GLSurfaceView mView = (GLSurfaceView)findViewById(R.id.matrix3dSurfaceView);
333
    if( mView!=null ) mView.onPause();
334
    super.onPause();
335
    }
336

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

    
347
///////////////////////////////////////////////////////////////////////////////////////////////////
348
    
349
  @Override
350
  protected void onDestroy()
351
    {
352
    Distorted.onDestroy();
353
    super.onDestroy();
354
    }
355

    
356
  }
(1-1/4)