Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikActivity.java @ 42772cff

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.library.main.DistortedLibrary;
34

    
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

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

    
43
    public static final int MIN_SCRAMBLE =  1;
44
    public static final int MAX_SCRAMBLE = 10;
45

    
46
    private static int mSize = DEFAULT_SIZE;
47

    
48
    private int[] mPos;
49
    private int[] mType;
50

    
51
    private HorizontalNumberPicker mPicker;
52

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

    
55
    @Override
56
    protected void onCreate(Bundle savedState)
57
      {
58
      super.onCreate(savedState);
59
      setTheme(R.style.CustomActivityThemeNoActionBar);
60
      setContentView(R.layout.main);
61
      markButton(mSize);
62

    
63
      mPos = new int[RubikSettingsEnum.LENGTH];
64
      mType= new int[RubikSettingsEnum.LENGTH];
65

    
66
      mPicker = findViewById(R.id.rubikNumberPicker);
67
      mPicker.setMin(MIN_SCRAMBLE);
68
      mPicker.setMax(MAX_SCRAMBLE);
69

    
70
      restorePreferences();
71

    
72
      for (int i=0; i<RubikSettingsEnum.LENGTH; i++)
73
       {
74
       applyPreferences(i);
75
       }
76
      }
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79
    
80
    @Override
81
    protected void onPause() 
82
      {
83
      GLSurfaceView view = findViewById(R.id.rubikSurfaceView);
84
      view.onPause();
85
      DistortedLibrary.onPause();
86
      savePreferences();
87
      super.onPause();
88
      }
89

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91
    
92
    @Override
93
    protected void onResume() 
94
      {
95
      super.onResume();
96
      GLSurfaceView view = findViewById(R.id.rubikSurfaceView);
97
      view.onResume();
98
      }
99
    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101
    
102
    @Override
103
    protected void onDestroy() 
104
      {
105
      DistortedLibrary.onDestroy();
106
      super.onDestroy();
107
      }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

    
111
    static int getSize()
112
      {
113
      return mSize;
114
      }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

    
118
    public void Settings(View v)
119
      {
120
      Bundle args = new Bundle();
121

    
122
      String name;
123

    
124
      for (int i=0; i<RubikSettingsEnum.LENGTH; i++)
125
        {
126
        RubikSettingsEnum rse = RubikSettingsEnum.getEnum(i);
127
        name = rse.name();
128

    
129
        args.putInt(name+"_Pos" , mPos[i]);
130
        args.putInt(name+"_Type", mType[i]);
131
        }
132

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

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

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

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

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

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

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

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

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
    public void onComplete(int index, int position, int type )
167
      {
168
      if( index>=0 && index<RubikSettingsEnum.LENGTH )
169
        {
170
        mPos[index] = position;
171
        mType[index]= type;
172

    
173
        applyPreferences(index);
174
        }
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
     String name;
227

    
228
     for (int i=0; i<RubikSettingsEnum.LENGTH; i++)
229
       {
230
       RubikSettingsEnum rse = RubikSettingsEnum.getEnum(i);
231
       name = rse.name();
232

    
233
       editor.putInt(name+"_Pos" , mPos[i]);
234
       editor.putInt(name+"_Type", mType[i]);
235
       }
236

    
237
     editor.putInt("scramble", mPicker.getValue() );
238

    
239
     editor.apply();
240
     }
241

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

    
244
   private void restorePreferences()
245
     {
246
     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
247

    
248
     String name;
249

    
250
     for (int i=0; i<RubikSettingsEnum.LENGTH; i++)
251
       {
252
       RubikSettingsEnum rse = RubikSettingsEnum.getEnum(i);
253
       name = rse.name();
254

    
255
       mPos[i]  = preferences.getInt(name+"_Pos" , rse.getDefaultPos() );
256
       mType[i] = preferences.getInt(name+"_Type", rse.getDefaultType());
257
       }
258

    
259
     int scramble= preferences.getInt("scramble", MIN_SCRAMBLE);
260

    
261
     mPicker.setValue(scramble);
262
     }
263

    
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265

    
266
   private void applyPreferences(int index)
267
     {
268
     RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
269
     RubikRenderer renderer = view.getRenderer();
270

    
271
     renderer.setPos (index, translateDuration(mPos[index])+1);
272
     renderer.setType(index, mType[index] );
273
     }
274

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

    
277
   public static int translateDuration(int pos)
278
     {
279
     return (pos/2)*100;
280
     }
281
}
(2-2/7)