Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikStateSolving.java @ 88fb92ba

1 ad9e8bb3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 1f9772f3 Leszek Koltunski
package org.distorted.states;
21 ad9e8bb3 Leszek Koltunski
22
import android.content.SharedPreferences;
23
import android.util.DisplayMetrics;
24 e3c74c0f Leszek Koltunski
import android.util.TypedValue;
25 ad9e8bb3 Leszek Koltunski
import android.view.LayoutInflater;
26 a6d3b158 Leszek Koltunski
import android.view.View;
27 ad9e8bb3 Leszek Koltunski
import android.widget.Button;
28 0e5ad27c Leszek Koltunski
import android.widget.ImageButton;
29 ad9e8bb3 Leszek Koltunski
import android.widget.LinearLayout;
30
import android.widget.TextView;
31
32 1f9772f3 Leszek Koltunski
import org.distorted.main.R;
33
import org.distorted.main.RubikActivity;
34 5a4d4fba Leszek Koltunski
import org.distorted.main.RubikPreRender;
35 0e5ad27c Leszek Koltunski
import org.distorted.objects.RubikObject;
36 1f9772f3 Leszek Koltunski
import org.distorted.objects.RubikObjectList;
37 f0e87514 Leszek Koltunski
import org.distorted.scores.RubikScores;
38 ad9e8bb3 Leszek Koltunski
39 0e5ad27c Leszek Koltunski
import java.util.ArrayList;
40 0333d81e Leszek Koltunski
import java.util.Timer;
41
import java.util.TimerTask;
42
43 ad9e8bb3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
44
45 5a4d4fba Leszek Koltunski
public class RubikStateSolving extends RubikStateAbstract implements RubikPreRender.ActionFinishedListener
46 ad9e8bb3 Leszek Koltunski
  {
47 0e5ad27c Leszek Koltunski
  private static final int DURATION_MILLIS = 750;
48
49 0333d81e Leszek Koltunski
  private TextView mTime;
50
  private Timer mTimer;
51
  private long mStartTime;
52
  private boolean mRunning;
53 f0e87514 Leszek Koltunski
  private RubikScores mScores;
54 0e5ad27c Leszek Koltunski
  private ImageButton mPrevButton;
55
  private boolean mCanPrevMove;
56
  private ArrayList<Move> mMoves;
57 2e8ec627 Leszek Koltunski
  private long mElapsed;
58 0e5ad27c Leszek Koltunski
59
  private static class Move
60
    {
61
    private int mAxis, mRow, mAngle;
62
63
    Move(int axis, int row, int angle)
64
      {
65
      mAxis = axis;
66
      mRow  = row;
67
      mAngle= angle;
68
      }
69
    }
70 329c0aeb Leszek Koltunski
71 0333d81e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
72
73 329c0aeb Leszek Koltunski
  RubikStateSolving()
74
    {
75 714292f1 Leszek Koltunski
    mScores = RubikScores.getInstance();
76 329c0aeb Leszek Koltunski
    }
77
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79
80
  void leaveState(RubikActivity act)
81 ad9e8bb3 Leszek Koltunski
    {
82 807d82b7 Leszek Koltunski
    stopCounting();
83 ad9e8bb3 Leszek Koltunski
    }
84
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86
87 a6d3b158 Leszek Koltunski
  void enterState(final RubikActivity act)
88 ad9e8bb3 Leszek Koltunski
    {
89 e3c74c0f Leszek Koltunski
    float width = act.getScreenWidthInPixels();
90
    float buttonSize = width*RubikActivity.BUTTON_TEXT_SIZE;
91
    float titleSize  = width*RubikActivity.TITLE_TEXT_SIZE;
92
93 0e5ad27c Leszek Koltunski
    mCanPrevMove = true;
94
95 2e8ec627 Leszek Koltunski
    startCounting(act);
96
97 0e5ad27c Leszek Koltunski
    if( mMoves==null ) mMoves = new ArrayList<>();
98
    else               mMoves.clear();
99
100 ad9e8bb3 Leszek Koltunski
    LayoutInflater inflater = act.getLayoutInflater();
101 0e5ad27c Leszek Koltunski
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
102
    float scale = metrics.density;
103 ad9e8bb3 Leszek Koltunski
104
    // TOP ////////////////////////////
105 7289fd6c Leszek Koltunski
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
106 ad9e8bb3 Leszek Koltunski
    layoutTop.removeAllViews();
107 0333d81e Leszek Koltunski
    mTime = (TextView)inflater.inflate(R.layout.upper_text, null);
108 2e8ec627 Leszek Koltunski
    int elapsed = (int)mElapsed/1000;
109 e3c74c0f Leszek Koltunski
    mTime.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
110 2e8ec627 Leszek Koltunski
    mTime.setText(act.getString(R.string.tm_placeholder,elapsed/60,elapsed%60));
111 0333d81e Leszek Koltunski
    layoutTop.addView(mTime);
112 ad9e8bb3 Leszek Koltunski
113
    // BOT ////////////////////////////
114 4c0cd600 Leszek Koltunski
    LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
115
    layoutLeft.removeAllViews();
116 0e5ad27c Leszek Koltunski
117 98e7cc0f Leszek Koltunski
    setupPrevMoveButtom(act,scale,width);
118 0e5ad27c Leszek Koltunski
    layoutLeft.addView(mPrevButton);
119
120 4c0cd600 Leszek Koltunski
    LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
121
    layoutRight.removeAllViews();
122 ad9e8bb3 Leszek Koltunski
123
    int padding = (int)(5*scale + 0.5f);
124 4c0cd600 Leszek Koltunski
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
125 ad9e8bb3 Leszek Koltunski
126 807d82b7 Leszek Koltunski
    Button back = new Button(act);
127
    back.setLayoutParams(params);
128
    back.setPadding(padding,0,padding,0);
129 e3c74c0f Leszek Koltunski
    back.setTextSize(TypedValue.COMPLEX_UNIT_PX, buttonSize);
130 807d82b7 Leszek Koltunski
    back.setText(R.string.back);
131 a6d3b158 Leszek Koltunski
132 807d82b7 Leszek Koltunski
    back.setOnClickListener( new View.OnClickListener()
133 a6d3b158 Leszek Koltunski
      {
134
      @Override
135
      public void onClick(View v)
136
        {
137
        RubikState.goBack(act);
138
        }
139
      });
140
141 807d82b7 Leszek Koltunski
    layoutRight.addView(back);
142 ad9e8bb3 Leszek Koltunski
    }
143
144 0e5ad27c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
145
146 98e7cc0f Leszek Koltunski
  private void setupPrevMoveButtom(final RubikActivity act, float scale, float width)
147 0e5ad27c Leszek Koltunski
    {
148
    int padding = (int)( 3*scale + 0.5f);
149 98e7cc0f Leszek Koltunski
    int widthBut= (int)(width/6);
150
151
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(widthBut,LinearLayout.LayoutParams.MATCH_PARENT);
152 0e5ad27c Leszek Koltunski
    mPrevButton = new ImageButton(act);
153
    mPrevButton.setLayoutParams(params);
154
    mPrevButton.setPadding(padding,0,padding,0);
155 15846fe4 Leszek Koltunski
    mPrevButton.setImageResource(R.drawable.cube_back);
156 0e5ad27c Leszek Koltunski
157
    mPrevButton.setOnClickListener( new View.OnClickListener()
158
      {
159
      @Override
160
      public void onClick(View v)
161
        {
162 5a4d4fba Leszek Koltunski
        RubikPreRender pre = act.getPreRender();
163
        backMove(pre);
164 0e5ad27c Leszek Koltunski
        }
165
      });
166
    }
167
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169
170 5a4d4fba Leszek Koltunski
  private void backMove(RubikPreRender pre)
171 0e5ad27c Leszek Koltunski
    {
172
    if( mCanPrevMove )
173
      {
174
      int numMoves = mMoves.size();
175
176
      if( numMoves>0 )
177
        {
178
        Move move = mMoves.remove(numMoves-1);
179 5a4d4fba Leszek Koltunski
        RubikObject object = pre.getObject();
180 0e5ad27c Leszek Koltunski
181
        int axis  = move.mAxis;
182
        int row   = (1<<move.mRow);
183
        int angle = move.mAngle;
184
        int numRot= Math.abs(angle*object.getBasicAngle()/360);
185
186
        if( angle!=0 )
187
          {
188
          mCanPrevMove = false;
189 5a4d4fba Leszek Koltunski
          pre.addRotation(this, axis, row, -angle, numRot*DURATION_MILLIS);
190 0e5ad27c Leszek Koltunski
          }
191
        else
192
          {
193
          android.util.Log.e("solution", "error: trying to back move of angle 0");
194
          }
195
        }
196
      }
197
    }
198
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200
201
  public void addMove(int axis, int row, int angle)
202
    {
203
    mMoves.add(new Move(axis,row,angle));
204
    }
205
206 ad9e8bb3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
207
208
  public void savePreferences(SharedPreferences.Editor editor)
209
    {
210 2e8ec627 Leszek Koltunski
    mElapsed = System.currentTimeMillis()-mStartTime;
211
    editor.putLong("stateSolving_elapsed" , mElapsed);
212 f0e87514 Leszek Koltunski
    mScores.savePreferences(editor);
213 ad9e8bb3 Leszek Koltunski
    }
214
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216
217
  public void restorePreferences(SharedPreferences preferences)
218
    {
219 2e8ec627 Leszek Koltunski
    mElapsed = preferences.getLong("stateSolving_elapsed" , 0 );
220 f0e87514 Leszek Koltunski
    mScores.restorePreferences(preferences);
221 ad9e8bb3 Leszek Koltunski
    }
222 0333d81e Leszek Koltunski
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224
225 2e8ec627 Leszek Koltunski
  private void startCounting(final RubikActivity act)
226 0333d81e Leszek Koltunski
    {
227
    if( !mRunning )
228
      {
229
      mRunning = true;
230 2e8ec627 Leszek Koltunski
      mStartTime = System.currentTimeMillis() - mElapsed;
231 0333d81e Leszek Koltunski
      mTimer = new Timer();
232
233
      mTimer.scheduleAtFixedRate(new TimerTask()
234
        {
235
        @Override
236
        public void run()
237
          {
238
          act.runOnUiThread(new Runnable()
239
            {
240
            @Override
241
            public void run()
242
              {
243
              int elapsed = (int)(System.currentTimeMillis()-mStartTime)/1000;
244
              mTime.setText(act.getString(R.string.tm_placeholder,elapsed/60,elapsed%60));
245
              }
246
            });
247
          }
248
        }, 0, 1000);
249
      }
250
    }
251
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253
254 1f894991 Leszek Koltunski
  private void stopCounting()
255
    {
256
    if( mTimer!=null )
257
      {
258
      mTimer.cancel();
259
      mTimer = null;
260
      }
261
262
    mRunning = false;
263
    }
264
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266
267
  public long getRecord()
268 0333d81e Leszek Koltunski
    {
269
    if( mRunning )
270
      {
271 1f894991 Leszek Koltunski
      stopCounting();
272 0333d81e Leszek Koltunski
273 2e8ec627 Leszek Koltunski
      mElapsed = System.currentTimeMillis()-mStartTime;
274 329c0aeb Leszek Koltunski
275
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
276 4888e97c Leszek Koltunski
      int object  = play.getObject();
277
      int size    = play.getSize();
278 85b09df4 Leszek Koltunski
      int level   = play.getLevel();
279 53f23b64 Leszek Koltunski
      int realSize= RubikObjectList.getSizeIndex(object,size);
280 329c0aeb Leszek Koltunski
281 2e8ec627 Leszek Koltunski
      boolean isNew = mScores.setRecord(object, realSize, level, mElapsed);
282 e41e7dc3 Leszek Koltunski
283 2e8ec627 Leszek Koltunski
      return isNew ? mElapsed : -mElapsed;
284 0333d81e Leszek Koltunski
      }
285
286
    return 0;
287
    }
288 0e5ad27c Leszek Koltunski
289 2e8ec627 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
290
291
  public void resetElapsed()
292
    {
293
    mElapsed = 0;
294
    }
295
296 0e5ad27c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
297
298
  public void onActionFinished(final long effectID)
299
    {
300
    mCanPrevMove = true;
301
    }
302 ad9e8bb3 Leszek Koltunski
  }