Project

General

Profile

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

examples / src / main / java / org / distorted / examples / predeform / PredeformActivity2.java @ 641ea00c

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 c1f047b9 Leszek Koltunski
import android.widget.CheckBox;
31 59835a0a Leszek Koltunski
import android.widget.SeekBar;
32
import android.widget.TextView;
33
34
import org.distorted.examples.R;
35
import org.distorted.library.main.DistortedLibrary;
36
import org.distorted.library.main.DistortedTexture;
37
import org.distorted.library.mesh.MeshBase;
38
39
import java.io.IOException;
40
import java.io.InputStream;
41
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43
44 c1f047b9 Leszek Koltunski
public class PredeformActivity2 extends Activity implements SeekBar.OnSeekBarChangeListener
45 59835a0a Leszek Koltunski
{
46
    private Bitmap mBitmap;
47
    private TextView mTextLevel;
48
    private DistortedTexture mTexture;
49
    private MeshBase mMesh;
50
    private int mNumRows, mNumCols, mNumSlic;
51
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53
    
54
    @Override
55
    protected void onPause() 
56
      {
57 4b7c432e Leszek Koltunski
      GLSurfaceView mView = findViewById(R.id.predeformSurfaceView);
58 59835a0a Leszek Koltunski
      if( mView!=null ) mView.onPause();
59
60
      DistortedLibrary.onPause();
61
      super.onPause();
62
      }
63
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65
    
66
    @Override
67
    protected void onResume() 
68
      {
69
      super.onResume();
70
      
71 4b7c432e Leszek Koltunski
      GLSurfaceView mView = findViewById(R.id.predeformSurfaceView);
72 59835a0a Leszek Koltunski
      if( mView!=null ) mView.onResume();  
73
      }
74
    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76
    
77
    @Override
78
    protected void onDestroy() 
79
      {
80
      DistortedLibrary.onDestroy();
81
      super.onDestroy();
82
      }
83
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85
86
    @Override
87
    protected void onCreate(Bundle savedState)
88
      {
89
      super.onCreate(savedState);
90
91
      Bundle b = getIntent().getExtras();
92
93
      int objectType = b.getInt("type");
94
      int bitmapID   = b.getInt("bitmap");
95
      mNumCols       = b.getInt("cols");
96
      mNumRows       = b.getInt("rows");
97
      mNumSlic       = b.getInt("slices");
98
99 9df01f21 Leszek Koltunski
      StringBuilder strBuilder = new StringBuilder();
100
      for(int i=0; i<mNumCols*mNumRows; i++) strBuilder.append('1');
101
102 59835a0a Leszek Koltunski
      int maxsize = mNumCols > mNumRows ? (Math.max(mNumCols, mNumSlic)) : (Math.max(mNumRows, mNumSlic));
103
      createBitmap(maxsize,bitmapID);
104 9df01f21 Leszek Koltunski
      mMesh =  PredeformMeshList.createMesh(objectType, mNumCols, mNumRows, mNumSlic, bitmapID, strBuilder.toString() );
105 59835a0a Leszek Koltunski
106
      mTexture = new DistortedTexture();
107
108
      setContentView(R.layout.predeformlayout);
109 c1f047b9 Leszek Koltunski
;
110 4b7c432e Leszek Koltunski
      mTextLevel = findViewById(R.id.predeformInflateText);
111 59835a0a Leszek Koltunski
112 4b7c432e Leszek Koltunski
      SeekBar inflateBar = findViewById(R.id.predeformInflateLevel);
113 59835a0a Leszek Koltunski
      inflateBar.setOnSeekBarChangeListener(this);
114
      inflateBar.setProgress(50);
115
      }
116
117 c1f047b9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
118
119
    public void showNormal(View view)
120
      {
121
      CheckBox box = (CheckBox)view;
122 e6129574 Leszek Koltunski
      boolean show = box.isChecked();
123 c1f047b9 Leszek Koltunski
124 e6129574 Leszek Koltunski
      PredeformSurfaceView v = findViewById(R.id.predeformSurfaceView);
125
      PredeformRenderer renderer = v.getRenderer();
126
127
      renderer.showNormal(show);
128 c1f047b9 Leszek Koltunski
      }
129
130 59835a0a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 onProgressChanged(SeekBar bar, int progress, boolean fromUser)
197
      {
198 c1f047b9 Leszek Koltunski
      if(bar.getId() == R.id.predeformInflateLevel)
199 59835a0a Leszek Koltunski
        {
200 c1f047b9 Leszek Koltunski
        PredeformSurfaceView v2 = findViewById(R.id.predeformSurfaceView);
201
        float level = v2.getRenderer().setLevel(progress);
202
        mTextLevel.setText(getString(R.string.inflate_placeholder, level));
203 59835a0a Leszek Koltunski
        }
204
      }
205
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207
208
    public void onStartTrackingTouch(SeekBar bar) { }
209
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211
212
    public void onStopTrackingTouch(SeekBar bar)  { }
213
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215
216
    public DistortedTexture getTexture()
217
      {
218
      return mTexture;
219
      }
220
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222
223
    public MeshBase getMesh()
224
      {
225
      return mMesh;
226
      }
227
}