Project

General

Profile

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

examples / src / main / java / org / distorted / examples / predeform / PredeformActivity2.java @ c0f27889

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.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.CheckBox;
31
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
public class PredeformActivity2 extends Activity implements SeekBar.OnSeekBarChangeListener
45
{
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
      super.onPause();
58
      GLSurfaceView view = findViewById(R.id.predeformSurfaceView);
59
      view.onPause();
60
      DistortedLibrary.onPause();
61
      }
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64
    
65
    @Override
66
    protected void onResume() 
67
      {
68
      super.onResume();
69
      GLSurfaceView view = findViewById(R.id.predeformSurfaceView);
70
      view.onResume();
71
      }
72
    
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74
    
75
    @Override
76
    protected void onDestroy() 
77
      {
78
      DistortedLibrary.onDestroy();
79
      super.onDestroy();
80
      }
81

    
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83

    
84
    @Override
85
    protected void onCreate(Bundle savedState)
86
      {
87
      super.onCreate(savedState);
88

    
89
      Bundle b = getIntent().getExtras();
90

    
91
      int objectType = b.getInt("type");
92
      int bitmapID   = b.getInt("bitmap");
93
      mNumCols       = b.getInt("cols");
94
      mNumRows       = b.getInt("rows");
95
      mNumSlic       = b.getInt("slices");
96

    
97
      StringBuilder strBuilder = new StringBuilder();
98
      for(int i=0; i<mNumCols*mNumRows; i++) strBuilder.append('1');
99

    
100
      int maxsize = mNumCols > mNumRows ? (Math.max(mNumCols, mNumSlic)) : (Math.max(mNumRows, mNumSlic));
101
      createBitmap(maxsize,bitmapID);
102
      mMesh =  PredeformMeshList.createMesh(objectType, mNumCols, mNumRows, mNumSlic, bitmapID, strBuilder.toString() );
103

    
104
      mTexture = new DistortedTexture();
105

    
106
      setContentView(R.layout.predeformlayout);
107
;
108
      mTextLevel = findViewById(R.id.predeformInflateText);
109

    
110
      SeekBar inflateBar = findViewById(R.id.predeformInflateLevel);
111
      inflateBar.setOnSeekBarChangeListener(this);
112
      inflateBar.setProgress(50);
113
      }
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116

    
117
    public void showNormal(View view)
118
      {
119
      CheckBox box = (CheckBox)view;
120
      boolean show = box.isChecked();
121

    
122
      PredeformSurfaceView v = findViewById(R.id.predeformSurfaceView);
123
      PredeformRenderer renderer = v.getRenderer();
124

    
125
      renderer.showNormal(show);
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 onProgressChanged(SeekBar bar, int progress, boolean fromUser)
195
      {
196
      if(bar.getId() == R.id.predeformInflateLevel)
197
        {
198
        PredeformSurfaceView v2 = findViewById(R.id.predeformSurfaceView);
199
        float level = v2.getRenderer().setLevel(progress);
200
        mTextLevel.setText(getString(R.string.inflate_placeholder, level));
201
        }
202
      }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

    
206
    public void onStartTrackingTouch(SeekBar bar) { }
207

    
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209

    
210
    public void onStopTrackingTouch(SeekBar bar)  { }
211

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

    
214
    public DistortedTexture getTexture()
215
      {
216
      return mTexture;
217
      }
218

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

    
221
    public MeshBase getMesh()
222
      {
223
      return mMesh;
224
      }
225
}
(2-2/7)