Project

General

Profile

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

examples / src / main / java / org / distorted / examples / matrix3d / Matrix3DActivity.java @ 6161fe9a

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
import org.distorted.library.DistortedCubes;
36
import org.distorted.library.DistortedObject;
37
import org.distorted.library.EffectNames;
38
import org.distorted.library.EffectTypes;
39
40
import java.io.IOException;
41
import java.io.InputStream;
42
import java.util.ArrayList;
43 427ab7bf Leszek Koltunski
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45 4d5b37fe Leszek Koltunski
46
public class Matrix3DActivity extends Activity implements AdapterView.OnItemSelectedListener
47
  {
48
  private DistortedObject mObject;
49
50
  private ArrayList<Matrix3DEffect> mEffects;
51
  private int mEffectAdd;
52
53
  private EffectNames[] mEffectNames;
54
55
  private static boolean mSupportsCenter;
56 334c13fa Leszek Koltunski
  private float mCenterX, mCenterY, mCenterZ;
57 427ab7bf Leszek Koltunski
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59
60 4d5b37fe Leszek Koltunski
  @Override
61
  protected void onCreate(Bundle savedState)
62
    {
63
    super.onCreate(savedState);
64
65
    mEffects = new ArrayList<>();
66
67
    createEffectNames();
68
69
    mObject = new DistortedCubes( 1, "1", 100);
70
71
    setEffectView();
72
    }
73
74 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
75 4d5b37fe Leszek Koltunski
76
  private void createEffectNames()
77
    {
78
    EffectTypes type = EffectTypes.MATRIX;
79
80
    EffectNames[] names = EffectNames.values();
81
82
    int numEffects=0;
83
84
    for(int i=0; i<names.length; i++)
85
      if( names[i].getType() == type ) numEffects++;
86
87
    mEffectNames = new EffectNames[numEffects];
88
89
    numEffects=0;
90
91
    for(int i=0; i<names.length; i++)
92
      if( names[i].getType() == type )
93 427ab7bf Leszek Koltunski
        {
94 4d5b37fe Leszek Koltunski
        mEffectNames[numEffects++] = names[i];
95 427ab7bf Leszek Koltunski
        }
96 4d5b37fe Leszek Koltunski
    }
97 427ab7bf Leszek Koltunski
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99
100 4d5b37fe Leszek Koltunski
  public DistortedObject getObject()
101
    {
102
    return mObject;
103
    }
104
105 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
106
107 4d5b37fe Leszek Koltunski
  public void setRegion(float x, float y, float r)
108
    {
109
110
    }
111 427ab7bf Leszek Koltunski
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113
114 334c13fa Leszek Koltunski
  public void setCenter(float x, float y, float z)
115 4d5b37fe Leszek Koltunski
    {
116
    mCenterX = x;
117
    mCenterY = y;
118 334c13fa Leszek Koltunski
    mCenterZ = z;
119 4d5b37fe Leszek Koltunski
    }
120
121 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
122
123 4d5b37fe Leszek Koltunski
  public static void setSupportsRegion(boolean supports)
124
    {
125
126
    }
127 427ab7bf Leszek Koltunski
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
130 4d5b37fe Leszek Koltunski
  public static boolean supportsRegion()
131
    {
132
    return false;
133
    }
134
135 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
136 4d5b37fe Leszek Koltunski
137
  public static void setSupportsCenter(boolean supports)
138
    {
139
    mSupportsCenter = supports;
140
    }
141 427ab7bf Leszek Koltunski
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143
144 4d5b37fe Leszek Koltunski
  public static boolean supportsCenter()
145
    {
146
    return mSupportsCenter;
147
    }
148 427ab7bf Leszek Koltunski
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150
151 4d5b37fe Leszek Koltunski
  public Bitmap getBitmap()
152
    {
153
    Bitmap bmp;
154
155
    InputStream is = getResources().openRawResource(R.raw.grid);
156
157
    try
158
      {
159
      bmp = BitmapFactory.decodeStream(is);
160
      }
161
    finally
162 427ab7bf Leszek Koltunski
      {
163 4d5b37fe Leszek Koltunski
      try
164
        {
165
        is.close();
166
        }
167
      catch(IOException e) { }
168 427ab7bf Leszek Koltunski
      }
169
170 4d5b37fe Leszek Koltunski
    return bmp;
171
    }
172
173 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
174
175 4d5b37fe Leszek Koltunski
  public int getWidth()
176
    {
177
    return mObject==null ? 0: mObject.getWidth();
178
    }
179 427ab7bf Leszek Koltunski
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181
182 4d5b37fe Leszek Koltunski
  public int getHeight()
183
    {
184
    return mObject==null ? 0: mObject.getHeight();
185
    }
186 427ab7bf Leszek Koltunski
187 334c13fa Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
188
189
  public int getScreenWidth()
190
    {
191
    Matrix3DRenderer r = ((Matrix3DSurfaceView)findViewById(R.id.matrix3dSurfaceView)).getRenderer();
192
    return r.getWidth();
193
    }
194
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196
197
  public int getScreenHeight()
198
    {
199
    Matrix3DRenderer r = ((Matrix3DSurfaceView)findViewById(R.id.matrix3dSurfaceView)).getRenderer();
200
    return r.getHeight();
201
    }
202
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204
205
  public int getDepth()
206
    {
207
    return mObject==null ? 0: mObject.getDepth();
208
    }
209
210 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
211
212 4d5b37fe Leszek Koltunski
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
213
    {
214
    switch(parent.getId())
215 427ab7bf Leszek Koltunski
      {
216 4d5b37fe Leszek Koltunski
      case R.id.matrix3dspinner          : mEffectAdd = pos;
217
                                           break;
218 427ab7bf Leszek Koltunski
      }
219 4d5b37fe Leszek Koltunski
    }
220
221 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
222
223 4d5b37fe Leszek Koltunski
  public void onNothingSelected(AdapterView<?> parent)
224
    {
225
    }
226 427ab7bf Leszek Koltunski
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228
229 4d5b37fe Leszek Koltunski
  private void resetData()
230
    {
231
    mCenterX = 0.5f*getWidth();
232
    mCenterY = 0.5f*getHeight();
233 1d811f85 Leszek Koltunski
234 4d5b37fe Leszek Koltunski
    mSupportsCenter=false;
235
    }
236 427ab7bf Leszek Koltunski
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238
239 4d5b37fe Leszek Koltunski
  private void setEffectView()
240
    {
241
    resetData();
242
243
    final View view = getLayoutInflater().inflate(R.layout.matrix3dlayout, null);
244
245
    setContentView(view);
246
247
    String[] effects = new String[mEffectNames.length];
248
249
    for(int i=0; i<mEffectNames.length; i++) effects[i] = mEffectNames[i].name();
250
251
    Spinner effectSpinner = (Spinner)findViewById(R.id.matrix3dspinner );
252
    effectSpinner.setOnItemSelectedListener(this);
253
254
    ArrayAdapter<String> adapterEffect = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, effects);
255
    adapterEffect.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
256
    effectSpinner.setAdapter(adapterEffect);
257
258
    mEffectAdd = 0;
259
    }
