Project

General

Profile

« Previous | Next » 

Revision 33499c56

Added by Leszek Koltunski over 4 years ago

Major rearrangement of the UI.

View differences:

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

  
20 20
package org.distorted.magic;
21 21

  
22
import android.content.SharedPreferences;
22 23
import android.os.Bundle;
24
import android.preference.PreferenceManager;
25
import android.support.v4.app.FragmentManager;
23 26
import android.support.v7.app.AppCompatActivity;
24 27
import android.view.View;
25 28

  
29
import org.distorted.effect.BaseEffect;
26 30
import org.distorted.library.main.DistortedLibrary;
27 31

  
28 32
///////////////////////////////////////////////////////////////////////////////////////////////////
29 33

  
30 34
public class RubikActivity extends AppCompatActivity implements View.OnClickListener
31 35
{
32
    private RubikDialogMain mMain;
36
    RubikState mCurrentState;
33 37

  
34 38
///////////////////////////////////////////////////////////////////////////////////////////////////
35 39

  
......
40 44
      setTheme(R.style.CustomActivityThemeNoActionBar);
41 45
      setContentView(R.layout.main);
42 46

  
43
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
44
      view.enterMainState(this);
45

  
46 47
      if( savedState==null )
47 48
        {
48
        mMain = new RubikDialogMain();
49
        mMain.show(getSupportFragmentManager(), null);
49
        RubikDialogMain diag = new RubikDialogMain();
50
        diag.show(getSupportFragmentManager(), RubikDialogMain.getDialogTag() );
50 51
        }
51 52
      }
52 53

  
......
59 60
      view.onPause();
60 61
      DistortedLibrary.onPause();
61 62
      RubikScoresDownloader.onPause();
62
      view.savePreferences();
63
      savePreferences();
63 64
      super.onPause();
64 65
      }
65 66

  
......
71 72
      super.onResume();
72 73
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
73 74
      view.onResume();
74
      view.restorePreferences();
75
      restorePreferences();
76
      view.enterState(this, mCurrentState);
75 77
      }
76 78
    
77 79
///////////////////////////////////////////////////////////////////////////////////////////////////
......
105 107

  
106 108
      if( id == RubikSurfaceView.BUTTON_ID_BACK )
107 109
        {
108
        RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
109
        view.enterMainState(this);
110
        mMain.show(getSupportFragmentManager(), null);
110
        mCurrentState = mCurrentState.getBack();
111

  
112
        if( mCurrentState!=null )
113
          {
114
          RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
115
          view.enterState(this, mCurrentState);
116

  
117
          RubikDialogMain diag = new RubikDialogMain();
118
          diag.show(getSupportFragmentManager(), RubikDialogMain.getDialogTag() );
119
          }
120
        else
121
          {
122
          mCurrentState = RubikState.MAIN;
123
          finish();
124
          }
111 125
        }
112
      if( id == RubikSurfaceView.BUTTON_ID_EXIT )
126
      }
127

  
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

  
130
    private void savePreferences()
131
      {
132
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
133
      SharedPreferences.Editor editor = preferences.edit();
134

  
135
      for (int i = 0; i< BaseEffect.Type.LENGTH; i++)
136
        {
137
        BaseEffect.Type.getType(i).savePreferences(editor);
138
        }
139

  
140
      editor.putInt("state", mCurrentState.ordinal() );
141

  
142
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
143
      view.savePreferences(editor);
144

  
145
      editor.apply();
146
      }
147

  
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

  
150
    private void restorePreferences()
151
      {
152
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
153

  
154
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
113 155
        {
114
        finish();
156
        BaseEffect.Type.getType(i).restorePreferences(preferences);
115 157
        }
158

  
159
      int stateOrdinal = preferences.getInt("state", RubikState.MAIN.ordinal() );
160
      mCurrentState = RubikState.getState(stateOrdinal);
161

  
162
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
163
      view.restorePreferences(preferences);
116 164
      }
117 165

  
118 166
///////////////////////////////////////////////////////////////////////////////////////////////////
......
121 169

  
122 170
    public void Play(View v)
123 171
      {
124
      mMain.dismiss();
172
      FragmentManager mana = getSupportFragmentManager();
173
      RubikDialogMain diag = (RubikDialogMain) mana.findFragmentByTag(RubikDialogMain.getDialogTag());
174

  
175
      if( diag!=null )
176
        {
177
        diag.dismiss();
178
        }
179
      else
180
        {
181
        android.util.Log.e("act", "cannot find main dialog!");
182
        }
183

  
184
      mCurrentState = RubikState.PLAY;
125 185
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
126
      view.enterPlayState(this);
186
      view.enterState(this,mCurrentState);
127 187
      }
128 188

  
129 189
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/magic/RubikDialogMain.java
59 59

  
60 60
    return builder.create();
61 61
    }
62

  
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

  
65
  static String getDialogTag()
66
    {
67
    return "DialogMain";
68
    }
62 69
  }
