Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenSolving.java @ 70688a23

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 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.screens;
11

    
12
import java.util.Timer;
13
import java.util.TimerTask;
14

    
15
import android.content.SharedPreferences;
16
import android.util.TypedValue;
17
import android.view.LayoutInflater;
18
import android.view.View;
19
import android.widget.LinearLayout;
20
import android.widget.TextView;
21

    
22
import org.distorted.dialogs.RubikDialogAbandon;
23
import org.distorted.helpers.TransparentImageButton;
24
import org.distorted.main.R;
25
import org.distorted.main.RubikActivity;
26
import org.distorted.external.RubikScores;
27
import org.distorted.objects.RubikObjectList;
28

    
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30

    
31
public class RubikScreenSolving extends RubikScreenBase
32
  {
33
  private static final int MOVES_THRESHHOLD = 10;
34

    
35
  private TextView mTime;
36
  private Timer mTimer;
37
  private long mStartTime;
38
  private boolean mRunning;
39
  private final RubikScores mScores;
40
  private long mElapsed;
41
  private TransparentImageButton mBackButton;
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

    
45
  RubikScreenSolving()
46
    {
47
    mScores = RubikScores.getInstance();
48
    }
49

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
  void leaveScreen(RubikActivity act)
53
    {
54
    stopCounting();
55
    }
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

    
59
  void enterScreen(final RubikActivity act)
60
    {
61
    float width = act.getScreenWidthInPixels();
62
    float titleSize  = width*RubikActivity.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
    mTime = (TextView)inflater.inflate(R.layout.upper_text, null);
72
    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 RubikActivity 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

    
112
    mElapsed = System.currentTimeMillis()-mStartTime;
113
    editor.putLong("stateSolving_elapsed" , mElapsed);
114
    mScores.savePreferences(editor);
115
    }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

    
119
  public void restorePreferences(SharedPreferences preferences)
120
    {
121
    mElapsed = preferences.getLong("stateSolving_elapsed" , 0 );
122
    mScores.restorePreferences(preferences);
123
    }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
  private void startCounting(final RubikActivity act)
128
    {
129
    if( !mRunning )
130
      {
131
      mRunning = true;
132
      mStartTime = System.currentTimeMillis() - mElapsed;
133
      mTimer = new Timer();
134

    
135
      mTimer.scheduleAtFixedRate(new TimerTask()
136
        {
137
        @Override
138
        public void run()
139
          {
140
          act.runOnUiThread(new Runnable()
141
            {
142
            @Override
143
            public void run()
144
              {
145
              int elapsed = (int)(System.currentTimeMillis()-mStartTime)/1000;
146
              mTime.setText(act.getString(R.string.tm_placeholder,elapsed/60,elapsed%60));
147
              }
148
            });
149
          }
150
        }, 0, 1000);
151
      }
152
    }
153

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155

    
156
  private void stopCounting()
157
    {
158
    if( mTimer!=null )
159
      {
160
      mTimer.cancel();
161
      mTimer = null;
162
      }
163

    
164
    mRunning = false;
165
    }
166

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168

    
169
  public int stopTimerAndGetRecord()
170
    {
171
    if( mRunning )
172
      {
173
      stopCounting();
174
      mElapsed = System.currentTimeMillis()-mStartTime;
175
      return (int)mElapsed;
176
      }
177

    
178
    return 0;
179
    }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

    
183
  public int setRecord()
184
    {
185
    RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
186
    int object = RubikObjectList.getCurrObject();
187
    int level = play.getLevel()-1;
188
    return mScores.setRecord(object, level, (int)mElapsed);
189
    }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

    
193
  public void resetElapsed()
194
    {
195
    mElapsed = 0;
196
    }
197
  }
(9-9/10)