Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikStateSolving.java @ 8fcd8cba

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