Project

General

Profile

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

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

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};
43

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

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

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

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

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

    
64
      restorePreferences();
65
      }
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68
    
69
    @Override
70
    protected void onPause() 
71
      {
72
      GLSurfaceView view = findViewById(R.id.rubikSurfaceView);
73
      view.onPause();
74
      DistortedLibrary.onPause();
75
      savePreferences();
76
      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
      DistortedLibrary.onDestroy();
95
      super.onDestroy();
96
      }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

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

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106

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

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114

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

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122

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

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

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132

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

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
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
          size = b+SMALLEST_SIZE;
149
          break;
150
          }
151

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

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

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162

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

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

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

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

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

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

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

    
196
     editor.apply();
197
     }
198

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200

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

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

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

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