Project

General

Profile

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

magiccube / src / main / java / org / distorted / uistate / RubikStateSolving.java @ 4888e97c

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.uistate;
21

    
22
import android.content.SharedPreferences;
23
import android.util.DisplayMetrics;
24
import android.view.LayoutInflater;
25
import android.widget.Button;
26
import android.widget.LinearLayout;
27
import android.widget.TextView;
28

    
29
import org.distorted.magic.R;
30
import org.distorted.magic.RubikActivity;
31
import org.distorted.object.RubikObjectList;
32

    
33
import java.util.Timer;
34
import java.util.TimerTask;
35

    
36
import static android.view.View.INVISIBLE;
37
import static org.distorted.object.RubikObjectList.NUM_OBJECTS;
38
import static org.distorted.object.RubikObjectList.MAX_SIZE;
39
import static org.distorted.uistate.RubikStatePlay.MAX_SCRAMBLE;
40

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

    
43
public class RubikStateSolving extends RubikStateAbstract
44
  {
45
  private static final long NO_RECORD = Integer.MAX_VALUE;
46

    
47
  private TextView mTime;
48
  private Timer mTimer;
49
  private long mStartTime;
50
  private boolean mRunning;
51

    
52
  private long[][][] mRecords;
53

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

    
56
  RubikStateSolving()
57
    {
58
    mRecords = new long[NUM_OBJECTS][MAX_SIZE][MAX_SCRAMBLE];
59

    
60
    for(int i=0; i<NUM_OBJECTS; i++)
61
      for(int j=0; j<MAX_SIZE; j++)
62
        for(int k=0; k<MAX_SCRAMBLE; k++)
63
          {
64
          mRecords[i][j][k] = NO_RECORD;
65
          }
66
    }
67

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

    
70
  void leaveState(RubikActivity act)
71
    {
72
    stopCounting();
73
    }
74

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

    
77
  void enterState(RubikActivity act)
78
    {
79
    LayoutInflater inflater = act.getLayoutInflater();
80

    
81
    // TOP ////////////////////////////
82
    LinearLayout layoutTop = act.findViewById(R.id.mainTitle);
83
    layoutTop.removeAllViews();
84
    mTime = (TextView)inflater.inflate(R.layout.upper_text, null);
85
    mTime.setText(R.string.ready);
86
    layoutTop.addView(mTime);
87

    
88
    // BOT ////////////////////////////
89
    LinearLayout layoutBot = act.findViewById(R.id.mainBar);
90
    layoutBot.removeAllViews();
91

    
92
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
93
    float scale = metrics.density;
94
    int size = (int)(60*scale +0.5f);
95
    int padding = (int)(5*scale + 0.5f);
96
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,size,0.5f);
97

    
98
    Button buttonL = new Button(act);
99
    buttonL.setLayoutParams(params);
100
    buttonL.setId(BUTTON_ID_BACK);
101
    buttonL.setPadding(padding,0,padding,0);
102
    buttonL.setText(R.string.back);
103
    buttonL.setOnClickListener(act);
104
    layoutBot.addView(buttonL);
105

    
106
    Button buttonR = new Button(act);
107
    buttonR.setLayoutParams(params);
108
    buttonR.setId(BUTTON_ID_BACK);
109
    buttonR.setPadding(padding,0,padding,0);
110
    buttonR.setText(R.string.back);
111
    buttonR.setOnClickListener(act);
112
    layoutBot.addView(buttonR);
113

    
114
    buttonL.setVisibility(INVISIBLE);
115
    }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

    
119
  public void savePreferences(SharedPreferences.Editor editor)
120
    {
121
    for(int i=0; i<NUM_OBJECTS; i++)
122
      for(int j=0; j<MAX_SIZE; j++)
123
        for(int k=0; k<MAX_SCRAMBLE; k++)
124
          {
125
          if( mRecords[i][j][k]!=NO_RECORD)
126
            {
127
            editor.putLong("record_"+i+"_"+j+"_"+k, mRecords[i][j][k]);
128
            }
129
          }
130
    }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

    
134
  public void restorePreferences(SharedPreferences preferences)
135
    {
136
    for(int i=0; i<NUM_OBJECTS; i++)
137
      for(int j=0; j<MAX_SIZE; j++)
138
        for(int k=0; k<MAX_SCRAMBLE; k++)
139
          {
140
          mRecords[i][j][k] = preferences.getLong("record_"+i+"_"+j+"_"+k, NO_RECORD );
141
          }
142
    }
143

    
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145

    
146
  public void startCounting(final RubikActivity act)
147
    {
148
    if( !mRunning )
149
      {
150
      mRunning = true;
151
      mStartTime = System.currentTimeMillis();
152
      mTimer = new Timer();
153

    
154
      mTimer.scheduleAtFixedRate(new TimerTask()
155
        {
156
        @Override
157
        public void run()
158
          {
159
          act.runOnUiThread(new Runnable()
160
            {
161
            @Override
162
            public void run()
163
              {
164
              int elapsed = (int)(System.currentTimeMillis()-mStartTime)/1000;
165
              mTime.setText(act.getString(R.string.tm_placeholder,elapsed/60,elapsed%60));
166
              }
167
            });
168
          }
169
        }, 0, 1000);
170
      }
171
    }
172

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

    
175
  public long stopCounting()
176
    {
177
    if( mRunning )
178
      {
179
      if( mTimer!=null )
180
        {
181
        mTimer.cancel();
182
        mTimer = null;
183
        }
184
      mRunning = false;
185

    
186
      long timeTaken = System.currentTimeMillis()-mStartTime;
187

    
188
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
189
      int object  = play.getObject();
190
      int size    = play.getSize();
191
      int scramble= play.getPicker();
192
      int maxsize = RubikObjectList.getObject(object).getSizes().length;
193

    
194
      if( object>=0 && object< NUM_OBJECTS && size>=0 && size<maxsize && scramble>=0 && scramble<MAX_SCRAMBLE )
195
        {
196
        if( mRecords[object][size][scramble]> timeTaken )
197
          {
198
          mRecords[object][size][scramble] = timeTaken;
199
          android.util.Log.e("solv","new record!");
200
          }
201
        }
202

    
203
      return timeTaken;
204
      }
205

    
206
    return 0;
207
    }
208
  }
(5-5/5)