Project

General

Profile

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

examples / src / main / java / org / distorted / examples / inflate / InflateActivity2.java @ 77e66c58

1 77a500b3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 77a500b3 Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 77a500b3 Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 77a500b3 Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 77a500b3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20
package org.distorted.examples.inflate;
21
22 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
23 64558e4e Leszek Koltunski
import org.distorted.examples.R;
24
import org.distorted.library.main.DistortedTexture;
25
import org.distorted.library.mesh.MeshBase;
26
27 77a500b3 Leszek Koltunski
import android.app.Activity;
28
import android.graphics.Bitmap;
29
import android.graphics.BitmapFactory;
30
import android.graphics.Canvas;
31
import android.graphics.Paint;
32
import android.opengl.GLSurfaceView;
33
import android.os.Bundle;
34 64558e4e Leszek Koltunski
import android.view.View;
35
import android.widget.AdapterView;
36
import android.widget.ArrayAdapter;
37 77a500b3 Leszek Koltunski
import android.widget.SeekBar;
38 64558e4e Leszek Koltunski
import android.widget.Spinner;
39 77a500b3 Leszek Koltunski
import android.widget.TextView;
40
41
import java.io.IOException;
42
import java.io.InputStream;
43
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45
46 64558e4e Leszek Koltunski
public class InflateActivity2 extends Activity implements SeekBar.OnSeekBarChangeListener, AdapterView.OnItemSelectedListener
47 77a500b3 Leszek Koltunski
{
48
    private Bitmap mBitmap;
49 64558e4e Leszek Koltunski
    private TextView mTextLevel;
50
    private DistortedTexture mTexture;
51
    private MeshBase mMesh;
52 e3c87517 Leszek Koltunski
    private int mNumRows, mNumCols, mNumSlic;
53 77a500b3 Leszek Koltunski
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55
56
    @Override
57
    protected void onCreate(Bundle savedState)
58
      {
59
      super.onCreate(savedState);
60 a9df241d Leszek Koltunski
      DistortedLibrary.onCreate();
61 77a500b3 Leszek Koltunski
      Bundle b = getIntent().getExtras();
62
63
      String str     = b.getString("string");
64
      int objectType = b.getInt("type");
65 4db4e39b Leszek Koltunski
      int bitmapID   = b.getInt("bitmap");
66 e3c87517 Leszek Koltunski
      mNumCols       = b.getInt("cols");
67
      mNumRows       = b.getInt("rows");
68
      mNumSlic       = b.getInt("slices");
69 4db4e39b Leszek Koltunski
70 e3c87517 Leszek Koltunski
      int maxsize = mNumCols > mNumRows ? (Math.max(mNumCols, mNumSlic)) : (Math.max(mNumRows, mNumSlic));
71 4db4e39b Leszek Koltunski
      createBitmap(maxsize,bitmapID);
72 e3c87517 Leszek Koltunski
      mMesh =  InflateMeshList.createMesh(objectType, mNumCols, mNumRows, mNumSlic, bitmapID, str);
73
74 687263cc Leszek Koltunski
      mTexture = new DistortedTexture();
75 77a500b3 Leszek Koltunski
76
      setContentView(R.layout.inflatelayout);
77
78 83e0ca5e Leszek Koltunski
      Spinner renderSpinner = findViewById(R.id.inflateSpinnerMode);
79 64558e4e Leszek Koltunski
      renderSpinner.setOnItemSelectedListener(this);
80 77a500b3 Leszek Koltunski
81 64558e4e Leszek Koltunski
      String[] objectBitmap = new String[] { "Render: Normal", "Render: OIT" };
82
83
      ArrayAdapter<String> adapterBitmap = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, objectBitmap);
84
      adapterBitmap.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
85
      renderSpinner.setAdapter(adapterBitmap);
86
87
      mTextLevel = findViewById(R.id.inflateInflateText);
88 77a500b3 Leszek Koltunski
89 64558e4e Leszek Koltunski
      SeekBar transparencyBar = findViewById(R.id.inflateTransparency);
90
      transparencyBar.setOnSeekBarChangeListener(this);
91
      transparencyBar.setProgress(50);
92
93
      SeekBar inflateBar = findViewById(R.id.inflateInflateLevel);
94
      inflateBar.setOnSeekBarChangeListener(this);
95
      inflateBar.setProgress(50);
96
      }
97 77a500b3 Leszek Koltunski
98 a9df241d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
99
    
100
    @Override
101
    protected void onPause() 
102
      {
103
      super.onPause();
104
      GLSurfaceView view = findViewById(R.id.inflateSurfaceView);
105
      view.onPause();
106
      DistortedLibrary.onPause();
107
      }
