Project

General

Profile

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

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

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;
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)
115
    {
116
    mCenterX = x;
117
    mCenterY = y;
118
    }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

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

    
125
    }
126

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

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

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

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

    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142

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

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

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

    
169
    return bmp;
170
    }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

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

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

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

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187

    
188
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
189
    {
190
    switch(parent.getId())
191
      {
192
      case R.id.matrix3dspinner          : mEffectAdd = pos;
193
                                           break;
194
      }
195
    }
196

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198

    
199
  public void onNothingSelected(AdapterView<?> parent)
200
    {
201
    }
202

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204

    
205
  private void resetData()
206
    {
207
    mCenterX = 0.5f*getWidth();
208
    mCenterY = 0.5f*getHeight();
209

    
210
    mSupportsCenter=false;
211
    }
212

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

    
215
  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

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

    
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
      {
250
      View center = eff.createCenter();
251
      layout.addView(center);
252
      }
253

    
254
    eff.apply(mObject);
255
    }
256

    
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258

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

    
271
  public void remove(View v)
272
    {
273
    for(Matrix3DEffect effect: mEffects)
274
      {
275
      if( effect.thisView(v) )
276
        {
277
        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
        }
295
      }
296
    }
297

    
298
///////////////////////////////////////////////////////////////////////////////////////////////////
299
// 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

    
309
///////////////////////////////////////////////////////////////////////////////////////////////////
310
    
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

    
319
///////////////////////////////////////////////////////////////////////////////////////////////////
320
    
321
  @Override
322
  protected void onDestroy()
323
    {
324
    Distorted.onDestroy();
325
    super.onDestroy();
326
    }
327

    
328
  }
(1-1/4)