Project

General

Profile

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

examples / src / main / java / org / distorted / examples / cubes / CubesActivity.java @ 01782e85

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.main.Distorted;
23
import org.distorted.examples.R;
24
import org.distorted.library.main.DistortedTexture;
25
import org.distorted.library.main.MeshObject;
26
import org.distorted.library.main.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 int mNumSlic = 1;
49
    private NumberPicker mColsPicker, mRowsPicker, mSlicPicker;
50
    private LinearLayout mLay;
51
    private boolean[] mShape;
52
    private DistortedTexture mTexture;
53
    private MeshObject mMesh;
54

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

    
62
      setContentView(R.layout.cubespickerlayout);
63
      
64
      mLay = (LinearLayout)findViewById(R.id.cubespicker_buttongrid);
65
      
66
      mColsPicker = (NumberPicker)findViewById(R.id.cubespicker_cols);
67
      mRowsPicker = (NumberPicker)findViewById(R.id.cubespicker_rows);
68
      mSlicPicker = (NumberPicker)findViewById(R.id.cubespicker_slices);
69

    
70
      mColsPicker.setMaxValue(40);
71
      mColsPicker.setMinValue( 0);
72
      mRowsPicker.setMaxValue(40);
73
      mRowsPicker.setMinValue( 0);
74
      mSlicPicker.setMaxValue(40);
75
      mSlicPicker.setMinValue( 0);
76

    
77
      mColsPicker.setOnValueChangedListener(new OnValueChangeListener() 
78
         {
79
         @Override
80
         public void onValueChange(NumberPicker picker, int oldVal, int newVal) 
81
           { 
82
           setGrid();
83
           }
84
         });
85
      
86
      mRowsPicker.setOnValueChangedListener(new OnValueChangeListener() 
87
         {
88
         @Override
89
         public void onValueChange(NumberPicker picker, int oldVal, int newVal) 
90
           { 
91
           setGrid();
92
           }
93
         });
94

    
95
      mSlicPicker.setOnValueChangedListener(new OnValueChangeListener()
96
         {
97
         @Override
98
         public void onValueChange(NumberPicker picker, int oldVal, int newVal)
99
           {
100
           mNumSlic = mSlicPicker.getValue();
101
           }
102
         });
103
      }
104

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106
    
107
    private void setGrid()
108
      {
109
      mNumCols = mColsPicker.getValue();
110
      mNumRows = mRowsPicker.getValue();
111
      
112
      int width = mLay.getWidth();
113
      int height= mLay.getHeight();
114
      int w = mNumCols>0 ? (int)( 0.9f*width / mNumCols) : 0;
115
      int h = mNumRows>0 ? (int)( 0.9f*height/ mNumRows) : 0;
116
      int size= w<h ? w:h;
117
      int pad = size<20 ? 1 : size/20;
118
     
119
      mLay.removeAllViews();
120
      
121
      mShape = new boolean[mNumRows*mNumCols];
122

    
123
      TableRow.LayoutParams p = new android.widget.TableRow.LayoutParams();
124

    
125
      p.rightMargin  = pad;
126
      p.leftMargin   = pad;
127
      p.topMargin    = pad;
128
      p.bottomMargin = pad;
129
      p.height       = size;
130
      p.width        = size;
131
      
132
      for (int rows=0; rows<mNumRows; rows++) 
133
        {
134
        TableRow tr = new TableRow(this);
135
        tr.setGravity(Gravity.CENTER);
136
        
137
        for(int cols=0; cols<mNumCols; cols++) 
138
          {
139
          Button b = new Button(this);
140
          b.setOnClickListener(this);
141
          b.setId(rows*mNumCols+cols);
142
          b.setLayoutParams(p);          
143
          b.setBackgroundColor(COLOR_ON);
144
          tr.addView(b, p);
145
          mShape[rows*mNumCols+cols] = true;
146
          }
147
        
148
        mLay.addView(tr);
149
        }
150
      }
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153
    
154
    public void onClick(View view) 
155
      {
156
      Button tmp = (Button)view;  
157
      int id = tmp.getId();
158
      mShape[id] = !mShape[id];
159
      tmp.setBackgroundColor(mShape[id] ? COLOR_ON:COLOR_OFF);
160
      }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163
    
164
    @Override
165
    public void onWindowFocusChanged(boolean hasFocus) 
166
      {
167
      super.onWindowFocusChanged(hasFocus);
168

    
169
      mColsPicker.setValue(mNumCols);
170
      mRowsPicker.setValue(mNumRows);
171
      mSlicPicker.setValue(mNumSlic);
172

    
173
      if( hasFocus ) setGrid();
174
      }
175
    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177
    
178
    @Override
179
    protected void onPause() 
180
      {
181
      GLSurfaceView mView = (GLSurfaceView) this.findViewById(R.id.cubesSurfaceView);
182
      if( mView!=null ) mView.onPause();
183

    
184
      Distorted.onPause();
185
      super.onPause();
186
      }
187

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189
    
190
    @Override
191
    protected void onResume() 
192
      {
193
      super.onResume();
194
      
195
      GLSurfaceView mView = (GLSurfaceView) this.findViewById(R.id.cubesSurfaceView);
196
      if( mView!=null ) mView.onResume();  
197
      }
198
    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200
    
201
    @Override
202
    protected void onDestroy() 
203
      {
204
      Distorted.onDestroy();  
205
      super.onDestroy();
206
      }
207
 
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209
    
210
    public void Create(View v)
211
      {
212
      String str = "";
213

    
214
      for(int i=0; i<mNumRows*mNumCols; i++)
215
        str += mShape[i] ? "1" : "0";
216

    
217
      mMesh = new MeshCubes(mNumCols, str, mNumSlic);
218
      mTexture = new DistortedTexture(mNumCols,mNumRows);
219

    
220
      setContentView(R.layout.cubeslayout);
221
      }
222

    
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224

    
225
    public DistortedTexture getTexture()
226
      {
227
      return mTexture;
228
      }
229

    
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231

    
232
    public MeshObject getMesh()
233
      {
234
      return mMesh;
235
      }
236
}
(1-1/3)