Project

General

Profile

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

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

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

    
22
import org.distorted.library.main.DistortedLibrary;
23
import org.distorted.examples.R;
24
import org.distorted.library.main.DistortedTexture;
25
import org.distorted.library.mesh.MeshBase;
26

    
27
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
import android.view.View;
35
import android.widget.AdapterView;
36
import android.widget.ArrayAdapter;
37
import android.widget.SeekBar;
38
import android.widget.Spinner;
39
import android.widget.TextView;
40

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

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

    
46
public class InflateActivity2 extends Activity implements SeekBar.OnSeekBarChangeListener, AdapterView.OnItemSelectedListener
47
{
48
    private Bitmap mBitmap;
49
    private TextView mTextLevel;
50
    private DistortedTexture mTexture;
51
    private MeshBase mMesh;
52
    private int mNumRows, mNumCols, mNumSlic;
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

    
56
    @Override
57
    protected void onCreate(Bundle savedState)
58
      {
59
      super.onCreate(savedState);
60
      DistortedLibrary.onCreate();
61
      Bundle b = getIntent().getExtras();
62

    
63
      String str     = b.getString("string");
64
      int objectType = b.getInt("type");
65
      int bitmapID   = b.getInt("bitmap");
66
      mNumCols       = b.getInt("cols");
67
      mNumRows       = b.getInt("rows");
68
      mNumSlic       = b.getInt("slices");
69

    
70
      int maxsize = mNumCols > mNumRows ? (Math.max(mNumCols, mNumSlic)) : (Math.max(mNumRows, mNumSlic));
71
      createBitmap(maxsize,bitmapID);
72
      mMesh =  InflateMeshList.createMesh(objectType, mNumCols, mNumRows, mNumSlic, bitmapID, str);
73

    
74
      mTexture = new DistortedTexture();
75

    
76
      setContentView(R.layout.inflatelayout);
77

    
78
      Spinner renderSpinner = findViewById(R.id.inflateSpinnerMode);
79
      renderSpinner.setOnItemSelectedListener(this);
80

    
81
      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

    
89
      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

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
129

    
130
    private void createBitmap(int size, int bitmapID)
131
      {
132
      if( bitmapID!=-1)
133
        {
134
        try (InputStream is = getResources().openRawResource(bitmapID))
135
          {
136
          mBitmap = BitmapFactory.decodeStream(is);
137
          }
138
        catch( IOException ex ) { android.util.Log.e("act", "failed to open resource "+bitmapID); }
139
        }
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
          {
158
          canvas.drawRect( T*i-1, 0, T*i+1, S, paint);
159
          canvas.drawRect( 0, T*i-1, S, T*i+1, paint);
160
          }
161
        }
162
      }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
186

    
187
    public Bitmap getBitmap()
188
      {
189
      return mBitmap;
190
      }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
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
      if( parent.getId()==R.id.inflateSpinnerMode)
200
        {
201
        renderer.setRenderModeToOIT(pos==1);
202
        }
203
      }
204

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

    
207
    public void onNothingSelected(AdapterView<?> parent)
208
      {
209
      }
210

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

    
213
    public void onProgressChanged(SeekBar bar, int progress, boolean fromUser)
214
      {
215
      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
        {
224
        float level = v.getRenderer().setLevel(progress);
225
        mTextLevel.setText(getString(R.string.inflate_placeholder, level));
226
        }
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
}
(2-2/5)