Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikActivity.java @ 36e2cbdd

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube 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
// Magic Cube 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 Magic Cube.  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.util.DisplayMetrics;
31
import android.view.View;
32
import android.view.ViewGroup;
33
import android.widget.ImageButton;
34
import android.widget.LinearLayout;
35

    
36
import org.distorted.component.HorizontalNumberPicker;
37
import org.distorted.library.main.DistortedLibrary;
38
import org.distorted.effect.BaseEffect;
39

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

    
42
public class RubikActivity extends AppCompatActivity implements View.OnClickListener
43
{
44
    public static final int MIN_SCRAMBLE =  1;
45
    public static final int DEF_SCRAMBLE =  1;
46
    public static final int MAX_SCRAMBLE = 18;
47

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

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

    
53
    private void markButton(int size)
54
      {
55
      mSize = size;
56

    
57
      for(int b=0; b<RubikSize.LENGTH; b++)
58
        {
59
        Drawable d = findViewById(b).getBackground();
60

    
61
        if( size == RubikSize.getSize(b).getCubeSize() )
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
    private void addSizeButtons()
108
      {
109
      LinearLayout layout = findViewById(R.id.sizeLayout);
110
      DisplayMetrics metrics = getResources().getDisplayMetrics();
111
      float scale = metrics.density;
112
      int size = (int)(64*scale +0.5f);
113
      int padding = (int)(3*scale + 0.5f);
114
      ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(size,size);
115

    
116
      for(int i=0; i<RubikSize.LENGTH; i++)
117
        {
118
        ImageButton button = new ImageButton(this);
119
        button.setLayoutParams(params);
120
        button.setId(i);
121
        button.setPadding(padding,0,padding,0);
122
        int iconID = RubikSize.getSize(i).getIconID();
123
        button.setImageResource(iconID);
124
        button.setOnClickListener(this);
125
        layout.addView(button);
126
        }
127
      }
128

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130

    
131
    static int getSize()
132
      {
133
      return mSize;
134
      }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137

    
138
    @Override
139
    protected void onCreate(Bundle savedState)
140
      {
141
      super.onCreate(savedState);
142
      setTheme(R.style.CustomActivityThemeNoActionBar);
143
      setContentView(R.layout.main);
144
      addSizeButtons();
145
      markButton(mSize);
146

    
147
      mPicker = findViewById(R.id.rubikNumberPicker);
148
      mPicker.setMin(MIN_SCRAMBLE);
149
      mPicker.setMax(MAX_SCRAMBLE);
150

    
151
      restorePreferences();
152
      }
153

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155
    
156
    @Override
157
    protected void onPause() 
158
      {
159
      GLSurfaceView view = findViewById(R.id.rubikSurfaceView);
160
      view.onPause();
161
      DistortedLibrary.onPause();
162
      RubikScoresDownloader.onPause();
163
      savePreferences();
164
      super.onPause();
165
      }
166

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168
    
169
    @Override
170
    protected void onResume() 
171
      {
172
      super.onResume();
173
      GLSurfaceView view = findViewById(R.id.rubikSurfaceView);
174
      view.onResume();
175
      }
176
    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178
    
179
    @Override
180
    protected void onDestroy() 
181
      {
182
      DistortedLibrary.onDestroy();
183
      super.onDestroy();
184
      }
185

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187

    
188
    @Override
189
    public void onClick(View v)
190
      {
191
      int id = v.getId();
192

    
193
      if( id>=0 && id<RubikSize.LENGTH )
194
        {
195
        int size = RubikSize.getSize(id).getCubeSize();
196

    
197
        RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
198
        boolean success = view.getRenderer().createCube(size);
199

    
200
        if( success )
201
          {
202
          markButton(size);
203
          }
204
        }
205
      }
206

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208
// PUBLIC API
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210

    
211
    public void Settings(View v)
212
      {
213
      RubikSettings settings = new RubikSettings();
214
      settings.show(getSupportFragmentManager(), null);
215
      }
216

    
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218

    
219
    public void Scores(View v)
220
      {
221
      RubikScores scores = new RubikScores();
222
      scores.show(getSupportFragmentManager(), null);
223
      }
224

    
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226

    
227
    public void About(View v)
228
      {
229
      RubikAbout about = new RubikAbout();
230
      about.show(getSupportFragmentManager(), null);
231
      }
232

    
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234

    
235
    public void Scramble(View v)
236
      {
237
      int scramble = mPicker.getValue();
238

    
239
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
240
      view.getRenderer().scrambleCube(scramble);
241
      }
242

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

    
245
    public void Solve(View v)
246
      {
247
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
248
      view.getRenderer().solveCube();
249
      }
250
}
(2-2/11)