Project

General

Profile

« Previous | Next » 

Revision 4b7c432e

Added by Leszek Koltunski almost 4 years ago

Progress (?) with Predeform: give up trying to render the Mesh in Activity1.

View differences:

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;
28 24
import android.os.Bundle;
25
import android.view.Gravity;
29 26
import android.view.View;
30 27
import android.widget.AdapterView;
31 28
import android.widget.ArrayAdapter;
29
import android.widget.Button;
30
import android.widget.LinearLayout;
32 31
import android.widget.NumberPicker;
33 32
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;
40 36

  
41 37
///////////////////////////////////////////////////////////////////////////////////////////////////
42 38

  
43
public class PredeformActivity extends Activity implements AdapterView.OnItemSelectedListener
39
public class PredeformActivity extends Activity
40
                               implements View.OnClickListener, AdapterView.OnItemSelectedListener
44 41
  {
45
  private int mNumCols;
46
  private int mNumRows;
47
  private int mNumSlic;
48
  private int mObjectType;
49
  private int mBitmapID;
50

  
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;
51 50
  private NumberPicker mColsPicker, mRowsPicker, mSlicPicker;
52 51
  private boolean[] mShape;
53
  private MeshBase mMesh;
52
  private int mObjectType;
53
  private int mBitmapID;
54
  private LinearLayout mLay;
54 55

  
55 56
///////////////////////////////////////////////////////////////////////////////////////////////////
56 57

  
......
59 60
    {
60 61
    super.onCreate(savedState);
61 62

  
62
    mNumCols = 1;
63
    mNumRows = 1;
64
    mNumSlic = 1;
65
    mBitmapID=-1;
66
    resetShape();
63
    setContentView(R.layout.objectpickerlayout);
67 64

  
68
    setContentView(R.layout.object_picker_layout);
65
    mLay = findViewById(R.id.objectpicker_buttongrid);
69 66

  
70 67
    mColsPicker = findViewById(R.id.objectpicker_cols);
71 68
    mRowsPicker = findViewById(R.id.objectpicker_rows);
......
83 80
         @Override
84 81
         public void onValueChange(NumberPicker picker, int oldVal, int newVal)
85 82
           {
86
           mNumCols = newVal;
87
           resetShape();
83
           setGrid();
88 84
           }
89 85
         });
90 86

  
......
93 89
         @Override
94 90
         public void onValueChange(NumberPicker picker, int oldVal, int newVal)
95 91
           {
96
           mNumRows = newVal;
97
           resetShape();
92
           setGrid();
98 93
           }
99 94
         });
100 95

  
......
103 98
         @Override
104 99
         public void onValueChange(NumberPicker picker, int oldVal, int newVal)
105 100
           {
106
           mNumSlic = newVal;
107
           resetShape();
101
           mNumSlic = mSlicPicker.getValue();
108 102
           }
109 103
         });
110 104

  
111 105
    mObjectType = 0;
106
    mGridInitialized = false;
112 107

  
113 108
    Spinner typeSpinner  = findViewById(R.id.objectpicker_spinnerType);
114 109
    typeSpinner.setOnItemSelectedListener(this);
115 110

  
116 111
    String[] objectType = new String[PredeformMeshList.LENGTH];
117 112

  
118
    for(int mesh = 0; mesh< PredeformMeshList.LENGTH; mesh++)
113
    for(int mesh=0; mesh<PredeformMeshList.LENGTH; mesh++)
119 114
      {
120
      objectType[mesh] = "Mesh: "+ PredeformMeshList.getName(mesh);
115
      objectType[mesh] = "Mesh: "+PredeformMeshList.getName(mesh);
121 116
      }
122 117

  
123 118
    ArrayAdapter<String> adapterType = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, objectType);
......
135 130
    bitmapSpinner.setAdapter(adapterBitmap);
136 131
    }
137 132

  
138

  
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

  
141
  private void resetShape()
142
    {
143
    mShape = new boolean[mNumRows*mNumCols];
144

  
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
      }
152

  
153
    recreateObject();
154
    }
155

  
156 133
///////////////////////////////////////////////////////////////////////////////////////////////////
157 134

  
158
  MeshBase getMesh()
135
  private void setGrid()
159 136
    {
160
    return mMesh;
161
    }
