Project

General

Profile

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

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

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
import org.distorted.scores.RubikScores;
32

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

    
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37

    
38
public class RubikStateSolving extends RubikStateAbstract
39
  {
40
  private TextView mTime;
41
  private Timer mTimer;
42
  private long mStartTime;
43
  private boolean mRunning;
44
  private RubikScores mScores;
45
  private Button mBack;
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

    
49
  RubikStateSolving()
50
    {
51
    mScores = RubikScores.getInstance();
52
    }
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

    
56
  void leaveState(RubikActivity act)
57
    {
58
    stopCounting(act);
59
    }
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
  void enterState(RubikActivity act)
64
    {
65
    LayoutInflater inflater = act.getLayoutInflater();
66

    
67
    // TOP ////////////////////////////
68
    LinearLayout layoutTop = act.findViewById(R.id.mainTitle);
69
    layoutTop.removeAllViews();
70
    mTime = (TextView)inflater.inflate(R.layout.upper_text, null);
71
    mTime.setText(R.string.ready);
72
    layoutTop.addView(mTime);
73

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

    
100
    buttonL.setVisibility(android.view.View.INVISIBLE);
101
    }
102

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104

    
105
  public void savePreferences(SharedPreferences.Editor editor)
106
    {
107
    mScores.savePreferences(editor);
108
    }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

    
112
  public void restorePreferences(SharedPreferences preferences)
113
    {
114
    mScores.restorePreferences(preferences);
115
    }
116

    
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
  public long stopCounting(RubikActivity act)
149
    {
150
    if( mRunning )
151
      {
152
      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
      if( mTimer!=null )
163
        {
164
        mTimer.cancel();
165
        mTimer = null;
166
        }
167
      mRunning = false;
168

    
169
      long timeTaken = System.currentTimeMillis()-mStartTime;
170

    
171
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
172
      int object  = play.getObject();
173
      int size    = play.getSize();
174
      int scramble= play.getPicker();
175

    
176
      boolean isNew = mScores.setRecord(object, size, scramble, timeTaken);
177

    
178
      return isNew ? timeTaken : -timeTaken;
179
      }
180

    
181
    return 0;
182
    }
183
  }
(5-5/5)