Project

General

Profile

« Previous | Next » 

Revision 4d5b37fe

Added by Leszek Koltunski over 7 years ago

Unify Effects3D and Matrix3D (still incomplete!)

View differences:

src/main/java/org/distorted/examples/matrix3d/Matrix3DActivity.java
19 19

  
20 20
package org.distorted.examples.matrix3d;
21 21

  
22
import org.distorted.library.Distorted;
23
import org.distorted.examples.R;
24
import org.distorted.library.EffectNames;
25

  
26 22
import android.app.Activity;
27
import android.os.Bundle;
23
import android.graphics.Bitmap;
24
import android.graphics.BitmapFactory;
28 25
import android.opengl.GLSurfaceView;
26
import android.os.Bundle;
29 27
import android.view.View;
28
import android.widget.AdapterView;
29
import android.widget.ArrayAdapter;
30 30
import android.widget.LinearLayout;
31
import android.widget.SeekBar;
32
import android.widget.TextView;
33
import android.widget.SeekBar.OnSeekBarChangeListener;
31
import android.widget.Spinner;
34 32

  
35
///////////////////////////////////////////////////////////////////////////////////////////////////
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;
36 43

  
37
public class Matrix3DActivity extends Activity implements OnSeekBarChangeListener
38
{
39
    private TextView textMove, textScale, textRotate, textShear;
40
    private int moveX, moveY, moveZ;
41
    private int scaleX, scaleY, scaleZ;
42
    private int rotateX, rotateY, rotateZ, rotateA;
43
    private int shearX, shearY, shearZ;
44
    private int maxX, maxY, maxZ;
45
    private float fmoveX, fmoveY, fmoveZ;
46
    private float fscaleX, fscaleY, fscaleZ;
47
    private float frotateX, frotateY, frotateZ, frotateA;
48
    private float fshearX, fshearY, fshearZ;
49
    
50
    private EffectNames[] effects = new EffectNames[4];
51
    
52 44
///////////////////////////////////////////////////////////////////////////////////////////////////
53
    
54
    @Override
55
    protected void onCreate(Bundle icicle) 
56
      {
57
      super.onCreate(icicle);
58
      setContentView(R.layout.matrix3dlayout);
59
      Default(null);
60
      }
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;
61 57

  
62 58
///////////////////////////////////////////////////////////////////////////////////////////////////
63 59

  
64
    public void Default(View view)
65
      {
66
      effects[0] = EffectNames.MOVE;
67
      effects[1] = EffectNames.SCALE;
68
      effects[2] = EffectNames.ROTATE;
69
      effects[3] = EffectNames.SHEAR;
70
    
71
      moveX = 50;
72
      moveY = 50;
73
      moveZ = 50;
74
      
75
      scaleX = 50;
76
      scaleY = 50;
77
      scaleZ = 50;
78
      
79
      rotateX = 100;
80
      rotateY =  50;
81
      rotateZ =  50;
82
      rotateA =  50;
83
      
84
      shearX = 50;
85
      shearY = 50;
86
      shearZ = 50;
87
      
88
      addViews();
89
      }
90
    
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

  
91 74
///////////////////////////////////////////////////////////////////////////////////////////////////
92
    
93
    private void addViews()
94
      {
95
      LinearLayout layout = (LinearLayout)findViewById(R.id.matrix3dlayout);
96
    
97
      layout.removeAllViews();
98
      
99
      View move   = getLayoutInflater().inflate(R.layout.matrix3dmove, null);
100
      View scale  = getLayoutInflater().inflate(R.layout.matrix3dscale, null);
101
      View rotate = getLayoutInflater().inflate(R.layout.matrix3drotate, null);
102
      View shear  = getLayoutInflater().inflate(R.layout.matrix3dshear, null);
103
     
104
      for( int i=effects.length-1 ; i>=0 ; i-- )
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 )
105 93
        {
106
        switch(effects[i])
107
          {
108
          case MOVE  : layout.addView(move   , 0); break; 
109
          case SCALE : layout.addView(scale  , 0); break;
110
          case ROTATE: layout.addView(rotate , 0); break;
111
          case SHEAR : layout.addView(shear  , 0); break;
112
          }
94
        mEffectNames[numEffects++] = names[i];
113 95
        }
114
      
115
      textMove  = (TextView)findViewById(R.id.matrix3dmoveText);
116
      textScale = (TextView)findViewById(R.id.matrix3dscaleText);
117
      textRotate= (TextView)findViewById(R.id.matrix3drotateText);
118
      textShear = (TextView)findViewById(R.id.matrix3dshearText);
119
     
120
      setMoveText();
121
      setScaleText();
122
      setRotateText();
123
      setShearText();
124
      
125
      setBar(R.id.matrix3dmoveBar1, moveX);
126
      setBar(R.id.matrix3dmoveBar2, moveY);
127
      setBar(R.id.matrix3dmoveBar3, moveZ);
128
      
129
      setBar(R.id.matrix3dscaleBar1, scaleX);
130
      setBar(R.id.matrix3dscaleBar2, scaleY);
131
      setBar(R.id.matrix3dscaleBar3, scaleZ);
132
      
133
      setBar(R.id.matrix3drotateBar1, rotateX);
134
      setBar(R.id.matrix3drotateBar2, rotateY);
135
      setBar(R.id.matrix3drotateBar3, rotateZ);
136
      setBar(R.id.matrix3drotateBar4, rotateA);
137
      
138
      setBar(R.id.matrix3dshearBar1, shearX);
139
      setBar(R.id.matrix3dshearBar2, shearY);
140
      setBar(R.id.matrix3dshearBar3, shearZ);
141
      
142
      Matrix3DSurfaceView view = (Matrix3DSurfaceView) this.findViewById(R.id.matrix3dSurfaceView);
143
      view.getRenderer().setOrder(effects);
144
      }
