Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 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.magic;
21

    
22
import android.content.SharedPreferences;
23
import android.graphics.PorterDuff;
24
import android.graphics.drawable.Drawable;
25
import android.opengl.GLSurfaceView;
26
import android.os.Bundle;
27
import android.preference.PreferenceManager;
28
import android.support.v4.content.ContextCompat;
29
import android.support.v7.app.AppCompatActivity;
30
import android.view.View;
31

    
32
import org.distorted.component.HorizontalNumberPicker;
33
import org.distorted.effect.ScrambleEffect;
34
import org.distorted.effect.SizeChangeEffect;
35
import org.distorted.effect.SolveEffect;
36
import org.distorted.library.main.DistortedLibrary;
37

    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

    
40
public class RubikActivity extends AppCompatActivity implements RubikSettings.OnCompleteListener
41
{
42
            static final int DEFAULT_SIZE  = 3;
43
    private static final int SMALLEST_SIZE = 2;
44
    private static final int[] button_ids  = {R.id.rubikSize2, R.id.rubikSize3, R.id.rubikSize4};
45

    
46
    public static final int DEFAULT_SIZECHANGE_POS = 20;
47
    public static final int DEFAULT_SOLVE_POS      = 20;
48
    public static final int DEFAULT_SCRAMBLE_POS   =100;
49
    public static final int DEFAULT_SIZECHANGE_TYPE= 1;
50
    public static final int DEFAULT_SOLVE_TYPE     = 1;
51
    public static final int DEFAULT_SCRAMBLE_TYPE  = 0;
52

    
53
    public static final int MIN_SCRAMBLE =  1;
54
    public static final int MAX_SCRAMBLE = 10;
55

    
56
    private static int mSize = DEFAULT_SIZE;
57

    
58
    private int mSizeChangePos, mSolvePos, mScramblePos;
59
    private int mSizeChangeType, mSolveType, mScrambleType;
60

    
61
    private HorizontalNumberPicker mPicker;
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

    
65
    @Override
66
    protected void onCreate(Bundle savedState)
67
      {
68
      super.onCreate(savedState);
69
      setTheme(R.style.CustomActivityThemeNoActionBar);
70
      setContentView(R.layout.main);
71
      markButton(mSize);
72

    
73
      mPicker = findViewById(R.id.rubikNumberPicker);
74
      mPicker.setMin(MIN_SCRAMBLE);
75
      mPicker.setMax(MAX_SCRAMBLE);
76

    
77
      restorePreferences();
78
      applyPreferences();
79
      }
80

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82
    
83
    @Override
84
    protected void onPause() 
85
      {
86
      GLSurfaceView view = findViewById(R.id.rubikSurfaceView);
87
      view.onPause();
88
      DistortedLibrary.onPause();
89
      savePreferences();
90
      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
      DistortedLibrary.onDestroy();
109
      super.onDestroy();
110
      }
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

    
114
    static int getSize()
115
      {
116
      return mSize;
117
      }
118

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

    
121
    public void Settings(View v)
122
      {
123
      Bundle args = new Bundle();
124

    
125
      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

    
132
      RubikSettings settings = new RubikSettings();
133
      settings.setArguments(args);
134
      settings.show(getSupportFragmentManager(), null);
135
      }
136

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138

    
139
    public void About(View v)
140
      {
141
      RubikAbout about = new RubikAbout();
142
      about.show(getSupportFragmentManager(), null);
143
      }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

    
147
    public void Scramble(View v)
148
      {
149
      int scramble = mPicker.getValue();
150

    
151
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
152
      view.getRenderer().scrambleCube(scramble);
153
      }
154

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156

    
157
    public void Solve(View v)
158
      {
159
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
160
      view.getRenderer().solveCube();
161
      }
162

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164

    
165
    public void onComplete(int sizeP, int solvP, int scraP, int sizeT, int solvT, int scraT )
166
      {
167
      mSizeChangePos = sizeP;
168
      mSolvePos      = solvP;
169
      mScramblePos   = scraP;
170
      mSizeChangeType= sizeT;
171
      mSolveType     = solvT;
172
      mScrambleType  = scraT;
173

    
174
      applyPreferences();
175
      }
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
          size = b+SMALLEST_SIZE;
187
          break;
188
          }
189

    
190
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
191
      boolean success = view.getRenderer().createCube(size);
192

    
193
      if( success )
194
        {
195
        markButton(size);
196
        }
197
      }
198

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200

    
201
   private void markButton(int size)
202
     {
203
     mSize = size;
204

    
205
     for(int b=0; b<button_ids.length; b++)
206
       {
207
       Drawable d = findViewById(button_ids[b]).getBackground();
208

    
209
       if( size == b+SMALLEST_SIZE )
210
         {
211
         d.setColorFilter(ContextCompat.getColor(this,R.color.red), PorterDuff.Mode.MULTIPLY);
212
         }
213
       else
214
         {
215
         d.clearColorFilter();
216
         }
217
       }
218
     }
219

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221

    
222
   private void savePreferences()
223
     {
224
     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
225
     SharedPreferences.Editor editor = preferences.edit();
226

    
227
     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

    
235
     editor.apply();
236
     }
237

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239

    
240
   private void restorePreferences()
241
     {
242
     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
243

    
244
     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

    
252
     mPicker.setValue(scramble);
253
     }
254

    
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256

    
257
   private void applyPreferences()
258
     {
259
     RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
260
     RubikRenderer renderer = view.getRenderer();
261

    
262
     renderer.setSizeChangeDuration(translateDuration(mSizeChangePos)+1);
263
     renderer.setSolveDuration(translateDuration(mSolvePos)+1);
264
     renderer.setScrambleDuration(translateDuration(mScramblePos)+1);
265
     renderer.setSizeChangeType(SizeChangeEffect.getType(mSizeChangeType));
266
     renderer.setSolveType(SolveEffect.getType(mSolveType));
267
     renderer.setScrambleType(ScrambleEffect.getType(mScrambleType));
268
     }
269

    
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271

    
272
   public static int translateDuration(int pos)
273
     {
274
     return (pos/2)*100;
275
     }
276
}
(2-2/7)