Project

General

Profile

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

examples / src / main / java / org / distorted / examples / matrix3d / Matrix3DActivity.java @ 334c13fa

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.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

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

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

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
  @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
///////////////////////////////////////////////////////////////////////////////////////////////////
75

    
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
        {
94
        mEffectNames[numEffects++] = names[i];
95
        }
96
    }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

    
100
  public DistortedObject getObject()
101
    {
102
    return mObject;
103
    }
104

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106

    
107
  public void setRegion(float x, float y, float r)
108
    {
109

    
110
    }
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

    
114
  public void setCenter(float x, float y, float z)
115
    {
116
    mCenterX = x;
117
    mCenterY = y;
118
    mCenterZ = z;
119
    }
120

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

    
123
  public static void setSupportsRegion(boolean supports)
124
    {
125

    
126
    }
127

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

    
130
  public static boolean supportsRegion()
131
    {
132
    return false;
133
    }
134

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

    
137
  public static void setSupportsCenter(boolean supports)
138
    {
139
    mSupportsCenter = supports;
140
    }
141

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

    
144
  public static boolean supportsCenter()
145
    {
146
    return mSupportsCenter;
147
    }
148

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

    
151
  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
      {
163
      try
164
        {
165
        is.close();
166
        }
167
      catch(IOException e) { }
168
      }
169

    
170
    return bmp;
171
    }
172

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

    
175
  public int getWidth()
176
    {
177
    return mObject==null ? 0: mObject.getWidth();
178
    }
179

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

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

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
211

    
212
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
213
    {
214
    switch(parent.getId())
215
      {
216
      case R.id.matrix3dspinner          : mEffectAdd = pos;
217
                                           break;
218
      }
219
    }
220

    
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222

    
223
  public void onNothingSelected(AdapterView<?> parent)
224
    {
225
    }
226

    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228

    
229
  private void resetData()
230
    {
231
    mCenterX = 0.5f*getWidth();
232
    mCenterY = 0.5f*getHeight();
233

    
234
    mSupportsCenter=false;
235
    }
236

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

    
239
  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

    
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262

    
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
      {
274
      View center = eff.createCenter();
275
      layout.addView(center);
276
      }
277

    
278
    eff.apply(mObject);
279
    }
280

    
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282

    
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
///////////////////////////////////////////////////////////////////////////////////////////////////
294

    
295
  public void remove(View v)
296
    {
297
    for(Matrix3DEffect effect: mEffects)
298
      {
299
      if( effect.thisView(v) )
300
        {
301
        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
        }
319
      }
320
    }
321

    
322
///////////////////////////////////////////////////////////////////////////////////////////////////
323
// 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

    
333
///////////////////////////////////////////////////////////////////////////////////////////////////
334
    
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

    
343
///////////////////////////////////////////////////////////////////////////////////////////////////
344
    
345
  @Override
346
  protected void onDestroy()
347
    {
348
    Distorted.onDestroy();
349
    super.onDestroy();
350
    }
351

    
352
  }
(1-1/4)