Project

General

Profile

Download (10.2 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / magic / RubikActivity.java @ 22fdfc36

1 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4 ffd744be Leszek Koltunski
// This file is part of Distorted.                                                               //
5 0c52af30 Leszek Koltunski
//                                                                                               //
6 ffd744be Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 0c52af30 Leszek Koltunski
// 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 ffd744be Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 0c52af30 Leszek Koltunski
// 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 ffd744be Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20
package org.distorted.magic;
21
22 fe381d8e Leszek Koltunski
import android.content.SharedPreferences;
23 0c52af30 Leszek Koltunski
import android.graphics.PorterDuff;
24
import android.graphics.drawable.Drawable;
25
import android.opengl.GLSurfaceView;
26
import android.os.Bundle;
27 fe381d8e Leszek Koltunski
import android.preference.PreferenceManager;
28 0c52af30 Leszek Koltunski
import android.support.v4.content.ContextCompat;
29 f6fcf06a Leszek Koltunski
import android.support.v7.app.AppCompatActivity;
30 0c52af30 Leszek Koltunski
import android.view.View;
31
32 f548942f Leszek Koltunski
import org.distorted.component.HorizontalNumberPicker;
33 3b12e641 Leszek Koltunski
import org.distorted.effect.ScrambleEffect;
34 086a24d0 Leszek Koltunski
import org.distorted.effect.SizeChangeEffect;
35 2ecf0c21 Leszek Koltunski
import org.distorted.effect.SolveEffect;
36 e1111500 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
37 0c52af30 Leszek Koltunski
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39
40 f6fcf06a Leszek Koltunski
public class RubikActivity extends AppCompatActivity implements RubikSettings.OnCompleteListener
41 0c52af30 Leszek Koltunski
{
42
            static final int DEFAULT_SIZE  = 3;
43 34998c9d Leszek Koltunski
    private static final int SMALLEST_SIZE = 2;
44 0c52af30 Leszek Koltunski
    private static final int[] button_ids  = {R.id.rubikSize2, R.id.rubikSize3, R.id.rubikSize4};
45
46 584f7954 Leszek Koltunski
    public static final int DEFAULT_SIZECHANGE_POS = 20;
47 2ecf0c21 Leszek Koltunski
    public static final int DEFAULT_SOLVE_POS      = 20;
48 30b68322 Leszek Koltunski
    public static final int DEFAULT_SCRAMBLE_POS   =100;
49 584f7954 Leszek Koltunski
    public static final int DEFAULT_SIZECHANGE_TYPE= 1;
50 2ecf0c21 Leszek Koltunski
    public static final int DEFAULT_SOLVE_TYPE     = 1;
51 3b12e641 Leszek Koltunski
    public static final int DEFAULT_SCRAMBLE_TYPE  = 0;
52 fe381d8e Leszek Koltunski
53 f548942f Leszek Koltunski
    public static final int MIN_SCRAMBLE =  1;
54
    public static final int MAX_SCRAMBLE = 10;
55
56 ee5c2ae1 Leszek Koltunski
    private static int mSize = DEFAULT_SIZE;
57 34747dd1 Leszek Koltunski
58 2ecf0c21 Leszek Koltunski
    private int mSizeChangePos, mSolvePos, mScramblePos;
59
    private int mSizeChangeType, mSolveType, mScrambleType;
60 f6fcf06a Leszek Koltunski
61 f548942f Leszek Koltunski
    private HorizontalNumberPicker mPicker;
62
63 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
64
65
    @Override
66 34747dd1 Leszek Koltunski
    protected void onCreate(Bundle savedState)
67 0c52af30 Leszek Koltunski
      {
68 34747dd1 Leszek Koltunski
      super.onCreate(savedState);
69 f6fcf06a Leszek Koltunski
      setTheme(R.style.CustomActivityThemeNoActionBar);
70 1cbcc6bf Leszek Koltunski
      setContentView(R.layout.main);
71 5e307a01 Leszek Koltunski
      markButton(mSize);
72 f6fcf06a Leszek Koltunski
73 f548942f Leszek Koltunski
      mPicker = findViewById(R.id.rubikNumberPicker);
74
      mPicker.setMin(MIN_SCRAMBLE);
75
      mPicker.setMax(MAX_SCRAMBLE);
76
77 fe381d8e Leszek Koltunski
      restorePreferences();
78
      applyPreferences();
79 0c52af30 Leszek Koltunski
      }
80
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82
    
83
    @Override
84
    protected void onPause() 
85
      {
86
      GLSurfaceView view = findViewById(R.id.rubikSurfaceView);
87
      view.onPause();
88 e1111500 Leszek Koltunski
      DistortedLibrary.onPause();
89 fe381d8e Leszek Koltunski
      savePreferences();
90 0c52af30 Leszek Koltunski
      super.onPause();
91
      }
92
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94
    
95
    @Override
96
    protected void onResume() 
97
      {
98
      super.onResume();
99
      GLSurfaceView view = findViewById(R.id.rubikSurfaceView);
100
      view.onResume();
101
      }
102
    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104
    
105
    @Override
106
    protected void onDestroy() 
107
      {
108 e1111500 Leszek Koltunski
      DistortedLibrary.onDestroy();
109 0c52af30 Leszek Koltunski
      super.onDestroy();
110
      }
111
112 34747dd1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
113
114
    static int getSize()
115
      {
116
      return mSize;
117
      }
118
119 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
120
121 a47ee432 Leszek Koltunski
    public void Settings(View v)
122 0c52af30 Leszek Koltunski
      {
123 f6fcf06a Leszek Koltunski
      Bundle args = new Bundle();
124
125 22fdfc36 Leszek Koltunski
      args.putInt("SIZECHANGE_Pos" , mSizeChangePos );
126
      args.putInt("SOLVE_Pos"      , mSolvePos      );
127
      args.putInt("SCRAMBLE_Pos"   , mScramblePos   );
128
      args.putInt("SIZECHANGE_Type", mSizeChangeType);
129
      args.putInt("SOLVE_Type"     , mSolveType     );
130
      args.putInt("SCRAMBLE_Type"  , mScrambleType  );
131 f6fcf06a Leszek Koltunski
132
      RubikSettings settings = new RubikSettings();
133
      settings.setArguments(args);
134
      settings.show(getSupportFragmentManager(), null);
135
      }
136
137 1cbcc6bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
138
139 5560eea9 Leszek Koltunski
    public void About(View v)
140 1cbcc6bf Leszek Koltunski
      {
141 5560eea9 Leszek Koltunski
      RubikAbout about = new RubikAbout();
142
      about.show(getSupportFragmentManager(), null);
143 1cbcc6bf Leszek Koltunski
      }
144
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146
147
    public void Scramble(View v)
148
      {
149 f548942f Leszek Koltunski
      int scramble = mPicker.getValue();
150
151 1cbcc6bf Leszek Koltunski
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
152 f548942f Leszek Koltunski
      view.getRenderer().scrambleCube(scramble);
153 1cbcc6bf Leszek Koltunski
      }
154
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156
157 2ecf0c21 Leszek Koltunski
    public void Solve(View v)
158 1cbcc6bf Leszek Koltunski
      {
159 f548942f Leszek Koltunski
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
160 2ecf0c21 Leszek Koltunski
      view.getRenderer().solveCube();
161 1cbcc6bf Leszek Koltunski
      }
162
163 f6fcf06a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
164
165 22fdfc36 Leszek Koltunski
    public void onComplete(int sizeP, int solvP, int scraP, int sizeT, int solvT, int scraT )
166 f6fcf06a Leszek Koltunski
      {
167 3b12e641 Leszek Koltunski
      mSizeChangePos = sizeP;
168 22fdfc36 Leszek Koltunski
      mSolvePos      = solvP;
169 3b12e641 Leszek Koltunski
      mScramblePos   = scraP;
170
      mSizeChangeType= sizeT;
171 22fdfc36 Leszek Koltunski
      mSolveType     = solvT;
172 3b12e641 Leszek Koltunski
      mScrambleType  = scraT;
173 b0a2ce63 Leszek Koltunski
174 fe381d8e Leszek Koltunski
      applyPreferences();
175 0c52af30 Leszek Koltunski
      }
176
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178
179
    public void setSize(View v)
180
      {
181
      int size=0, id = v.getId();
182
183
      for(int b=0; b<button_ids.length; b++)
184
        if( button_ids[b] == id )
185
          {
186 34998c9d Leszek Koltunski
          size = b+SMALLEST_SIZE;
187 0c52af30 Leszek Koltunski
          break;
188
          }
189
190
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
191 aa8b36aa Leszek Koltunski
      boolean success = view.getRenderer().createCube(size);
192 f291130e Leszek Koltunski
193
      if( success )
194
        {
195
        markButton(size);
196
        }
197 0c52af30 Leszek Koltunski
      }
198
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200
201
   private void markButton(int size)
202
     {
203 34747dd1 Leszek Koltunski
     mSize = size;
204
205 0c52af30 Leszek Koltunski
     for(int b=0; b<button_ids.length; b++)
206
       {
207
       Drawable d = findViewById(button_ids[b]).getBackground();
208
209 34998c9d Leszek Koltunski
       if( size == b+SMALLEST_SIZE )
210 0c52af30 Leszek Koltunski
         {
211
         d.setColorFilter(ContextCompat.getColor(this,R.color.red), PorterDuff.Mode.MULTIPLY);
212
         }
213
       else
214
         {
215
         d.clearColorFilter();
216
         }
217
       }
218
     }
219 fe381d8e Leszek Koltunski
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221
222
   private void savePreferences()
223
     {
224
     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
225
     SharedPreferences.Editor editor = preferences.edit();
226
227 22fdfc36 Leszek Koltunski
     editor.putInt("SIZECHANGE_Pos" , mSizeChangePos );
228
     editor.putInt("SOLVE_Pos"      , mSolvePos      );
229
     editor.putInt("SCRAMBLE_Pos"   , mScramblePos   );
230
     editor.putInt("SIZECHANGE_Type", mSizeChangeType);
231
     editor.putInt("SOLVE_Type"     , mSolveType     );
232
     editor.putInt("SCRAMBLE_Type"  , mScrambleType  );
233
     editor.putInt("scramble"       , mPicker.getValue() );
234 fe381d8e Leszek Koltunski
235
     editor.apply();
236
     }
237
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239
240
   private void restorePreferences()
241
     {
242
     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
243
244 22fdfc36 Leszek Koltunski
     mSizeChangePos = preferences.getInt("SIZECHANGE_Pos" , DEFAULT_SIZECHANGE_POS );
245
     mSolvePos      = preferences.getInt("SOLVE_Pos"      , DEFAULT_SOLVE_POS      );
246
     mScramblePos   = preferences.getInt("SCRAMBLE_Pos"   , DEFAULT_SCRAMBLE_POS   );
247
     mSizeChangeType= preferences.getInt("SIZECHANGE_Type", DEFAULT_SIZECHANGE_TYPE);
248
     mSolveType     = preferences.getInt("SOLVE_Type"     , DEFAULT_SOLVE_TYPE     );
249
     mScrambleType  = preferences.getInt("SCRAMBLE_Type"  , DEFAULT_SCRAMBLE_TYPE  );
250
     int scramble   = preferences.getInt("scramble"       , MIN_SCRAMBLE           );
251 f548942f Leszek Koltunski
252
     mPicker.setValue(scramble);
253 fe381d8e Leszek Koltunski
     }
254
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256
257
   private void applyPreferences()
258
     {
259
     RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
260
     RubikRenderer renderer = view.getRenderer();
261
262 584f7954 Leszek Koltunski
     renderer.setSizeChangeDuration(translateDuration(mSizeChangePos)+1);
263 2ecf0c21 Leszek Koltunski
     renderer.setSolveDuration(translateDuration(mSolvePos)+1);
264 3b12e641 Leszek Koltunski
     renderer.setScrambleDuration(translateDuration(mScramblePos)+1);
265 086a24d0 Leszek Koltunski
     renderer.setSizeChangeType(SizeChangeEffect.getType(mSizeChangeType));
266 2ecf0c21 Leszek Koltunski
     renderer.setSolveType(SolveEffect.getType(mSolveType));
267 3b12e641 Leszek Koltunski
     renderer.setScrambleType(ScrambleEffect.getType(mScrambleType));
268 fe381d8e Leszek Koltunski
     }
269 1cbcc6bf Leszek Koltunski
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271
272
   public static int translateDuration(int pos)
273
     {
274
     return (pos/2)*100;
275
     }
276 0c52af30 Leszek Koltunski
}