Project

General

Profile

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

magiccube / src / main / java / org / distorted / uistate / RubikStateSolving.java @ 53f23b64

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 a6d3b158 Leszek Koltunski
import android.view.View;
26 ad9e8bb3 Leszek Koltunski
import android.widget.Button;
27
import android.widget.LinearLayout;
28
import android.widget.TextView;
29
30
import org.distorted.magic.R;
31
import org.distorted.magic.RubikActivity;
32 53f23b64 Leszek Koltunski
import org.distorted.object.RubikObjectList;
33 f0e87514 Leszek Koltunski
import org.distorted.scores.RubikScores;
34 ad9e8bb3 Leszek Koltunski
35 0333d81e Leszek Koltunski
import java.util.Timer;
36
import java.util.TimerTask;
37
38 ad9e8bb3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
39
40
public class RubikStateSolving extends RubikStateAbstract
41
  {
42 0333d81e Leszek Koltunski
  private TextView mTime;
43
  private Timer mTimer;
44
  private long mStartTime;
45
  private boolean mRunning;
46 f0e87514 Leszek Koltunski
  private RubikScores mScores;
47 e41e7dc3 Leszek Koltunski
  private Button mBack;
48 329c0aeb Leszek Koltunski
49 0333d81e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
50
51 329c0aeb Leszek Koltunski
  RubikStateSolving()
52
    {
53 714292f1 Leszek Koltunski
    mScores = RubikScores.getInstance();
54 329c0aeb Leszek Koltunski
    }
55
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57
58
  void leaveState(RubikActivity act)
59 ad9e8bb3 Leszek Koltunski
    {
60 e41e7dc3 Leszek Koltunski
    stopCounting(act);
61 ad9e8bb3 Leszek Koltunski
    }
62
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64
65 a6d3b158 Leszek Koltunski
  void enterState(final RubikActivity act)
66 ad9e8bb3 Leszek Koltunski
    {
67
    LayoutInflater inflater = act.getLayoutInflater();
68
69
    // TOP ////////////////////////////
70
    LinearLayout layoutTop = act.findViewById(R.id.mainTitle);
71
    layoutTop.removeAllViews();
72 0333d81e Leszek Koltunski
    mTime = (TextView)inflater.inflate(R.layout.upper_text, null);
73
    mTime.setText(R.string.ready);
74
    layoutTop.addView(mTime);
75 ad9e8bb3 Leszek Koltunski
76
    // BOT ////////////////////////////
77 4c0cd600 Leszek Koltunski
    LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
78
    layoutLeft.removeAllViews();
79
    LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
80
    layoutRight.removeAllViews();
81 ad9e8bb3 Leszek Koltunski
82
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
83
    float scale = metrics.density;
84
    int padding = (int)(5*scale + 0.5f);
85 4c0cd600 Leszek Koltunski
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
86 ad9e8bb3 Leszek Koltunski
87 e41e7dc3 Leszek Koltunski
    mBack = new Button(act);
88
    mBack.setLayoutParams(params);
89
    mBack.setId(BUTTON_ID_BACK);
90
    mBack.setPadding(padding,0,padding,0);
91
    mBack.setText(R.string.back);
92 a6d3b158 Leszek Koltunski
93
    mBack.setOnClickListener( new View.OnClickListener()
94
      {
95
      @Override
96
      public void onClick(View v)
97
        {
98
        RubikState.goBack(act);
99
        }
100
      });
101
102 4c0cd600 Leszek Koltunski
    layoutRight.addView(mBack);
103 ad9e8bb3 Leszek Koltunski
    }
104
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106
107
  public void savePreferences(SharedPreferences.Editor editor)
108
    {
109 f0e87514 Leszek Koltunski
    mScores.savePreferences(editor);
110 ad9e8bb3 Leszek Koltunski
    }
111
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113
114
  public void restorePreferences(SharedPreferences preferences)
115
    {
116 f0e87514 Leszek Koltunski
    mScores.restorePreferences(preferences);
117 ad9e8bb3 Leszek Koltunski
    }
118 0333d81e Leszek Koltunski
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120
121
  public void startCounting(final RubikActivity act)
122
    {
123
    if( !mRunning )
124
      {
125
      mRunning = true;
126
      mStartTime = System.currentTimeMillis();
127
      mTimer = new Timer();
128
129
      mTimer.scheduleAtFixedRate(new TimerTask()
130
        {
131
        @Override
132
        public void run()
133
          {
134
          act.runOnUiThread(new Runnable()
135
            {
136
            @Override
137
            public void run()
138
              {
139
              int elapsed = (int)(System.currentTimeMillis()-mStartTime)/1000;
140
              mTime.setText(act.getString(R.string.tm_placeholder,elapsed/60,elapsed%60));
141
              }
142
            });
143
          }
144
        }, 0, 1000);
145
      }
146
    }
147
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149
150 e41e7dc3 Leszek Koltunski
  public long stopCounting(RubikActivity act)
151 0333d81e Leszek Koltunski
    {
152
    if( mRunning )
153
      {
154 e41e7dc3 Leszek Koltunski
      act.runOnUiThread(new Runnable()
155
        {
156
        @Override
157
        public void run()
158
          {
159
          mTime.setText(R.string.solved);
160
          mBack.setClickable(false);
161
          }
162
        });
163
164 0333d81e Leszek Koltunski
      if( mTimer!=null )
165
        {
166
        mTimer.cancel();
167
        mTimer = null;
168
        }
169
      mRunning = false;
170
171 329c0aeb Leszek Koltunski
      long timeTaken = System.currentTimeMillis()-mStartTime;
172
173
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
174 4888e97c Leszek Koltunski
      int object  = play.getObject();
175
      int size    = play.getSize();
176 329c0aeb Leszek Koltunski
      int scramble= play.getPicker();
177 53f23b64 Leszek Koltunski
      int realSize= RubikObjectList.getSizeIndex(object,size);
178 329c0aeb Leszek Koltunski
179 53f23b64 Leszek Koltunski
      boolean isNew = mScores.setRecord(object, realSize, scramble, timeTaken);
180 e41e7dc3 Leszek Koltunski
181
      return isNew ? timeTaken : -timeTaken;
182 0333d81e Leszek Koltunski
      }
183
184
    return 0;
185
    }
186 ad9e8bb3 Leszek Koltunski
  }