src/main/java/org/distorted/magic/RubikState.java
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 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
///////////////////////////////////////////////////////////////////////////////////////////////////
23

  
24
public enum RubikState
25
  {
26
  MAIN ( null ),
27
  PLAY ( MAIN ),
28
  ;
29

  
30
  static final int LENGTH = values().length;
31
  private final RubikState mBack;
32
  private static final RubikState[] sizes;
33

  
34
  static
35
    {
36
    int i = 0;
37
    sizes = new RubikState[LENGTH];
38

  
39
    for(RubikState size: RubikState.values())
40
      {
41
      sizes[i] = size;
42
      i++;
43
      }
44
    }
45

  
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

  
48
  static RubikState getState(int ordinal)
49
    {
50
    return sizes[ordinal];
51
    }
52

  
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

  
55
  RubikState(RubikState back)
56
    {
57
    mBack = back;
58
    }
59

  
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

  
62
  RubikState getBack()
63
    {
64
    return mBack;
65
    }
66
  }
src/main/java/org/distorted/magic/RubikSurfaceView.java
26 26
import android.graphics.PorterDuff;
27 27
import android.graphics.drawable.Drawable;
28 28
import android.opengl.GLSurfaceView;
29
import android.preference.PreferenceManager;
30 29
import android.support.v4.content.ContextCompat;
31 30
import android.util.AttributeSet;
32 31
import android.util.DisplayMetrics;
......
39 38
import android.widget.LinearLayout;
40 39

  
41 40
import org.distorted.component.HorizontalNumberPicker;
42
import org.distorted.effect.BaseEffect;
43 41
import org.distorted.library.type.Static2D;
44 42
import org.distorted.library.type.Static4D;
45 43
import org.distorted.object.RubikCube;
......
50 48
public class RubikSurfaceView extends GLSurfaceView
51 49
{
52 50
    public static final int BUTTON_ID_BACK= 1023;
53
    public static final int BUTTON_ID_EXIT= 1022;
54 51

  
55 52
    public static final int MIN_SCRAMBLE =  1;
56 53
    public static final int DEF_SCRAMBLE =  1;
......
91 88

  
92 89
///////////////////////////////////////////////////////////////////////////////////////////////////
93 90

  
94
    void savePreferences()
91
    void savePreferences(SharedPreferences.Editor editor)
95 92
      {
96
      RubikActivity act = (RubikActivity)getContext();
97
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(act);
98
      SharedPreferences.Editor editor = preferences.edit();
99

  
100
      for (int i = 0; i< BaseEffect.Type.LENGTH; i++)
101
        {
102
        BaseEffect.Type.getType(i).savePreferences(editor);
103
        }
104

  
105 93
      editor.putInt("scramble", mScrambleValue );
106

  
107
      editor.apply();
108 94
      }
109 95

  
110 96
///////////////////////////////////////////////////////////////////////////////////////////////////
111 97

  
112
    void restorePreferences()
98
    void restorePreferences(SharedPreferences preferences)
113 99
      {
114
      RubikActivity act = (RubikActivity)getContext();
115
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(act);
116

  
117
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
118
        {
119
        BaseEffect.Type.getType(i).restorePreferences(preferences);
120
        }
121

  
122 100
      mScrambleValue= preferences.getInt("scramble", DEF_SCRAMBLE);
123 101
      }
124 102

  
......
280 258

  
281 259
///////////////////////////////////////////////////////////////////////////////////////////////////
282 260

  
283
    void enterMainState(RubikActivity act)
261
    void enterState(RubikActivity act, RubikState state)
262
      {
263
      switch(state)
264
        {
265
        case MAIN: enterMainState(act); break;
266
        case PLAY: enterPlayState(act); break;
267
        }
268
      }
269

  
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271

  
272
    private void enterMainState(RubikActivity act)
284 273
      {
285 274
      LayoutInflater inflater = act.getLayoutInflater();
286 275

  
......
302 291

  
303 292
      Button buttonL = new Button(act);
304 293
      buttonL.setLayoutParams(params);
305
      buttonL.setId(BUTTON_ID_EXIT);
294
      buttonL.setId(BUTTON_ID_BACK);
306 295
      buttonL.setPadding(padding,0,padding,0);
307 296
      buttonL.setText(R.string.back);
308 297
      buttonL.setOnClickListener(act);
......
310 299

  
311 300
      Button buttonR = new Button(act);
312 301
      buttonR.setLayoutParams(params);
313
      buttonR.setId(BUTTON_ID_EXIT);
302
      buttonR.setId(BUTTON_ID_BACK);
314 303
      buttonR.setPadding(padding,0,padding,0);
315 304
      buttonR.setText(R.string.exit);
316 305
      buttonR.setOnClickListener(act);
......
321 310

  
322 311
///////////////////////////////////////////////////////////////////////////////////////////////////
323 312

  
324
    void enterPlayState(RubikActivity act)
313
    private void enterPlayState(RubikActivity act)
325 314
      {
326 315
      LayoutInflater inflater = act.getLayoutInflater();
327 316

  

Also available in: Unified diff