Project

General

Profile

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

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

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.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
      int realSize= RubikObjectList.getSizeIndex(object,size);
177

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

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

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