Project

General

Profile

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

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

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.view.View;
26
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
import org.distorted.scores.RubikScores;
33

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

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

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

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

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

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

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

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

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

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

    
75
    // BOT ////////////////////////////
76
    LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
77
    layoutLeft.removeAllViews();
78
    LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
79
    layoutRight.removeAllViews();
80

    
81
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
82
    float scale = metrics.density;
83
    int padding = (int)(5*scale + 0.5f);
84
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
85

    
86
    mBack = new Button(act);
87
    mBack.setLayoutParams(params);
88
    mBack.setId(BUTTON_ID_BACK);
89
    mBack.setPadding(padding,0,padding,0);
90
    mBack.setText(R.string.back);
91

    
92
    mBack.setOnClickListener( new View.OnClickListener()
93
      {
94
      @Override
95
      public void onClick(View v)
96
        {
97
        RubikState.goBack(act);
98
        }
99
      });
100

    
101
    layoutRight.addView(mBack);
102
    }
103

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105

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

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112

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

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

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

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

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

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

    
182
    return 0;
183
    }
184
  }
(6-6/6)