Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikStateSolving.java @ 0e5ad27c

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.states;
21

    
22
import android.content.SharedPreferences;
23
import android.util.DisplayMetrics;
24
import android.view.LayoutInflater;
25
import android.view.View;
26
import android.widget.Button;
27
import android.widget.ImageButton;
28
import android.widget.LinearLayout;
29
import android.widget.TextView;
30

    
31
import org.distorted.main.R;
32
import org.distorted.main.RubikActivity;
33
import org.distorted.main.RubikPostRender;
34
import org.distorted.objects.RubikObject;
35
import org.distorted.objects.RubikObjectList;
36
import org.distorted.scores.RubikScores;
37

    
38
import java.util.ArrayList;
39
import java.util.Timer;
40
import java.util.TimerTask;
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

    
44
public class RubikStateSolving extends RubikStateAbstract implements RubikPostRender.ActionFinishedListener
45
  {
46
  private static final int DURATION_MILLIS = 750;
47

    
48
  private TextView mTime;
49
  private Timer mTimer;
50
  private long mStartTime;
51
  private boolean mRunning;
52
  private RubikScores mScores;
53
  private Button mBack;
54
  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

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

    
72
  RubikStateSolving()
73
    {
74
    mScores = RubikScores.getInstance();
75
    }
76

    
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78

    
79
  void leaveState(RubikActivity act)
80
    {
81
    stopCounting(act);
82
    }
83

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

    
86
  void enterState(final RubikActivity act)
87
    {
88
    mCanPrevMove = true;
89

    
90
    if( mMoves==null ) mMoves = new ArrayList<>();
91
    else               mMoves.clear();
92

    
93
    LayoutInflater inflater = act.getLayoutInflater();
94
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
95
    float scale = metrics.density;
96

    
97
    // TOP ////////////////////////////
98
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
99
    layoutTop.removeAllViews();
100
    mTime = (TextView)inflater.inflate(R.layout.upper_text, null);
101
    mTime.setText(R.string.ready);
102
    layoutTop.addView(mTime);
103

    
104
    // BOT ////////////////////////////
105
    LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
106
    layoutLeft.removeAllViews();
107

    
108
    if( mPrevButton==null ) setupPrevMoveButtom(act,scale);
109
    layoutLeft.addView(mPrevButton);
110

    
111
    LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
112
    layoutRight.removeAllViews();
113

    
114
    int padding = (int)(5*scale + 0.5f);
115
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
116

    
117
    mBack = new Button(act);
118
    mBack.setLayoutParams(params);
119
    mBack.setPadding(padding,0,padding,0);
120
    mBack.setText(R.string.back);
121

    
122
    mBack.setOnClickListener( new View.OnClickListener()
123
      {
124
      @Override
125
      public void onClick(View v)
126
        {
127
        RubikState.goBack(act);
128
        }
129
      });
130

    
131
    layoutRight.addView(mBack);
132
    }
133

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
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
    mPrevButton.setEnabled(false);
146

    
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
            mPrevButton.setEnabled(false);
184
            }
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
      mPrevButton.setEnabled(true);
207
      }
208
    }
209

    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

    
212
  public void savePreferences(SharedPreferences.Editor editor)
213
    {
214
    mScores.savePreferences(editor);
215
    }
216

    
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218

    
219
  public void restorePreferences(SharedPreferences preferences)
220
    {
221
    mScores.restorePreferences(preferences);
222
    }
223

    
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225

    
226
  public void startCounting(final RubikActivity act)
227
    {
228
    if( !mRunning )
229
      {
230
      mRunning = true;
231
      mStartTime = System.currentTimeMillis();
232
      mTimer = new Timer();
233

    
234
      mTimer.scheduleAtFixedRate(new TimerTask()
235
        {
236
        @Override
237
        public void run()
238
          {
239
          act.runOnUiThread(new Runnable()
240
            {
241
            @Override
242
            public void run()
243
              {
244
              int elapsed = (int)(System.currentTimeMillis()-mStartTime)/1000;
245
              mTime.setText(act.getString(R.string.tm_placeholder,elapsed/60,elapsed%60));
246
              }
247
            });
248
          }
249
        }, 0, 1000);
250
      }
251
    }
252

    
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254

    
255
  public long stopCounting(RubikActivity act)
256
    {
257
    if( mRunning )
258
      {
259
      act.runOnUiThread(new Runnable()
260
        {
261
        @Override
262
        public void run()
263
          {
264
          mTime.setText(R.string.solved);
265
          mBack.setClickable(false);
266
          }
267
        });
268

    
269
      if( mTimer!=null )
270
        {
271
        mTimer.cancel();
272
        mTimer = null;
273
        }
274
      mRunning = false;
275

    
276
      long timeTaken = System.currentTimeMillis()-mStartTime;
277

    
278
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
279
      int object  = play.getObject();
280
      int size    = play.getSize();
281
      int level   = play.getLevel();
282
      int realSize= RubikObjectList.getSizeIndex(object,size);
283

    
284
      boolean isNew = mScores.setRecord(object, realSize, level, timeTaken);
285

    
286
      return isNew ? timeTaken : -timeTaken;
287
      }
288

    
289
    return 0;
290
    }
291

    
292
///////////////////////////////////////////////////////////////////////////////////////////////////
293

    
294
  public void onActionFinished(final long effectID)
295
    {
296
    mCanPrevMove = true;
297
    }
298
  }
(8-8/8)