Project

General

Profile

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

examples / src / main / java / org / distorted / examples / cubes / CubesActivity.java @ 01782e85

1 bc0a685b 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 427ab7bf Leszek Koltunski
20 5068fa06 Leszek Koltunski
package org.distorted.examples.cubes;
21 427ab7bf Leszek Koltunski
22 01782e85 Leszek Koltunski
import org.distorted.library.main.Distorted;
23 5068fa06 Leszek Koltunski
import org.distorted.examples.R;
24 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedTexture;
25
import org.distorted.library.main.MeshObject;
26
import org.distorted.library.main.MeshCubes;
27 427ab7bf Leszek Koltunski
28
import android.app.Activity;
29
import android.opengl.GLSurfaceView;
30
import android.os.Bundle;
31
import android.view.Gravity;
32
import android.view.View;
33
import android.widget.Button;
34
import android.widget.LinearLayout;
35
import android.widget.NumberPicker;
36
import android.widget.NumberPicker.OnValueChangeListener;
37
import android.widget.TableRow;
38
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40
41 462c74f4 Leszek Koltunski
public class CubesActivity extends Activity implements View.OnClickListener
42 427ab7bf Leszek Koltunski
{
43
    private static final int COLOR_OFF = 0xffffe81f;
44
    private static final int COLOR_ON  = 0xff0000ff;
45 50ac40a6 Leszek Koltunski
46
    private int mNumCols = 3;
47
    private int mNumRows = 3;
48 3a438b27 leszek
    private int mNumSlic = 1;
49
    private NumberPicker mColsPicker, mRowsPicker, mSlicPicker;
50 427ab7bf Leszek Koltunski
    private LinearLayout mLay;
51 50ac40a6 Leszek Koltunski
    private boolean[] mShape;
52 f6d884d5 Leszek Koltunski
    private DistortedTexture mTexture;
53 b01acdaf Leszek Koltunski
    private MeshObject mMesh;
54 50ac40a6 Leszek Koltunski
55 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
56
    
57
    @Override
58
    protected void onCreate(Bundle savedState) 
59
      {
60
      super.onCreate(savedState);
61
62 462c74f4 Leszek Koltunski
      setContentView(R.layout.cubespickerlayout);
63 427ab7bf Leszek Koltunski
      
64 462c74f4 Leszek Koltunski
      mLay = (LinearLayout)findViewById(R.id.cubespicker_buttongrid);
65 427ab7bf Leszek Koltunski
      
66 462c74f4 Leszek Koltunski
      mColsPicker = (NumberPicker)findViewById(R.id.cubespicker_cols);
67
      mRowsPicker = (NumberPicker)findViewById(R.id.cubespicker_rows);
68 3a438b27 leszek
      mSlicPicker = (NumberPicker)findViewById(R.id.cubespicker_slices);
69
70 57ba16f3 Leszek Koltunski
      mColsPicker.setMaxValue(40);
71 427ab7bf Leszek Koltunski
      mColsPicker.setMinValue( 0);
72 57ba16f3 Leszek Koltunski
      mRowsPicker.setMaxValue(40);
73 427ab7bf Leszek Koltunski
      mRowsPicker.setMinValue( 0);
74 3a438b27 leszek
      mSlicPicker.setMaxValue(40);
75
      mSlicPicker.setMinValue( 0);
76
77 427ab7bf Leszek Koltunski
      mColsPicker.setOnValueChangedListener(new OnValueChangeListener() 
78
         {
79
         @Override
80
         public void onValueChange(NumberPicker picker, int oldVal, int newVal) 
81
           { 
82
           setGrid();
83
           }
84
         });
85
      
86
      mRowsPicker.setOnValueChangedListener(new OnValueChangeListener() 
87
         {
88
         @Override
89
         public void onValueChange(NumberPicker picker, int oldVal, int newVal) 
90
           { 
91
           setGrid();
92
           }
93
         });
94 3a438b27 leszek
95
      mSlicPicker.setOnValueChangedListener(new OnValueChangeListener()
96
         {
97
         @Override
98
         public void onValueChange(NumberPicker picker, int oldVal, int newVal)
99
           {
100
           mNumSlic = mSlicPicker.getValue();
101
           }
102
         });
103 427ab7bf Leszek Koltunski
      }
104 50ac40a6 Leszek Koltunski
105 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
106
    
107 50ac40a6 Leszek Koltunski
    private void setGrid()
108 427ab7bf Leszek Koltunski
      {
109
      mNumCols = mColsPicker.getValue();
110
      mNumRows = mRowsPicker.getValue();
111
      
112
      int width = mLay.getWidth();
113
      int height= mLay.getHeight();
114 57ba16f3 Leszek Koltunski
      int w = mNumCols>0 ? (int)( 0.9f*width / mNumCols) : 0;
115
      int h = mNumRows>0 ? (int)( 0.9f*height/ mNumRows) : 0;
116 427ab7bf Leszek Koltunski
      int size= w<h ? w:h;
117 57ba16f3 Leszek Koltunski
      int pad = size<20 ? 1 : size/20;
118 427ab7bf Leszek Koltunski
     
119
      mLay.removeAllViews();
120
      
121
      mShape = new boolean[mNumRows*mNumCols];
122
123
      TableRow.LayoutParams p = new android.widget.TableRow.LayoutParams();
124 6f3024ae Leszek Koltunski
125 427ab7bf Leszek Koltunski
      p.rightMargin  = pad;
126
      p.leftMargin   = pad;
127
      p.topMargin    = pad;
128
      p.bottomMargin = pad;
129
      p.height       = size;
130
      p.width        = size;
131
      
132
      for (int rows=0; rows<mNumRows; rows++) 
133
        {
134
        TableRow tr = new TableRow(this);
135
        tr.setGravity(Gravity.CENTER);
136
        
137
        for(int cols=0; cols<mNumCols; cols++) 
138
          {
139
          Button b = new Button(this);
140
          b.setOnClickListener(this);
141
          b.setId(rows*mNumCols+cols);
142
          b.setLayoutParams(p);          
143 57ba16f3 Leszek Koltunski
          b.setBackgroundColor(COLOR_ON);
144 427ab7bf Leszek Koltunski
          tr.addView(b, p);
145 57ba16f3 Leszek Koltunski
          mShape[rows*mNumCols+cols] = true;
146 427ab7bf Leszek Koltunski
          }
147
        
148
        mLay.addView(tr);
149
        }
150
      }
151
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153
    
154
    public void onClick(View view) 
155
      {
156
      Button tmp = (Button)view;  
157
      int id = tmp.getId();
158
      mShape[id] = !mShape[id];
159
      tmp.setBackgroundColor(mShape[id] ? COLOR_ON:COLOR_OFF);
160
      }
161
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163
    
164
    @Override
165
    public void onWindowFocusChanged(boolean hasFocus) 
166
      {
167
      super.onWindowFocusChanged(hasFocus);
168
169
      mColsPicker.setValue(mNumCols);
170
      mRowsPicker.setValue(mNumRows);
171 3a438b27 leszek
      mSlicPicker.setValue(mNumSlic);
172
173 427ab7bf Leszek Koltunski
      if( hasFocus ) setGrid();
174
      }
175
    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177
    
178
    @Override
179
    protected void onPause() 
180
      {
181
      GLSurfaceView mView = (GLSurfaceView) this.findViewById(R.id.cubesSurfaceView);
182
      if( mView!=null ) mView.onPause();
183 b0ebdf5e Leszek Koltunski
184
      Distorted.onPause();
185 427ab7bf Leszek Koltunski
      super.onPause();
186
      }
187
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189
    
190
    @Override
191
    protected void onResume() 
192
      {
193
      super.onResume();
194
      
195
      GLSurfaceView mView = (GLSurfaceView) this.findViewById(R.id.cubesSurfaceView);
196
      if( mView!=null ) mView.onResume();  
197
      }
198
    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200
    
201
    @Override
202
    protected void onDestroy() 
203
      {
204
      Distorted.onDestroy();  
205
      super.onDestroy();
206
      }
207
 
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209
    
210 50ac40a6 Leszek Koltunski
    public void Create(View v)
211
      {
212 462c74f4 Leszek Koltunski
      String str = "";
213 50ac40a6 Leszek Koltunski
214 462c74f4 Leszek Koltunski
      for(int i=0; i<mNumRows*mNumCols; i++)
215
        str += mShape[i] ? "1" : "0";
216 50ac40a6 Leszek Koltunski
217 3a438b27 leszek
      mMesh = new MeshCubes(mNumCols, str, mNumSlic);
218 7451c98a Leszek Koltunski
      mTexture = new DistortedTexture(mNumCols,mNumRows);
219 50ac40a6 Leszek Koltunski
220
      setContentView(R.layout.cubeslayout);
221
      }
222
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224
225 f6d884d5 Leszek Koltunski
    public DistortedTexture getTexture()
226 50ac40a6 Leszek Koltunski
      {
227 f6d884d5 Leszek Koltunski
      return mTexture;
228 50ac40a6 Leszek Koltunski
      }
229 e8b6aa95 Leszek Koltunski
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231
232 7fca6741 Leszek Koltunski
    public MeshObject getMesh()
233 e8b6aa95 Leszek Koltunski
      {
234 b01acdaf Leszek Koltunski
      return mMesh;
235 e8b6aa95 Leszek Koltunski
      }
236 427ab7bf Leszek Koltunski
}