Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikActivity.java @ fdec60a3

1 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4 fdec60a3 Leszek Koltunski
// This file is part of Magic Cube.                                                              //
5 0c52af30 Leszek Koltunski
//                                                                                               //
6 fdec60a3 Leszek Koltunski
// Magic Cube 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 fdec60a3 Leszek Koltunski
// Magic Cube 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 fdec60a3 Leszek Koltunski
// along with Magic Cube.  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 e1111500 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
34 64975793 Leszek Koltunski
import org.distorted.effect.BaseEffect;
35 0c52af30 Leszek Koltunski
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37
38 ce0c7ca8 Leszek Koltunski
public class RubikActivity extends AppCompatActivity
39 0c52af30 Leszek Koltunski
{
40 fdec60a3 Leszek Koltunski
    static final int DEFAULT_SIZE  = 3;
41
    static final int SMALLEST_SIZE = 2;
42 a7a7cc9c Leszek Koltunski
    private static final int[] button_ids  = {R.id.rubikSize2, R.id.rubikSize3, R.id.rubikSize4, R.id.rubikSize5};
43 0c52af30 Leszek Koltunski
44 f548942f Leszek Koltunski
    public static final int MIN_SCRAMBLE =  1;
45 e4db5995 Leszek Koltunski
    public static final int DEF_SCRAMBLE =  1;
46
    public static final int MAX_SCRAMBLE = 17;
47 f548942f Leszek Koltunski
48 ee5c2ae1 Leszek Koltunski
    private static int mSize = DEFAULT_SIZE;
49 f548942f Leszek Koltunski
    private HorizontalNumberPicker mPicker;
50
51 47ba5ddc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
52
53
    private void markButton(int size)
54
      {
55
      mSize = size;
56
57
      for(int b=0; b<button_ids.length; b++)
58
        {
59
        Drawable d = findViewById(button_ids[b]).getBackground();
60
61
        if( size == b+SMALLEST_SIZE )
62
          {
63
          d.setColorFilter(ContextCompat.getColor(this,R.color.red), PorterDuff.Mode.MULTIPLY);
64
          }
65
        else
66
          {
67
          d.clearColorFilter();
68
          }
69
        }
70
      }
71
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73
74
    private void savePreferences()
75
      {
76
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
77
      SharedPreferences.Editor editor = preferences.edit();
78
79
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
80
        {
81
        BaseEffect.Type.getType(i).savePreferences(editor);
82
        }
83
84
      editor.putInt("scramble", mPicker.getValue() );
85
86
      editor.apply();
87
      }
88
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90
91
    private void restorePreferences()
92
      {
93
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
94
95
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
96
        {
97
        BaseEffect.Type.getType(i).restorePreferences(preferences);
98
        }
99
100
      int scramble= preferences.getInt("scramble", DEF_SCRAMBLE);
101
102
      mPicker.setValue(scramble);
103
      }
104
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106
107
    static int getSize()
108
      {
109
      return mSize;
110
      }
111
112 fdec60a3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
113
114
    static int getNumCubes()
115
      {
116
      return button_ids.length;
117
      }
118
119 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
120
121
    @Override
122 34747dd1 Leszek Koltunski
    protected void onCreate(Bundle savedState)
123 0c52af30 Leszek Koltunski
      {
124 34747dd1 Leszek Koltunski
      super.onCreate(savedState);
125 f6fcf06a Leszek Koltunski
      setTheme(R.style.CustomActivityThemeNoActionBar);
126 1cbcc6bf Leszek Koltunski
      setContentView(R.layout.main);
127 5e307a01 Leszek Koltunski
      markButton(mSize);
128 f6fcf06a Leszek Koltunski
129 f548942f Leszek Koltunski
      mPicker = findViewById(R.id.rubikNumberPicker);
130
      mPicker.setMin(MIN_SCRAMBLE);
131
      mPicker.setMax(MAX_SCRAMBLE);
132
133 fe381d8e Leszek Koltunski
      restorePreferences();
134 0c52af30 Leszek Koltunski
      }
135
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
    
138
    @Override
139
    protected void onPause() 
140
      {
141
      GLSurfaceView view = findViewById(R.id.rubikSurfaceView);
142
      view.onPause();
143 e1111500 Leszek Koltunski
      DistortedLibrary.onPause();
144 fe381d8e Leszek Koltunski
      savePreferences();
145 0c52af30 Leszek Koltunski
      super.onPause();
146
      }
147
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149
    
150
    @Override
151
    protected void onResume() 
152
      {
153
      super.onResume();
154
      GLSurfaceView view = findViewById(R.id.rubikSurfaceView);
155
      view.onResume();
156
      }
157
    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159
    
160
    @Override
161
    protected void onDestroy() 
162
      {
163 e1111500 Leszek Koltunski
      DistortedLibrary.onDestroy();
164 0c52af30 Leszek Koltunski
      super.onDestroy();
165
      }
166
167 34747dd1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
168 47ba5ddc Leszek Koltunski
// PUBLIC API
169 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
170
171 a47ee432 Leszek Koltunski
    public void Settings(View v)
172 0c52af30 Leszek Koltunski
      {
173 f6fcf06a Leszek Koltunski
      RubikSettings settings = new RubikSettings();
174
      settings.show(getSupportFragmentManager(), null);
175
      }
176
177 a7a7cc9c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
178
179
    public void Scores(View v)
180
      {
181
      RubikScores scores = new RubikScores();
182
      scores.show(getSupportFragmentManager(), null);
183
      }
184
185 1cbcc6bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
186
187 5560eea9 Leszek Koltunski
    public void About(View v)
188 1cbcc6bf Leszek Koltunski
      {
189 5560eea9 Leszek Koltunski
      RubikAbout about = new RubikAbout();
190
      about.show(getSupportFragmentManager(), null);
191 1cbcc6bf Leszek Koltunski
      }
192
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194
195
    public void Scramble(View v)
196
      {
197 f548942f Leszek Koltunski
      int scramble = mPicker.getValue();
198
199 1cbcc6bf Leszek Koltunski
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
200 f548942f Leszek Koltunski
      view.getRenderer().scrambleCube(scramble);
201 1cbcc6bf Leszek Koltunski
      }
202
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204
205 2ecf0c21 Leszek Koltunski
    public void Solve(View v)
206 1cbcc6bf Leszek Koltunski
      {
207 f548942f Leszek Koltunski
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
208 2ecf0c21 Leszek Koltunski
      view.getRenderer().solveCube();
209 1cbcc6bf Leszek Koltunski
      }
210
211 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
212
213
    public void setSize(View v)
214
      {
215
      int size=0, id = v.getId();
216
217
      for(int b=0; b<button_ids.length; b++)
218
        if( button_ids[b] == id )
219
          {
220 34998c9d Leszek Koltunski
          size = b+SMALLEST_SIZE;
221 0c52af30 Leszek Koltunski
          break;
222
          }
223
224
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
225 aa8b36aa Leszek Koltunski
      boolean success = view.getRenderer().createCube(size);
226 f291130e Leszek Koltunski
227
      if( success )
228
        {
229
        markButton(size);
230
        }
231 0c52af30 Leszek Koltunski
      }
232
}