| 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 | 5068fa06 | Leszek Koltunski | import org.distorted.library.Distorted;
 | 
      
        | 23 |  |  | import org.distorted.examples.R;
 | 
      
        | 24 | f6d884d5 | Leszek Koltunski | import org.distorted.library.DistortedTexture;
 | 
      
        | 25 | 10b7e588 | Leszek Koltunski | import org.distorted.library.GridObject;
 | 
      
        | 26 |  |  | import org.distorted.library.GridCubes;
 | 
      
        | 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 |  |  | 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 | 462c74f4 | Leszek Koltunski | public class CubesActivity extends Activity implements View.OnClickListener
 | 
      
        | 42 | 427ab7bf | Leszek Koltunski | {
 | 
      
        | 43 |  |  |     private static final int COLOR_OFF = 0xffffe81f;
 | 
      
        | 44 |  |  |     private static final int COLOR_ON  = 0xff0000ff;
 | 
      
        | 45 | 50ac40a6 | Leszek Koltunski | 
 | 
      
        | 46 |  |  |     private int mNumCols = 3;
 | 
      
        | 47 |  |  |     private int mNumRows = 3;
 | 
      
        | 48 | 427ab7bf | Leszek Koltunski |     private NumberPicker mColsPicker, mRowsPicker;
 | 
      
        | 49 |  |  |     private LinearLayout mLay;
 | 
      
        | 50 | 50ac40a6 | Leszek Koltunski |     private boolean[] mShape;
 | 
      
        | 51 | f6d884d5 | Leszek Koltunski |     private DistortedTexture mTexture;
 | 
      
        | 52 | 10b7e588 | Leszek Koltunski |     private GridObject mGrid;
 | 
      
        | 53 | 50ac40a6 | Leszek Koltunski | 
 | 
      
        | 54 | 427ab7bf | Leszek Koltunski | ///////////////////////////////////////////////////////////////////////////////////////////////////
 | 
      
        | 55 |  |  |     
 | 
      
        | 56 |  |  |     @Override
 | 
      
        | 57 |  |  |     protected void onCreate(Bundle savedState) 
 | 
      
        | 58 |  |  |       {
 | 
      
        | 59 |  |  |       super.onCreate(savedState);
 | 
      
        | 60 |  |  | 
 | 
      
        | 61 | 462c74f4 | Leszek Koltunski |       setContentView(R.layout.cubespickerlayout);
 | 
      
        | 62 | 427ab7bf | Leszek Koltunski |       
 | 
      
        | 63 | 462c74f4 | Leszek Koltunski |       mLay = (LinearLayout)findViewById(R.id.cubespicker_buttongrid);
 | 
      
        | 64 | 427ab7bf | Leszek Koltunski |       
 | 
      
        | 65 | 462c74f4 | Leszek Koltunski |       mColsPicker = (NumberPicker)findViewById(R.id.cubespicker_cols);
 | 
      
        | 66 |  |  |       mRowsPicker = (NumberPicker)findViewById(R.id.cubespicker_rows);
 | 
      
        | 67 | 427ab7bf | Leszek Koltunski |       
 | 
      
        | 68 | 57ba16f3 | Leszek Koltunski |       mColsPicker.setMaxValue(40);
 | 
      
        | 69 | 427ab7bf | Leszek Koltunski |       mColsPicker.setMinValue( 0);
 | 
      
        | 70 | 57ba16f3 | Leszek Koltunski |       mRowsPicker.setMaxValue(40);
 | 
      
        | 71 | 427ab7bf | Leszek Koltunski |       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 | 50ac40a6 | Leszek Koltunski | 
 | 
      
        | 92 | 427ab7bf | Leszek Koltunski | ///////////////////////////////////////////////////////////////////////////////////////////////////
 | 
      
        | 93 |  |  |     
 | 
      
        | 94 | 50ac40a6 | Leszek Koltunski |     private void setGrid()
 | 
      
        | 95 | 427ab7bf | Leszek Koltunski |       {
 | 
      
        | 96 |  |  |       mNumCols = mColsPicker.getValue();
 | 
      
        | 97 |  |  |       mNumRows = mRowsPicker.getValue();
 | 
      
        | 98 |  |  |       
 | 
      
        | 99 |  |  |       int width = mLay.getWidth();
 | 
      
        | 100 |  |  |       int height= mLay.getHeight();
 | 
      
        | 101 | 57ba16f3 | Leszek Koltunski |       int w = mNumCols>0 ? (int)( 0.9f*width / mNumCols) : 0;
 | 
      
        | 102 |  |  |       int h = mNumRows>0 ? (int)( 0.9f*height/ mNumRows) : 0;
 | 
      
        | 103 | 427ab7bf | Leszek Koltunski |       int size= w<h ? w:h;
 | 
      
        | 104 | 57ba16f3 | Leszek Koltunski |       int pad = size<20 ? 1 : size/20;
 | 
      
        | 105 | 427ab7bf | Leszek Koltunski |      
 | 
      
        | 106 |  |  |       mLay.removeAllViews();
 | 
      
        | 107 |  |  |       
 | 
      
        | 108 |  |  |       mShape = new boolean[mNumRows*mNumCols];
 | 
      
        | 109 |  |  | 
 | 
      
        | 110 |  |  |       TableRow.LayoutParams p = new android.widget.TableRow.LayoutParams();
 | 
      
        | 111 | 6f3024ae | Leszek Koltunski | 
 | 
      
        | 112 | 427ab7bf | Leszek Koltunski |       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 | 57ba16f3 | Leszek Koltunski |           b.setBackgroundColor(COLOR_ON);
 | 
      
        | 131 | 427ab7bf | Leszek Koltunski |           tr.addView(b, p);
 | 
      
        | 132 | 57ba16f3 | Leszek Koltunski |           mShape[rows*mNumCols+cols] = true;
 | 
      
        | 133 | 427ab7bf | Leszek Koltunski |           }
 | 
      
        | 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 |  |  |       super.onPause();
 | 
      
        | 171 |  |  |       }
 | 
      
        | 172 |  |  | 
 | 
      
        | 173 |  |  | ///////////////////////////////////////////////////////////////////////////////////////////////////
 | 
      
        | 174 |  |  |     
 | 
      
        | 175 |  |  |     @Override
 | 
      
        | 176 |  |  |     protected void onResume() 
 | 
      
        | 177 |  |  |       {
 | 
      
        | 178 |  |  |       super.onResume();
 | 
      
        | 179 |  |  |       
 | 
      
        | 180 |  |  |       GLSurfaceView mView = (GLSurfaceView) this.findViewById(R.id.cubesSurfaceView);
 | 
      
        | 181 |  |  |       if( mView!=null ) mView.onResume();  
 | 
      
        | 182 |  |  |       }
 | 
      
        | 183 |  |  |     
 | 
      
        | 184 |  |  | ///////////////////////////////////////////////////////////////////////////////////////////////////
 | 
      
        | 185 |  |  |     
 | 
      
        | 186 |  |  |     @Override
 | 
      
        | 187 |  |  |     protected void onDestroy() 
 | 
      
        | 188 |  |  |       {
 | 
      
        | 189 |  |  |       Distorted.onDestroy();  
 | 
      
        | 190 |  |  |       super.onDestroy();
 | 
      
        | 191 |  |  |       }
 | 
      
        | 192 |  |  |  
 | 
      
        | 193 |  |  | ///////////////////////////////////////////////////////////////////////////////////////////////////
 | 
      
        | 194 |  |  |     
 | 
      
        | 195 | 50ac40a6 | Leszek Koltunski |     public void Create(View v)
 | 
      
        | 196 |  |  |       {
 | 
      
        | 197 | 462c74f4 | Leszek Koltunski |       String str = "";
 | 
      
        | 198 | 50ac40a6 | Leszek Koltunski | 
 | 
      
        | 199 | 462c74f4 | Leszek Koltunski |       for(int i=0; i<mNumRows*mNumCols; i++)
 | 
      
        | 200 |  |  |         str += mShape[i] ? "1" : "0";
 | 
      
        | 201 | 50ac40a6 | Leszek Koltunski | 
 | 
      
        | 202 | 10b7e588 | Leszek Koltunski |       mGrid = new GridCubes(mNumCols, str, false);
 | 
      
        | 203 | 7451c98a | Leszek Koltunski |       mTexture = new DistortedTexture(mNumCols,mNumRows);
 | 
      
        | 204 | 50ac40a6 | Leszek Koltunski | 
 | 
      
        | 205 |  |  |       setContentView(R.layout.cubeslayout);
 | 
      
        | 206 |  |  |       }
 | 
      
        | 207 |  |  | 
 | 
      
        | 208 |  |  | ///////////////////////////////////////////////////////////////////////////////////////////////////
 | 
      
        | 209 |  |  | 
 | 
      
        | 210 | f6d884d5 | Leszek Koltunski |     public DistortedTexture getTexture()
 | 
      
        | 211 | 50ac40a6 | Leszek Koltunski |       {
 | 
      
        | 212 | f6d884d5 | Leszek Koltunski |       return mTexture;
 | 
      
        | 213 | 50ac40a6 | Leszek Koltunski |       }
 | 
      
        | 214 | e8b6aa95 | Leszek Koltunski | 
 | 
      
        | 215 |  |  | ///////////////////////////////////////////////////////////////////////////////////////////////////
 | 
      
        | 216 |  |  | 
 | 
      
        | 217 | 10b7e588 | Leszek Koltunski |     public GridObject getGrid()
 | 
      
        | 218 | e8b6aa95 | Leszek Koltunski |       {
 | 
      
        | 219 |  |  |       return mGrid;
 | 
      
        | 220 |  |  |       }
 | 
      
        | 221 | 427ab7bf | Leszek Koltunski | }
 |