Project

General

Profile

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

magiccube / src / main / java / org / distorted / uistate / RubikStateSolving.java @ 329c0aeb

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

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

    
35
import static android.view.View.INVISIBLE;
36
import static org.distorted.object.RubikObject.LENGTH;
37
import static org.distorted.uistate.RubikStatePlay.MAX_SCRAMBLE;
38

    
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40

    
41
public class RubikStateSolving extends RubikStateAbstract
42
  {
43
  private static final int NO_RECORD = Integer.MAX_VALUE;
44

    
45
  private TextView mTime;
46
  private Timer mTimer;
47
  private long mStartTime;
48
  private boolean mRunning;
49

    
50
  private long[][] mRecords;
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

    
54
  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
    {
69
    stopCounting();
70
    }
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

    
74
  void enterState(RubikActivity act)
75
    {
76
    LayoutInflater inflater = act.getLayoutInflater();
77

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

    
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
    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
    }
127

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

    
130
  public void restorePreferences(SharedPreferences preferences)
131
    {
132
    for(int i=0; i<LENGTH; i++)
133
      for(int j=0; j<MAX_SCRAMBLE; j++)
134
        {
135
        mRecords[i][j] = preferences.getInt("record_"+i+"_"+j, NO_RECORD );
136
        }
137
    }
138

    
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
      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
      }
198

    
199
    return 0;
200
    }
201
  }
(5-5/5)