Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikActivity.java @ 47ba5ddc

1 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4 ffd744be Leszek Koltunski
// This file is part of Distorted.                                                               //
5 0c52af30 Leszek Koltunski
//                                                                                               //
6 ffd744be Leszek Koltunski
// Distorted 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 ffd744be Leszek Koltunski
// Distorted 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 ffd744be Leszek Koltunski
// along with Distorted.  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
            static final int DEFAULT_SIZE  = 3;
41 34998c9d Leszek Koltunski
    private static final int SMALLEST_SIZE = 2;
42 0c52af30 Leszek Koltunski
    private static final int[] button_ids  = {R.id.rubikSize2, R.id.rubikSize3, R.id.rubikSize4};
43
44 f548942f Leszek Koltunski
    public static final int MIN_SCRAMBLE =  1;
45 47ba5ddc Leszek Koltunski
    public static final int DEF_SCRAMBLE =  3;
46 f548942f Leszek Koltunski
    public static final int MAX_SCRAMBLE = 10;
47
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 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
113
114
    @Override
115 34747dd1 Leszek Koltunski
    protected void onCreate(Bundle savedState)
116 0c52af30 Leszek Koltunski
      {
117 34747dd1 Leszek Koltunski
      super.onCreate(savedState);
118 f6fcf06a Leszek Koltunski
      setTheme(R.style.CustomActivityThemeNoActionBar);
119 1cbcc6bf Leszek Koltunski
      setContentView(R.layout.main);
120 5e307a01 Leszek Koltunski
      markButton(mSize);
121 f6fcf06a Leszek Koltunski
122 f548942f Leszek Koltunski
      mPicker = findViewById(R.id.rubikNumberPicker);
123
      mPicker.setMin(MIN_SCRAMBLE);
124
      mPicker.setMax(MAX_SCRAMBLE);
125
126 fe381d8e Leszek Koltunski
      restorePreferences();
127 0c52af30 Leszek Koltunski
      }
128
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130
    
131
    @Override
132
    protected void onPause() 
133
      {
134
      GLSurfaceView view = findViewById(R.id.rubikSurfaceView);
135
      view.onPause();
136 e1111500 Leszek Koltunski
      DistortedLibrary.onPause();
137 fe381d8e Leszek Koltunski
      savePreferences();
138 0c52af30 Leszek Koltunski
      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 e1111500 Leszek Koltunski
      DistortedLibrary.onDestroy();
157 0c52af30 Leszek Koltunski
      super.onDestroy();
158
      }
159
160 34747dd1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
161 47ba5ddc Leszek Koltunski
// PUBLIC API
162 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
163
164 a47ee432 Leszek Koltunski
    public void Settings(View v)
165 0c52af30 Leszek Koltunski
      {
166 f6fcf06a Leszek Koltunski
      RubikSettings settings = new RubikSettings();
167
      settings.show(getSupportFragmentManager(), null);
168
      }
169
170 1cbcc6bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
171
172 5560eea9 Leszek Koltunski
    public void About(View v)
173 1cbcc6bf Leszek Koltunski
      {
174 5560eea9 Leszek Koltunski
      RubikAbout about = new RubikAbout();
175
      about.show(getSupportFragmentManager(), null);
176 1cbcc6bf Leszek Koltunski
      }
177
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179
180
    public void Scramble(View v)
181
      {
182 f548942f Leszek Koltunski
      int scramble = mPicker.getValue();
183
184 1cbcc6bf Leszek Koltunski
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
185 f548942f Leszek Koltunski
      view.getRenderer().scrambleCube(scramble);
186 1cbcc6bf Leszek Koltunski
      }
187
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189
190 2ecf0c21 Leszek Koltunski
    public void Solve(View v)
191 1cbcc6bf Leszek Koltunski
      {
192 f548942f Leszek Koltunski
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
193 2ecf0c21 Leszek Koltunski
      view.getRenderer().solveCube();
194 1cbcc6bf Leszek Koltunski
      }
195
196 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
197
198
    public void setSize(View v)
199
      {
200
      int size=0, id = v.getId();
201
202
      for(int b=0; b<button_ids.length; b++)
203
        if( button_ids[b] == id )
204
          {
205 34998c9d Leszek Koltunski
          size = b+SMALLEST_SIZE;
206 0c52af30 Leszek Koltunski
          break;
207
          }
208
209
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
210 aa8b36aa Leszek Koltunski
      boolean success = view.getRenderer().createCube(size);
211 f291130e Leszek Koltunski
212
      if( success )
213
        {
214
        markButton(size);
215
        }
216 0c52af30 Leszek Koltunski
      }
217
}