Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikActivity.java @ 4918f19c

1 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4 fdec60a3 Leszek Koltunski
// This file is part of Magic Cube.                                                              //
5 0c52af30 Leszek Koltunski
//                                                                                               //
6 fdec60a3 Leszek Koltunski
// Magic Cube 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 fdec60a3 Leszek Koltunski
// Magic Cube 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 fdec60a3 Leszek Koltunski
// along with Magic Cube.  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 f3e12931 Leszek Koltunski
import android.util.DisplayMetrics;
31 0c52af30 Leszek Koltunski
import android.view.View;
32 f3e12931 Leszek Koltunski
import android.view.ViewGroup;
33
import android.widget.ImageButton;
34
import android.widget.LinearLayout;
35 0c52af30 Leszek Koltunski
36 f548942f Leszek Koltunski
import org.distorted.component.HorizontalNumberPicker;
37 e1111500 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
38 64975793 Leszek Koltunski
import org.distorted.effect.BaseEffect;
39 0c52af30 Leszek Koltunski
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41
42 f3e12931 Leszek Koltunski
public class RubikActivity extends AppCompatActivity implements View.OnClickListener
43 0c52af30 Leszek Koltunski
{
44 f548942f Leszek Koltunski
    public static final int MIN_SCRAMBLE =  1;
45 e4db5995 Leszek Koltunski
    public static final int DEF_SCRAMBLE =  1;
46 36e2cbdd Leszek Koltunski
    public static final int MAX_SCRAMBLE = 18;
47 f548942f Leszek Koltunski
48 32f60dec Leszek Koltunski
    private static int mButton = RubikSize.SIZE3.ordinal();
49 f548942f Leszek Koltunski
    private HorizontalNumberPicker mPicker;
50
51 47ba5ddc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
52
53 32f60dec Leszek Koltunski
    private void markButton(int button)
54 47ba5ddc Leszek Koltunski
      {
55 32f60dec Leszek Koltunski
      mButton = button;
56 47ba5ddc Leszek Koltunski
57 dd73fdab Leszek Koltunski
      for(int b=0; b<RubikSize.LENGTH; b++)
58 47ba5ddc Leszek Koltunski
        {
59 f3e12931 Leszek Koltunski
        Drawable d = findViewById(b).getBackground();
60 47ba5ddc Leszek Koltunski
61 32f60dec Leszek Koltunski
        if( b==button )
62 47ba5ddc Leszek Koltunski
          {
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 f3e12931 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 47ba5ddc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
130
131 32f60dec Leszek Koltunski
    static int getRedButton()
132 47ba5ddc Leszek Koltunski
      {
133 32f60dec Leszek Koltunski
      return mButton;
134 47ba5ddc Leszek Koltunski
      }
135
136 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
137
138
    @Override
139 34747dd1 Leszek Koltunski
    protected void onCreate(Bundle savedState)
140 0c52af30 Leszek Koltunski
      {
141 34747dd1 Leszek Koltunski
      super.onCreate(savedState);
142 f6fcf06a Leszek Koltunski
      setTheme(R.style.CustomActivityThemeNoActionBar);
143 1cbcc6bf Leszek Koltunski
      setContentView(R.layout.main);
144 f3e12931 Leszek Koltunski
      addSizeButtons();
145 32f60dec Leszek Koltunski
      markButton(mButton);
146 f6fcf06a Leszek Koltunski
147 f548942f Leszek Koltunski
      mPicker = findViewById(R.id.rubikNumberPicker);
148
      mPicker.setMin(MIN_SCRAMBLE);
149
      mPicker.setMax(MAX_SCRAMBLE);
150
151 fe381d8e Leszek Koltunski
      restorePreferences();
152 0c52af30 Leszek Koltunski
      }
153
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155
    
156
    @Override
157
    protected void onPause() 
158
      {
159
      GLSurfaceView view = findViewById(R.id.rubikSurfaceView);
160
      view.onPause();
161 e1111500 Leszek Koltunski
      DistortedLibrary.onPause();
162 f3e12931 Leszek Koltunski
      RubikScoresDownloader.onPause();
163 fe381d8e Leszek Koltunski
      savePreferences();
164 0c52af30 Leszek Koltunski
      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 e1111500 Leszek Koltunski
      DistortedLibrary.onDestroy();
183 0c52af30 Leszek Koltunski
      super.onDestroy();
184
      }
185
186 f3e12931 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 32f60dec Leszek Koltunski
          markButton(id);
203 f3e12931 Leszek Koltunski
          }
204
        }
205
      }
206
207 34747dd1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
208 47ba5ddc Leszek Koltunski
// PUBLIC API
209 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
210
211 a47ee432 Leszek Koltunski
    public void Settings(View v)
212 0c52af30 Leszek Koltunski
      {
213 f6fcf06a Leszek Koltunski
      RubikSettings settings = new RubikSettings();
214
      settings.show(getSupportFragmentManager(), null);
215
      }
216
217 a7a7cc9c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
218
219
    public void Scores(View v)
220
      {
221 32f60dec Leszek Koltunski
      Bundle bundle = new Bundle();
222 4918f19c Leszek Koltunski
      bundle.putInt("tab", mButton);
223
224
      RubikScores scores = new RubikScores();
225 32f60dec Leszek Koltunski
      scores.setArguments(bundle);
226 a7a7cc9c Leszek Koltunski
      scores.show(getSupportFragmentManager(), null);
227
      }
228
229 1cbcc6bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
230
231 5560eea9 Leszek Koltunski
    public void About(View v)
232 1cbcc6bf Leszek Koltunski
      {
233 5560eea9 Leszek Koltunski
      RubikAbout about = new RubikAbout();
234
      about.show(getSupportFragmentManager(), null);
235 1cbcc6bf Leszek Koltunski
      }
236
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238
239
    public void Scramble(View v)
240
      {
241 f548942f Leszek Koltunski
      int scramble = mPicker.getValue();
242
243 1cbcc6bf Leszek Koltunski
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
244 f548942f Leszek Koltunski
      view.getRenderer().scrambleCube(scramble);
245 1cbcc6bf Leszek Koltunski
      }
246
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248
249 2ecf0c21 Leszek Koltunski
    public void Solve(View v)
250 1cbcc6bf Leszek Koltunski
      {
251 f548942f Leszek Koltunski
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
252 2ecf0c21 Leszek Koltunski
      view.getRenderer().solveCube();
253 1cbcc6bf Leszek Koltunski
      }
254 0c52af30 Leszek Koltunski
}