Project

General

Profile

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

magiccube / src / main / java / org / distorted / uistate / RubikStateSolving.java @ 0a57000c

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.setPadding(padding,0,padding,0);
90
    mBack.setText(R.string.back);
91 a6d3b158 Leszek Koltunski
92
    mBack.setOnClickListener( new View.OnClickListener()
93
      {
94
      @Override
95
      public void onClick(View v)
96
        {
97
        RubikState.goBack(act);
98
        }
99
      });
100
101 4c0cd600 Leszek Koltunski
    layoutRight.addView(mBack);
102 ad9e8bb3 Leszek Koltunski
    }
103
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105
106
  public void savePreferences(SharedPreferences.Editor editor)
107
    {
108 f0e87514 Leszek Koltunski
    mScores.savePreferences(editor);
109 ad9e8bb3 Leszek Koltunski
    }
110
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112
113
  public void restorePreferences(SharedPreferences preferences)
114
    {
115 f0e87514 Leszek Koltunski
    mScores.restorePreferences(preferences);
116 ad9e8bb3 Leszek Koltunski
    }
117 0333d81e Leszek Koltunski
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119
120
  public void startCounting(final RubikActivity act)
121
    {
122
    if( !mRunning )
123
      {
124
      mRunning = true;
125
      mStartTime = System.currentTimeMillis();
126
      mTimer = new Timer();
127
128
      mTimer.scheduleAtFixedRate(new TimerTask()
129
        {
130
        @Override
131
        public void run()
132
          {
133
          act.runOnUiThread(new Runnable()
134
            {
135
            @Override
136
            public void run()
137
              {
138
              int elapsed = (int)(System.currentTimeMillis()-mStartTime)/1000;
139
              mTime.setText(act.getString(R.string.tm_placeholder,elapsed/60,elapsed%60));
140
              }
141
            });
142
          }
143
        }, 0, 1000);
144
      }
145
    }
146
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148
149 e41e7dc3 Leszek Koltunski
  public long stopCounting(RubikActivity act)
150 0333d81e Leszek Koltunski
    {
151
    if( mRunning )
152
      {
153 e41e7dc3 Leszek Koltunski
      act.runOnUiThread(new Runnable()
154
        {
155
        @Override
156
        public void run()
157
          {
158
          mTime.setText(R.string.solved);
159
          mBack.setClickable(false);
160
          }
161
        });
162
163 0333d81e Leszek Koltunski
      if( mTimer!=null )
164
        {
165
        mTimer.cancel();
166
        mTimer = null;
167
        }
168
      mRunning = false;
169
170 329c0aeb Leszek Koltunski
      long timeTaken = System.currentTimeMillis()-mStartTime;
171
172
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
173 4888e97c Leszek Koltunski
      int object  = play.getObject();
174
      int size    = play.getSize();
175 329c0aeb Leszek Koltunski
      int scramble= play.getPicker();
176 53f23b64 Leszek Koltunski
      int realSize= RubikObjectList.getSizeIndex(object,size);
177 329c0aeb Leszek Koltunski
178 53f23b64 Leszek Koltunski
      boolean isNew = mScores.setRecord(object, realSize, scramble, timeTaken);
179 e41e7dc3 Leszek Koltunski
180
      return isNew ? timeTaken : -timeTaken;
181 0333d81e Leszek Koltunski
      }
182
183
    return 0;
184
    }
185 ad9e8bb3 Leszek Koltunski
  }