Project

General

Profile

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

examples / src / main / java / org / distorted / examples / cubes / CubesActivity.java @ 57ba16f3

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