Project

General

Profile

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

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

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.object.RubikObjectList;
33
import org.distorted.scores.RubikScores;
34

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

    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

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

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

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

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

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

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

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

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

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

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

    
87
    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

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

    
102
    layoutRight.addView(mBack);
103
    }
104

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106

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

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

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

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

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

    
173
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
174
      int object  = play.getObject();
175
      int size    = play.getSize();
176
      int scramble= play.getPicker();
177
      int realSize= RubikObjectList.getSizeIndex(object,size);
178

    
179
      boolean isNew = mScores.setRecord(object, realSize, scramble, timeTaken);
180

    
181
      return isNew ? timeTaken : -timeTaken;
182
      }
183

    
184
    return 0;
185
    }
186
  }
(6-6/6)