96
    }
145 97

  
146 98
///////////////////////////////////////////////////////////////////////////////////////////////////
147 99

  
148
    private void moveUp(EffectNames name)
149
      {
150
      int len = effects.length-1;	
151
      int index = -1;
152
      
153
      for(int i=0; i<=len; i++)	
154
        {
155
        if( effects[i]==name )
156
          {
157
          index=i;
158
          break;
159
          }
160
        }
161
      
162
      if( index==0 )
163
        {
164
        for(int i=0; i<len; i++)
165
          effects[i] = effects[i+1];
166
    	
167
        effects[len] = name;
168
        }
169
      else if( index>0 )
170
        {
171
        effects[index]   = effects[index-1];
172
        effects[index-1] = name;
173
        }
174
      
175
      addViews();
176
      }
177
 
100
  public DistortedObject getObject()
101
    {
102
    return mObject;
103
    }
104

  
178 105
///////////////////////////////////////////////////////////////////////////////////////////////////
179 106

  
180
    public void ButtonMove(View v)
181
      {
182
      moveUp(EffectNames.MOVE);
183
      }
107
  public void setRegion(float x, float y, float r)
108
    {
109

  
110
    }
184 111

  
185 112
///////////////////////////////////////////////////////////////////////////////////////////////////
186 113

  
187
    public void ButtonScale(View v)
188
      {
189
      moveUp(EffectNames.SCALE);
190
      }
191
    
114
  public void setCenter(float x, float y)
115
    {
116
    mCenterX = x;
117
    mCenterY = y;
118
    }
119

  
192 120
///////////////////////////////////////////////////////////////////////////////////////////////////
193 121

  
194
    public void ButtonRotate(View v)
195
      {
196
      moveUp(EffectNames.ROTATE);
197
      }
122
  public static void setSupportsRegion(boolean supports)
123
    {
124

  
125
    }
198 126

  
199 127
///////////////////////////////////////////////////////////////////////////////////////////////////
200 128

  
201
    public void ButtonShear(View v)
202
      {
203
      moveUp(EffectNames.SHEAR);
204
      }
205
 
129
  public static boolean supportsRegion()
130
    {
131
    return false;
132
    }
133

  
206 134
///////////////////////////////////////////////////////////////////////////////////////////////////
207
    
208
    private void setBar(int id, int value)
209
      {
210
      SeekBar bar = (SeekBar)findViewById(id);
211
      bar.setOnSeekBarChangeListener(this); 
212
      bar.setProgress(value);
213
      }
135

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

  
215 141
///////////////////////////////////////////////////////////////////////////////////////////////////
216 142

  
217
    private void computeMove()
218
      {
219
      fmoveX = (moveX-50)*maxX/50.0f;
220
      fmoveY = (moveY-50)*maxY/50.0f;
221
      fmoveZ = (moveZ-50)*maxZ/50.0f;
143
  public static boolean supportsCenter()
144
    {
145
    return mSupportsCenter;
146
    }
222 147

  
223
      Matrix3DSurfaceView view = (Matrix3DSurfaceView) this.findViewById(R.id.matrix3dSurfaceView);
224
      view.getRenderer().setMove( fmoveX, fmoveY, fmoveZ);
225
      }
