Project

General

Profile

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

magiccube / src / main / java / org / distorted / playui / ScreenSolving.java @ 5a2a7682

1 15ed3429 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2023 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8
///////////////////////////////////////////////////////////////////////////////////////////////////
9
10
package org.distorted.playui;
11
12
import android.content.SharedPreferences;
13
import android.util.TypedValue;
14
import android.view.LayoutInflater;
15
import android.view.View;
16
import android.widget.LinearLayout;
17
import android.widget.TextView;
18
19
import org.distorted.dialogs.RubikDialogAbandon;
20
import org.distorted.external.RubikScores;
21
import org.distorted.helpers.TransparentImageButton;
22
import org.distorted.main.R;
23
24
import java.util.Timer;
25
import java.util.TimerTask;
26
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28
29
public class ScreenSolving extends ScreenBase
30
  {
31
  private static final int MOVES_THRESHHOLD = 10;
32
33
  private TextView mTime;
34
  private Timer mTimer;
35
  private long mStartTime;
36
  private boolean mRunning;
37
  private final RubikScores mScores;
38
  private long mElapsed;
39 7bb30586 leszek
  private int mLevel;
40 15ed3429 leszek
  private TransparentImageButton mBackButton;
41
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43
44
  ScreenSolving()
45
    {
46
    mScores = RubikScores.getInstance();
47
    }
48
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50
51
  void leaveScreen(PlayActivity act)
52
    {
53
    stopCounting();
54
    }
55
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57
58
  void enterScreen(final PlayActivity act)
59
    {
60 7bb30586 leszek
    mLevel = act.getLevel();
61 15ed3429 leszek
    float width = act.getScreenWidthInPixels();
62
    float titleSize  = width*PlayActivity.TITLE_TEXT_SIZE;
63
64
    startCounting(act);
65
66
    LayoutInflater inflater = act.getLayoutInflater();
67
68
    // TOP ////////////////////////////
69
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
70
    layoutTop.removeAllViews();
71 d634fc57 leszek
    mTime = (TextView)inflater.inflate(R.layout.upper_text, null);
72 15ed3429 leszek
    int elapsed = (int)mElapsed/1000;
73
    mTime.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
74
    mTime.setText(act.getString(R.string.tm_placeholder,elapsed/60,elapsed%60));
75
    layoutTop.addView(mTime);
76
77
    setupBackButton(act);
78
    createBottomPane(act,mBackButton);
79
    }
80
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82
83
  private void setupBackButton(final PlayActivity act)
84
    {
85
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
86
    mBackButton = new TransparentImageButton(act,R.drawable.ui_back,params);
87
88
    mBackButton.setOnClickListener( new View.OnClickListener()
89
      {
90
      @Override
91
      public void onClick(View v)
92
        {
93
        if( mMovesController.getNumMoves() > MOVES_THRESHHOLD )
94
          {
95
          RubikDialogAbandon abaDiag = new RubikDialogAbandon();
96
          abaDiag.show(act.getSupportFragmentManager(), null);
97
          }
98
        else
99
          {
100
          ScreenList.goBack(act);
101
          }
102
        }
103
      });
104
    }
105
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107
108
  public void savePreferences(SharedPreferences.Editor editor)
109
    {
110
    stopCounting();
111
    mElapsed = System.currentTimeMillis()-mStartTime;
112
    editor.putLong("stateSolving_elapsed" , mElapsed);
113
    }
114
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116
117
  public void restorePreferences(SharedPreferences preferences)
118
    {
119
    mElapsed = preferences.getLong("stateSolving_elapsed" , 0 );
120
    }
121
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123
124
  private void startCounting(final PlayActivity act)
125
    {
126
    if( !mRunning )
127
      {
128
      mRunning = true;
129
      mStartTime = System.currentTimeMillis() - mElapsed;
130
      mTimer = new Timer();
131
132
      mTimer.scheduleAtFixedRate(new TimerTask()
133
        {
134
        @Override
135
        public void run()
136
          {
137
          act.runOnUiThread(new Runnable()
138
            {
139
            @Override
140
            public void run()
141
              {
142
              int elapsed = (int)(System.currentTimeMillis()-mStartTime)/1000;
143
              mTime.setText(act.getString(R.string.tm_placeholder,elapsed/60,elapsed%60));
144
              }
145
            });
146
          }
147
        }, 0, 1000);
148
      }
149
    }
150
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152
153
  private void stopCounting()
154
    {
155
    if( mTimer!=null )
156
      {
157
      mTimer.cancel();
158
      mTimer = null;
159
      }
160
161
    mRunning = false;
162
    }
163
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165
166
  public int stopTimerAndGetRecord()
167
    {
168
    if( mRunning )
169
      {
170
      stopCounting();
171
      mElapsed = System.currentTimeMillis()-mStartTime;
172
      return (int)mElapsed;
173
      }
174
175
    return 0;
176
    }
177
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179
180 c02235d5 leszek
  public int setRecord(int object)
181 15ed3429 leszek
    {
182 7bb30586 leszek
    return mScores.setRecord(object, mLevel, (int)mElapsed);
183 15ed3429 leszek
    }
184
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186
187
  public void resetElapsed()
188
    {
189
    mElapsed = 0;
190
    }
191
  }