137
    mGridInitialized = true;
162 138

  
163
///////////////////////////////////////////////////////////////////////////////////////////////////
139
    mNumCols = mColsPicker.getValue();
140
    mNumRows = mRowsPicker.getValue();
164 141

  
165
  Bitmap getBitmap()
166
    {
167
    return createBitmap(mBitmapID);
168
    }
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;
169 148

  
170
///////////////////////////////////////////////////////////////////////////////////////////////////
149
    mLay.removeAllViews();
171 150

  
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
    }
151
    mShape = new boolean[mNumRows*mNumCols];
177 152

  
178
///////////////////////////////////////////////////////////////////////////////////////////////////
153
    TableRow.LayoutParams p = new TableRow.LayoutParams();
179 154

  
180
  private Bitmap createBitmap(int bitmapID)
181
    {
182
    int size = mNumCols > mNumRows ? (Math.max(mNumCols, mNumSlic)) : (Math.max(mNumRows, mNumSlic));
155
    p.rightMargin  = pad;
156
    p.leftMargin   = pad;
157
    p.topMargin    = pad;
158
    p.bottomMargin = pad;
159
    p.height       = size;
160
    p.width        = size;
183 161

  
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
162
    for (int rows=0; rows<mNumRows; rows++)
193 163
      {
194
      final int T = 64;
195
      final int S = T*size;
196

  
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);
164
      TableRow tr = new TableRow(this);
165
      tr.setGravity(Gravity.CENTER);
207 166

  
208
      for(int i=0; i<=size ; i++ )
167
      for(int cols=0; cols<mNumCols; cols++)
209 168
        {
210
        canvas.drawRect( T*i-1, 0, T*i+1, S, paint);
211
        canvas.drawRect( 0, T*i-1, S, T*i+1, paint);
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;
212 176
        }
213 177

  
214
      return bmp;
178
      mLay.addView(tr);
215 179
      }
216

  
217
    return null;
218 180
    }
219 181

  
220 182
///////////////////////////////////////////////////////////////////////////////////////////////////
221 183

  
222
  void click(int row, int col)
184
  public void onClick(View view)
223 185
    {
224 186
    if( mObjectType==0 )  // cubes
225 187
      {
226
      int id = row*mNumCols+col;
188
      Button tmp = (Button)view;
189
      int id = tmp.getId();
227 190
      mShape[id] = !mShape[id];
228
      recreateObject();
191
      tmp.setBackgroundColor(mShape[id] ? COLOR_ON:COLOR_OFF);
229 192
      }
230 193
    }
231 194

  
......
233 196

  
234 197
  private void uncheckAll()
235 198
    {
236
    if( mObjectType==0 )  // cubes
199
    TableRow tr;
200
    Button butt;
201

  
202
    for (int row=0; row<mNumRows; row++)
237 203
      {
238
      for (int row=0; row<mNumRows; row++)
239
        for(int col=0; col<mNumCols; col++)
240
          mShape[row*mNumCols+col] = true;
204
      tr = (TableRow)mLay.getChildAt(row);
241 205

  
242
      recreateObject();
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
        }
243 212
      }
244 213
    }
245 214

  
......
278 247
                                              mRowsPicker.setEnabled(dim>=1);
279 248
                                              mColsPicker.setEnabled(dim>=2);
280 249
                                              mSlicPicker.setEnabled(dim>=3);
281

  
282
                                              recreateObject();
283 250
                                              }
284 251
                                            break;
285 252
      case R.id.objectpicker_spinnerBitmap: switch(pos)
......
293 260
                                              case 6: mBitmapID = R.raw.monalisa; break;
294 261
                                              case 7: mBitmapID = R.raw.world   ; break;
295 262
                                              }
296

  
297
                                            PredeformSurfaceView v = findViewById(R.id.predeformSurfaceView);
298
                                            v.getRenderer().setTexture( createBitmap(mBitmapID) );
299 263
                                            break;
300 264
      }
301 265
    }
......
319 283
    mRowsPicker.setValue(mNumRows);
320 284
    mSlicPicker.setValue(mNumSlic);
321 285

  
322
    resetShape();
286
    if( !mGridInitialized ) setGrid();
323 287
    }
324 288
  }

Also available in: Unified diff