Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikActivity.java @ 1cbcc6bf

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.effect.AppearEffect;
33
import org.distorted.effect.DisappearEffect;
34
import org.distorted.library.main.DistortedLibrary;
35

    
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37

    
38
public class RubikActivity extends AppCompatActivity implements RubikSettings.OnCompleteListener
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 DEFAULT_APPEAR_POS     = 10;
45
    public static final int DEFAULT_DISAPPEAR_POS  = 10;
46
    public static final int DEFAULT_APPEAR_TYPE    = 1;
47
    public static final int DEFAULT_DISAPPEAR_TYPE = 1;
48

    
49
    private static int mSize = DEFAULT_SIZE;
50

    
51
    private int mAppearPos, mDisappearPos;
52
    private int mAppearType, mDisappearType;
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

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

    
64
      restorePreferences();
65
      applyPreferences();
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
      Bundle args = new Bundle();
111

    
112
      args.putInt("appearPos"    , mAppearPos    );
113
      args.putInt("disappearPos" , mDisappearPos );
114
      args.putInt("appearType"   , mAppearType   );
115
      args.putInt("disappearType", mDisappearType);
116

    
117
      RubikSettings settings = new RubikSettings();
118
      settings.setArguments(args);
119
      settings.show(getSupportFragmentManager(), null);
120
      }
121

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

    
124
    public void Credits(View v)
125
      {
126
      RubikCredits credits = new RubikCredits();
127
      credits.show(getSupportFragmentManager(), null);
128
      }
129

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131

    
132
    public void Scramble(View v)
133
      {
134
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
135
      view.getRenderer().scrambleCube();
136
      }
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

    
140
    public void Solve(View v)
141
      {
142

    
143
      }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

    
147
    public void onComplete(int aP, int dP, int aT, int dT)
148
      {
149
      mAppearPos    = aP;
150
      mDisappearPos = dP;
151
      mAppearType   = aT;
152
      mDisappearType= dT;
153

    
154
      applyPreferences();
155
      }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

    
159
    public void setSize(View v)
160
      {
161
      int size=0, id = v.getId();
162

    
163
      for(int b=0; b<button_ids.length; b++)
164
        if( button_ids[b] == id )
165
          {
166
          size = b+SMALLEST_SIZE;
167
          break;
168
          }
169

    
170
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
171
      boolean success = view.getRenderer().createCube(size);
172

    
173
      if( success )
174
        {
175
        markButton(size);
176
        }
177
      }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

    
181
   private void markButton(int size)
182
     {
183
     mSize = size;
184

    
185
     for(int b=0; b<button_ids.length; b++)
186
       {
187
       Drawable d = findViewById(button_ids[b]).getBackground();
188

    
189
       if( size == b+SMALLEST_SIZE )
190
         {
191
         d.setColorFilter(ContextCompat.getColor(this,R.color.red), PorterDuff.Mode.MULTIPLY);
192
         }
193
       else
194
         {
195
         d.clearColorFilter();
196
         }
197
       }
198
     }
199

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

    
202
   private void savePreferences()
203
     {
204
     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
205
     SharedPreferences.Editor editor = preferences.edit();
206

    
207
     editor.putInt("appearPos"    , mAppearPos    );
208
     editor.putInt("disappearPos" , mDisappearPos );
209
     editor.putInt("appearType"   , mAppearType   );
210
     editor.putInt("disappearType", mDisappearType);
211

    
212
     editor.apply();
213
     }
214

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

    
217
   private void restorePreferences()
218
     {
219
     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
220

    
221
     mAppearPos     = preferences.getInt("appearPos"    , DEFAULT_APPEAR_POS    );
222
     mDisappearPos  = preferences.getInt("disappearPos" , DEFAULT_DISAPPEAR_POS );
223
     mAppearType    = preferences.getInt("appearType"   , DEFAULT_APPEAR_TYPE   );
224
     mDisappearType = preferences.getInt("disappearType", DEFAULT_DISAPPEAR_TYPE);
225
     }
226

    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228

    
229
   private void applyPreferences()
230
     {
231
     RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
232
     RubikRenderer renderer = view.getRenderer();
233

    
234
     renderer.setAppearDuration(translateDuration(mAppearPos)+1);
235
     renderer.setDisappearDuration(translateDuration(mDisappearPos) +1);
236
     renderer.setAppearType(AppearEffect.getType(mAppearType));
237
     renderer.setDisappearType(DisappearEffect.getType(mDisappearType));
238
     }
239

    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241

    
242
   public static int translateDuration(int pos)
243
     {
244
     return (pos/2)*100;
245
     }
246
}
(1-1/6)