Project

General

Profile

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

magiccube / src / main / java / org / distorted / uistate / RubikStateSolving.java @ e41e7dc3

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