108
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
    
111
    @Override
112
    protected void onResume() 
113
      {
114
      super.onResume();
115
      GLSurfaceView view = findViewById(R.id.inflateSurfaceView);
116
      view.onResume();
117
      }
118
    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120
    
121
    @Override
122
    protected void onDestroy() 
123
      {
124
      DistortedLibrary.onDestroy();
125
      super.onDestroy();
126
      }
127
128 77a500b3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
129
130 4db4e39b Leszek Koltunski
    private void createBitmap(int size, int bitmapID)
131 77a500b3 Leszek Koltunski
      {
132 4db4e39b Leszek Koltunski
      if( bitmapID!=-1)
133 77a500b3 Leszek Koltunski
        {
134 e3c87517 Leszek Koltunski
        try (InputStream is = getResources().openRawResource(bitmapID))
135 4db4e39b Leszek Koltunski
          {
136
          mBitmap = BitmapFactory.decodeStream(is);
137
          }
138 e3c87517 Leszek Koltunski
        catch( IOException ex ) { android.util.Log.e("act", "failed to open resource "+bitmapID); }
139 4db4e39b Leszek Koltunski
        }
140
      else
141
        {
142
        final int T = 64;
143
        final int S = T*size;
144
145
        Paint paint = new Paint();
146
        mBitmap = Bitmap.createBitmap(S,S, Bitmap.Config.ARGB_8888);
147
        Canvas canvas = new Canvas(mBitmap);
148
149
        paint.setAntiAlias(true);
150
        paint.setTextAlign(Paint.Align.CENTER);
151
        paint.setColor(0xff008800);
152
        paint.setStyle(Paint.Style.FILL);
153
        canvas.drawRect(0, 0, S, S, paint);
154
        paint.setColor(0xffffffff);
155
156
        for(int i=0; i<=size ; i++ )
157 77a500b3 Leszek Koltunski
          {
158 4db4e39b Leszek Koltunski
          canvas.drawRect( T*i-1, 0, T*i+1, S, paint);
159
          canvas.drawRect( 0, T*i-1, S, T*i+1, paint);
160 77a500b3 Leszek Koltunski
          }
161
        }
162 4db4e39b Leszek Koltunski
      }
163 77a500b3 Leszek Koltunski
164 e3c87517 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
165
166
    int getRows()
167
      {
168
      return mNumRows;
169
      }
170
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172
173
    int getCols()
174
      {
175
      return mNumCols;
176
      }
177
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179
180
    int getSlic()
181
      {
182
      return mNumSlic;
183
      }
184
185 4db4e39b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
186
187
    public Bitmap getBitmap()
188
      {
189 77a500b3 Leszek Koltunski
      return mBitmap;
190
      }
191
192 64558e4e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
193
194
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
195
      {
196
      InflateSurfaceView v = this.findViewById(R.id.inflateSurfaceView);
197
      InflateRenderer renderer = v.getRenderer();
198
199 e3c87517 Leszek Koltunski
      if( parent.getId()==R.id.inflateSpinnerMode)
200 64558e4e Leszek Koltunski
        {
201 e3c87517 Leszek Koltunski
        renderer.setRenderModeToOIT(pos==1);
202 64558e4e Leszek Koltunski
        }
203
      }
204
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206
207
    public void onNothingSelected(AdapterView<?> parent)
208
      {
209
      }
210
211 77a500b3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
212
213
    public void onProgressChanged(SeekBar bar, int progress, boolean fromUser)
214
      {
215 77e66c58 Leszek Koltunski
      int id = bar.getId();
216
      InflateSurfaceView v = findViewById(R.id.inflateSurfaceView);
217
218
      if( id == R.id.inflateTransparency )
219
        {
220
        v.getRenderer().setTransparency(progress);
221
        }
222
      if( id == R.id.inflateInflateLevel )
223 77a500b3 Leszek Koltunski
        {
224 77e66c58 Leszek Koltunski
        float level = v.getRenderer().setLevel(progress);
225
        mTextLevel.setText(getString(R.string.inflate_placeholder, level));
226 77a500b3 Leszek Koltunski
        }
227
      }
228
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230
231
    public void onStartTrackingTouch(SeekBar bar) { }
232
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234
235
    public void onStopTrackingTouch(SeekBar bar)  { }
236
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238
239
    public DistortedTexture getTexture()
240
      {
241
      return mTexture;
242
      }
243
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245
246
    public MeshBase getMesh()
247
      {
248
      return mMesh;
249
      }
250
}