Project

General

Profile

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

magiccube / src / main / java / org / distorted / uistate / RubikStateSolving.java @ 714292f1

1 ad9e8bb3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 f0e87514 Leszek Koltunski
import org.distorted.scores.RubikScores;
32 ad9e8bb3 Leszek Koltunski
33 0333d81e Leszek Koltunski
import java.util.Timer;
34
import java.util.TimerTask;
35
36 ad9e8bb3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
37
38
public class RubikStateSolving extends RubikStateAbstract
39
  {
40 0333d81e Leszek Koltunski
  private TextView mTime;
41
  private Timer mTimer;
42
  private long mStartTime;
43
  private boolean mRunning;
44 f0e87514 Leszek Koltunski
  private RubikScores mScores;
45 329c0aeb Leszek Koltunski
46 0333d81e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
47
48 329c0aeb Leszek Koltunski
  RubikStateSolving()
49
    {
50 714292f1 Leszek Koltunski
    mScores = RubikScores.getInstance();
51 329c0aeb Leszek Koltunski
    }
52
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54
55
  void leaveState(RubikActivity act)
56 ad9e8bb3 Leszek Koltunski
    {
57 0333d81e Leszek Koltunski
    stopCounting();
58 ad9e8bb3 Leszek Koltunski
    }
59
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61
62 329c0aeb Leszek Koltunski
  void enterState(RubikActivity act)
63 ad9e8bb3 Leszek Koltunski
    {
64
    LayoutInflater inflater = act.getLayoutInflater();
65
66
    // TOP ////////////////////////////
67
    LinearLayout layoutTop = act.findViewById(R.id.mainTitle);
68
    layoutTop.removeAllViews();
69 0333d81e Leszek Koltunski
    mTime = (TextView)inflater.inflate(R.layout.upper_text, null);
70
    mTime.setText(R.string.ready);
71
    layoutTop.addView(mTime);
72 ad9e8bb3 Leszek Koltunski
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 f0e87514 Leszek Koltunski
    buttonL.setVisibility(android.view.View.INVISIBLE);
100 ad9e8bb3 Leszek Koltunski
    }
101
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103
104
  public void savePreferences(SharedPreferences.Editor editor)
105
    {
106 f0e87514 Leszek Koltunski
    mScores.savePreferences(editor);
107 ad9e8bb3 Leszek Koltunski
    }
108
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
111
  public void restorePreferences(SharedPreferences preferences)
112
    {
113 f0e87514 Leszek Koltunski
    mScores.restorePreferences(preferences);
114 ad9e8bb3 Leszek Koltunski
    }
115 0333d81e Leszek Koltunski
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 329c0aeb Leszek Koltunski
      long timeTaken = System.currentTimeMillis()-mStartTime;
159
160
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
161 4888e97c Leszek Koltunski
      int object  = play.getObject();
162
      int size    = play.getSize();
163 329c0aeb Leszek Koltunski
      int scramble= play.getPicker();
164
165 f0e87514 Leszek Koltunski
      mScores.newRecord(object, size, scramble, timeTaken);
166 329c0aeb Leszek Koltunski
      return timeTaken;
167 0333d81e Leszek Koltunski
      }
168
169
    return 0;
170
    }
171 ad9e8bb3 Leszek Koltunski
  }