Project

General

Profile

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

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

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.DistortedCubes;
25
import org.distorted.library.DistortedObject;
26

    
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
public class CubesActivity extends Activity implements View.OnClickListener
41
{
42
    private static final int COLOR_OFF = 0xffffe81f;
43
    private static final int COLOR_ON  = 0xff0000ff;
44

    
45
    private int mNumCols = 3;
46
    private int mNumRows = 3;
47
    private NumberPicker mColsPicker, mRowsPicker;
48
    private LinearLayout mLay;
49
    private boolean[] mShape;
50
    private DistortedObject mObject;
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53
    
54
    @Override
55
    protected void onCreate(Bundle savedState) 
56
      {
57
      super.onCreate(savedState);
58

    
59
      setContentView(R.layout.cubespickerlayout);
60
      
61
      mLay = (LinearLayout)findViewById(R.id.cubespicker_buttongrid);
62
      
63
      mColsPicker = (NumberPicker)findViewById(R.id.cubespicker_cols);
64
      mRowsPicker = (NumberPicker)findViewById(R.id.cubespicker_rows);
65
      
66
      mColsPicker.setMaxValue(40);
67
      mColsPicker.setMinValue( 0);
68
      mRowsPicker.setMaxValue(40);
69
      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

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91
    
92
    private void setGrid()
93
      {
94
      mNumCols = mColsPicker.getValue();
95
      mNumRows = mRowsPicker.getValue();
96
      
97
      int width = mLay.getWidth();
98
      int height= mLay.getHeight();
99
      int w = mNumCols>0 ? (int)( 0.9f*width / mNumCols) : 0;
100
      int h = mNumRows>0 ? (int)( 0.9f*height/ mNumRows) : 0;
101
      int size= w<h ? w:h;
102
      int pad = size<20 ? 1 : size/20;
103
     
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
          b.setBackgroundColor(COLOR_ON);
129
          tr.addView(b, p);
130
          mShape[rows*mNumCols+cols] = true;
131
          }
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
    public void Create(View v)
194
      {
195
      String str = "";
196

    
197
      for(int i=0; i<mNumRows*mNumCols; i++)
198
        str += mShape[i] ? "1" : "0";
199

    
200
      mObject = new DistortedCubes(mNumCols, str, 10);
201

    
202
      setContentView(R.layout.cubeslayout);
203
      }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

    
207
    public DistortedObject getObject()
208
      {
209
      return mObject;
210
      }
211
}
(1-1/3)