Project

General

Profile

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

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

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 427ab7bf Leszek Koltunski
20 db5d943e Leszek Koltunski
package org.distorted.examples.matrix3d;
21 427ab7bf Leszek Koltunski
22
import android.app.Activity;
23 4d5b37fe Leszek Koltunski
import android.graphics.Bitmap;
24
import android.graphics.BitmapFactory;
25 427ab7bf Leszek Koltunski
import android.opengl.GLSurfaceView;
26 4d5b37fe Leszek Koltunski
import android.os.Bundle;
27 427ab7bf Leszek Koltunski
import android.view.View;
28 4d5b37fe Leszek Koltunski
import android.widget.AdapterView;
29
import android.widget.ArrayAdapter;
30 427ab7bf Leszek Koltunski
import android.widget.LinearLayout;
31 4d5b37fe Leszek Koltunski
import android.widget.Spinner;
32 427ab7bf Leszek Koltunski
33 4d5b37fe Leszek Koltunski
import org.distorted.examples.R;
34
import org.distorted.library.Distorted;
35 e8b6aa95 Leszek Koltunski
import org.distorted.library.DistortedObjectGrid;
36
import org.distorted.library.DistortedCubesGrid;
37 4d5b37fe Leszek Koltunski
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 427ab7bf Leszek Koltunski
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46 4d5b37fe Leszek Koltunski
47
public class Matrix3DActivity extends Activity implements AdapterView.OnItemSelectedListener
48
  {
49
  private DistortedObject mObject;
50 e8b6aa95 Leszek Koltunski
  private DistortedObjectGrid mGrid;
51 4d5b37fe Leszek Koltunski
52
  private ArrayList<Matrix3DEffect> mEffects;
53
  private int mEffectAdd;
54
55
  private EffectNames[] mEffectNames;
56
57
  private static boolean mSupportsCenter;
58 334c13fa Leszek Koltunski
  private float mCenterX, mCenterY, mCenterZ;
59 427ab7bf Leszek Koltunski
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61
62 4d5b37fe Leszek Koltunski
  @Override
63
  protected void onCreate(Bundle savedState)
64
    {
65
    super.onCreate(savedState);
66
67
    mEffects = new ArrayList<>();
68
69
    createEffectNames();
70
71 e8b6aa95 Leszek Koltunski
    mGrid   = new DistortedCubesGrid(1,1,false);
72
    mObject = new DistortedObject(1,1,1);
73 4d5b37fe Leszek Koltunski
74
    setEffectView();
75
    }
76
77 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
78 4d5b37fe Leszek Koltunski
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 427ab7bf Leszek Koltunski
        {
97 4d5b37fe Leszek Koltunski
        mEffectNames[numEffects++] = names[i];
98 427ab7bf Leszek Koltunski
        }
99 4d5b37fe Leszek Koltunski
    }
100 427ab7bf Leszek Koltunski
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
103 4d5b37fe Leszek Koltunski
  public DistortedObject getObject()
104
    {
105
    return mObject;
106
    }
107
108 e8b6aa95 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
109
110
  public DistortedObjectGrid getGrid()
111
    {
112
    return mGrid;
113
    }
114
115 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
116
117 4d5b37fe Leszek Koltunski
  public void setRegion(float x, float y, float r)
118
    {
119
120
    }
121 427ab7bf Leszek Koltunski
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123
124 334c13fa Leszek Koltunski
  public void setCenter(float x, float y, float z)
125 4d5b37fe Leszek Koltunski
    {
126
    mCenterX = x;
127
    mCenterY = y;
128 334c13fa Leszek Koltunski
    mCenterZ = z;
129 4d5b37fe Leszek Koltunski
    }
130
131 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
132
133 4d5b37fe Leszek Koltunski
  public static void setSupportsRegion(boolean supports)
134
    {
135
136
    }
137 427ab7bf Leszek Koltunski
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139
140 4d5b37fe Leszek Koltunski
  public static boolean supportsRegion()
