Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikStateSolving.java @ ad0c8e0e

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.TypedValue;
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.RubikPreRender;
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 RubikPreRender.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 ImageButton mPrevButton;
54
  private boolean mCanPrevMove;
55
  private ArrayList<Move> mMoves;
56
  private long mElapsed;
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();
82
    }
83

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

    
86
  void enterState(final RubikActivity act)
87
    {
88
    float width = act.getScreenWidthInPixels();
89
    float buttonSize = width*RubikActivity.BUTTON_TEXT_SIZE;
90
    float titleSize  = width*RubikActivity.TITLE_TEXT_SIZE;
91

    
92
    mCanPrevMove = true;
93

    
94
    startCounting(act);
95

    
96
    if( mMoves==null ) mMoves = new ArrayList<>();
97
    else               mMoves.clear();
98

    
99
    LayoutInflater inflater = act.getLayoutInflater();
100

    
101
    // TOP ////////////////////////////
102
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
103
    layoutTop.removeAllViews();
104
    mTime = (TextView)inflater.inflate(R.layout.upper_text, null);
105
    int elapsed = (int)mElapsed/1000;
106
    mTime.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
107
    mTime.setText(act.getString(R.string.tm_placeholder,elapsed/60,elapsed%60));
108
    layoutTop.addView(mTime);
109

    
110
    // BOT ////////////////////////////
111
    LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
112
    layoutLeft.removeAllViews();
113

    
114
    setupPrevMoveButtom(act,width);
115
    layoutLeft.addView(mPrevButton);
116

    
117
    LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
118
    layoutRight.removeAllViews();
119

    
120
    int padding = (int)(width*RubikActivity.PADDING);
121
    int margin  = (int)(width*RubikActivity.MARGIN);
122
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
123
    params.topMargin    = margin;
124
    params.bottomMargin = margin;
125
    params.leftMargin   = margin;
126
    params.rightMargin  = margin;
127

    
128
    Button back = new Button(act);
129
    back.setLayoutParams(params);
130
    back.setPadding(padding,0,padding,0);
131
    back.setTextSize(TypedValue.COMPLEX_UNIT_PX, buttonSize);
132
    back.setText(R.string.back);
133

    
134
    back.setOnClickListener( new View.OnClickListener()
135
      {
136
      @Override
137
      public void onClick(View v)
138
        {
139
        RubikState.goBack(act);
140
        }
141
      });
142

    
143
    layoutRight.addView(back);
144
    }
145

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

    
148
  private void setupPrevMoveButtom(final RubikActivity act, float width)
149
    {
150
    int padding = (int)(width*RubikActivity.PADDING);
151
    int margin  = (int)(width*RubikActivity.MARGIN);
152
    int widthBut= (int)(width/6);
153

    
154
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(widthBut,LinearLayout.LayoutParams.MATCH_PARENT);
155
    params.topMargin    = margin;
156
    params.bottomMargin = margin;
157
    params.leftMargin   = margin;
158
    params.rightMargin  = margin;
159

    
160
    mPrevButton = new ImageButton(act);
161
    mPrevButton.setLayoutParams(params);
162
    mPrevButton.setPadding(padding,0,padding,0);
163
    mPrevButton.setImageResource(R.drawable.cube_back);
164

    
165
    mPrevButton.setOnClickListener( new View.OnClickListener()
166
      {
167
      @Override
168
      public void onClick(View v)
169
        {
170
        RubikPreRender pre = act.getPreRender();
171
        backMove(pre);
172
        }
173
      });
174
    }
175

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177

    
178
  private void backMove(RubikPreRender pre)
179
    {
180
    if( mCanPrevMove )
181
      {
182
      int numMoves = mMoves.size();
183

    
184
      if( numMoves>0 )
185
        {
186
        Move move = mMoves.remove(numMoves-1);
187
        RubikObject object = pre.getObject();
188

    
189
        int axis  = move.mAxis;
190
        int row   = (1<<move.mRow);
191
        int angle = move.mAngle;
192
        int numRot= Math.abs(angle*object.getBasicAngle()/360);
193

    
194
        if( angle!=0 )
195
          {
196
          mCanPrevMove = false;
197
          pre.addRotation(this, axis, row, -angle, numRot*DURATION_MILLIS);
198
          }
199
        else
200
          {
201
          android.util.Log.e("solution", "error: trying to back move of angle 0");
202
          }
203
        }
204
      }
205
    }
206

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

    
209
  public void addMove(int axis, int row, int angle)
210
    {
211
    mMoves.add(new Move(axis,row,angle));
212
    }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

    
216
  public void savePreferences(SharedPreferences.Editor editor)
217
    {
218
    mElapsed = System.currentTimeMillis()-mStartTime;
219
    editor.putLong("stateSolving_elapsed" , mElapsed);
220
    mScores.savePreferences(editor);
221
    }
222

    
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224

    
225
  public void restorePreferences(SharedPreferences preferences)
226
    {
227
    mElapsed = preferences.getLong("stateSolving_elapsed" , 0 );
228
    mScores.restorePreferences(preferences);
229
    }
230

    
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232

    
233
  private void startCounting(final RubikActivity act)
234
    {
235
    if( !mRunning )
236
      {
237
      mRunning = true;
238
      mStartTime = System.currentTimeMillis() - mElapsed;
239
      mTimer = new Timer();
240

    
241
      mTimer.scheduleAtFixedRate(new TimerTask()
242
        {
243
        @Override
244
        public void run()
245
          {
246
          act.runOnUiThread(new Runnable()
247
            {
248
            @Override
249
            public void run()
250
              {
251
              int elapsed = (int)(System.currentTimeMillis()-mStartTime)/1000;
252
              mTime.setText(act.getString(R.string.tm_placeholder,elapsed/60,elapsed%60));
253
              }
254
            });
255
          }
256
        }, 0, 1000);
257
      }
258
    }
259

    
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261

    
262
  private void stopCounting()
263
    {
264
    if( mTimer!=null )
265
      {
266
      mTimer.cancel();
267
      mTimer = null;
268
      }
269

    
270
    mRunning = false;
271
    }
272

    
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274

    
275
  public long getRecord()
276
    {
277
    if( mRunning )
278
      {
279
      stopCounting();
280

    
281
      mElapsed = System.currentTimeMillis()-mStartTime;
282

    
283
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
284
      int object  = play.getObject();
285
      int size    = play.getSize();
286
      int level   = play.getLevel();
287
      int realSize= RubikObjectList.getSizeIndex(object,size);
288

    
289
      boolean isNew = mScores.setRecord(object, realSize, level, mElapsed);
290

    
291
      return isNew ? mElapsed : -mElapsed;
292
      }
293

    
294
    return 0;
295
    }
296

    
297
///////////////////////////////////////////////////////////////////////////////////////////////////
298

    
299
  public void resetElapsed()
300
    {
301
    mElapsed = 0;
302
    }
303

    
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305

    
306
  public void onActionFinished(final long effectID)
307
    {
308
    mCanPrevMove = true;
309
    }
310
  }
(9-9/9)