Project

General

Profile

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

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

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.effect.win.WinEffect;
34
import org.distorted.library.main.DistortedLibrary;
35
import org.distorted.effect.BaseEffect;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

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

    
45
    public static final int MIN_SCRAMBLE =  1;
46
    public static final int MAX_SCRAMBLE = 10;
47

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

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

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

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

    
65
      restorePreferences();
66
      }
67

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

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

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

    
101
    static int getSize()
102
      {
103
      return mSize;
104
      }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

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

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

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

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

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

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

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

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

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141

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

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

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

    
156
      if( success )
157
        {
158
        markButton(size);
159
        }
160
      }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163

    
164
   private void markButton(int size)
165
     {
166
     mSize = size;
167

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

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

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184

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

    
190
     for (int i=0; i< BaseEffect.Type.LENGTH; i++)
191
       {
192
       BaseEffect.Type.getType(i).savePreferences(editor);
193
       }
194

    
195
     editor.putInt("scramble", mPicker.getValue() );
196

    
197
     editor.apply();
198
     }
199

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201

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

    
206
     for (int i=0; i< BaseEffect.Type.LENGTH; i++)
207
       {
208
       BaseEffect.Type.getType(i).restorePreferences(preferences);
209
       }
210

    
211
     int scramble= preferences.getInt("scramble", MIN_SCRAMBLE);
212

    
213
     mPicker.setValue(scramble);
214
     }
215
}
(2-2/6)