226
    
227 148
///////////////////////////////////////////////////////////////////////////////////////////////////
228 149

  
229
    private void setMoveText()
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
230 161
      {
231
      fmoveX = ((int)(100*fmoveX))/100.0f;
232
      fmoveY = ((int)(100*fmoveY))/100.0f;
233
      fmoveZ = ((int)(100*fmoveZ))/100.0f;
234
      
235
      textMove.setText("move("+fmoveX+" , "+fmoveY+" , "+fmoveZ+")");
162
      try
163
        {
164
        is.close();
165
        }
166
      catch(IOException e) { }
236 167
      }
237 168

  
169
    return bmp;
170
    }
171

  
238 172
///////////////////////////////////////////////////////////////////////////////////////////////////
239 173

  
240
    private void computeScale()
241
      {
242
      fscaleX = (scaleX>50 ? 0.18f : 0.018f)*(scaleX-50)+1;
243
      fscaleY = (scaleY>50 ? 0.18f : 0.018f)*(scaleY-50)+1;
244
      fscaleZ = (scaleZ>50 ? 0.18f : 0.018f)*(scaleZ-50)+1;
174
  public int getWidth()
175
    {
176
    return mObject==null ? 0: mObject.getWidth();
177
    }
245 178

  
246
      Matrix3DSurfaceView view = (Matrix3DSurfaceView) this.findViewById(R.id.matrix3dSurfaceView);
247
      view.getRenderer().setScale(fscaleX, fscaleY, fscaleZ);
248
      }
249
    
250 179
///////////////////////////////////////////////////////////////////////////////////////////////////
251 180

  
252
    private void setScaleText()
253
      {
254
      fscaleX = ((int)(100*fscaleX))/100.0f;
255
      fscaleY = ((int)(100*fscaleY))/100.0f;
256
      fscaleZ = ((int)(100*fscaleZ))/100.0f;
257
      
258
      textScale.setText("scale("+fscaleX+" , "+fscaleY+" , "+fscaleZ+")");
259
      }
181
  public int getHeight()
182
    {
183
    return mObject==null ? 0: mObject.getHeight();
184
    }
260 185

  
261 186
///////////////////////////////////////////////////////////////////////////////////////////////////
262 187

  
263
    private void computeRotate()
188
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
189
    {
190
    switch(parent.getId())
264 191
      {
265
      frotateX = (rotateX-50)/50.0f;
266
      frotateY = (rotateY-50)/50.0f;
267
      frotateZ = (rotateZ-50)/50.0f;
268
    
269
      Matrix3DSurfaceView view = (Matrix3DSurfaceView) this.findViewById(R.id.matrix3dSurfaceView);
270
      view.getRenderer().setRotate( frotateA, frotateX, frotateY, frotateZ );
192
      case R.id.matrix3dspinner          : mEffectAdd = pos;
193
                                           break;
271 194
      }
272
    
195
    }
196

  
273 197
///////////////////////////////////////////////////////////////////////////////////////////////////
274 198

  
275
    private void setRotateText()
276
      {
277
      frotateX = ((int)(100*frotateX))/100.0f;
278
      frotateY = ((int)(100*frotateY))/100.0f;
279
      frotateZ = ((int)(100*frotateZ))/100.0f;
280
      
281
      frotateA = ((rotateA-50)*180)/50;
282
      
283
      textRotate.setText("rotate( "+frotateA+" ("+frotateX+","+frotateY+","+frotateZ+") )");
284
      }
199
  public void onNothingSelected(AdapterView<?> parent)
200
    {
201
    }
285 202

  
286 203
///////////////////////////////////////////////////////////////////////////////////////////////////
287 204

  
288
    private void computeShear()
289
      {
290
      fshearX = (shearX-50)/25.0f;
291
      fshearY = (shearY-50)/25.0f;
292
      fshearZ = (shearZ-50)/25.0f;
205
  private void resetData()
206
    {
207
    mCenterX = 0.5f*getWidth();
208
    mCenterY = 0.5f*getHeight();
293 209

  
294
    	Matrix3DSurfaceView view = (Matrix3DSurfaceView) this.findViewById(R.id.matrix3dSurfaceView);
295
      view.getRenderer().setShear( fshearX, fshearY, fshearZ );
296
      }
297
    
298
///////////////////////////////////////////////////////////////////////////////////////////////////
210
    mSupportsCenter=false;
211
    }
299 212

  
300
    private void setShearText()
