Project

General

Profile

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

examples / src / main / java / org / distorted / examples / generic / GenericActivity2.java @ 61420934

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 com.google.android.material.tabs.TabLayout;
29
import androidx.appcompat.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.MeshBase;
38
import org.distorted.library.type.Static3D;
39
import org.distorted.library.type.Static4D;
40

    
41
import java.io.IOException;
42
import java.io.InputStream;
43

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

    
46
public class GenericActivity2 extends AppCompatActivity
47
  {
48
  private DistortedTexture mTexture;
49
  private MeshBase mMesh;
50
  private Bitmap mBitmap;
51
  private DistortedEffects mEffects;
52
  private GenericViewPager mViewPager;
53

    
54
  private boolean mShowCenter, mShowRegion, mShowNormal, mUseOIT;
55

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

    
58
  @Override
59
  protected void onCreate(Bundle savedState)
60
    {
61
    super.onCreate(savedState);
62

    
63
    setTheme(R.style.CustomActivityThemeNoActionBar);
64

    
65
    Bundle b = getIntent().getExtras();
66

    
67
    int objectType = b.getInt("type");
68
    int numCols    = b.getInt("cols");
69
    int numRows    = b.getInt("rows");
70
    int numSlic    = b.getInt("slices");
71
    int bitmapID   = b.getInt("bitmap");
72
    String str     = b.getString("string");
73

    
74
    mShowCenter = false;
75
    mShowRegion = false;
76
    mShowNormal = false;
77
    mUseOIT     = false;
78

    
79
    int maxsize = numCols > numRows ? (Math.max(numCols, numSlic)) : (Math.max(numRows, numSlic));
80
    createBitmap(maxsize, bitmapID);
81
    mMesh =  GenericMeshList.createMesh(objectType, numCols, numRows, numSlic, bitmapID, str);
82
    mMesh.setStretch(numCols, numRows, numSlic);
83

    
84
    mMesh.setShowNormals(mShowNormal);
85
    mTexture= new DistortedTexture();
86
    mEffects= new DistortedEffects();
87

    
88
    final View view = getLayoutInflater().inflate(R.layout.genericlayout, null);
89

    
90
    setContentView(view);
91

    
92
    mViewPager = new GenericViewPager(this,R.id.generic_viewpager);
93
    TabLayout tabLayout = findViewById(R.id.generic_sliding_tabs);
94
    tabLayout.setupWithViewPager(mViewPager.getPager());
95
    }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98

    
99
  private void createBitmap(int size, int bitmapID)
100
    {
101
    if( bitmapID!=-1)
102
      {
103
      InputStream is = getResources().openRawResource(bitmapID);
104

    
105
      try
106
        {
107
        mBitmap = BitmapFactory.decodeStream(is);
108
        }
109
      finally
110
        {
111
        try
112
          {
113
          is.close();
114
          }
115
        catch(IOException e) { }
116
        }
117
      }
118
    else
119
      {
120
      final int T = 64;
121
      final int S = T*size;
122

    
123
      Paint paint = new Paint();
124
      mBitmap = Bitmap.createBitmap(S,S, Bitmap.Config.ARGB_8888);
125
      Canvas canvas = new Canvas(mBitmap);
126

    
127
      paint.setAntiAlias(true);
128
      paint.setTextAlign(Paint.Align.CENTER);
129
      paint.setColor(0xff008800);
130
      paint.setStyle(Paint.Style.FILL);
131
      canvas.drawRect(0, 0, S, S, paint);
132
      paint.setColor(0xffffffff);
133

    
134
      for(int i=0; i<=size ; i++ )
135
        {
136
        canvas.drawRect( T*i-1, 0, T*i+1, S, paint);
137
        canvas.drawRect( 0, T*i-1, S, T*i+1, paint);
138
        }
139
      }
140
    }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143

    
144
  public DistortedTexture getTexture()
145
    {
146
    return mTexture;
147
    }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

    
151
  public MeshBase getMesh()
152
    {
153
    return mMesh;
154
    }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

    
158
  public Bitmap getBitmap()
159
    {
160
    return mBitmap;
161
    }
162

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164

    
165
  public DistortedEffects getEffects()
166
    {
167
    return mEffects;
168
    }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

    
172
  public GenericViewPager getPager()
173
    {
174
    return mViewPager;
175
    }
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

    
179
  public float getWidth()
180
    {
181
    return mMesh==null ? 0: mMesh.getStretchX();
182
    }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

    
186
  public float getHeight()
187
    {
188
    return mMesh==null ? 0: mMesh.getStretchY();
189
    }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

    
193
  public float getDepth()
194
    {
195
    return mMesh==null ? 0: mMesh.getStretchZ();
196
    }
197

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

    
200
  public int getScreenWidth()
201
    {
202
    GenericSurfaceView view = findViewById(R.id.genericSurfaceView);
203
    GenericRenderer renderer = view.getRenderer();
204
    return renderer.getWidth();
205
    }
206

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

    
209
  public float getScaleFactor()
210
    {
211
    GenericSurfaceView view = findViewById(R.id.genericSurfaceView);
212
    GenericRenderer renderer = view.getRenderer();
213
    return renderer.getScaleFactor();
214
    }
215

    
216
///////////////////////////////////////////////////////////////////////////////////////////////////
217

    
218
  void setCenter(float x, float y, float z)
219
    {
220
    GenericSurfaceView view = findViewById(R.id.genericSurfaceView);
221
    GenericRenderer renderer = view.getRenderer();
222
    renderer.setCenter(x,y,z);
223
    }
224

    
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226

    
227
  void setRegion(Static4D region)
228
    {
229
    GenericSurfaceView view = findViewById(R.id.genericSurfaceView);
230
    GenericRenderer renderer = view.getRenderer();
231
    renderer.setRegion( region.get0(), region.get1(), region.get2(), region.get3() );
232
    }
233

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235

    
236
  public void showRegionAndCenter(View view)
237
    {
238
    CheckBox center = findViewById(R.id.genericCheckBoxCenter);
239
    mShowCenter = center.isChecked();
240
    CheckBox region = findViewById(R.id.genericCheckBoxRegion);
241
    mShowRegion = region.isChecked();
242

    
243
    GenericSurfaceView sv = findViewById(R.id.genericSurfaceView);
244

    
245
    mViewPager.showRegionAndCenter(mShowRegion, mShowCenter, sv);
246
    }
247

    
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249

    
250
  public void showNormal(View view)
251
    {
252
    CheckBox box = (CheckBox)view;
253
    mShowNormal = box.isChecked();
254

    
255
    if ( mMesh!=null )
256
      {
257
      mMesh.setShowNormals(mShowNormal);
258
      }
259
    }
260

    
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262

    
263
  public void triggerOIT(View view)
264
    {
265
    CheckBox box = (CheckBox)view;
266
    mUseOIT = box.isChecked();
267

    
268
    GenericSurfaceView sv = findViewById(R.id.genericSurfaceView);
269
    sv.getRenderer().useOIT(mUseOIT);
270
    }
271

    
272
///////////////////////////////////////////////////////////////////////////////////////////////////
273
// Overrides
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275

    
276
  @Override
277
  protected void onPause()
278
    {
279
    GLSurfaceView mView = findViewById(R.id.genericSurfaceView);
280
    if( mView!=null ) mView.onPause();
281

    
282
    DistortedLibrary.onPause();
283
    super.onPause();
284
    }
285

    
286
///////////////////////////////////////////////////////////////////////////////////////////////////
287
    
288
  @Override
289
  protected void onResume()
290
    {
291
    super.onResume();
292
    GLSurfaceView mView = findViewById(R.id.genericSurfaceView);
293
    if( mView!=null ) mView.onResume();
294
    }
295

    
296
///////////////////////////////////////////////////////////////////////////////////////////////////
297
    
298
  @Override
299
  protected void onDestroy()
300
    {
301
    DistortedLibrary.onDestroy();
302
    super.onDestroy();
303
    }
304
  }
(2-2/8)