260 427ab7bf Leszek Koltunski
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262 4d5b37fe Leszek Koltunski
263
  public void newEffect(View v)
264
    {
265
    Matrix3DEffect eff = new Matrix3DEffect(mEffectNames[mEffectAdd], this);
266
    mEffects.add(eff);
267
268
    LinearLayout layout = (LinearLayout)findViewById(R.id.matrix3dlayout);
269
    View view = eff.createView();
270
    layout.addView(view);
271
272
    if( mSupportsCenter )
273 427ab7bf Leszek Koltunski
      {
274 4d5b37fe Leszek Koltunski
      View center = eff.createCenter();
275
      layout.addView(center);
276 427ab7bf Leszek Koltunski
      }
277
278 4d5b37fe Leszek Koltunski
    eff.apply(mObject);
279
    }
280
281 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
282 4d5b37fe Leszek Koltunski
283
  public void removeAll(View v)
284
    {
285
    mEffects.clear();
286
    LinearLayout layout = (LinearLayout)findViewById(R.id.matrix3dlayout);
287
    layout.removeAllViews();
288
    mObject.abortEffects(EffectTypes.MATRIX);
289
290
    resetData();
291
    }
292
293 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
294 4d5b37fe Leszek Koltunski
295
  public void remove(View v)
296
    {
297
    for(Matrix3DEffect effect: mEffects)
298 427ab7bf Leszek Koltunski
      {
299 4d5b37fe Leszek Koltunski
      if( effect.thisView(v) )
300 427ab7bf Leszek Koltunski
        {
301 4d5b37fe Leszek Koltunski
        LinearLayout layout = (LinearLayout)findViewById(R.id.matrix3dlayout);
302
        View view;
303
304
        view = effect.getEffect();
305
        if( view!=null ) layout.removeView(view);
306
        view = effect.getCenter();
307
        if( view!=null ) layout.removeView(view);
308
        view = effect.getRegion();
309
        if( view!=null ) layout.removeView(view);
310
311
        long id = effect.getId();
312
        mObject.abortEffect(id);
313
        mEffects.remove(effect);
314
315
        resetData();
316
317
        break;
318 427ab7bf Leszek Koltunski
        }
319
      }
320 4d5b37fe Leszek Koltunski
    }
321 427ab7bf Leszek Koltunski
322
///////////////////////////////////////////////////////////////////////////////////////////////////
323 4d5b37fe Leszek Koltunski
// Overrides
324
325
  @Override
326
  protected void onPause()
327
    {
328
    GLSurfaceView mView = (GLSurfaceView)findViewById(R.id.matrix3dSurfaceView);
329
    if( mView!=null ) mView.onPause();
330
    super.onPause();
331
    }
332 427ab7bf Leszek Koltunski
333
///////////////////////////////////////////////////////////////////////////////////////////////////
334 4d5b37fe Leszek Koltunski
    
335
  @Override
336
  protected void onResume()
337
    {
338
    super.onResume();
339
    GLSurfaceView mView = (GLSurfaceView)findViewById(R.id.matrix3dSurfaceView);
340
    if( mView!=null ) mView.onResume();
341
    }
342 427ab7bf Leszek Koltunski
343 4d5b37fe Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
344 427ab7bf Leszek Koltunski
    
345 4d5b37fe Leszek Koltunski
  @Override
346
  protected void onDestroy()
347
    {
348
    Distorted.onDestroy();
349
    super.onDestroy();
350
    }
351
352
  }