Project

General

Profile

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

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

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.main.R;
31
import org.distorted.main.RubikActivity;
32
import org.distorted.objects.ObjectList;
33
import org.distorted.network.RubikScores;
34

    
35
import java.util.Timer;
36
import java.util.TimerTask;
37

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

    
40
public class RubikScreenSolving extends RubikScreenBase
41
  {
42
  private TextView mTime;
43
  private Timer mTimer;
44
  private long mStartTime;
45
  private boolean mRunning;
46
  private RubikScores mScores;
47
  private long mElapsed;
48
  private ImageButton mBackButton;
49

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
  RubikScreenSolving()
53
    {
54
    mScores = RubikScores.getInstance();
55
    }
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

    
59
  void leaveState(RubikActivity act)
60
    {
61
    stopCounting();
62
    }
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
  void enterState(final RubikActivity act)
67
    {
68
    float width = act.getScreenWidthInPixels();
69
    float titleSize  = width*RubikActivity.TITLE_TEXT_SIZE;
70

    
71
    startCounting(act);
72

    
73
    LayoutInflater inflater = act.getLayoutInflater();
74

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

    
84
    setupBackButton(act,width);
85
    createBottomPane(act,width,mBackButton);
86
    }
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89

    
90
  private void setupBackButton(final RubikActivity act, final float width)
91
    {
92
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_back,R.drawable.ui_medium_back, R.drawable.ui_big_back, R.drawable.ui_huge_back);
93
    mBackButton = new TransparentImageButton(act, icon, width, LinearLayout.LayoutParams.MATCH_PARENT);
94

    
95
    mBackButton.setOnClickListener( new View.OnClickListener()
96
      {
97
      @Override
98
      public void onClick(View v)
99
        {
100
        ScreenList.goBack(act);
101
        }
102
      });
103
    }
104

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106

    
107
  public void savePreferences(SharedPreferences.Editor editor)
108
    {
109
    mElapsed = System.currentTimeMillis()-mStartTime;
110
    editor.putLong("stateSolving_elapsed" , mElapsed);
111
    mScores.savePreferences(editor);
112
    }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

    
116
  public void restorePreferences(SharedPreferences preferences)
117
    {
118
    mElapsed = preferences.getLong("stateSolving_elapsed" , 0 );
119
    mScores.restorePreferences(preferences);
120
    }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

    
124
  private void startCounting(final RubikActivity act)
125
    {
126
    if( !mRunning )
127
      {
128
      mRunning = true;
129
      mStartTime = System.currentTimeMillis() - mElapsed;
130
      mTimer = new Timer();
131

    
132
      mTimer.scheduleAtFixedRate(new TimerTask()
133
        {
134
        @Override
135
        public void run()
136
          {
137
          act.runOnUiThread(new Runnable()
138
            {
139
            @Override
140
            public void run()
141
              {
142
              int elapsed = (int)(System.currentTimeMillis()-mStartTime)/1000;
143
              mTime.setText(act.getString(R.string.tm_placeholder,elapsed/60,elapsed%60));
144
              }
145
            });
146
          }
147
        }, 0, 1000);
148
      }
149
    }
150

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

    
153
  private void stopCounting()
154
    {
155
    if( mTimer!=null )
156
      {
157
      mTimer.cancel();
158
      mTimer = null;
159
      }
160

    
161
    mRunning = false;
162
    }
163

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

    
166
  public long getRecord()
167
    {
168
    if( mRunning )
169
      {
170
      stopCounting();
171

    
172
      mElapsed = System.currentTimeMillis()-mStartTime;
173

    
174
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getStateClass();
175
      int object  = play.getObject();
176
      int size    = play.getSize();
177
      int level   = play.getLevel();
178
      int realSize= ObjectList.getSizeIndex(object,size);
179

    
180
      boolean isNew = mScores.setRecord(object, realSize, level, mElapsed);
181

    
182
      return isNew ? mElapsed : -mElapsed;
183
      }
184

    
185
    return 0;
186
    }
187

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189

    
190
  public void resetElapsed()
191
    {
192
    mElapsed = 0;
193
    }
194
  }
(9-9/12)