Project

General

Profile

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

examples / src / main / java / org / distorted / examples / cubes / CubesActivity.java @ 462c74f4

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.Button;
34
import android.widget.LinearLayout;
35
import android.widget.NumberPicker;
36
import android.widget.NumberPicker.OnValueChangeListener;
37
import android.widget.TableRow;
38

    
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40

    
41
public class CubesActivity extends Activity implements View.OnClickListener
42
{
43
    private static final int COLOR_OFF = 0xffffe81f;
44
    private static final int COLOR_ON  = 0xff0000ff;
45

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

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

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

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92
    
93
    private void setGrid()
94
      {
95
      mNumCols = mColsPicker.getValue();
96
      mNumRows = mRowsPicker.getValue();
97
      
98
      int width = mLay.getWidth();
99
      int height= mLay.getHeight();
100
      int w = mNumCols>0 ? (width / mNumCols) -10 : 0;
101
      int h = mNumRows>0 ? (height/ mNumRows) -10 : 0;
102
      int size= w<h ? w:h;
103
      int pad = size/20;
104
     
105
      mLay.removeAllViews();
106
      
107
      mShape = new boolean[mNumRows*mNumCols];
108

    
109
      TableRow.LayoutParams p = new android.widget.TableRow.LayoutParams();
110
          
111
      p.rightMargin  = pad;
112
      p.leftMargin   = pad;
113
      p.topMargin    = pad;
114
      p.bottomMargin = pad;
115
      p.height       = size;
116
      p.width        = size;
117
      
118
      for (int rows=0; rows<mNumRows; rows++) 
119
        {
120
        TableRow tr = new TableRow(this);
121
        tr.setGravity(Gravity.CENTER);
122
        
123
        for(int cols=0; cols<mNumCols; cols++) 
124
          {
125
          Button b = new Button(this);
126
          b.setOnClickListener(this);
127
          b.setId(rows*mNumCols+cols);
128
          b.setLayoutParams(p);          
129
          b.setBackgroundColor(COLOR_OFF);
130
          tr.addView(b, p);
131
          mShape[rows*mNumCols+cols] = false;
132
          }
133
        
134
        mLay.addView(tr);
135
        }
136
      }
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139
    
140
    public void onClick(View view) 
141
      {
142
      Button tmp = (Button)view;  
143
      int id = tmp.getId();
144
      mShape[id] = !mShape[id];
145
      tmp.setBackgroundColor(mShape[id] ? COLOR_ON:COLOR_OFF);
146
      }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149
    
150
    @Override
151
    public void onWindowFocusChanged(boolean hasFocus) 
152
      {
153
      super.onWindowFocusChanged(hasFocus);
154

    
155
      mColsPicker.setValue(mNumCols);
156
      mRowsPicker.setValue(mNumRows);
157
      
158
      if( hasFocus ) setGrid();
159
      }
160
    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162
    
163
    @Override
164
    protected void onPause() 
165
      {
166
      GLSurfaceView mView = (GLSurfaceView) this.findViewById(R.id.cubesSurfaceView);
167
      if( mView!=null ) mView.onPause();
168
        
169
      super.onPause();
170
      }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173
    
174
    @Override
175
    protected void onResume() 
176
      {
177
      super.onResume();
178
      
179
      GLSurfaceView mView = (GLSurfaceView) this.findViewById(R.id.cubesSurfaceView);
180
      if( mView!=null ) mView.onResume();  
181
      }
182
    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184
    
185
    @Override
186
    protected void onDestroy() 
187
      {
188
      Distorted.onDestroy();  
189
      super.onDestroy();
190
      }
191
 
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193
    
194
    public void Create(View v)
195
      {
196
      String str = "";
197

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

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

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

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

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