Project

General

Profile

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

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

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 01782e85 Leszek Koltunski
import org.distorted.library.main.Distorted;
23 5068fa06 Leszek Koltunski
import org.distorted.examples.R;
24 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedTexture;
25 57d7fdba Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
26 107e4b72 Leszek Koltunski
import org.distorted.library.mesh.MeshCubes;
27 427ab7bf Leszek Koltunski
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 9c3e749e Leszek Koltunski
import android.widget.AdapterView;
34
import android.widget.ArrayAdapter;
35 427ab7bf Leszek Koltunski
import android.widget.Button;
36
import android.widget.LinearLayout;
37
import android.widget.NumberPicker;
38
import android.widget.NumberPicker.OnValueChangeListener;
39 9c3e749e Leszek Koltunski
import android.widget.SeekBar;
40
import android.widget.Spinner;
41 427ab7bf Leszek Koltunski
import android.widget.TableRow;
42
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44
45 9c3e749e Leszek Koltunski
public class CubesActivity extends Activity implements View.OnClickListener, SeekBar.OnSeekBarChangeListener, AdapterView.OnItemSelectedListener
46 427ab7bf Leszek Koltunski
{
47
    private static final int COLOR_OFF = 0xffffe81f;
48
    private static final int COLOR_ON  = 0xff0000ff;
49 50ac40a6 Leszek Koltunski
50
    private int mNumCols = 3;
51
    private int mNumRows = 3;
52 3a438b27 leszek
    private int mNumSlic = 1;
53
    private NumberPicker mColsPicker, mRowsPicker, mSlicPicker;
54 427ab7bf Leszek Koltunski
    private LinearLayout mLay;
55 50ac40a6 Leszek Koltunski
    private boolean[] mShape;
56 f6d884d5 Leszek Koltunski
    private DistortedTexture mTexture;
57 57d7fdba Leszek Koltunski
    private MeshBase mMesh;
58 50ac40a6 Leszek Koltunski
59 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
60
    
61
    @Override
62
    protected void onCreate(Bundle savedState) 
63
      {
64
      super.onCreate(savedState);
65
66 462c74f4 Leszek Koltunski
      setContentView(R.layout.cubespickerlayout);
67 427ab7bf Leszek Koltunski
      
68 9c3e749e Leszek Koltunski
      mLay = findViewById(R.id.cubespicker_buttongrid);
69 427ab7bf Leszek Koltunski
      
70 9c3e749e Leszek Koltunski
      mColsPicker = findViewById(R.id.cubespicker_cols);
71
      mRowsPicker = findViewById(R.id.cubespicker_rows);
72
      mSlicPicker = findViewById(R.id.cubespicker_slices);
73 3a438b27 leszek
74 57ba16f3 Leszek Koltunski
      mColsPicker.setMaxValue(40);
75 427ab7bf Leszek Koltunski
      mColsPicker.setMinValue( 0);
76 57ba16f3 Leszek Koltunski
      mRowsPicker.setMaxValue(40);
77 427ab7bf Leszek Koltunski
      mRowsPicker.setMinValue( 0);
78 3a438b27 leszek
      mSlicPicker.setMaxValue(40);
79
      mSlicPicker.setMinValue( 0);
80
81 427ab7bf Leszek Koltunski
      mColsPicker.setOnValueChangedListener(new OnValueChangeListener() 
82
         {
83
         @Override
84
         public void onValueChange(NumberPicker picker, int oldVal, int newVal) 
85
           { 
86
           setGrid();
87
           }
88
         });
89
      
90
      mRowsPicker.setOnValueChangedListener(new OnValueChangeListener() 
91
         {
92
         @Override
93
         public void onValueChange(NumberPicker picker, int oldVal, int newVal) 
94
           { 
95
           setGrid();
96
           }
97
         });
98 3a438b27 leszek
99
      mSlicPicker.setOnValueChangedListener(new OnValueChangeListener()
100
         {
101
         @Override
102
         public void onValueChange(NumberPicker picker, int oldVal, int newVal)
103
           {
104
           mNumSlic = mSlicPicker.getValue();
105
           }
106
         });
107 427ab7bf Leszek Koltunski
      }
108 50ac40a6 Leszek Koltunski
109 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
110
    
111 50ac40a6 Leszek Koltunski
    private void setGrid()
112 427ab7bf Leszek Koltunski
      {
113
      mNumCols = mColsPicker.getValue();
114
      mNumRows = mRowsPicker.getValue();
115
      
116
      int width = mLay.getWidth();
117
      int height= mLay.getHeight();
118 57ba16f3 Leszek Koltunski
      int w = mNumCols>0 ? (int)( 0.9f*width / mNumCols) : 0;
119
      int h = mNumRows>0 ? (int)( 0.9f*height/ mNumRows) : 0;
120 427ab7bf Leszek Koltunski
      int size= w<h ? w:h;
121 57ba16f3 Leszek Koltunski
      int pad = size<20 ? 1 : size/20;
122 427ab7bf Leszek Koltunski
     
123
      mLay.removeAllViews();
124
      
125
      mShape = new boolean[mNumRows*mNumCols];
126
127
      TableRow.LayoutParams p = new android.widget.TableRow.LayoutParams();
128 6f3024ae Leszek Koltunski
129 427ab7bf Leszek Koltunski
      p.rightMargin  = pad;
130
      p.leftMargin   = pad;
131
      p.topMargin    = pad;
132
      p.bottomMargin = pad;
133
      p.height       = size;
134
      p.width        = size;
135
      
136
      for (int rows=0; rows<mNumRows; rows++) 
137
        {
138
        TableRow tr = new TableRow(this);
139
        tr.setGravity(Gravity.CENTER);
140
        
141
        for(int cols=0; cols<mNumCols; cols++) 
142
          {
143
          Button b = new Button(this);
144
          b.setOnClickListener(this);
145
          b.setId(rows*mNumCols+cols);
146
          b.setLayoutParams(p);          
147 57ba16f3 Leszek Koltunski
          b.setBackgroundColor(COLOR_ON);
148 427ab7bf Leszek Koltunski
          tr.addView(b, p);
149 57ba16f3 Leszek Koltunski
          mShape[rows*mNumCols+cols] = true;
150 427ab7bf Leszek Koltunski
          }
151
        
152
        mLay.addView(tr);
153
        }
154
      }
155
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157
    
158
    public void onClick(View view) 
159
      {
160
      Button tmp = (Button)view;  
161
      int id = tmp.getId();
162
      mShape[id] = !mShape[id];
163
      tmp.setBackgroundColor(mShape[id] ? COLOR_ON:COLOR_OFF);
164
      }
165
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167
    
168
    @Override
169
    public void onWindowFocusChanged(boolean hasFocus) 
170
      {
171
      super.onWindowFocusChanged(hasFocus);
172
173
      mColsPicker.setValue(mNumCols);
174
      mRowsPicker.setValue(mNumRows);
175 3a438b27 leszek
      mSlicPicker.setValue(mNumSlic);
176
177 427ab7bf Leszek Koltunski
      if( hasFocus ) setGrid();
178
      }
179
    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181
    
182
    @Override
183
    protected void onPause() 
184
      {
185 9c3e749e Leszek Koltunski
      GLSurfaceView mView = this.findViewById(R.id.cubesSurfaceView);
186 427ab7bf Leszek Koltunski
      if( mView!=null ) mView.onPause();
187 b0ebdf5e Leszek Koltunski
188
      Distorted.onPause();
189 427ab7bf Leszek Koltunski
      super.onPause();
190
      }
191
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193
    
194
    @Override
195
    protected void onResume() 
196
      {
197
      super.onResume();
198
      
199 9c3e749e Leszek Koltunski
      GLSurfaceView mView = this.findViewById(R.id.cubesSurfaceView);
200 427ab7bf Leszek Koltunski
      if( mView!=null ) mView.onResume();  
201
      }
202
    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204
    
205
    @Override
206
    protected void onDestroy() 
207
      {
208
      Distorted.onDestroy();  
209
      super.onDestroy();
210
      }
211
 
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213
    
214 50ac40a6 Leszek Koltunski
    public void Create(View v)
215
      {
216 462c74f4 Leszek Koltunski
      String str = "";
217 50ac40a6 Leszek Koltunski
218 462c74f4 Leszek Koltunski
      for(int i=0; i<mNumRows*mNumCols; i++)
219
        str += mShape[i] ? "1" : "0";
220 50ac40a6 Leszek Koltunski
221 3a438b27 leszek
      mMesh = new MeshCubes(mNumCols, str, mNumSlic);
222 7451c98a Leszek Koltunski
      mTexture = new DistortedTexture(mNumCols,mNumRows);
223 50ac40a6 Leszek Koltunski
224
      setContentView(R.layout.cubeslayout);
225 9c3e749e Leszek Koltunski
226
      Spinner renderSpinner  = findViewById(R.id.cubes_spinnerMode);
227
      renderSpinner.setOnItemSelectedListener(this);
228
229
      String[] objectBitmap = new String[] { "Render: Normal", "Render: OIT" };
230
231
      ArrayAdapter<String> adapterBitmap = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, objectBitmap);
232
      adapterBitmap.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
233
      renderSpinner.setAdapter(adapterBitmap);
234
235
      SeekBar transparencyBar = findViewById(R.id.cubesTransparency);
236
237
      transparencyBar.setOnSeekBarChangeListener(this);
238
239
      CubesSurfaceView view = this.findViewById(R.id.cubesSurfaceView);
240
      view.getRenderer().setTransparency(50);
241
      transparencyBar.setProgress(50);
242
      }
