Project

General

Profile

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

magiccube / src / main / java / org / distorted / uistate / RubikStateSolving.java @ 27a70eae

1 ad9e8bb3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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
32 0333d81e Leszek Koltunski
import java.util.Timer;
33
import java.util.TimerTask;
34
35 ad9e8bb3 Leszek Koltunski
import static android.view.View.INVISIBLE;
36 27a70eae Leszek Koltunski
import static org.distorted.object.RubikObjectList.LENGTH;
37 329c0aeb Leszek Koltunski
import static org.distorted.uistate.RubikStatePlay.MAX_SCRAMBLE;
38 ad9e8bb3 Leszek Koltunski
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40
41
public class RubikStateSolving extends RubikStateAbstract
42
  {
43 27a70eae Leszek Koltunski
  private static final long NO_RECORD = Integer.MAX_VALUE;
44 329c0aeb Leszek Koltunski
45 0333d81e Leszek Koltunski
  private TextView mTime;
46
  private Timer mTimer;
47
  private long mStartTime;
48
  private boolean mRunning;
49
50 329c0aeb Leszek Koltunski
  private long[][] mRecords;
51
52 0333d81e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
53
54 329c0aeb Leszek Koltunski
  RubikStateSolving()
55
    {
56
    mRecords = new long[LENGTH][MAX_SCRAMBLE];
57
58
    for(int i=0; i<LENGTH; i++)
59
      for(int j=0; j<MAX_SCRAMBLE; j++)
60
        {
61
        mRecords[i][j] = NO_RECORD;
62
        }
63
    }
64
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66
67
  void leaveState(RubikActivity act)
68 ad9e8bb3 Leszek Koltunski
    {
69 0333d81e Leszek Koltunski
    stopCounting();
70 ad9e8bb3 Leszek Koltunski
    }
71
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73
74 329c0aeb Leszek Koltunski
  void enterState(RubikActivity act)
75 ad9e8bb3 Leszek Koltunski
    {
76
    LayoutInflater inflater = act.getLayoutInflater();
77
78
    // TOP ////////////////////////////
79
    LinearLayout layoutTop = act.findViewById(R.id.mainTitle);
80
    layoutTop.removeAllViews();
81 0333d81e Leszek Koltunski
    mTime = (TextView)inflater.inflate(R.layout.upper_text, null);
82
    mTime.setText(R.string.ready);
83
    layoutTop.addView(mTime);
84 ad9e8bb3 Leszek Koltunski
85
    // BOT ////////////////////////////
86
    LinearLayout layoutBot = act.findViewById(R.id.mainBar);
87
    layoutBot.removeAllViews();
88
89
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
90
    float scale = metrics.density;
91
    int size = (int)(60*scale +0.5f);
92
    int padding = (int)(5*scale + 0.5f);
93
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,size,0.5f);
94
95
    Button buttonL = new Button(act);
96
    buttonL.setLayoutParams(params);
97
    buttonL.setId(BUTTON_ID_BACK);
98
    buttonL.setPadding(padding,0,padding,0);
99
    buttonL.setText(R.string.back);
100
    buttonL.setOnClickListener(act);
101
    layoutBot.addView(buttonL);
102
103
    Button buttonR = new Button(act);
104
    buttonR.setLayoutParams(params);
105
    buttonR.setId(BUTTON_ID_BACK);
106
    buttonR.setPadding(padding,0,padding,0);
107
    buttonR.setText(R.string.back);
108
    buttonR.setOnClickListener(act);
109
    layoutBot.addView(buttonR);
110
111
    buttonL.setVisibility(INVISIBLE);
112
    }
113
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115
116
  public void savePreferences(SharedPreferences.Editor editor)
117
    {
118 329c0aeb Leszek Koltunski
    for(int i=0; i<LENGTH; i++)
119
      for(int j=0; j<MAX_SCRAMBLE; j++)
120
        {
121
        if( mRecords[i][j]!=NO_RECORD)
122
          {
123
          editor.putLong("record_"+i+"_"+j, mRecords[i][j]);
124
          }
125
        }
126 ad9e8bb3 Leszek Koltunski
    }
127
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
130
  public void restorePreferences(SharedPreferences preferences)
131
    {
132 329c0aeb Leszek Koltunski
    for(int i=0; i<LENGTH; i++)
133
      for(int j=0; j<MAX_SCRAMBLE; j++)
134
        {
135 27a70eae Leszek Koltunski
        mRecords[i][j] = preferences.getLong("record_"+i+"_"+j, NO_RECORD );
136 329c0aeb Leszek Koltunski
        }
137 ad9e8bb3 Leszek Koltunski
    }
138 0333d81e Leszek Koltunski
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140
141
  public void startCounting(final RubikActivity act)
142
    {
143
    if( !mRunning )
144
      {
145
      mRunning = true;
146
      mStartTime = System.currentTimeMillis();
147
      mTimer = new Timer();
148
149
      mTimer.scheduleAtFixedRate(new TimerTask()
150
        {
151
        @Override
152
        public void run()
153
          {
154
          act.runOnUiThread(new Runnable()
155
            {
156
            @Override
157
            public void run()
158
              {
159
              int elapsed = (int)(System.currentTimeMillis()-mStartTime)/1000;
160
              mTime.setText(act.getString(R.string.tm_placeholder,elapsed/60,elapsed%60));
161
              }
162
            });
163
          }
164
        }, 0, 1000);
165
      }
166
    }
167
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169
170
  public long stopCounting()
171
    {
172
    if( mRunning )
173
      {
174
      if( mTimer!=null )
175
        {
176
        mTimer.cancel();
177
        mTimer = null;
178
        }
179
      mRunning = false;
180
181 329c0aeb Leszek Koltunski
      long timeTaken = System.currentTimeMillis()-mStartTime;
182
183
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
184
      int object  = play.getButton();
185
      int scramble= play.getPicker();
186
187
      if( object>=0 && object<LENGTH && scramble>=0 && scramble<MAX_SCRAMBLE )
188
        {
189
        if( mRecords[object][scramble]> timeTaken )
190
          {
191
          mRecords[object][scramble] = timeTaken;
192
          android.util.Log.e("solv","new record!");
193
          }
194
        }
195
196
      return timeTaken;
197 0333d81e Leszek Koltunski
      }
198
199
    return 0;
200
    }
201 ad9e8bb3 Leszek Koltunski
  }