Project

General

Profile

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

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

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.mesh.MeshBase;
26
import org.distorted.library.mesh.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.AdapterView;
34
import android.widget.ArrayAdapter;
35
import android.widget.Button;
36
import android.widget.LinearLayout;
37
import android.widget.NumberPicker;
38
import android.widget.NumberPicker.OnValueChangeListener;
39
import android.widget.SeekBar;
40
import android.widget.Spinner;
41
import android.widget.TableRow;
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

    
45
public class CubesActivity extends Activity implements View.OnClickListener, SeekBar.OnSeekBarChangeListener, AdapterView.OnItemSelectedListener
46
{
47
    private static final int COLOR_OFF = 0xffffe81f;
48
    private static final int COLOR_ON  = 0xff0000ff;
49

    
50
    private int mNumCols = 3;
51
    private int mNumRows = 3;
52
    private int mNumSlic = 1;
53
    private NumberPicker mColsPicker, mRowsPicker, mSlicPicker;
54
    private LinearLayout mLay;
55
    private boolean[] mShape;
56
    private DistortedTexture mTexture;
57
    private MeshBase mMesh;
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60
    
61
    @Override
62
    protected void onCreate(Bundle savedState) 
63
      {
64
      super.onCreate(savedState);
65

    
66
      setContentView(R.layout.cubespickerlayout);
67
      
68
      mLay = findViewById(R.id.cubespicker_buttongrid);
69
      
70
      mColsPicker = findViewById(R.id.cubespicker_cols);
71
      mRowsPicker = findViewById(R.id.cubespicker_rows);
72
      mSlicPicker = findViewById(R.id.cubespicker_slices);
73

    
74
      mColsPicker.setMaxValue(40);
75
      mColsPicker.setMinValue( 0);
76
      mRowsPicker.setMaxValue(40);
77
      mRowsPicker.setMinValue( 0);
78
      mSlicPicker.setMaxValue(40);
79
      mSlicPicker.setMinValue( 0);
80

    
81
      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

    
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
      }
108

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

    
127
      TableRow.LayoutParams p = new android.widget.TableRow.LayoutParams();
128

    
129
      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
          b.setBackgroundColor(COLOR_ON);
148
          tr.addView(b, p);
149
          mShape[rows*mNumCols+cols] = true;
150
          }
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
      mSlicPicker.setValue(mNumSlic);
176

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

    
188
      Distorted.onPause();
189
      super.onPause();
190
      }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193
    
194
    @Override
195
    protected void onResume() 
196
      {
197
      super.onResume();
198
      
199
      GLSurfaceView mView = this.findViewById(R.id.cubesSurfaceView);
200
      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
    public void Create(View v)
215
      {
216
      String str = "";
217

    
218
      for(int i=0; i<mNumRows*mNumCols; i++)
219
        str += mShape[i] ? "1" : "0";
220

    
221
      mMesh = new MeshCubes(mNumCols, str, mNumSlic);
222
      mTexture = new DistortedTexture(mNumCols,mNumRows);
223

    
224
      setContentView(R.layout.cubeslayout);
225

    
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
      }
275

    
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277

    
278
    public void onStartTrackingTouch(SeekBar bar) { }
279

    
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281

    
282
    public void onStopTrackingTouch(SeekBar bar)  { }
283

    
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285

    
286
    public DistortedTexture getTexture()
287
      {
288
      return mTexture;
289
      }
290

    
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292

    
293
    public MeshBase getMesh()
294
      {
295
      return mMesh;
296
      }
297
}
(1-1/3)