Project

General

Profile

« Previous | Next » 

Revision ce0c7ca8

Added by Leszek Koltunski over 4 years ago

RubikCube: major progress with a separate RubikSettingsEnum enum - almost finished.

View differences:

src/main/java/org/distorted/magic/RubikActivity.java
34 34

  
35 35
///////////////////////////////////////////////////////////////////////////////////////////////////
36 36

  
37
public class RubikActivity extends AppCompatActivity implements RubikSettings.OnCompleteListener
37
public class RubikActivity extends AppCompatActivity
38 38
{
39 39
            static final int DEFAULT_SIZE  = 3;
40 40
    private static final int SMALLEST_SIZE = 2;
......
44 44
    public static final int MAX_SCRAMBLE = 10;
45 45

  
46 46
    private static int mSize = DEFAULT_SIZE;
47

  
48
    private int[] mPos;
49
    private int[] mType;
50

  
51 47
    private HorizontalNumberPicker mPicker;
52 48

  
53 49
///////////////////////////////////////////////////////////////////////////////////////////////////
......
60 56
      setContentView(R.layout.main);
61 57
      markButton(mSize);
62 58

  
63
      mPos = new int[RubikSettingsEnum.LENGTH];
64
      mType= new int[RubikSettingsEnum.LENGTH];
65

  
66 59
      mPicker = findViewById(R.id.rubikNumberPicker);
67 60
      mPicker.setMin(MIN_SCRAMBLE);
68 61
      mPicker.setMax(MAX_SCRAMBLE);
69 62

  
70 63
      restorePreferences();
71

  
72
      for (int i=0; i<RubikSettingsEnum.LENGTH; i++)
73
       {
74
       applyPreferences(i);
75
       }
76 64
      }
77 65

  
78 66
///////////////////////////////////////////////////////////////////////////////////////////////////
......
117 105

  
118 106
    public void Settings(View v)
119 107
      {
120
      Bundle args = new Bundle();
121

  
122
      String name;
123

  
124
      for (int i=0; i<RubikSettingsEnum.LENGTH; i++)
125
        {
126
        RubikSettingsEnum rse = RubikSettingsEnum.getEnum(i);
127
        name = rse.name();
128

  
129
        args.putInt(name+"_Pos" , mPos[i]);
130
        args.putInt(name+"_Type", mType[i]);
131
        }
132

  
133 108
      RubikSettings settings = new RubikSettings();
134
      settings.setArguments(args);
135 109
      settings.show(getSupportFragmentManager(), null);
136 110
      }
137 111

  
......
161 135
      view.getRenderer().solveCube();
162 136
      }
163 137

  
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

  
166
    public void onComplete(int index, int position, int type )
167
      {
168
      if( index>=0 && index<RubikSettingsEnum.LENGTH )
169
        {
170
        mPos[index] = position;
171
        mType[index]= type;
172

  
173
        applyPreferences(index);
174
        }
175
      }
176

  
177 138
///////////////////////////////////////////////////////////////////////////////////////////////////
178 139

  
179 140
    public void setSize(View v)
......
223 184
     {
224 185
     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
225 186
     SharedPreferences.Editor editor = preferences.edit();
226
     String name;
227 187

  
228 188
     for (int i=0; i<RubikSettingsEnum.LENGTH; i++)
229 189
       {
230
       RubikSettingsEnum rse = RubikSettingsEnum.getEnum(i);
231
       name = rse.name();
232

  
233
       editor.putInt(name+"_Pos" , mPos[i]);
234
       editor.putInt(name+"_Type", mType[i]);
190
       RubikSettingsEnum.getEnum(i).savePreferences(editor);
235 191
       }
236 192

  
237 193
     editor.putInt("scramble", mPicker.getValue() );
......
245 201
     {
246 202
     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
247 203

  
248
     String name;
249

  
250 204
     for (int i=0; i<RubikSettingsEnum.LENGTH; i++)
251 205
       {
252
       RubikSettingsEnum rse = RubikSettingsEnum.getEnum(i);
253
       name = rse.name();
254

  
255
       mPos[i]  = preferences.getInt(name+"_Pos" , rse.getDefaultPos() );
256
       mType[i] = preferences.getInt(name+"_Type", rse.getDefaultType());
206
       RubikSettingsEnum.getEnum(i).restorePreferences(preferences);
257 207
       }
258 208

  
259 209
     int scramble= preferences.getInt("scramble", MIN_SCRAMBLE);
260 210

  
261 211
     mPicker.setValue(scramble);
262 212
     }
263

  
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265

  
266
   private void applyPreferences(int index)
267
     {
268
     RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
269
     RubikRenderer renderer = view.getRenderer();
270

  
271
     renderer.setPos (index, translateDuration(mPos[index])+1);
272
     renderer.setType(index, mType[index] );
273
     }
274

  
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

  
277
   public static int translateDuration(int pos)
278
     {
279
     return (pos/2)*100;
280
     }
281 213
}

Also available in: Unified diff