Project

General

Profile

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

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

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.network.RubikScores;
37

    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

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

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

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

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

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

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

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

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

    
73
    startCounting(act);
74

    
75
    LayoutInflater inflater = act.getLayoutInflater();
76

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

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

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91

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

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

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

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

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

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

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

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136

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

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

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

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

    
174
    mRunning = false;
175
    }
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

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

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

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

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

    
195
    return 0;
196
    }
197

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

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