Project

General

Profile

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

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

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
import org.distorted.effect.BaseEffect;
35

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

    
38
public class RubikActivity extends AppCompatActivity
39
{
40
            static final int DEFAULT_SIZE  = 3;
41
    private 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 =  3;
46
    public static final int MAX_SCRAMBLE = 10;
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
    @Override
115
    protected void onCreate(Bundle savedState)
116
      {
117
      super.onCreate(savedState);
118
      setTheme(R.style.CustomActivityThemeNoActionBar);
119
      setContentView(R.layout.main);
120
      markButton(mSize);
121

    
122
      mPicker = findViewById(R.id.rubikNumberPicker);
123
      mPicker.setMin(MIN_SCRAMBLE);
124
      mPicker.setMax(MAX_SCRAMBLE);
125

    
126
      restorePreferences();
127
      }
128

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130
    
131
    @Override
132
    protected void onPause() 
133
      {
134
      GLSurfaceView view = findViewById(R.id.rubikSurfaceView);
135
      view.onPause();
136
      DistortedLibrary.onPause();
137
      savePreferences();
138
      super.onPause();
139
      }
140

    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142
    
143
    @Override
144
    protected void onResume() 
145
      {
146
      super.onResume();
147
      GLSurfaceView view = findViewById(R.id.rubikSurfaceView);
148
      view.onResume();
149
      }
150
    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152
    
153
    @Override
154
    protected void onDestroy() 
155
      {
156
      DistortedLibrary.onDestroy();
157
      super.onDestroy();
158
      }
159

    
160
///////////////////////////////////////////////////////////////////////////////////////////////////
161
// PUBLIC API
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163

    
164
    public void Settings(View v)
165
      {
166
      RubikSettings settings = new RubikSettings();
167
      settings.show(getSupportFragmentManager(), null);
168
      }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

    
172
    public void Scores(View v)
173
      {
174
      RubikScores scores = new RubikScores();
175
      scores.show(getSupportFragmentManager(), null);
176
      }
177

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179

    
180
    public void About(View v)
181
      {
182
      RubikAbout about = new RubikAbout();
183
      about.show(getSupportFragmentManager(), null);
184
      }
185

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187

    
188
    public void Scramble(View v)
189
      {
190
      int scramble = mPicker.getValue();
191

    
192
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
193
      view.getRenderer().scrambleCube(scramble);
194
      }
195

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197

    
198
    public void Solve(View v)
199
      {
200
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
201
      view.getRenderer().solveCube();
202
      }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

    
206
    public void setSize(View v)
207
      {
208
      int size=0, id = v.getId();
209

    
210
      for(int b=0; b<button_ids.length; b++)
211
        if( button_ids[b] == id )
212
          {
213
          size = b+SMALLEST_SIZE;
214
          break;
215
          }
216

    
217
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
218
      boolean success = view.getRenderer().createCube(size);
219

    
220
      if( success )
221
        {
222
        markButton(size);
223
        }
224
      }
225
}
(2-2/7)