Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenSolving.java @ 7ac0ee88

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.ImageButton;
30
import android.widget.LinearLayout;
31
import android.widget.TextView;
32

    
33
import org.distorted.objectlib.main.ObjectList;
34

    
35
import org.distorted.dialogs.RubikDialogAbandon;
36
import org.distorted.helpers.TransparentImageButton;
37
import org.distorted.main.R;
38
import org.distorted.main.RubikActivity;
39
import org.distorted.network.RubikScores;
40

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

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

    
47
  private TextView mTime;
48
  private Timer mTimer;
49
  private long mStartTime;
50
  private boolean mRunning;
51
  private final RubikScores mScores;
52
  private long mElapsed;
53
  private ImageButton mBackButton;
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

    
57
  RubikScreenSolving()
58
    {
59
    mScores = RubikScores.getInstance();
60
    }
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

    
64
  void leaveScreen(RubikActivity act)
65
    {
66
    stopCounting();
67
    }
68

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

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

    
76
    startCounting(act);
77

    
78
    LayoutInflater inflater = act.getLayoutInflater();
79

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

    
89
    setupBackButton(act,width);
90
    createBottomPane(act,width,mBackButton);
91
    }
92

    
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94

    
95
  private void setupBackButton(final RubikActivity act, final float width)
96
    {
97
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_back,R.drawable.ui_medium_back, R.drawable.ui_big_back, R.drawable.ui_huge_back);
98
    mBackButton = new TransparentImageButton(act, icon, width, LinearLayout.LayoutParams.MATCH_PARENT);
99

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

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

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

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

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130

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

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138

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

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

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

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

    
176
    mRunning = false;
177
    }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

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

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

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

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

    
197
    return 0;
198
    }
199

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201

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