Project

General

Profile

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

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

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 layoutBot = act.findViewById(R.id.lowerBar);
112
    layoutBot.removeAllViews();
113

    
114
    LinearLayout.LayoutParams paramsL = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1);
115

    
116
    LinearLayout layoutLeft = new LinearLayout(act);
117
    layoutLeft.setLayoutParams(paramsL);
118
    LinearLayout layoutMid = new LinearLayout(act);
119
    layoutMid.setLayoutParams(paramsL);
120
    LinearLayout layoutRight = new LinearLayout(act);
121
    layoutRight.setLayoutParams(paramsL);
122

    
123
    setupPrevMoveButtom(act,width);
124
    layoutLeft.addView(mPrevButton);
125

    
126
    int padding = (int)(width*RubikActivity.PADDING);
127
    int margin  = (int)(width*RubikActivity.MARGIN);
128
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
129
    params.topMargin    = margin;
130
    params.bottomMargin = margin;
131
    params.leftMargin   = margin;
132
    params.rightMargin  = margin;
133

    
134
    final int icon = RubikActivity.getDrawable(R.drawable.ui_small_back,R.drawable.ui_medium_back, R.drawable.ui_big_back, R.drawable.ui_huge_back);
135

    
136
    ImageButton back = new ImageButton(act);
137
    back.setLayoutParams(params);
138
    back.setPadding(padding,0,padding,0);
139
    back.setImageResource(icon);
140

    
141
    back.setOnClickListener( new View.OnClickListener()
142
      {
143
      @Override
144
      public void onClick(View v)
145
        {
146
        RubikState.goBack(act);
147
        }
148
      });
149

    
150
    layoutRight.addView(back);
151

    
152
    layoutBot.addView(layoutLeft);
153
    layoutBot.addView(layoutMid);
154
    layoutBot.addView(layoutRight);
155
    }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

    
159
  private void setupPrevMoveButtom(final RubikActivity act, float width)
160
    {
161
    int padding = (int)(width*RubikActivity.PADDING);
162
    int margin  = (int)(width*RubikActivity.MARGIN);
163
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube_back,R.drawable.ui_medium_cube_back, R.drawable.ui_big_cube_back, R.drawable.ui_huge_cube_back);
164

    
165
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
166
    params.topMargin    = margin;
167
    params.bottomMargin = margin;
168
    params.leftMargin   = margin;
169
    params.rightMargin  = margin;
170

    
171
    mPrevButton = new ImageButton(act);
172
    mPrevButton.setLayoutParams(params);
173
    mPrevButton.setPadding(padding,0,padding,0);
174
    mPrevButton.setImageResource(icon);
175

    
176
    mPrevButton.setOnClickListener( new View.OnClickListener()
177
      {
178
      @Override
179
      public void onClick(View v)
180
        {
181
        RubikPreRender pre = act.getPreRender();
182
        backMove(pre);
183
        }
184
      });
185
    }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

    
189
  private void backMove(RubikPreRender pre)
190
    {
191
    if( mCanPrevMove )
192
      {
193
      int numMoves = mMoves.size();
194

    
195
      if( numMoves>0 )
196
        {
197
        Move move = mMoves.remove(numMoves-1);
198
        RubikObject object = pre.getObject();
199

    
200
        int axis  = move.mAxis;
201
        int row   = (1<<move.mRow);
202
        int angle = move.mAngle;
203
        int numRot= Math.abs(angle*object.getBasicAngle()/360);
204

    
205
        if( angle!=0 )
206
          {
207
          mCanPrevMove = false;
208
          pre.addRotation(this, axis, row, -angle, numRot*DURATION_MILLIS);
209
          }
210
        else
211
          {
212
          android.util.Log.e("solution", "error: trying to back move of angle 0");
213
          }
214
        }
215
      }
216
    }
217

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

    
220
  public void addMove(int axis, int row, int angle)
221
    {
222
    mMoves.add(new Move(axis,row,angle));
223
    }
224

    
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226

    
227
  public void savePreferences(SharedPreferences.Editor editor)
228
    {
229
    mElapsed = System.currentTimeMillis()-mStartTime;
230
    editor.putLong("stateSolving_elapsed" , mElapsed);
231
    mScores.savePreferences(editor);
232
    }
233

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235

    
236
  public void restorePreferences(SharedPreferences preferences)
237
    {
238
    mElapsed = preferences.getLong("stateSolving_elapsed" , 0 );
239
    mScores.restorePreferences(preferences);
240
    }
241

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

    
244
  private void startCounting(final RubikActivity act)
245
    {
246
    if( !mRunning )
247
      {
248
      mRunning = true;
249
      mStartTime = System.currentTimeMillis() - mElapsed;
250
      mTimer = new Timer();
251

    
252
      mTimer.scheduleAtFixedRate(new TimerTask()
253
        {
254
        @Override
255
        public void run()
256
          {
257
          act.runOnUiThread(new Runnable()
258
            {
259
            @Override
260
            public void run()
261
              {
262
              int elapsed = (int)(System.currentTimeMillis()-mStartTime)/1000;
263
              mTime.setText(act.getString(R.string.tm_placeholder,elapsed/60,elapsed%60));
264
              }
265
            });
266
          }
267
        }, 0, 1000);
268
      }
269
    }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

    
273
  private void stopCounting()
274
    {
275
    if( mTimer!=null )
276
      {
277
      mTimer.cancel();
278
      mTimer = null;
279
      }
280

    
281
    mRunning = false;
282
    }
283

    
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285

    
286
  public long getRecord()
287
    {
288
    if( mRunning )
289
      {
290
      stopCounting();
291

    
292
      mElapsed = System.currentTimeMillis()-mStartTime;
293

    
294
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
295
      int object  = play.getObject();
296
      int size    = play.getSize();
297
      int level   = play.getLevel();
298
      int realSize= RubikObjectList.getSizeIndex(object,size);
299

    
300
      boolean isNew = mScores.setRecord(object, realSize, level, mElapsed);
301

    
302
      return isNew ? mElapsed : -mElapsed;
303
      }
304

    
305
    return 0;
306
    }
307

    
308
///////////////////////////////////////////////////////////////////////////////////////////////////
309

    
310
  public void resetElapsed()
311
    {
312
    mElapsed = 0;
313
    }
314

    
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316

    
317
  public void onActionFinished(final long effectID)
318
    {
319
    mCanPrevMove = true;
320
    }
321
  }
(9-9/9)