Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube 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
// Magic Cube 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 Magic Cube.  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
import org.distorted.effect.BaseEffect;
35

    
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37

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

    
44
    public static final int MIN_SCRAMBLE =  1;
45
    public static final int DEF_SCRAMBLE =  1;
46
    public static final int MAX_SCRAMBLE = 17;
47

    
48
    private static int mSize = DEFAULT_SIZE;
49
    private HorizontalNumberPicker mPicker;
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
113

    
114
    static int getNumCubes()
115
      {
116
      return button_ids.length;
117
      }
118

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

    
121
    @Override
122
    protected void onCreate(Bundle savedState)
123
      {
124
      super.onCreate(savedState);
125
      setTheme(R.style.CustomActivityThemeNoActionBar);
126
      setContentView(R.layout.main);
127
      markButton(mSize);
128

    
129
      mPicker = findViewById(R.id.rubikNumberPicker);
130
      mPicker.setMin(MIN_SCRAMBLE);
131
      mPicker.setMax(MAX_SCRAMBLE);
132

    
133
      restorePreferences();
134
      }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
    
138
    @Override
139
    protected void onPause() 
140
      {
141
      GLSurfaceView view = findViewById(R.id.rubikSurfaceView);
142
      view.onPause();
143
      DistortedLibrary.onPause();
144
      savePreferences();
145
      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
      DistortedLibrary.onDestroy();
164
      super.onDestroy();
165
      }
166

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168
// PUBLIC API
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170

    
171
    public void Settings(View v)
172
      {
173
      RubikSettings settings = new RubikSettings();
174
      settings.show(getSupportFragmentManager(), null);
175
      }
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

    
179
    public void Scores(View v)
180
      {
181
      RubikScores scores = new RubikScores();
182
      scores.show(getSupportFragmentManager(), null);
183
      }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186

    
187
    public void About(View v)
188
      {
189
      RubikAbout about = new RubikAbout();
190
      about.show(getSupportFragmentManager(), null);
191
      }
192

    
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194

    
195
    public void Scramble(View v)
196
      {
197
      int scramble = mPicker.getValue();
198

    
199
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
200
      view.getRenderer().scrambleCube(scramble);
201
      }
202

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204

    
205
    public void Solve(View v)
206
      {
207
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
208
      view.getRenderer().solveCube();
209
      }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
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
          size = b+SMALLEST_SIZE;
221
          break;
222
          }
223

    
224
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
225
      boolean success = view.getRenderer().createCube(size);
226

    
227
      if( success )
228
        {
229
        markButton(size);
230
        }
231
      }
232
}
(2-2/10)