Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikStateSolving.java @ 4d23405c

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