Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenSolving.java @ acabdd83

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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.screens;
21

    
22
import java.util.Timer;
23
import java.util.TimerTask;
24

    
25
import android.content.SharedPreferences;
26
import android.util.TypedValue;
27
import android.view.LayoutInflater;
28
import android.view.View;
29
import android.widget.LinearLayout;
30
import android.widget.TextView;
31

    
32
import org.distorted.dialogs.RubikDialogAbandon;
33
import org.distorted.helpers.TransparentImageButton;
34
import org.distorted.main.R;
35
import org.distorted.main.RubikActivity;
36
import org.distorted.external.RubikScores;
37
import org.distorted.objects.RubikObjectList;
38

    
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40

    
41
public class RubikScreenSolving extends RubikScreenBase
42
  {
43
  private static final int MOVES_THRESHHOLD = 10;
44

    
45
  private TextView mTime;
46
  private Timer mTimer;
47
  private long mStartTime;
48
  private boolean mRunning;
49
  private final RubikScores mScores;
50
  private long mElapsed;
51
  private TransparentImageButton mBackButton;
52

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

    
55
  RubikScreenSolving()
56
    {
57
    mScores = RubikScores.getInstance();
58
    }
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
  void leaveScreen(RubikActivity act)
63
    {
64
    stopCounting();
65
    }
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

    
69
  void enterScreen(final RubikActivity act)
70
    {
71
    float width = act.getScreenWidthInPixels();
72
    float titleSize  = width*RubikActivity.TITLE_TEXT_SIZE;
73

    
74
    startCounting(act);
75

    
76
    LayoutInflater inflater = act.getLayoutInflater();
77

    
78
    // TOP ////////////////////////////
79
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
80
    layoutTop.removeAllViews();
81
    mTime = (TextView)inflater.inflate(R.layout.upper_text, null);
82
    int elapsed = (int)mElapsed/1000;
83
    mTime.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
84
    mTime.setText(act.getString(R.string.tm_placeholder,elapsed/60,elapsed%60));
85
    layoutTop.addView(mTime);
86

    
87
    setupBackButton(act);
88
    createBottomPane(act,mBackButton,null);
89
    }
90

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

    
93
  private void setupBackButton(final RubikActivity act)
94
    {
95
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_back,R.drawable.ui_medium_back, R.drawable.ui_big_back, R.drawable.ui_huge_back);
96
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
97
    mBackButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
98

    
99
    mBackButton.setOnClickListener( new View.OnClickListener()
100
      {
101
      @Override
102
      public void onClick(View v)
103
        {
104
        if( mMovesController.getNumMoves() > MOVES_THRESHHOLD )
105
          {
106
          RubikDialogAbandon abaDiag = new RubikDialogAbandon();
107
          abaDiag.show(act.getSupportFragmentManager(), null);
108
          }
109
        else
110
          {
111
          ScreenList.goBack(act);
112
          }
113
        }
114
      });
115
    }
116

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

    
119
  public void savePreferences(SharedPreferences.Editor editor)
120
    {
121
    stopCounting();
122

    
123
    mElapsed = System.currentTimeMillis()-mStartTime;
124
    editor.putLong("stateSolving_elapsed" , mElapsed);
125
    mScores.savePreferences(editor);
126
    }
127

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

    
130
  public void restorePreferences(SharedPreferences preferences)
131
    {
132
    mElapsed = preferences.getLong("stateSolving_elapsed" , 0 );
133
    mScores.restorePreferences(preferences);
134
    }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137

    
138
  private void startCounting(final RubikActivity act)
139
    {
140
    if( !mRunning )
141
      {
142
      mRunning = true;
143
      mStartTime = System.currentTimeMillis() - mElapsed;
144
      mTimer = new Timer();
145

    
146
      mTimer.scheduleAtFixedRate(new TimerTask()
147
        {
148
        @Override
149
        public void run()
150
          {
151
          act.runOnUiThread(new Runnable()
152
            {
153
            @Override
154
            public void run()
155
              {
156
              int elapsed = (int)(System.currentTimeMillis()-mStartTime)/1000;
157
              mTime.setText(act.getString(R.string.tm_placeholder,elapsed/60,elapsed%60));
158
              }
159
            });
160
          }
161
        }, 0, 1000);
162
      }
163
    }
164

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166

    
167
  private void stopCounting()
168
    {
169
    if( mTimer!=null )
170
      {
171
      mTimer.cancel();
172
      mTimer = null;
173
      }
174

    
175
    mRunning = false;
176
    }
177

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179

    
180
  public long getRecord()
181
    {
182
    if( mRunning )
183
      {
184
      stopCounting();
185

    
186
      mElapsed = System.currentTimeMillis()-mStartTime;
187

    
188
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
189
      int object = RubikObjectList.getCurrObject();
190
      int level = play.getLevel();
191
      boolean isNew = mScores.setRecord(object, level, mElapsed);
192

    
193
      return isNew ? mElapsed : -mElapsed;
194
      }
195

    
196
    return 0;
197
    }
198

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200

    
201
  public void resetElapsed()
202
    {
203
    mElapsed = 0;
204
    }
205
  }
(9-9/10)