Project

General

Profile

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

examples / src / main / java / org / distorted / examples / inflate / InflateActivity2.java @ 64558e4e

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.Distorted;
23
import org.distorted.examples.R;
24
import org.distorted.library.main.DistortedTexture;
25
import org.distorted.library.mesh.MeshBase;
26
import org.distorted.library.mesh.MeshCubes;
27
import org.distorted.library.mesh.MeshFlat;
28
import org.distorted.library.mesh.MeshQuad;
29
import org.distorted.library.mesh.MeshSphere;
30

    
31
import android.app.Activity;
32
import android.graphics.Bitmap;
33
import android.graphics.BitmapFactory;
34
import android.graphics.Canvas;
35
import android.graphics.Paint;
36
import android.opengl.GLSurfaceView;
37
import android.os.Bundle;
38
import android.view.View;
39
import android.widget.AdapterView;
40
import android.widget.ArrayAdapter;
41
import android.widget.SeekBar;
42
import android.widget.Spinner;
43
import android.widget.TextView;
44

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

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

    
50
public class InflateActivity2 extends Activity implements SeekBar.OnSeekBarChangeListener, AdapterView.OnItemSelectedListener
51
{
52
    private int mNumCols = 3;
53
    private int mNumRows = 3;
54
    private int mNumSlic = 1;
55
    private int mBitmapID;
56
    private Bitmap mBitmap;
57
    private TextView mTextLevel;
58
    private DistortedTexture mTexture;
59
    private MeshBase mMesh;
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62
    
63
    @Override
64
    protected void onPause() 
65
      {
66
      GLSurfaceView mView = this.findViewById(R.id.inflateSurfaceView);
67
      if( mView!=null ) mView.onPause();
68

    
69
      Distorted.onPause();
70
      super.onPause();
71
      }
72

    
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74
    
75
    @Override
76
    protected void onResume() 
77
      {
78
      super.onResume();
79
      
80
      GLSurfaceView mView = this.findViewById(R.id.inflateSurfaceView);
81
      if( mView!=null ) mView.onResume();  
82
      }
83
    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85
    
86
    @Override
87
    protected void onDestroy() 
88
      {
89
      Distorted.onDestroy();  
90
      super.onDestroy();
91
      }
92

    
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94

    
95
    @Override
96
    protected void onCreate(Bundle savedState)
97
      {
98
      super.onCreate(savedState);
99

    
100
      Bundle b = getIntent().getExtras();
101

    
102
      String str     = b.getString("string");
103
      int objectType = b.getInt("type");
104
      mNumCols       = b.getInt("cols");
105
      mNumRows       = b.getInt("rows");
106
      mNumSlic       = b.getInt("slices");
107
      mBitmapID      = b.getInt("bitmap");
108

    
109
      switch(objectType)
110
        {
111
        case 0: mMesh = new MeshCubes(mNumCols, str, mNumSlic);
112
          break;
113
        case 1: mMesh = new MeshFlat(mNumCols,mNumRows);
114
          break;
115
        case 2: mMesh = new MeshSphere(mNumRows);
116
          break;
117
        case 3: mMesh = new MeshQuad();
118
          break;
119
        }
120

    
121
      mTexture = new DistortedTexture(mNumCols,mNumRows);
122

    
123
      setContentView(R.layout.inflatelayout);
124

    
125
      Spinner renderSpinner  = findViewById(R.id.inflateSpinnerMode);
126
      renderSpinner.setOnItemSelectedListener(this);
127

    
128
      String[] objectBitmap = new String[] { "Render: Normal", "Render: OIT" };
129

    
130
      ArrayAdapter<String> adapterBitmap = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, objectBitmap);
131
      adapterBitmap.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
132
      renderSpinner.setAdapter(adapterBitmap);
133

    
134
      mTextLevel = findViewById(R.id.inflateInflateText);
135

    
136
      SeekBar transparencyBar = findViewById(R.id.inflateTransparency);
137
      transparencyBar.setOnSeekBarChangeListener(this);
138
      transparencyBar.setProgress(50);
139

    
140
      SeekBar inflateBar = findViewById(R.id.inflateInflateLevel);
141
      inflateBar.setOnSeekBarChangeListener(this);
142
      inflateBar.setProgress(50);
143
      }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

    
147
    public Bitmap getBitmap()
148
      {
149
      if( mBitmap==null )
150
        {
151
        if( mBitmapID!=-1)
152
          {
153
          InputStream is = getResources().openRawResource(mBitmapID);
154

    
155
          try
156
            {
157
            mBitmap = BitmapFactory.decodeStream(is);
158
            }
159
          finally
160
            {
161
            try
162
              {
163
              is.close();
164
              }
165
            catch(IOException e) { }
166
            }
167
          }
168
        else
169
          {
170
          final int W = 64*mNumCols;
171
          final int H = 64*mNumRows;
172

    
173
          Paint paint = new Paint();
174
          mBitmap = Bitmap.createBitmap(W,H, Bitmap.Config.ARGB_8888);
175
          Canvas canvas = new Canvas(mBitmap);
176

    
177
          paint.setAntiAlias(true);
178
          paint.setTextAlign(Paint.Align.CENTER);
179
          paint.setColor(0xff008800);
180
          paint.setStyle(Paint.Style.FILL);
181
          canvas.drawRect(0, 0, W, H, paint);
182
          paint.setColor(0xffffffff);
183

    
184
          for(int i=0; i<=mNumCols ; i++ ) canvas.drawRect(W*i/mNumCols-1, 0, W*i/mNumCols + 1, H, paint);
185
          for(int i=0; i<=mNumRows ; i++ ) canvas.drawRect( 0, H*i/mNumRows-1, W,  H*i/mNumRows+1, paint);
186
          }
187
        }
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
      switch(parent.getId())
200
        {
201
        case R.id.inflateSpinnerMode: renderer.setRenderModeToOIT(pos==1);
202
                                      break;
203
        }
204
      }
205

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

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

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

    
214
    public void onProgressChanged(SeekBar bar, int progress, boolean fromUser)
215
      {
216
      switch (bar.getId())
217
        {
218
        case R.id.inflateTransparency: InflateSurfaceView v1 = this.findViewById(R.id.inflateSurfaceView);
219
                                       v1.getRenderer().setTransparency(progress);
220
                                       break;
221
        case R.id.inflateInflateLevel: InflateSurfaceView v2 = this.findViewById(R.id.inflateSurfaceView);
222
                                       float level = v2.getRenderer().setLevel(progress);
223
                                       mTextLevel.setText(getString(R.string.inflate_placeholder, level));
224
                                       break;
225
        }
226
      }
227

    
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229

    
230
    public void onStartTrackingTouch(SeekBar bar) { }
231

    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233

    
234
    public void onStopTrackingTouch(SeekBar bar)  { }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
    public DistortedTexture getTexture()
239
      {
240
      return mTexture;
241
      }
242

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

    
245
    public MeshBase getMesh()
246
      {
247
      return mMesh;
248
      }
249
}
(2-2/4)