Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenSolving.java @ 55e6be1d

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

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

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

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

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

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

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

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

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

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

    
72
    startCounting(act);
73

    
74
    LayoutInflater inflater = act.getLayoutInflater();
75

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

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

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

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

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

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

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

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116

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

    
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124

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

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

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153

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

    
162
    mRunning = false;
163
    }
164

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

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

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

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

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

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

    
186
    return 0;
187
    }
188

    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190

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