Project

General

Profile

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

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

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
  private float mCenterX, mCenterY, mCenterZ;
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

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

    
67
    mEffects = new ArrayList<>();
68

    
69
    createEffectNames();
70

    
71
    mGrid   = new DistortedCubesGrid(1,1,false);
72
    mObject = new DistortedObject(1,1,1);
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 DistortedObject getObject()
104
    {
105
    return mObject;
106
    }
107

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

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

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

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

    
120
    }
121

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

    
124
  public void setCenter(float x, float y, float z)
125
    {
126
    mCenterX = x;
127
    mCenterY = y;
128
    mCenterZ = z;
129
    }
130

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

    
133
  public static void setSupportsRegion(boolean supports)
134
    {
135

    
136
    }
137

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

    
140
  public static boolean supportsRegion()
141
    {
142
    return false;
143
    }
144

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

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

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

    
154
  public static boolean supportsCenter()
155
    {
156
    return mSupportsCenter;
157
    }
158

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

    
161
  public Bitmap getBitmap()
162
    {
163
    Bitmap bmp;
164

    
165
    InputStream is = getResources().openRawResource(R.raw.grid);
166

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

    
180
    return bmp;
181
    }
182

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

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

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

    
192
  public int getHeight()
193
    {
194
    return mObject==null ? 0: mObject.getHeight();
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 int getScreenHeight()
208
    {
209
    Matrix3DRenderer r = ((Matrix3DSurfaceView)findViewById(R.id.matrix3dSurfaceView)).getRenderer();
210
    return r.getHeight();
211
    }
212

    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214

    
215
  public int getDepth()
216
    {
217
    return mObject==null ? 0: mObject.getDepth();
218
    }
219

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

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

    
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232

    
233
  public void onNothingSelected(AdapterView<?> parent)
234
    {
235
    }
236

    
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238

    
239
  private void resetData()
240
    {
241
    mCenterX = 0.5f*getWidth();
242
    mCenterY = 0.5f*getHeight();
243

    
244
    mSupportsCenter=false;
245
    }
246

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248

    
249
  private void setEffectView()
250
    {
251
    resetData();
252

    
253
    final View view = getLayoutInflater().inflate(R.layout.matrix3dlayout, null);
254

    
255
    setContentView(view);
256

    
257
    String[] effects = new String[mEffectNames.length];
258

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

    
261
    Spinner effectSpinner = (Spinner)findViewById(R.id.matrix3dspinner );
262
    effectSpinner.setOnItemSelectedListener(this);
263

    
264
    ArrayAdapter<String> adapterEffect = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, effects);
265
    adapterEffect.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
266
    effectSpinner.setAdapter(adapterEffect);
267

    
268
    mEffectAdd = 0;
269
    }
270

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

    
273
  public void newEffect(View v)
274
    {
275
    Matrix3DEffect eff = new Matrix3DEffect(mEffectNames[mEffectAdd], this);
276
    mEffects.add(eff);
277

    
278
    LinearLayout layout = (LinearLayout)findViewById(R.id.matrix3dlayout);
279
    View view = eff.createView();
280
    layout.addView(view);
281

    
282
    if( mSupportsCenter )
283
      {
284
      View center = eff.createCenter();
285
      layout.addView(center);
286
      }
287

    
288
    eff.apply(mObject);
289
    }
290

    
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292

    
293
  public void removeAll(View v)
294
    {
295
    mEffects.clear();
296
    LinearLayout layout = (LinearLayout)findViewById(R.id.matrix3dlayout);
297
    layout.removeAllViews();
298
    mObject.abortEffects(EffectTypes.MATRIX);
299

    
300
    resetData();
301
    }
302

    
303
///////////////////////////////////////////////////////////////////////////////////////////////////
304

    
305
  public void remove(View v)
306
    {
307
    for(Matrix3DEffect effect: mEffects)
308
      {
309
      if( effect.thisView(v) )
310
        {
311
        LinearLayout layout = (LinearLayout)findViewById(R.id.matrix3dlayout);
312
        View view;
313

    
314
        view = effect.getEffect();
315
        if( view!=null ) layout.removeView(view);
316
        view = effect.getCenter();
317
        if( view!=null ) layout.removeView(view);
318
        view = effect.getRegion();
319
        if( view!=null ) layout.removeView(view);
320

    
321
        long id = effect.getId();
322
        mObject.abortEffect(id);
323
        mEffects.remove(effect);
324

    
325
        resetData();
326

    
327
        break;
328
        }
329
      }
330
    }
331

    
332
///////////////////////////////////////////////////////////////////////////////////////////////////
333
// Overrides
334

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

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

    
353
///////////////////////////////////////////////////////////////////////////////////////////////////
354
    
355
  @Override
356
  protected void onDestroy()
357
    {
358
    Distorted.onDestroy();
359
    super.onDestroy();
360
    }
361

    
362
  }
(1-1/4)