Project

General

Profile

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

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

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.DistortedTexture;
25
import org.distorted.library.MeshObject;
26
import org.distorted.library.MeshCubes;
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 DistortedTexture mTexture;
52
    private MeshObject mMesh;
53

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

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

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

    
110
      TableRow.LayoutParams p = new android.widget.TableRow.LayoutParams();
111

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

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

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

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

    
170
      Distorted.onPause();
171
      super.onPause();
172
      }
173

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

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

    
203
      mMesh = new MeshCubes(mNumCols, str, false);
204
      mTexture = new DistortedTexture(mNumCols,mNumRows);
205

    
206
      setContentView(R.layout.cubeslayout);
207
      }
208

    
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210

    
211
    public DistortedTexture getTexture()
212
      {
213
      return mTexture;
214
      }
215

    
216
///////////////////////////////////////////////////////////////////////////////////////////////////
217

    
218
    public MeshObject getMesh()
219
      {
220
      return mMesh;
221
      }
222
}
(1-1/3)