1
|
|
2
|
package org.distortedandroid.examples.cubes;
|
3
|
|
4
|
import org.distortedandroid.library.Distorted;
|
5
|
import org.distortedandroid.examples.R;
|
6
|
|
7
|
import android.app.Activity;
|
8
|
import android.opengl.GLSurfaceView;
|
9
|
import android.os.Bundle;
|
10
|
import android.view.Gravity;
|
11
|
import android.view.View;
|
12
|
import android.widget.Button;
|
13
|
import android.widget.LinearLayout;
|
14
|
import android.widget.NumberPicker;
|
15
|
import android.widget.NumberPicker.OnValueChangeListener;
|
16
|
import android.widget.TableRow;
|
17
|
|
18
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19
|
|
20
|
public class CubesActivity extends Activity implements View.OnClickListener
|
21
|
{
|
22
|
private static int mNumCols = 3;
|
23
|
private static int mNumRows = 3;
|
24
|
|
25
|
private static final int COLOR_OFF = 0xffffe81f;
|
26
|
private static final int COLOR_ON = 0xff0000ff;
|
27
|
|
28
|
private NumberPicker mColsPicker, mRowsPicker;
|
29
|
private LinearLayout mLay;
|
30
|
private static boolean[] mShape;
|
31
|
|
32
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
33
|
|
34
|
@Override
|
35
|
protected void onCreate(Bundle savedState)
|
36
|
{
|
37
|
super.onCreate(savedState);
|
38
|
|
39
|
setContentView(R.layout.cubes1layout);
|
40
|
|
41
|
mLay = (LinearLayout)findViewById(R.id.buttongrid);
|
42
|
|
43
|
mColsPicker = (NumberPicker)findViewById(R.id.colsPicker);
|
44
|
mRowsPicker = (NumberPicker)findViewById(R.id.rowsPicker);
|
45
|
|
46
|
mColsPicker.setMaxValue(10);
|
47
|
mColsPicker.setMinValue( 0);
|
48
|
mRowsPicker.setMaxValue(10);
|
49
|
mRowsPicker.setMinValue( 0);
|
50
|
|
51
|
mColsPicker.setOnValueChangedListener(new OnValueChangeListener()
|
52
|
{
|
53
|
@Override
|
54
|
public void onValueChange(NumberPicker picker, int oldVal, int newVal)
|
55
|
{
|
56
|
setGrid();
|
57
|
}
|
58
|
});
|
59
|
|
60
|
mRowsPicker.setOnValueChangedListener(new OnValueChangeListener()
|
61
|
{
|
62
|
@Override
|
63
|
public void onValueChange(NumberPicker picker, int oldVal, int newVal)
|
64
|
{
|
65
|
setGrid();
|
66
|
}
|
67
|
});
|
68
|
}
|
69
|
|
70
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
71
|
|
72
|
public static int getCols()
|
73
|
{
|
74
|
return mNumCols;
|
75
|
}
|
76
|
|
77
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
78
|
|
79
|
public static String getShape()
|
80
|
{
|
81
|
String str = "";
|
82
|
|
83
|
for(int i=0; i<mNumRows*mNumCols; i++)
|
84
|
str += mShape[i] ? "1" : "0";
|
85
|
|
86
|
//android.util.Log.d("CUBES", str);
|
87
|
|
88
|
return str;
|
89
|
}
|
90
|
|
91
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
92
|
|
93
|
public void setGrid()
|
94
|
{
|
95
|
mNumCols = mColsPicker.getValue();
|
96
|
mNumRows = mRowsPicker.getValue();
|
97
|
|
98
|
int width = mLay.getWidth();
|
99
|
int height= mLay.getHeight();
|
100
|
int w = mNumCols>0 ? (width / mNumCols) -10 : 0;
|
101
|
int h = mNumRows>0 ? (height/ mNumRows) -10 : 0;
|
102
|
int size= w<h ? w:h;
|
103
|
int pad = size/20;
|
104
|
|
105
|
mLay.removeAllViews();
|
106
|
|
107
|
mShape = new boolean[mNumRows*mNumCols];
|
108
|
|
109
|
TableRow.LayoutParams p = new android.widget.TableRow.LayoutParams();
|
110
|
|
111
|
p.rightMargin = pad;
|
112
|
p.leftMargin = pad;
|
113
|
p.topMargin = pad;
|
114
|
p.bottomMargin = pad;
|
115
|
p.height = size;
|
116
|
p.width = size;
|
117
|
|
118
|
for (int rows=0; rows<mNumRows; rows++)
|
119
|
{
|
120
|
TableRow tr = new TableRow(this);
|
121
|
tr.setGravity(Gravity.CENTER);
|
122
|
|
123
|
for(int cols=0; cols<mNumCols; cols++)
|
124
|
{
|
125
|
Button b = new Button(this);
|
126
|
b.setOnClickListener(this);
|
127
|
b.setId(rows*mNumCols+cols);
|
128
|
b.setLayoutParams(p);
|
129
|
b.setBackgroundColor(COLOR_OFF);
|
130
|
tr.addView(b, p);
|
131
|
mShape[rows*mNumCols+cols] = false;
|
132
|
}
|
133
|
|
134
|
mLay.addView(tr);
|
135
|
}
|
136
|
}
|
137
|
|
138
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
139
|
|
140
|
public void onClick(View view)
|
141
|
{
|
142
|
Button tmp = (Button)view;
|
143
|
int id = tmp.getId();
|
144
|
mShape[id] = !mShape[id];
|
145
|
tmp.setBackgroundColor(mShape[id] ? COLOR_ON:COLOR_OFF);
|
146
|
}
|
147
|
|
148
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
149
|
|
150
|
@Override
|
151
|
public void onWindowFocusChanged(boolean hasFocus)
|
152
|
{
|
153
|
super.onWindowFocusChanged(hasFocus);
|
154
|
|
155
|
mColsPicker.setValue(mNumCols);
|
156
|
mRowsPicker.setValue(mNumRows);
|
157
|
|
158
|
if( hasFocus ) setGrid();
|
159
|
}
|
160
|
|
161
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
162
|
|
163
|
@Override
|
164
|
protected void onPause()
|
165
|
{
|
166
|
GLSurfaceView mView = (GLSurfaceView) this.findViewById(R.id.cubesSurfaceView);
|
167
|
if( mView!=null ) mView.onPause();
|
168
|
|
169
|
super.onPause();
|
170
|
}
|
171
|
|
172
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
173
|
|
174
|
@Override
|
175
|
protected void onResume()
|
176
|
{
|
177
|
super.onResume();
|
178
|
|
179
|
GLSurfaceView mView = (GLSurfaceView) this.findViewById(R.id.cubesSurfaceView);
|
180
|
if( mView!=null ) mView.onResume();
|
181
|
}
|
182
|
|
183
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
184
|
|
185
|
@Override
|
186
|
protected void onDestroy()
|
187
|
{
|
188
|
Distorted.onDestroy();
|
189
|
super.onDestroy();
|
190
|
}
|
191
|
|
192
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
193
|
|
194
|
public void Continue(View v)
|
195
|
{
|
196
|
setContentView(R.layout.cubes2layout);
|
197
|
}
|
198
|
}
|