Project

General

Profile

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

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

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