Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenSolving.java @ 0c233a9a

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,null);
79
    }
80

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82

    
83
  private void setupBackButton(final RubikActivity act)
84
    {
85
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_back,R.drawable.ui_medium_back, R.drawable.ui_big_back, R.drawable.ui_huge_back);
86
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
87
    mBackButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, 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 RubikActivity 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 long stopTimerAndGetRecord()
171
    {
172
    if( mRunning )
173
      {
174
      stopCounting();
175
      mElapsed = System.currentTimeMillis()-mStartTime;
176
      return mElapsed;
177
      }
178

    
179
    return 0;
180
    }
181

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

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

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193

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