Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenSolving.java @ 9f006481

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 android.content.SharedPreferences;
23
import android.util.TypedValue;
24
import android.view.LayoutInflater;
25
import android.view.View;
26
import android.widget.ImageButton;
27
import android.widget.LinearLayout;
28
import android.widget.TextView;
29

    
30
import org.distorted.dialogs.RubikDialogAbandon;
31
import org.distorted.helpers.TransparentImageButton;
32
import org.distorted.main.R;
33
import org.distorted.main.RubikActivity;
34
import org.distorted.objects.ObjectList;
35
import org.distorted.network.RubikScores;
36

    
37
import java.util.Timer;
38
import java.util.TimerTask;
39

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

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

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

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

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

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

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

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

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

    
75
    startCounting(act);
76

    
77
    LayoutInflater inflater = act.getLayoutInflater();
78

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

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

    
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93

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

    
99
    mBackButton.setOnClickListener( new View.OnClickListener()
100
      {
101
      @Override
102
      public void onClick(View v)
103
        {
104
        if( mController.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
    mElapsed = System.currentTimeMillis()-mStartTime;
122
    editor.putLong("stateSolving_elapsed" , mElapsed);
123
    mScores.savePreferences(editor);
124
    }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127

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

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

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

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

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164

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

    
173
    mRunning = false;
174
    }
175

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177

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

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

    
186
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
187
      int object  = play.getObject();
188
      int size    = play.getSize();
189
      int level   = play.getLevel();
190
      int realSize= ObjectList.getSizeIndex(object,size);
191

    
192
      boolean isNew = mScores.setRecord(object, realSize, 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)