141
    {
142
    return false;
143
    }
144
145 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
146 4d5b37fe Leszek Koltunski
147
  public static void setSupportsCenter(boolean supports)
148
    {
149
    mSupportsCenter = supports;
150
    }
151 427ab7bf Leszek Koltunski
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153
154 4d5b37fe Leszek Koltunski
  public static boolean supportsCenter()
155
    {
156
    return mSupportsCenter;
157
    }
158 427ab7bf Leszek Koltunski
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160
161 4d5b37fe Leszek Koltunski
  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 427ab7bf Leszek Koltunski
      {
173 4d5b37fe Leszek Koltunski
      try
174
        {
175
        is.close();
176
        }
177
      catch(IOException e) { }
178 427ab7bf Leszek Koltunski
      }
179
180 4d5b37fe Leszek Koltunski
    return bmp;
181
    }
182
183 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
184
185 4d5b37fe Leszek Koltunski
  public int getWidth()
186
    {
187
    return mObject==null ? 0: mObject.getWidth();
188
    }
189 427ab7bf Leszek Koltunski
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191
192 4d5b37fe Leszek Koltunski
  public int getHeight()
193
    {
194
    return mObject==null ? 0: mObject.getHeight();
195
    }
196 427ab7bf Leszek Koltunski
197 334c13fa Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
221
222 4d5b37fe Leszek Koltunski
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
223
    {
224
    switch(parent.getId())
225 427ab7bf Leszek Koltunski
      {
226 4d5b37fe Leszek Koltunski
      case R.id.matrix3dspinner          : mEffectAdd = pos;
227
                                           break;
228 427ab7bf Leszek Koltunski
      }
229 4d5b37fe Leszek Koltunski
    }
230
231 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
232
233 4d5b37fe Leszek Koltunski
  public void onNothingSelected(AdapterView<?> parent)
234
    {
235
    }
236 427ab7bf Leszek Koltunski
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238
239 4d5b37fe Leszek Koltunski
  private void resetData()
240
    {
241
    mCenterX = 0.5f*getWidth();
242
    mCenterY = 0.5f*getHeight();
243 1d811f85 Leszek Koltunski
244 4d5b37fe Leszek Koltunski
    mSupportsCenter=false;
245
    }
246 427ab7bf Leszek Koltunski
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248
249 4d5b37fe Leszek Koltunski
  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 427ab7bf Leszek Koltunski
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272 4d5b37fe Leszek Koltunski
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 427ab7bf Leszek Koltunski
      {
284 4d5b37fe Leszek Koltunski
      View center = eff.createCenter();
285
      layout.addView(center);
286 427ab7bf Leszek Koltunski
      }
287
288 4d5b37fe Leszek Koltunski
    eff.apply(mObject);
289
    }
290
291 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
292 4d5b37fe Leszek Koltunski
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 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
304 4d5b37fe Leszek Koltunski
305
  public void remove(View v)
306
    {
307
    for(Matrix3DEffect effect: mEffects)
308 427ab7bf Leszek Koltunski
      {
309 4d5b37fe Leszek Koltunski
      if( effect.thisView(v) )
310 427ab7bf Leszek Koltunski
        {
311 4d5b37fe Leszek Koltunski
        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 427ab7bf Leszek Koltunski
        }
329
      }
330 4d5b37fe Leszek Koltunski
    }
331 427ab7bf Leszek Koltunski
332
///////////////////////////////////////////////////////////////////////////////////////////////////
333 4d5b37fe Leszek Koltunski
// 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 427ab7bf Leszek Koltunski
343
///////////////////////////////////////////////////////////////////////////////////////////////////
344 4d5b37fe Leszek Koltunski
    
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 427ab7bf Leszek Koltunski
353 4d5b37fe Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
354 427ab7bf Leszek Koltunski
    
355 4d5b37fe Leszek Koltunski
  @Override
356
  protected void onDestroy()
357
    {
358
    Distorted.onDestroy();
359
    super.onDestroy();
360
    }
361
362
  }