301
      {
302
      fshearX = ((int)(100*fshearX))/100.0f;
303
      fshearY = ((int)(100*fshearY))/100.0f;
304
      fshearZ = ((int)(100*fshearZ))/100.0f;
305
      
306
      textShear.setText("shear("+fshearX+" , "+fshearY+" , "+fshearZ+")");	
307
      }
308
   
309 213
///////////////////////////////////////////////////////////////////////////////////////////////////
310
    
311
    @Override
312
    protected void onPause() 
313
      {
314
      GLSurfaceView view = (GLSurfaceView) this.findViewById(R.id.matrix3dSurfaceView);
315
      view.onPause();
316
      super.onPause();
317
      }
318 214

  
319
///////////////////////////////////////////////////////////////////////////////////////////////////
320
    
321
    @Override
322
    protected void onResume() 
323
      {
324
      super.onResume();
325
      GLSurfaceView view = (GLSurfaceView) this.findViewById(R.id.matrix3dSurfaceView);
326
      view.onResume();
327
      }
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
    }
328 236

  
329 237
///////////////////////////////////////////////////////////////////////////////////////////////////
330
    
331
    @Override
332
    public void onWindowFocusChanged(boolean hasFocus) 
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 )
333 249
      {
334
      super.onWindowFocusChanged(hasFocus);
335
  
336
      GLSurfaceView view = (GLSurfaceView) this.findViewById(R.id.matrix3dSurfaceView);
337
      
338
      maxX = view.getWidth();
339
      maxY = view.getHeight();
340
      maxZ = (maxX+maxY)/2;
250
      View center = eff.createCenter();
251
      layout.addView(center);
341 252
      }
342 253

  
254
    eff.apply(mObject);
255
    }
256

  
343 257
///////////////////////////////////////////////////////////////////////////////////////////////////
344
    
345
    @Override
346
    protected void onDestroy() 
347
      {
348
      Distorted.onDestroy();  
349
      super.onDestroy();
350
      }
351
    
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

  
352 269
///////////////////////////////////////////////////////////////////////////////////////////////////
353
    
354
    public void onProgressChanged(SeekBar bar, int progress, boolean fromUser) 
270

  
271
  public void remove(View v)
272
    {
273
    for(Matrix3DEffect effect: mEffects)
355 274
      {
356
      switch (bar.getId()) 
275
      if( effect.thisView(v) )
357 276
        {
358
        case R.id.matrix3dmoveBar1  : moveX = progress; computeMove()  ; setMoveText()  ; break;
359
        case R.id.matrix3dmoveBar2  : moveY = progress; computeMove()  ; setMoveText()  ; break;
360
        case R.id.matrix3dmoveBar3  : moveZ = progress; computeMove()  ; setMoveText()  ; break;
361
        
362
        case R.id.matrix3dscaleBar1 : scaleX= progress; computeScale() ; setScaleText() ; break;
363
        case R.id.matrix3dscaleBar2 : scaleY= progress; computeScale() ; setScaleText() ; break;
364
        case R.id.matrix3dscaleBar3 : scaleZ= progress; computeScale() ; setScaleText() ; break;
365
        
366
        case R.id.matrix3drotateBar1: rotateX=progress; computeRotate(); setRotateText(); break;
367
        case R.id.matrix3drotateBar2: rotateY=progress; computeRotate(); setRotateText(); break;
368
        case R.id.matrix3drotateBar3: rotateZ=progress; computeRotate(); setRotateText(); break;
369
        case R.id.matrix3drotateBar4: rotateA=progress; computeRotate(); setRotateText(); break;
370
        
371
        case R.id.matrix3dshearBar1 : shearX= progress; computeShear() ; setShearText() ; break;
372
        case R.id.matrix3dshearBar2 : shearY= progress; computeShear() ; setShearText() ; break;
373
        case R.id.matrix3dshearBar3 : shearZ= progress; computeShear() ; setShearText() ; break;
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;
374 294
        }
375 295
      }
296
    }
376 297

  
377 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
    }
378 308

  
379
    public void onStartTrackingTouch(SeekBar bar) { }
380
    
381 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
    }
382 318

  
383
    public void onStopTrackingTouch(SeekBar bar)  { }
319
///////////////////////////////////////////////////////////////////////////////////////////////////
384 320
    
385
}
321
  @Override
322
  protected void onDestroy()
323
    {
324
    Distorted.onDestroy();
325
    super.onDestroy();
326
    }
327

  
328
  }

Also available in: Unified diff