Project

General

Profile

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

examples / src / main / java / org / distorted / examples / generic / GenericActivity2.java @ 698ad0a8

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.main.DistortedLibrary;
35
import org.distorted.library.main.DistortedEffects;
36
import org.distorted.library.main.DistortedTexture;
37
import org.distorted.library.mesh.MeshCubes;
38
import org.distorted.library.mesh.MeshRectangles;
39
import org.distorted.library.mesh.MeshBase;
40
import org.distorted.library.mesh.MeshQuad;
41
import org.distorted.library.mesh.MeshSphere;
42
import org.distorted.library.type.Static3D;
43
import org.distorted.library.type.Static4D;
44

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

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

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

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

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

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

    
67
    setTheme(R.style.CustomActivityThemeNoActionBar);
68

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

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

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

    
83
    mEffects= new DistortedEffects();
84

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

    
87
    createBitmap(maxsize, bitmapID);
88

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

    
101
                mMesh = new MeshCubes(numCols, str, numSlic, mapFB, mapFB, mapLR, mapLR, mapTB, mapTB);
102
                }
103
              mMesh.setStretch(numCols,numRows,numSlic);
104
              break;
105
      case 1: mMesh = new MeshRectangles(numCols,numRows);
106
              mMesh.setStretch(numCols,numRows,0);
107
              break;
108
      case 2: mMesh = new MeshSphere(numRows);
109
              mMesh.setStretch(numRows,numRows,numRows);
110
              break;
111
      case 3: mMesh = new MeshQuad();
112
              mMesh.setStretch(1,1,0);
113
              break;
114
      }
115

    
116
    mMesh.setShowNormals(mShowNormal);
117
    mTexture= new DistortedTexture();
118

    
119
    final View view = getLayoutInflater().inflate(R.layout.genericlayout, null);
120

    
121
    setContentView(view);
122

    
123
    mViewPager = new GenericViewPager(this,R.id.generic_viewpager);
124
    TabLayout tabLayout = findViewById(R.id.generic_sliding_tabs);
125
    tabLayout.setupWithViewPager(mViewPager.getPager());
126
    }
127

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

    
130
  private void createBitmap(int size, int bitmapID)
131
    {
132
    if( bitmapID!=-1)
133
      {
134
      InputStream is = getResources().openRawResource(bitmapID);
135

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

    
154
      Paint paint = new Paint();
155
      mBitmap = Bitmap.createBitmap(S,S, Bitmap.Config.ARGB_8888);
156
      Canvas canvas = new Canvas(mBitmap);
157

    
158
      paint.setAntiAlias(true);
159
      paint.setTextAlign(Paint.Align.CENTER);
160
      paint.setColor(0xff008800);
161
      paint.setStyle(Paint.Style.FILL);
162
      canvas.drawRect(0, 0, S, S, paint);
163
      paint.setColor(0xffffffff);
164

    
165
      for(int i=0; i<=size ; i++ )
166
        {
167
        canvas.drawRect( T*i-1, 0, T*i+1, S, paint);
168
        canvas.drawRect( 0, T*i-1, S, T*i+1, paint);
169
        }
170
      }
171
    }
172

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

    
175
  public DistortedTexture getTexture()
176
    {
177
    return mTexture;
178
    }
179

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

    
182
  public MeshBase getMesh()
183
    {
184
    return mMesh;
185
    }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

    
189
  public Bitmap getBitmap()
190
    {
191
    return mBitmap;
192
    }
193

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

    
196
  public DistortedEffects getEffects()
197
    {
198
    return mEffects;
199
    }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

    
203
  public GenericViewPager getPager()
204
    {
205
    return mViewPager;
206
    }
207

    
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209

    
210
  public float getWidth()
211
    {
212
    return mMesh==null ? 0: mMesh.getStretchX();
213
    }
214

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

    
217
  public float getHeight()
218
    {
219
    return mMesh==null ? 0: mMesh.getStretchY();
220
    }
221

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

    
224
  public float getDepth()
225
    {
226
    return mMesh==null ? 0: mMesh.getStretchZ();
227
    }
228

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

    
231
  public int getScreenWidth()
232
    {
233
    GenericRenderer r = ((GenericSurfaceView)findViewById(R.id.genericSurfaceView)).getRenderer();
234
    return r.getWidth();
235
    }
236

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

    
239
  void setCenter(Static3D center)
240
    {
241
    GenericSurfaceView view = findViewById(R.id.genericSurfaceView);
242
    view.getRenderer().setCenter( center.get0(), center.get1(), center.get2() );
243
    }
244

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

    
247
  void setRegion(Static4D region)
248
    {
249
    GenericSurfaceView view = findViewById(R.id.genericSurfaceView);
250
    view.getRenderer().setRegion( region.get0(), region.get1(), region.get2(), region.get3() );
251
    }
252

    
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254

    
255
  public void showRegionAndCenter(View view)
256
    {
257
    CheckBox center = findViewById(R.id.genericCheckBoxCenter);
258
    mShowCenter = center.isChecked();
259
    CheckBox region = findViewById(R.id.genericCheckBoxRegion);
260
    mShowRegion = region.isChecked();
261

    
262
    GenericSurfaceView sv = findViewById(R.id.genericSurfaceView);
263

    
264
    mViewPager.showRegionAndCenter(mShowRegion, mShowCenter, sv);
265
    }
266

    
267
///////////////////////////////////////////////////////////////////////////////////////////////////
268

    
269
  public void showNormal(View view)
270
    {
271
    CheckBox box = (CheckBox)view;
272
    mShowNormal = box.isChecked();
273

    
274
    if ( mMesh!=null )
275
      {
276
      mMesh.setShowNormals(mShowNormal);
277
      }
278
    }
279

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

    
282
  public void triggerOIT(View view)
283
    {
284
    CheckBox box = (CheckBox)view;
285
    mUseOIT = box.isChecked();
286

    
287
    GenericSurfaceView sv = findViewById(R.id.genericSurfaceView);
288
    sv.getRenderer().useOIT(mUseOIT);
289
    }
290

    
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292
// Overrides
293
///////////////////////////////////////////////////////////////////////////////////////////////////
294

    
295
  @Override
296
  protected void onPause()
297
    {
298
    GLSurfaceView mView = findViewById(R.id.genericSurfaceView);
299
    if( mView!=null ) mView.onPause();
300

    
301
    DistortedLibrary.onPause();
302
    super.onPause();
303
    }
304

    
305
///////////////////////////////////////////////////////////////////////////////////////////////////
306
    
307
  @Override
308
  protected void onResume()
309
    {
310
    super.onResume();
311
    GLSurfaceView mView = findViewById(R.id.genericSurfaceView);
312
    if( mView!=null ) mView.onResume();
313
    }
314

    
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316
    
317
  @Override
318
  protected void onDestroy()
319
    {
320
    DistortedLibrary.onDestroy();
321
    super.onDestroy();
322
    }
323
  }
(2-2/7)