Project

General

Profile

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

magiccube / src / main / java / org / distorted / playui / ScreenSolving.java @ c02235d5

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