Revision 0dc8ffef
Added by Leszek Koltunski almost 4 years ago
src/main/java/org/distorted/examples/predeform/PredeformActivity.java | ||
---|---|---|
21 | 21 |
|
22 | 22 |
import android.app.Activity; |
23 | 23 |
import android.content.Intent; |
24 |
import android.graphics.Bitmap; |
|
25 |
import android.graphics.BitmapFactory; |
|
26 |
import android.graphics.Canvas; |
|
27 |
import android.graphics.Paint; |
|
24 | 28 |
import android.os.Bundle; |
25 |
import android.view.Gravity; |
|
26 | 29 |
import android.view.View; |
27 | 30 |
import android.widget.AdapterView; |
28 | 31 |
import android.widget.ArrayAdapter; |
29 |
import android.widget.Button; |
|
30 |
import android.widget.LinearLayout; |
|
31 | 32 |
import android.widget.NumberPicker; |
32 | 33 |
import android.widget.Spinner; |
33 |
import android.widget.TableRow; |
|
34 | 34 |
|
35 | 35 |
import org.distorted.examples.R; |
36 |
import org.distorted.library.mesh.MeshBase; |
|
37 |
|
|
38 |
import java.io.IOException; |
|
39 |
import java.io.InputStream; |
|
36 | 40 |
|
37 | 41 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
38 | 42 |
|
39 |
public class PredeformActivity extends Activity |
|
40 |
implements View.OnClickListener, AdapterView.OnItemSelectedListener |
|
43 |
public class PredeformActivity extends Activity implements AdapterView.OnItemSelectedListener |
|
41 | 44 |
{ |
42 |
private static final int COLOR_OFF = 0xffffe81f; |
|
43 |
private static final int COLOR_ON = 0xff0000ff; |
|
44 |
private static final int COLOR_INAC= 0xff999999; |
|
45 |
|
|
46 |
private int mNumCols = 1; |
|
47 |
private int mNumRows = 1; |
|
48 |
private int mNumSlic = 1; |
|
49 |
private boolean mGridInitialized; |
|
50 |
private NumberPicker mColsPicker, mRowsPicker, mSlicPicker; |
|
51 |
private boolean[] mShape; |
|
45 |
private int mNumCols; |
|
46 |
private int mNumRows; |
|
47 |
private int mNumSlic; |
|
52 | 48 |
private int mObjectType; |
53 | 49 |
private int mBitmapID; |
54 |
private LinearLayout mLay; |
|
50 |
|
|
51 |
private NumberPicker mColsPicker, mRowsPicker, mSlicPicker; |
|
52 |
private boolean[] mShape; |
|
53 |
private MeshBase mMesh; |
|
55 | 54 |
|
56 | 55 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
57 | 56 |
|
... | ... | |
60 | 59 |
{ |
61 | 60 |
super.onCreate(savedState); |
62 | 61 |
|
63 |
setContentView(R.layout.objectpickerlayout); |
|
62 |
mNumCols = 1; |
|
63 |
mNumRows = 1; |
|
64 |
mNumSlic = 1; |
|
65 |
mBitmapID=-1; |
|
66 |
resetShape(); |
|
64 | 67 |
|
65 |
mLay = findViewById(R.id.objectpicker_buttongrid);
|
|
68 |
setContentView(R.layout.object_picker_layout);
|
|
66 | 69 |
|
67 | 70 |
mColsPicker = findViewById(R.id.objectpicker_cols); |
68 | 71 |
mRowsPicker = findViewById(R.id.objectpicker_rows); |
... | ... | |
80 | 83 |
@Override |
81 | 84 |
public void onValueChange(NumberPicker picker, int oldVal, int newVal) |
82 | 85 |
{ |
83 |
setGrid(); |
|
86 |
mNumCols = newVal; |
|
87 |
resetShape(); |
|
84 | 88 |
} |
85 | 89 |
}); |
86 | 90 |
|
... | ... | |
89 | 93 |
@Override |
90 | 94 |
public void onValueChange(NumberPicker picker, int oldVal, int newVal) |
91 | 95 |
{ |
92 |
setGrid(); |
|
96 |
mNumRows = newVal; |
|
97 |
resetShape(); |
|
93 | 98 |
} |
94 | 99 |
}); |
95 | 100 |
|
... | ... | |
98 | 103 |
@Override |
99 | 104 |
public void onValueChange(NumberPicker picker, int oldVal, int newVal) |
100 | 105 |
{ |
101 |
mNumSlic = mSlicPicker.getValue(); |
|
106 |
mNumSlic = newVal; |
|
107 |
resetShape(); |
|
102 | 108 |
} |
103 | 109 |
}); |
104 | 110 |
|
105 | 111 |
mObjectType = 0; |
106 |
mGridInitialized = false; |
|
107 | 112 |
|
108 | 113 |
Spinner typeSpinner = findViewById(R.id.objectpicker_spinnerType); |
109 | 114 |
typeSpinner.setOnItemSelectedListener(this); |
... | ... | |
130 | 135 |
bitmapSpinner.setAdapter(adapterBitmap); |
131 | 136 |
} |
132 | 137 |
|
138 |
|
|
133 | 139 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
134 | 140 |
|
135 |
private void setGrid()
|
|
141 |
private void resetShape()
|
|
136 | 142 |
{ |
137 |
mGridInitialized = true;
|
|
143 |
mShape = new boolean[mNumRows*mNumCols];
|
|
138 | 144 |
|
139 |
mNumCols = mColsPicker.getValue(); |
|
140 |
mNumRows = mRowsPicker.getValue(); |
|
145 |
for (int rows=0; rows<mNumRows; rows++) |
|
146 |
{ |
|
147 |
for(int cols=0; cols<mNumCols; cols++) |
|
148 |
{ |
|
149 |
mShape[rows*mNumCols+cols] = true; |
|
150 |
} |
|
151 |
} |
|
141 | 152 |
|
142 |
int width = mLay.getWidth(); |
|
143 |
int height= mLay.getHeight(); |
|
144 |
int w = mNumCols>0 ? (int)( 0.9f*width / mNumCols) : 0; |
|
145 |
int h = mNumRows>0 ? (int)( 0.9f*height/ mNumRows) : 0; |
|
146 |
int size= Math.min(w, h); |
|
147 |
int pad = size<20 ? 1 : size/20; |
|
153 |
recreateObject(); |
|
154 |
} |
|
148 | 155 |
|
149 |
mLay.removeAllViews();
|
|
156 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
150 | 157 |
|
151 |
mShape = new boolean[mNumRows*mNumCols]; |
|
158 |
MeshBase getMesh() |
|
159 |
{ |
|
160 |
return mMesh; |
|
161 |
} |
|
152 | 162 |
|
153 |
TableRow.LayoutParams p = new TableRow.LayoutParams();
|
|
163 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
154 | 164 |
|
155 |
p.rightMargin = pad; |
|
156 |
p.leftMargin = pad; |
|
157 |
p.topMargin = pad; |
|
158 |
p.bottomMargin = pad; |
|
159 |
p.height = size; |
|
160 |
p.width = size; |
|
165 |
Bitmap getBitmap() |
|
166 |
{ |
|
167 |
return createBitmap(mBitmapID); |
|
168 |
} |
|
161 | 169 |
|
162 |
for (int rows=0; rows<mNumRows; rows++) |
|
170 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
171 |
|
|
172 |
private void recreateObject() |
|
173 |
{ |
|
174 |
String str = PredeformMeshList.getString(mObjectType, mNumCols, mNumRows, mShape); |
|
175 |
mMesh = PredeformMeshList.createMesh(mObjectType, mNumCols, mNumRows, mNumSlic, mBitmapID, str); |
|
176 |
} |
|
177 |
|
|
178 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
179 |
|
|
180 |
private Bitmap createBitmap(int bitmapID) |
|
181 |
{ |
|
182 |
int size = mNumCols > mNumRows ? (Math.max(mNumCols, mNumSlic)) : (Math.max(mNumRows, mNumSlic)); |
|
183 |
|
|
184 |
if( bitmapID!=-1) |
|
185 |
{ |
|
186 |
try (InputStream is = getResources().openRawResource(bitmapID)) |
|
187 |
{ |
|
188 |
return BitmapFactory.decodeStream(is); |
|
189 |
} |
|
190 |
catch( IOException ex ) { android.util.Log.e("act", "failed to open resource "+bitmapID); } |
|
191 |
} |
|
192 |
else |
|
163 | 193 |
{ |
164 |
TableRow tr = new TableRow(this);
|
|
165 |
tr.setGravity(Gravity.CENTER);
|
|
194 |
final int T = 64;
|
|
195 |
final int S = T*size;
|
|
166 | 196 |
|
167 |
for(int cols=0; cols<mNumCols; cols++) |
|
197 |
Paint paint = new Paint(); |
|
198 |
Bitmap bmp = Bitmap.createBitmap(S,S, Bitmap.Config.ARGB_8888); |
|
199 |
Canvas canvas = new Canvas(bmp); |
|
200 |
|
|
201 |
paint.setAntiAlias(true); |
|
202 |
paint.setTextAlign(Paint.Align.CENTER); |
|
203 |
paint.setColor(0xff008800); |
|
204 |
paint.setStyle(Paint.Style.FILL); |
|
205 |
canvas.drawRect(0, 0, S, S, paint); |
|
206 |
paint.setColor(0xffffffff); |
|
207 |
|
|
208 |
for(int i=0; i<=size ; i++ ) |
|
168 | 209 |
{ |
169 |
Button b = new Button(this); |
|
170 |
b.setOnClickListener(this); |
|
171 |
b.setId(rows*mNumCols+cols); |
|
172 |
b.setLayoutParams(p); |
|
173 |
b.setBackgroundColor(mObjectType==0 ? COLOR_ON:COLOR_INAC); |
|
174 |
tr.addView(b, p); |
|
175 |
mShape[rows*mNumCols+cols] = true; |
|
210 |
canvas.drawRect( T*i-1, 0, T*i+1, S, paint); |
|
211 |
canvas.drawRect( 0, T*i-1, S, T*i+1, paint); |
|
176 | 212 |
} |
177 | 213 |
|
178 |
mLay.addView(tr);
|
|
214 |
return bmp;
|
|
179 | 215 |
} |
216 |
|
|
217 |
return null; |
|
180 | 218 |
} |
181 | 219 |
|
182 | 220 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
183 | 221 |
|
184 |
public void onClick(View view)
|
|
222 |
void click(int row, int col)
|
|
185 | 223 |
{ |
186 | 224 |
if( mObjectType==0 ) // cubes |
187 | 225 |
{ |
188 |
Button tmp = (Button)view; |
|
189 |
int id = tmp.getId(); |
|
226 |
int id = row*mNumCols+col; |
|
190 | 227 |
mShape[id] = !mShape[id]; |
191 |
tmp.setBackgroundColor(mShape[id] ? COLOR_ON:COLOR_OFF);
|
|
228 |
recreateObject();
|
|
192 | 229 |
} |
193 | 230 |
} |
194 | 231 |
|
... | ... | |
196 | 233 |
|
197 | 234 |
private void uncheckAll() |
198 | 235 |
{ |
199 |
TableRow tr; |
|
200 |
Button butt; |
|
201 |
|
|
202 |
for (int row=0; row<mNumRows; row++) |
|
236 |
if( mObjectType==0 ) // cubes |
|
203 | 237 |
{ |
204 |
tr = (TableRow)mLay.getChildAt(row); |
|
238 |
for (int row=0; row<mNumRows; row++) |
|
239 |
for(int col=0; col<mNumCols; col++) |
|
240 |
mShape[row*mNumCols+col] = true; |
|
205 | 241 |
|
206 |
for(int col=0; col<mNumCols; col++) |
|
207 |
{ |
|
208 |
butt = (Button)tr.getVirtualChildAt(col); |
|
209 |
butt.setBackgroundColor(mObjectType==0 ? COLOR_ON : COLOR_INAC); |
|
210 |
mShape[row*mNumCols+col] = true; |
|
211 |
} |
|
242 |
recreateObject(); |
|
212 | 243 |
} |
213 | 244 |
} |
214 | 245 |
|
... | ... | |
247 | 278 |
mRowsPicker.setEnabled(dim>=1); |
248 | 279 |
mColsPicker.setEnabled(dim>=2); |
249 | 280 |
mSlicPicker.setEnabled(dim>=3); |
281 |
|
|
282 |
recreateObject(); |
|
250 | 283 |
} |
251 | 284 |
break; |
252 | 285 |
case R.id.objectpicker_spinnerBitmap: switch(pos) |
... | ... | |
260 | 293 |
case 6: mBitmapID = R.raw.monalisa; break; |
261 | 294 |
case 7: mBitmapID = R.raw.world ; break; |
262 | 295 |
} |
296 |
|
|
297 |
PredeformSurfaceView v = findViewById(R.id.predeformSurfaceView); |
|
298 |
v.getRenderer().setTexture( createBitmap(mBitmapID) ); |
|
263 | 299 |
break; |
264 | 300 |
} |
265 | 301 |
} |
... | ... | |
283 | 319 |
mRowsPicker.setValue(mNumRows); |
284 | 320 |
mSlicPicker.setValue(mNumSlic); |
285 | 321 |
|
286 |
if( !mGridInitialized ) setGrid();
|
|
322 |
resetShape();
|
|
287 | 323 |
} |
288 | 324 |
} |
Also available in: Unified diff
Progress with Predeform.