Project

General

Profile

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

examples / src / main / java / org / distorted / examples / generic / GenericActivity2.java @ 2fad84a7

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 ? (numCols>numSlic ? numCols:numSlic) : (numRows>numSlic ? numRows:numSlic);
80
    createBitmap(maxsize, bitmapID);
81
    mMesh =  GenericMeshList.createMesh(objectType, numCols, numRows, numSlic, bitmapID, str);
82

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

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

    
89
    setContentView(view);
90

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

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97

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

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

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

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

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

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

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

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

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

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156

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

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163

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

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

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

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

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

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

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

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

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

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

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

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

    
207
  void setCenter(Static3D center)
208
    {
209
    GenericSurfaceView view = findViewById(R.id.genericSurfaceView);
210
    view.getRenderer().setCenter( center.get0(), center.get1(), center.get2() );
211
    }
212

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

    
215
  void setRegion(Static4D region)
216
    {
217
    GenericSurfaceView view = findViewById(R.id.genericSurfaceView);
218
    view.getRenderer().setRegion( region.get0(), region.get1(), region.get2(), region.get3() );
219
    }
220

    
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222

    
223
  public void showRegionAndCenter(View view)
224
    {
225
    CheckBox center = findViewById(R.id.genericCheckBoxCenter);
226
    mShowCenter = center.isChecked();
227
    CheckBox region = findViewById(R.id.genericCheckBoxRegion);
228
    mShowRegion = region.isChecked();
229

    
230
    GenericSurfaceView sv = findViewById(R.id.genericSurfaceView);
231

    
232
    mViewPager.showRegionAndCenter(mShowRegion, mShowCenter, sv);
233
    }
234

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

    
237
  public void showNormal(View view)
238
    {
239
    CheckBox box = (CheckBox)view;
240
    mShowNormal = box.isChecked();
241

    
242
    if ( mMesh!=null )
243
      {
244
      mMesh.setShowNormals(mShowNormal);
245
      }
246
    }
247

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

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

    
255
    GenericSurfaceView sv = findViewById(R.id.genericSurfaceView);
256
    sv.getRenderer().useOIT(mUseOIT);
257
    }
258

    
259
///////////////////////////////////////////////////////////////////////////////////////////////////
260
// Overrides
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262

    
263
  @Override
264
  protected void onPause()
265
    {
266
    GLSurfaceView mView = findViewById(R.id.genericSurfaceView);
267
    if( mView!=null ) mView.onPause();
268

    
269
    DistortedLibrary.onPause();
270
    super.onPause();
271
    }
272

    
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274
    
275
  @Override
276
  protected void onResume()
277
    {
278
    super.onResume();
279
    GLSurfaceView mView = findViewById(R.id.genericSurfaceView);
280
    if( mView!=null ) mView.onResume();
281
    }
282

    
283
///////////////////////////////////////////////////////////////////////////////////////////////////
284
    
285
  @Override
286
  protected void onDestroy()
287
    {
288
    DistortedLibrary.onDestroy();
289
    super.onDestroy();
290
    }
291
  }
(2-2/8)