Project

General

Profile

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

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

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