243
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245
246
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
247
      {
248
      CubesSurfaceView v = this.findViewById(R.id.cubesSurfaceView);
249
      CubesRenderer renderer = v.getRenderer();
250
251
      switch(parent.getId())
252
        {
253
        case R.id.cubes_spinnerMode: renderer.setRenderModeToOIT(pos==1);
254
                                     break;
255
        }
256
      }
257
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259
260
    public void onNothingSelected(AdapterView<?> parent)
261
      {
262
      }
263
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265
266
    public void onProgressChanged(SeekBar bar, int progress, boolean fromUser)
267
      {
268
      switch (bar.getId())
269
        {
270
        case R.id.cubesTransparency: CubesSurfaceView view = this.findViewById(R.id.cubesSurfaceView);
271
                                     view.getRenderer().setTransparency(progress);
272
                                     break;
273
        }
274 50ac40a6 Leszek Koltunski
      }
275
276 9c3e749e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
277
278
    public void onStartTrackingTouch(SeekBar bar) { }
279
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281
282
    public void onStopTrackingTouch(SeekBar bar)  { }
283
284 50ac40a6 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
285
286 f6d884d5 Leszek Koltunski
    public DistortedTexture getTexture()
287 50ac40a6 Leszek Koltunski
      {
288 f6d884d5 Leszek Koltunski
      return mTexture;
289 50ac40a6 Leszek Koltunski
      }
290 e8b6aa95 Leszek Koltunski
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292
293 57d7fdba Leszek Koltunski
    public MeshBase getMesh()
294 e8b6aa95 Leszek Koltunski
      {
295 b01acdaf Leszek Koltunski
      return mMesh;
296 e8b6aa95 Leszek Koltunski
      }
297 427ab7bf Leszek Koltunski
}