Project

General

Profile

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

examples / src / main / java / org / distorted / examples / cubes / CubesActivity.java @ b0ebdf5e

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