Project

General

Profile

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

examples / src / main / java / org / distorted / examples / matrix3d / Matrix3DActivity.java @ 4d5b37fe

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
  private float mCenterX, mCenterY;
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 4d5b37fe Leszek Koltunski
  public void setCenter(float x, float y)
115
    {
116
    mCenterX = x;
117
    mCenterY = y;
118
    }
119
120 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
121
122 4d5b37fe Leszek Koltunski
  public static void setSupportsRegion(boolean supports)
123
    {
124
125
    }
126 427ab7bf Leszek Koltunski
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128
129 4d5b37fe Leszek Koltunski
  public static boolean supportsRegion()
130
    {
131
    return false;
132
    }
133
134 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
135 4d5b37fe Leszek Koltunski
136
  public static void setSupportsCenter(boolean supports)
137
    {
138
    mSupportsCenter = supports;
139
    }
140 427ab7bf Leszek Koltunski
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142
143 4d5b37fe Leszek Koltunski
  public static boolean supportsCenter()
144
    {
145
    return mSupportsCenter;
146
    }
147 427ab7bf Leszek Koltunski
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149
150 4d5b37fe Leszek Koltunski
  public Bitmap getBitmap()
151
    {
152
    Bitmap bmp;
153
154
    InputStream is = getResources().openRawResource(R.raw.grid);
155
156
    try
157
      {
158
      bmp = BitmapFactory.decodeStream(is);
159
      }
160
    finally
161 427ab7bf Leszek Koltunski
      {
162 4d5b37fe Leszek Koltunski
      try
163
        {
164
        is.close();
165
        }
166
      catch(IOException e) { }
167 427ab7bf Leszek Koltunski
      }
168
169 4d5b37fe Leszek Koltunski
    return bmp;
170
    }
171
172 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
173
174 4d5b37fe Leszek Koltunski
  public int getWidth()
175
    {
176
    return mObject==null ? 0: mObject.getWidth();
177
    }
178 427ab7bf Leszek Koltunski
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180
181 4d5b37fe Leszek Koltunski
  public int getHeight()
182
    {
183
    return mObject==null ? 0: mObject.getHeight();
184
    }
185 427ab7bf Leszek Koltunski
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187
188 4d5b37fe Leszek Koltunski
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
189
    {
190
    switch(parent.getId())
191 427ab7bf Leszek Koltunski
      {
192 4d5b37fe Leszek Koltunski
      case R.id.matrix3dspinner          : mEffectAdd = pos;
193
                                           break;
194 427ab7bf Leszek Koltunski
      }
195 4d5b37fe Leszek Koltunski
    }
196
197 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
198
199 4d5b37fe Leszek Koltunski
  public void onNothingSelected(AdapterView<?> parent)
200
    {
201
    }
202 427ab7bf Leszek Koltunski
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204
205 4d5b37fe Leszek Koltunski
  private void resetData()
206
    {
207
    mCenterX = 0.5f*getWidth();
208
    mCenterY = 0.5f*getHeight();
209 1d811f85 Leszek Koltunski
210 4d5b37fe Leszek Koltunski
    mSupportsCenter=false;
211
    }
212 427ab7bf Leszek Koltunski
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214
215 4d5b37fe Leszek Koltunski
  private void setEffectView()
216
    {
217
    resetData();
218
219
    final View view = getLayoutInflater().inflate(R.layout.matrix3dlayout, null);
220
221
    setContentView(view);
222
223
    String[] effects = new String[mEffectNames.length];
224
225
    for(int i=0; i<mEffectNames.length; i++) effects[i] = mEffectNames[i].name();
226
227
    Spinner effectSpinner = (Spinner)findViewById(R.id.matrix3dspinner );
228
    effectSpinner.setOnItemSelectedListener(this);
229
230
    ArrayAdapter<String> adapterEffect = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, effects);
231
    adapterEffect.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
232
    effectSpinner.setAdapter(adapterEffect);
233
234
    mEffectAdd = 0;
235
    }
236 427ab7bf Leszek Koltunski
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238 4d5b37fe Leszek Koltunski
239
  public void newEffect(View v)
240
    {
241
    Matrix3DEffect eff = new Matrix3DEffect(mEffectNames[mEffectAdd], this);
242
    mEffects.add(eff);
243
244
    LinearLayout layout = (LinearLayout)findViewById(R.id.matrix3dlayout);
245
    View view = eff.createView();
246
    layout.addView(view);
247
248
    if( mSupportsCenter )
249 427ab7bf Leszek Koltunski
      {
250 4d5b37fe Leszek Koltunski
      View center = eff.createCenter();
251
      layout.addView(center);
252 427ab7bf Leszek Koltunski
      }
253
254 4d5b37fe Leszek Koltunski
    eff.apply(mObject);
255
    }
256
257 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
258 4d5b37fe Leszek Koltunski
259
  public void removeAll(View v)
260
    {
261
    mEffects.clear();
262
    LinearLayout layout = (LinearLayout)findViewById(R.id.matrix3dlayout);
263
    layout.removeAllViews();
264
    mObject.abortEffects(EffectTypes.MATRIX);
265
266
    resetData();
267
    }
268
269 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
270 4d5b37fe Leszek Koltunski
271
  public void remove(View v)
272
    {
273
    for(Matrix3DEffect effect: mEffects)
274 427ab7bf Leszek Koltunski
      {
275 4d5b37fe Leszek Koltunski
      if( effect.thisView(v) )
276 427ab7bf Leszek Koltunski
        {
277 4d5b37fe Leszek Koltunski
        LinearLayout layout = (LinearLayout)findViewById(R.id.matrix3dlayout);
278
        View view;
279
280
        view = effect.getEffect();
281
        if( view!=null ) layout.removeView(view);
282
        view = effect.getCenter();
283
        if( view!=null ) layout.removeView(view);
284
        view = effect.getRegion();
285
        if( view!=null ) layout.removeView(view);
286
287
        long id = effect.getId();
288
        mObject.abortEffect(id);
289
        mEffects.remove(effect);
290
291
        resetData();
292
293
        break;
294 427ab7bf Leszek Koltunski
        }
295
      }
296 4d5b37fe Leszek Koltunski
    }
297 427ab7bf Leszek Koltunski
298
///////////////////////////////////////////////////////////////////////////////////////////////////
299 4d5b37fe Leszek Koltunski
// Overrides
300
301
  @Override
302
  protected void onPause()
303
    {
304
    GLSurfaceView mView = (GLSurfaceView)findViewById(R.id.matrix3dSurfaceView);
305
    if( mView!=null ) mView.onPause();
306
    super.onPause();
307
    }
308 427ab7bf Leszek Koltunski
309
///////////////////////////////////////////////////////////////////////////////////////////////////
310 4d5b37fe Leszek Koltunski
    
311
  @Override
312
  protected void onResume()
313
    {
314
    super.onResume();
315
    GLSurfaceView mView = (GLSurfaceView)findViewById(R.id.matrix3dSurfaceView);
316
    if( mView!=null ) mView.onResume();
317
    }
318 427ab7bf Leszek Koltunski
319 4d5b37fe Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
320 427ab7bf Leszek Koltunski
    
321 4d5b37fe Leszek Koltunski
  @Override
322
  protected void onDestroy()
323
    {
324
    Distorted.onDestroy();
325
    super.onDestroy();
326
    }
327
328
  }