Project

General

Profile

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

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

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

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

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

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

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

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

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

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

    
73
    // BOT ////////////////////////////
74
    LinearLayout layoutBot = act.findViewById(R.id.mainBar);
75
    layoutBot.removeAllViews();
76

    
77
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
78
    float scale = metrics.density;
79
    int size = (int)(60*scale +0.5f);
80
    int padding = (int)(5*scale + 0.5f);
81
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,size,0.5f);
82

    
83
    Button buttonL = new Button(act);
84
    buttonL.setLayoutParams(params);
85
    buttonL.setId(BUTTON_ID_BACK);
86
    buttonL.setPadding(padding,0,padding,0);
87
    buttonL.setText(R.string.back);
88
    buttonL.setOnClickListener(act);
89
    layoutBot.addView(buttonL);
90

    
91
    Button buttonR = new Button(act);
92
    buttonR.setLayoutParams(params);
93
    buttonR.setId(BUTTON_ID_BACK);
94
    buttonR.setPadding(padding,0,padding,0);
95
    buttonR.setText(R.string.back);
96
    buttonR.setOnClickListener(act);
97
    layoutBot.addView(buttonR);
98

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

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

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

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

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

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

    
118
  public void startCounting(final RubikActivity act)
119
    {
120
    if( !mRunning )
121
      {
122
      mRunning = true;
123
      mStartTime = System.currentTimeMillis();
124
      mTimer = new Timer();
125

    
126
      mTimer.scheduleAtFixedRate(new TimerTask()
127
        {
128
        @Override
129
        public void run()
130
          {
131
          act.runOnUiThread(new Runnable()
132
            {
133
            @Override
134
            public void run()
135
              {
136
              int elapsed = (int)(System.currentTimeMillis()-mStartTime)/1000;
137
              mTime.setText(act.getString(R.string.tm_placeholder,elapsed/60,elapsed%60));
138
              }
139
            });
140
          }
141
        }, 0, 1000);
142
      }
143
    }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

    
147
  public long stopCounting()
148
    {
149
    if( mRunning )
150
      {
151
      if( mTimer!=null )
152
        {
153
        mTimer.cancel();
154
        mTimer = null;
155
        }
156
      mRunning = false;
157

    
158
      long timeTaken = System.currentTimeMillis()-mStartTime;
159

    
160
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
161
      int object  = play.getObject();
162
      int size    = play.getSize();
163
      int scramble= play.getPicker();
164

    
165
      mScores.setRecord(object, size, scramble, timeTaken);
166
      return timeTaken;
167
      }
168

    
169
    return 0;
170
    }
171
  }
(5-5/5)