Project

General

Profile

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

examples / src / main / java / org / distorted / examples / cubes / CubesActivity.java @ 50ac40a6

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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

    
20
package org.distorted.examples.cubes;
21

    
22
import org.distorted.library.Distorted;
23
import org.distorted.examples.R;
24
import org.distorted.library.DistortedBitmap;
25
import org.distorted.library.DistortedCubes;
26
import org.distorted.library.DistortedObject;
27

    
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.AdapterView;
34
import android.widget.ArrayAdapter;
35
import android.widget.Button;
36
import android.widget.LinearLayout;
37
import android.widget.NumberPicker;
38
import android.widget.NumberPicker.OnValueChangeListener;
39
import android.widget.Spinner;
40
import android.widget.TableRow;
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

    
44
public class CubesActivity extends Activity implements View.OnClickListener, AdapterView.OnItemSelectedListener
45
{
46
    private static final int COLOR_OFF = 0xffffe81f;
47
    private static final int COLOR_ON  = 0xff0000ff;
48

    
49
    private int mNumCols = 3;
50
    private int mNumRows = 3;
51
    private NumberPicker mColsPicker, mRowsPicker;
52
    private LinearLayout mLay;
53
    private boolean[] mShape;
54
    private DistortedObject mObject;
55
    private int mObjectType;
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58
    
59
    @Override
60
    protected void onCreate(Bundle savedState) 
61
      {
62
      super.onCreate(savedState);
63

    
64
      setContentView(R.layout.objectpickerlayout);
65
      
66
      mLay = (LinearLayout)findViewById(R.id.objectpicker_buttongrid);
67
      
68
      mColsPicker = (NumberPicker)findViewById(R.id.objectpicker_cols);
69
      mRowsPicker = (NumberPicker)findViewById(R.id.objectpicker_rows);
70
      
71
      mColsPicker.setMaxValue(10);
72
      mColsPicker.setMinValue( 0);
73
      mRowsPicker.setMaxValue(10);
74
      mRowsPicker.setMinValue( 0);
75
     
76
      mColsPicker.setOnValueChangedListener(new OnValueChangeListener() 
77
         {
78
         @Override
79
         public void onValueChange(NumberPicker picker, int oldVal, int newVal) 
80
           { 
81
           setGrid();
82
           }
83
         });
84
      
85
      mRowsPicker.setOnValueChangedListener(new OnValueChangeListener() 
86
         {
87
         @Override
88
         public void onValueChange(NumberPicker picker, int oldVal, int newVal) 
89
           { 
90
           setGrid();
91
           }
92
         });
93

    
94
      mObjectType = 0;
95

    
96
      Spinner typeSpinner  = (Spinner)findViewById(R.id.objectpicker_spinnerType);
97
      typeSpinner.setOnItemSelectedListener(this);
98

    
99
      String[] objectType = new String[] {"DistortedCubes", "DistortedBitmap"};
100

    
101
      ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, objectType);
102
      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
103
      typeSpinner.setAdapter(adapter);
104
      }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107
    
108
    private void setGrid()
109
      {
110
      mNumCols = mColsPicker.getValue();
111
      mNumRows = mRowsPicker.getValue();
112
      
113
      int width = mLay.getWidth();
114
      int height= mLay.getHeight();
115
      int w = mNumCols>0 ? (width / mNumCols) -10 : 0;
116
      int h = mNumRows>0 ? (height/ mNumRows) -10 : 0;
117
      int size= w<h ? w:h;
118
      int pad = size/20;
119
     
120
      mLay.removeAllViews();
121
      
122
      mShape = new boolean[mNumRows*mNumCols];
123

    
124
      TableRow.LayoutParams p = new android.widget.TableRow.LayoutParams();
125
          
126
      p.rightMargin  = pad;
127
      p.leftMargin   = pad;
128
      p.topMargin    = pad;
129
      p.bottomMargin = pad;
130
      p.height       = size;
131
      p.width        = size;
132
      
133
      for (int rows=0; rows<mNumRows; rows++) 
134
        {
135
        TableRow tr = new TableRow(this);
136
        tr.setGravity(Gravity.CENTER);
137
        
138
        for(int cols=0; cols<mNumCols; cols++) 
139
          {
140
          Button b = new Button(this);
141
          b.setOnClickListener(this);
142
          b.setId(rows*mNumCols+cols);
143
          b.setLayoutParams(p);          
144
          b.setBackgroundColor(COLOR_OFF);
145
          tr.addView(b, p);
146
          mShape[rows*mNumCols+cols] = false;
147
          }
148
        
149
        mLay.addView(tr);
150
        }
151
      }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

    
155
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
156
    {
157
    switch(parent.getId())
158
      {
159
      case R.id.objectpicker_spinnerType: mObjectType = pos; break;
160
      }
161
    }
162

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164

    
165
  public void onNothingSelected(AdapterView<?> parent)
166
    {
167
    }
168

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170
    
171
    public void onClick(View view) 
172
      {
173
      Button tmp = (Button)view;  
174
      int id = tmp.getId();
175
      mShape[id] = !mShape[id];
176
      tmp.setBackgroundColor(mShape[id] ? COLOR_ON:COLOR_OFF);
177
      }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180
    
181
    @Override
182
    public void onWindowFocusChanged(boolean hasFocus) 
183
      {
184
      super.onWindowFocusChanged(hasFocus);
185

    
186
      mColsPicker.setValue(mNumCols);
187
      mRowsPicker.setValue(mNumRows);
188
      
189
      if( hasFocus ) setGrid();
190
      }
191
    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193
    
194
    @Override
195
    protected void onPause() 
196
      {
197
      GLSurfaceView mView = (GLSurfaceView) this.findViewById(R.id.cubesSurfaceView);
198
      if( mView!=null ) mView.onPause();
199
        
200
      super.onPause();
201
      }
202

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204
    
205
    @Override
206
    protected void onResume() 
207
      {
208
      super.onResume();
209
      
210
      GLSurfaceView mView = (GLSurfaceView) this.findViewById(R.id.cubesSurfaceView);
211
      if( mView!=null ) mView.onResume();  
212
      }
213
    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215
    
216
    @Override
217
    protected void onDestroy() 
218
      {
219
      Distorted.onDestroy();  
220
      super.onDestroy();
221
      }
222
 
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224
    
225
    public void Create(View v)
226
      {
227
      if( mObjectType==1 )
228
        {
229
        mObject = new DistortedBitmap(100,100,mNumCols);
230
        }
231
      else
232
        {
233
        String str = "";
234

    
235
        for(int i=0; i<mNumRows*mNumCols; i++)
236
          str += mShape[i] ? "1" : "0";
237

    
238
        mObject = new DistortedCubes(mNumCols, str, 10);
239
        }
240

    
241
      setContentView(R.layout.cubeslayout);
242
      }
243

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

    
246
    public DistortedObject getObject()
247
      {
248
      return mObject;
249
      }
250
}
(1-1/3)