Project

General

Profile

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

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

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
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
    private HorizontalNumberPicker mPicker;
48

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

    
51
    @Override
52
    protected void onCreate(Bundle savedState)
53
      {
54
      super.onCreate(savedState);
55
      setTheme(R.style.CustomActivityThemeNoActionBar);
56
      setContentView(R.layout.main);
57
      markButton(mSize);
58

    
59
      mPicker = findViewById(R.id.rubikNumberPicker);
60
      mPicker.setMin(MIN_SCRAMBLE);
61
      mPicker.setMax(MAX_SCRAMBLE);
62

    
63
      restorePreferences();
64
      }
65

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67
    
68
    @Override
69
    protected void onPause() 
70
      {
71
      GLSurfaceView view = findViewById(R.id.rubikSurfaceView);
72
      view.onPause();
73
      DistortedLibrary.onPause();
74
      savePreferences();
75
      super.onPause();
76
      }
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79
    
80
    @Override
81
    protected void onResume() 
82
      {
83
      super.onResume();
84
      GLSurfaceView view = findViewById(R.id.rubikSurfaceView);
85
      view.onResume();
86
      }
87
    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89
    
90
    @Override
91
    protected void onDestroy() 
92
      {
93
      DistortedLibrary.onDestroy();
94
      super.onDestroy();
95
      }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98

    
99
    static int getSize()
100
      {
101
      return mSize;
102
      }
103

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105

    
106
    public void Settings(View v)
107
      {
108
      RubikSettings settings = new RubikSettings();
109
      settings.show(getSupportFragmentManager(), null);
110
      }
111

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

    
114
    public void About(View v)
115
      {
116
      RubikAbout about = new RubikAbout();
117
      about.show(getSupportFragmentManager(), null);
118
      }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

    
122
    public void Scramble(View v)
123
      {
124
      int scramble = mPicker.getValue();
125

    
126
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
127
      view.getRenderer().scrambleCube(scramble);
128
      }
129

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131

    
132
    public void Solve(View v)
133
      {
134
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
135
      view.getRenderer().solveCube();
136
      }
137

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

    
140
    public void setSize(View v)
141
      {
142
      int size=0, id = v.getId();
143

    
144
      for(int b=0; b<button_ids.length; b++)
145
        if( button_ids[b] == id )
146
          {
147
          size = b+SMALLEST_SIZE;
148
          break;
149
          }
150

    
151
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
152
      boolean success = view.getRenderer().createCube(size);
153

    
154
      if( success )
155
        {
156
        markButton(size);
157
        }
158
      }
159

    
160
///////////////////////////////////////////////////////////////////////////////////////////////////
161

    
162
   private void markButton(int size)
163
     {
164
     mSize = size;
165

    
166
     for(int b=0; b<button_ids.length; b++)
167
       {
168
       Drawable d = findViewById(button_ids[b]).getBackground();
169

    
170
       if( size == b+SMALLEST_SIZE )
171
         {
172
         d.setColorFilter(ContextCompat.getColor(this,R.color.red), PorterDuff.Mode.MULTIPLY);
173
         }
174
       else
175
         {
176
         d.clearColorFilter();
177
         }
178
       }
179
     }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

    
183
   private void savePreferences()
184
     {
185
     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
186
     SharedPreferences.Editor editor = preferences.edit();
187

    
188
     for (int i=0; i<RubikSettingsEnum.LENGTH; i++)
189
       {
190
       RubikSettingsEnum.getEnum(i).savePreferences(editor);
191
       }
192

    
193
     editor.putInt("scramble", mPicker.getValue() );
194

    
195
     editor.apply();
196
     }
197

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

    
200
   private void restorePreferences()
201
     {
202
     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
203

    
204
     for (int i=0; i<RubikSettingsEnum.LENGTH; i++)
205
       {
206
       RubikSettingsEnum.getEnum(i).restorePreferences(preferences);
207
       }
208

    
209
     int scramble= preferences.getInt("scramble", MIN_SCRAMBLE);
210

    
211
     mPicker.setValue(scramble);
212
     }
213
}
(2-2/7)