Project

General

Profile

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

examples / src / main / java / org / distorted / examples / earth / EarthActivity2.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.earth;
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
import org.distorted.library.mesh.MeshQuad;
39
import org.distorted.library.mesh.MeshSphere;
40

    
41
import java.io.IOException;
42
import java.io.InputStream;
43

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

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

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

    
65
      Distorted.onPause();
66
      super.onPause();
67
      }
68

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

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

    
96
      Bundle b = getIntent().getExtras();
97

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

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

    
117
      mTexture= new DistortedTexture(mNumCols,mNumRows);
118

    
119
      setContentView(R.layout.earthlayout);
120

    
121
      mTextLevel = findViewById(R.id.earthInflateText);
122
      SeekBar levelBar = findViewById(R.id.earthInflateLevel);
123
      levelBar.setOnSeekBarChangeListener(this);
124
      levelBar.setProgress(50);
125
      }
126

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

    
129
    public Bitmap getBitmap()
130
      {
131
      if( mBitmap==null )
132
        {
133
        if( mBitmapID!=-1)
134
          {
135
          InputStream is = getResources().openRawResource(mBitmapID);
136

    
137
          try
138
            {
139
            mBitmap = BitmapFactory.decodeStream(is);
140
            }
141
          finally
142
            {
143
            try
144
              {
145
              is.close();
146
              }
147
            catch(IOException e) { }
148
            }
149
          }
150
        else
151
          {
152
          final int W = 64*mNumCols;
153
          final int H = 64*mNumRows;
154

    
155
          Paint paint = new Paint();
156
          mBitmap = Bitmap.createBitmap(W,H, Bitmap.Config.ARGB_8888);
157
          Canvas canvas = new Canvas(mBitmap);
158

    
159
          paint.setAntiAlias(true);
160
          paint.setTextAlign(Paint.Align.CENTER);
161
          paint.setColor(0xff008800);
162
          paint.setStyle(Paint.Style.FILL);
163
          canvas.drawRect(0, 0, W, H, paint);
164
          paint.setColor(0xffffffff);
165

    
166
          for(int i=0; i<=mNumCols ; i++ ) canvas.drawRect(W*i/mNumCols-1, 0, W*i/mNumCols + 1, H, paint);
167
          for(int i=0; i<=mNumRows ; i++ ) canvas.drawRect( 0, H*i/mNumRows-1, W,  H*i/mNumRows+1, paint);
168
          }
169
        }
170

    
171
      return mBitmap;
172
      }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175

    
176
    public void onProgressChanged(SeekBar bar, int progress, boolean fromUser)
177
      {
178
      switch (bar.getId())
179
        {
180
        case R.id.earthInflateLevel: EarthSurfaceView view = this.findViewById(R.id.earthSurfaceView);
181
                                     float level = view.getRenderer().setLevel(progress);
182
                                     mTextLevel.setText(getString(R.string.inflate_placeholder, level));
183
                                     break;
184
        }
185
      }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

    
189
    public void onStartTrackingTouch(SeekBar bar) { }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

    
193
    public void onStopTrackingTouch(SeekBar bar)  { }
194

    
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196

    
197
    public DistortedTexture getTexture()
198
      {
199
      return mTexture;
200
      }
201

    
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203

    
204
    public MeshBase getMesh()
205
      {
206
      return mMesh;
207
      }
208
}
(2-2/4)