Project

General

Profile

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

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

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 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.widget.SeekBar;
30
import android.widget.TextView;
31

    
32
import org.distorted.examples.R;
33
import org.distorted.library.main.Distorted;
34
import org.distorted.library.main.DistortedTexture;
35
import org.distorted.library.mesh.MeshBase;
36
import org.distorted.library.mesh.MeshCubes;
37
import org.distorted.library.mesh.MeshFlat;
38

    
39
import java.io.IOException;
40
import java.io.InputStream;
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

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

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56
    
57
    @Override
58
    protected void onPause() 
59
      {
60
      GLSurfaceView mView = this.findViewById(R.id.inflateSurfaceView);
61
      if( mView!=null ) mView.onPause();
62

    
63
      Distorted.onPause();
64
      super.onPause();
65
      }
66

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

    
89
    @Override
90
    protected void onCreate(Bundle savedState)
91
      {
92
      super.onCreate(savedState);
93

    
94
      Bundle b = getIntent().getExtras();
95

    
96
      String str     = b.getString("string");
97
      int objectType = b.getInt("type");
98
      mNumCols       = b.getInt("cols");
99
      mNumRows       = b.getInt("rows");
100
      mNumSlic       = b.getInt("slices");
101
      mBitmapID      = b.getInt("bitmap");
102

    
103
      if( objectType==1 ) mMesh = new MeshFlat(mNumCols,mNumRows);
104
      else                mMesh = new MeshCubes(mNumCols, str, mNumSlic);
105

    
106
      mTexture= new DistortedTexture(mNumCols,mNumRows);
107

    
108
      setContentView(R.layout.inflatelayout);
109

    
110
      mTextLevel = (TextView)findViewById(R.id.inflateText);
111
      SeekBar levelBar = findViewById(R.id.inflateLevel);
112
      levelBar.setOnSeekBarChangeListener(this);
113

    
114
      InflateSurfaceView view = this.findViewById(R.id.inflateSurfaceView);
115
      float level = view.getRenderer().setLevel(50);
116
      mTextLevel.setText(getString(R.string.inflate_placeholder, level));
117
      levelBar.setProgress(50);
118
      }
119

    
120

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122

    
123
    public Bitmap getBitmap()
124
      {
125
      if( mBitmap==null )
126
        {
127
        if( mBitmapID!=-1)
128
          {
129
          InputStream is = getResources().openRawResource(mBitmapID);
130

    
131
          try
132
            {
133
            mBitmap = BitmapFactory.decodeStream(is);
134
            }
135
          finally
136
            {
137
            try
138
              {
139
              is.close();
140
              }
141
            catch(IOException e) { }
142
            }
143
          }
144
        else
145
          {
146
          final int W = 64*mNumCols;
147
          final int H = 64*mNumRows;
148

    
149
          Paint paint = new Paint();
150
          mBitmap = Bitmap.createBitmap(W,H, Bitmap.Config.ARGB_8888);
151
          Canvas canvas = new Canvas(mBitmap);
152

    
153
          paint.setAntiAlias(true);
154
          paint.setTextAlign(Paint.Align.CENTER);
155
          paint.setColor(0xff008800);
156
          paint.setStyle(Paint.Style.FILL);
157
          canvas.drawRect(0, 0, W, H, paint);
158
          paint.setColor(0xffffffff);
159

    
160
          for(int i=0; i<=mNumCols ; i++ ) canvas.drawRect(W*i/mNumCols-1, 0, W*i/mNumCols + 1, H, paint);
161
          for(int i=0; i<=mNumRows ; i++ ) canvas.drawRect( 0, H*i/mNumRows-1, W,  H*i/mNumRows+1, paint);
162
          }
163
        }
164

    
165
      return mBitmap;
166
      }
167

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

    
170
    public void onProgressChanged(SeekBar bar, int progress, boolean fromUser)
171
      {
172
      switch (bar.getId())
173
        {
174
        case R.id.inflateLevel: InflateSurfaceView view = this.findViewById(R.id.inflateSurfaceView);
175
                                float level = view.getRenderer().setLevel(progress);
176
                                mTextLevel.setText(getString(R.string.inflate_placeholder, level));
177
                                break;
178
        }
179
      }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

    
183
    public void onStartTrackingTouch(SeekBar bar) { }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186

    
187
    public void onStopTrackingTouch(SeekBar bar)  { }
188

    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190

    
191
    public DistortedTexture getTexture()
192
      {
193
      return mTexture;
194
      }
195

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197

    
198
    public MeshBase getMesh()
199
      {
200
      return mMesh;
201
      }
202
}
(2-2/4)