Project

General

Profile

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

examples / src / main / java / org / distorted / examples / generic / GenericActivity2.java @ 1e7603bb

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.generic;
21

    
22
import android.graphics.Bitmap;
23
import android.graphics.BitmapFactory;
24
import android.graphics.Canvas;
25
import android.graphics.Paint;
26
import android.opengl.GLSurfaceView;
27
import android.os.Bundle;
28
import android.support.design.widget.TabLayout;
29
import android.support.v7.app.AppCompatActivity;
30
import android.view.View;
31
import android.widget.CheckBox;
32

    
33
import org.distorted.examples.R;
34
import org.distorted.library.effect.EffectType;
35
import org.distorted.library.main.Distorted;
36
import org.distorted.library.main.DistortedEffects;
37
import org.distorted.library.main.DistortedTexture;
38
import org.distorted.library.mesh.MeshCubes;
39
import org.distorted.library.mesh.MeshFlat;
40
import org.distorted.library.mesh.MeshBase;
41
import org.distorted.library.mesh.MeshQuad;
42
import org.distorted.library.mesh.MeshSphere;
43
import org.distorted.library.type.Static3D;
44
import org.distorted.library.type.Static4D;
45

    
46
import java.io.IOException;
47
import java.io.InputStream;
48

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

    
51
public class GenericActivity2 extends AppCompatActivity
52
  {
53
  private DistortedTexture mTexture;
54
  private MeshBase mMesh;
55
  private Bitmap mBitmap;
56
  private DistortedEffects mEffects;
57
  private GenericViewPager mViewPager;
58

    
59
  private boolean mShowCenter, mShowRegion, mShowNormal, mUseOIT;
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
  @Override
64
  protected void onCreate(Bundle savedState)
65
    {
66
    super.onCreate(savedState);
67

    
68
    setTheme(R.style.CustomActivityThemeNoActionBar);
69

    
70
    Bundle b = getIntent().getExtras();
71

    
72
    int objectType = b.getInt("type");
73
    int numCols    = b.getInt("cols");
74
    int numRows    = b.getInt("rows");
75
    int numSlic    = b.getInt("slices");
76
    int bitmapID   = b.getInt("bitmap");
77
    String str     = b.getString("string");
78

    
79
    mShowCenter = false;
80
    mShowRegion = false;
81
    mShowNormal = false;
82
    mUseOIT     = false;
83

    
84
    int maxsize = numCols > numRows ? (numCols>numSlic ? numCols:numSlic) : (numRows>numSlic ? numRows:numSlic) ;
85

    
86
    createBitmap(maxsize, bitmapID);
87

    
88
    switch(objectType)
89
      {
90
      case 0: if( bitmapID!=-1 )
91
                {
92
                mMesh = new MeshCubes(numCols, str, numSlic);
93
                }
94
              else
95
                {
96
                Static4D mapFB = new Static4D(0.0f,0.0f, (float)numCols/maxsize, (float)numRows/maxsize);
97
                Static4D mapLR = new Static4D(0.0f,0.0f, (float)numSlic/maxsize, (float)numRows/maxsize);
98
                Static4D mapTB = new Static4D(0.0f,0.0f, (float)numCols/maxsize, (float)numSlic/maxsize);
99

    
100
                mMesh = new MeshCubes(numCols, str, numSlic, mapFB, mapFB, mapLR, mapLR, mapTB, mapTB);
101
                }
102
              break;
103
      case 1: mMesh = new MeshFlat(numCols,numRows);
104
              break;
105
      case 2: mMesh = new MeshSphere(numRows);
106
              break;
107
      case 3: mMesh = new MeshQuad();
108
              break;
109
      }
110

    
111
    mMesh.setShowNormals(mShowNormal);
112
    mTexture= new DistortedTexture(numCols,numRows);
113
    mEffects= new DistortedEffects();
114

    
115
    final View view = getLayoutInflater().inflate(R.layout.genericlayout, null);
116

    
117
    setContentView(view);
118

    
119
    mViewPager = new GenericViewPager(this,R.id.generic_viewpager);
120
    TabLayout tabLayout = findViewById(R.id.generic_sliding_tabs);
121
    tabLayout.setupWithViewPager(mViewPager.getPager());
122
    }
123

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

    
126
  private void createBitmap(int size, int bitmapID)
127
    {
128
    if( bitmapID!=-1)
129
      {
130
      InputStream is = getResources().openRawResource(bitmapID);
131

    
132
      try
133
        {
134
        mBitmap = BitmapFactory.decodeStream(is);
135
        }
136
      finally
137
        {
138
        try
139
          {
140
          is.close();
141
          }
142
        catch(IOException e) { }
143
        }
144
      }
145
    else
146
      {
147
      final int T = 64;
148
      final int S = T*size;
149

    
150
      Paint paint = new Paint();
151
      mBitmap = Bitmap.createBitmap(S,S, Bitmap.Config.ARGB_8888);
152
      Canvas canvas = new Canvas(mBitmap);
153

    
154
      paint.setAntiAlias(true);
155
      paint.setTextAlign(Paint.Align.CENTER);
156
      paint.setColor(0xff008800);
157
      paint.setStyle(Paint.Style.FILL);
158
      canvas.drawRect(0, 0, S, S, paint);
159
      paint.setColor(0xffffffff);
160

    
161
      for(int i=0; i<=size ; i++ )
162
        {
163
        canvas.drawRect( T*i-1, 0, T*i+1, S, paint);
164
        canvas.drawRect( 0, T*i-1, S, T*i+1, paint);
165
        }
166
      }
167
    }
168

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170

    
171
  public DistortedTexture getTexture()
172
    {
173
    return mTexture;
174
    }
175

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177

    
178
  public MeshBase getMesh()
179
    {
180
    return mMesh;
181
    }
182

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184

    
185
  public Bitmap getBitmap()
186
    {
187
    return mBitmap;
188
    }
189

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191

    
192
  public int getWidth()
193
    {
194
    return mTexture==null ? 0: mTexture.getWidth();
195
    }
196

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

    
199
  public int getHeight()
200
    {
201
    return mTexture==null ? 0: mTexture.getHeight();
202
    }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

    
206
  public int getDepth()
207
    {
208
    return mTexture==null ? 0: mTexture.getDepth(mMesh);
209
    }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

    
213
  public int getScreenWidth()
214
    {
215
    GenericRenderer r = ((GenericSurfaceView)findViewById(R.id.genericSurfaceView)).getRenderer();
216
    return r.getWidth();
217
    }
218

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

    
221
  void setCenter(Static3D center)
222
    {
223
    GenericSurfaceView view = findViewById(R.id.genericSurfaceView);
224
    view.getRenderer().setCenter( center.get1(), center.get2(), center.get3() );
225
    }
226

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

    
229
  void setRegion(Static4D region)
230
    {
231
    GenericSurfaceView view = findViewById(R.id.genericSurfaceView);
232
    view.getRenderer().setRegion( region.get1(), region.get2(), region.get3(), region.get4() );
233
    }
234

    
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236

    
237
  void resetMatrixEffects()
238
    {
239
    GenericSurfaceView view = findViewById(R.id.genericSurfaceView);
240
    GenericRenderer renderer= view.getRenderer();
241

    
242
    renderer.resetMatrixEffects();
243
    }
244

    
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246

    
247
  void setCurrentTab(EffectType type)
248
    {
249
    mViewPager.setCurrentTab(type);
250
    }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253

    
254
  void setTab(GenericTab tab, int position)
255
    {
256
    mViewPager.setTab(tab,position);
257
    }
258

    
259
///////////////////////////////////////////////////////////////////////////////////////////////////
260

    
261
  public DistortedEffects getEffects()
262
    {
263
    return mEffects;
264
    }
265

    
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267

    
268
  public void newEffect(View v)
269
    {
270
    mViewPager.newEffect();
271
    }
272

    
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274

    
275
  public void removeAll(View v)
276
    {
277
    mViewPager.removeAll();
278
    }
279

    
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281

    
282
  public void remove(View v)
283
    {
284
    mViewPager.remove(v);
285
    }
286

    
287
///////////////////////////////////////////////////////////////////////////////////////////////////
288

    
289
  public void showRegionAndCenter(View view)
290
    {
291
    CheckBox center = findViewById(R.id.genericCheckBoxCenter);
292
    mShowCenter = center.isChecked();
293
    CheckBox region = findViewById(R.id.genericCheckBoxRegion);
294
    mShowRegion = region.isChecked();
295

    
296
    GenericSurfaceView sv = findViewById(R.id.genericSurfaceView);
297

    
298
    mViewPager.showRegionAndCenter(mShowRegion, mShowCenter, sv);
299
    }
300

    
301
///////////////////////////////////////////////////////////////////////////////////////////////////
302

    
303
  public void showNormal(View view)
304
    {
305
    CheckBox box = (CheckBox)view;
306
    mShowNormal = box.isChecked();
307

    
308
    if ( mMesh!=null )
309
      {
310
      mMesh.setShowNormals(mShowNormal);
311
      }
312
    }
313

    
314
///////////////////////////////////////////////////////////////////////////////////////////////////
315

    
316
  public void triggerOIT(View view)
317
    {
318
    CheckBox box = (CheckBox)view;
319
    mUseOIT = box.isChecked();
320

    
321
    GenericSurfaceView sv = findViewById(R.id.genericSurfaceView);
322
    sv.getRenderer().useOIT(mUseOIT);
323
    }
324

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326
// Overrides
327
///////////////////////////////////////////////////////////////////////////////////////////////////
328

    
329
  @Override
330
  protected void onPause()
331
    {
332
    GLSurfaceView mView = findViewById(R.id.genericSurfaceView);
333
    if( mView!=null ) mView.onPause();
334

    
335
    Distorted.onPause();
336
    super.onPause();
337
    }
338

    
339
///////////////////////////////////////////////////////////////////////////////////////////////////
340
    
341
  @Override
342
  protected void onResume()
343
    {
344
    super.onResume();
345
    GLSurfaceView mView = findViewById(R.id.genericSurfaceView);
346
    if( mView!=null ) mView.onResume();
347
    }
348

    
349
///////////////////////////////////////////////////////////////////////////////////////////////////
350
    
351
  @Override
352
  protected void onDestroy()
353
    {
354
    Distorted.onDestroy();
355
    super.onDestroy();
356
    }
357

    
358
  }
(2-2/7)