Project

General

Profile

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

examples / src / main / java / org / distorted / examples / predeform / PredeformActivity2.java @ 0dc8ffef

1 59835a0a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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.predeform;
21
22
import android.app.Activity;
23
import android.graphics.Bitmap;
24
import android.graphics.BitmapFactory;
25
import android.graphics.Canvas;
26
import android.graphics.Paint;
27
import android.opengl.GLSurfaceView;
28
import android.os.Bundle;
29
import android.view.View;
30
import android.widget.AdapterView;
31
import android.widget.ArrayAdapter;
32
import android.widget.SeekBar;
33
import android.widget.Spinner;
34
import android.widget.TextView;
35
36
import org.distorted.examples.R;
37
import org.distorted.library.main.DistortedLibrary;
38
import org.distorted.library.main.DistortedTexture;
39
import org.distorted.library.mesh.MeshBase;
40
41
import java.io.IOException;
42
import java.io.InputStream;
43
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45
46
public class PredeformActivity2 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 onPause() 
58
      {
59 0dc8ffef Leszek Koltunski
      GLSurfaceView mView = findViewById(R.id.predeform2SurfaceView);
60 59835a0a Leszek Koltunski
      if( mView!=null ) mView.onPause();
61
62
      DistortedLibrary.onPause();
63
      super.onPause();
64
      }
65
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67
    
68
    @Override
69
    protected void onResume() 
70
      {
71
      super.onResume();
72
      
73 0dc8ffef Leszek Koltunski
      GLSurfaceView mView = findViewById(R.id.predeform2SurfaceView);
74 59835a0a Leszek Koltunski
      if( mView!=null ) mView.onResume();  
75
      }
76
    
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78
    
79
    @Override
80
    protected void onDestroy() 
81
      {
82
      DistortedLibrary.onDestroy();
83
      super.onDestroy();
84
      }
85
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87
88
    @Override
89
    protected void onCreate(Bundle savedState)
90
      {
91
      super.onCreate(savedState);
92
93
      Bundle b = getIntent().getExtras();
94
95
      String str     = b.getString("string");
96
      int objectType = b.getInt("type");
97
      int bitmapID   = b.getInt("bitmap");
98
      mNumCols       = b.getInt("cols");
99
      mNumRows       = b.getInt("rows");
100
      mNumSlic       = b.getInt("slices");
101
102
      int maxsize = mNumCols > mNumRows ? (Math.max(mNumCols, mNumSlic)) : (Math.max(mNumRows, mNumSlic));
103
      createBitmap(maxsize,bitmapID);
104
      mMesh =  PredeformMeshList.createMesh(objectType, mNumCols, mNumRows, mNumSlic, bitmapID, str);
105
106
      mTexture = new DistortedTexture();
107
108
      setContentView(R.layout.predeformlayout);
109
110 0dc8ffef Leszek Koltunski
      Spinner renderSpinner = findViewById(R.id.predeform2SpinnerMode);
111 59835a0a Leszek Koltunski
      renderSpinner.setOnItemSelectedListener(this);
112
113
      String[] objectBitmap = new String[] { "Render: Normal", "Render: OIT" };
114
115
      ArrayAdapter<String> adapterBitmap = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, objectBitmap);
116
      adapterBitmap.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
117
      renderSpinner.setAdapter(adapterBitmap);
118
119 0dc8ffef Leszek Koltunski
      mTextLevel = findViewById(R.id.predeform2InflateText);
120 59835a0a Leszek Koltunski
121 0dc8ffef Leszek Koltunski
      SeekBar transparencyBar = findViewById(R.id.predeform2Transparency);
122 59835a0a Leszek Koltunski
      transparencyBar.setOnSeekBarChangeListener(this);
123
      transparencyBar.setProgress(50);
124
125 0dc8ffef Leszek Koltunski
      SeekBar inflateBar = findViewById(R.id.predeform2InflateLevel);
126 59835a0a Leszek Koltunski
      inflateBar.setOnSeekBarChangeListener(this);
127
      inflateBar.setProgress(50);
128
      }
129
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131
132
    private void createBitmap(int size, int bitmapID)
133
      {
134
      if( bitmapID!=-1)
135
        {
136
        try (InputStream is = getResources().openRawResource(bitmapID))
137
          {
138
          mBitmap = BitmapFactory.decodeStream(is);
139
          }
140
        catch( IOException ex ) { android.util.Log.e("act", "failed to open resource "+bitmapID); }
141
        }
142
      else
143
        {
144
        final int T = 64;
145
        final int S = T*size;
146
147
        Paint paint = new Paint();
148
        mBitmap = Bitmap.createBitmap(S,S, Bitmap.Config.ARGB_8888);
149
        Canvas canvas = new Canvas(mBitmap);
150
151
        paint.setAntiAlias(true);
152
        paint.setTextAlign(Paint.Align.CENTER);
153
        paint.setColor(0xff008800);
154
        paint.setStyle(Paint.Style.FILL);
155
        canvas.drawRect(0, 0, S, S, paint);
156
        paint.setColor(0xffffffff);
157
158
        for(int i=0; i<=size ; i++ )
159
          {
160
          canvas.drawRect( T*i-1, 0, T*i+1, S, paint);
161
          canvas.drawRect( 0, T*i-1, S, T*i+1, paint);
162
          }
163
        }
164
      }
165
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167
168
    int getRows()
169
      {
170
      return mNumRows;
171
      }
172
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174
175
    int getCols()
176
      {
177
      return mNumCols;
178
      }
179
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181
182
    int getSlic()
183
      {
184
      return mNumSlic;
185
      }
186
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188
189
    public Bitmap getBitmap()
190
      {
191
      return mBitmap;
192
      }
193
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195
196
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
197
      {
198 0dc8ffef Leszek Koltunski
      PredeformSurfaceView2 v = findViewById(R.id.predeform2SurfaceView);
199
      PredeformRenderer2 renderer = v.getRenderer();
200 59835a0a Leszek Koltunski
201 0dc8ffef Leszek Koltunski
      if( parent.getId()==R.id.predeform2SpinnerMode)
202 59835a0a Leszek Koltunski
        {
203
        renderer.setRenderModeToOIT(pos==1);
204
        }
205
      }
206
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208
209
    public void onNothingSelected(AdapterView<?> parent)
210
      {
211
      }
212
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214
215
    public void onProgressChanged(SeekBar bar, int progress, boolean fromUser)
216
      {
217
      switch (bar.getId())
218
        {
219 0dc8ffef Leszek Koltunski
        case R.id.predeform2Transparency: PredeformSurfaceView2 v1 = findViewById(R.id.predeform2SurfaceView);
220
                                          v1.getRenderer().setTransparency(progress);
221
                                          break;
222
        case R.id.predeform2InflateLevel: PredeformSurfaceView2 v2 = findViewById(R.id.predeform2SurfaceView);
223
                                          float level = v2.getRenderer().setLevel(progress);
224
                                          mTextLevel.setText(getString(R.string.inflate_placeholder, level));
225
                                          break;
226 59835a0a 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
}