Project

General

Profile

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

magiccube / src / main / java / org / distorted / helpers / MovesAndLockController.java @ 9f006481

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2021 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.helpers;
21

    
22
import android.view.View;
23
import android.widget.ImageButton;
24
import android.widget.LinearLayout;
25

    
26
import org.distorted.main.R;
27
import org.distorted.main.RubikActivity;
28

    
29
import java.util.ArrayList;
30
import java.util.Timer;
31
import java.util.TimerTask;
32

    
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34

    
35
public class MovesAndLockController implements MovesFinished
36
  {
37
  private static final int MILLIS_PER_DEGREE = 6;
38
  private static final int LOCK_TIME = 300;
39

    
40
  private static class Move
41
    {
42
    private final int mAxis, mRow, mAngle;
43

    
44
    Move(int axis, int row, int angle)
45
      {
46
      mAxis = axis;
47
      mRow  = row;
48
      mAngle= angle;
49
      }
50
    }
51

    
52
  private ArrayList<Move> mMoves;
53
  private boolean mCanPrevMove;
54
  private TwistyPreRender mPre;
55
  private ImageButton mPrevButton, mLockButton;
56
  private long mLockTime;
57
  private Timer mTimer;
58
  private boolean mTimerRunning;
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61
// PUBLIC API
62

    
63
  public MovesAndLockController()
64
    {
65
    mTimerRunning = false;
66
    mLockTime = 0;
67
    mCanPrevMove = true;
68

    
69
    if( mMoves==null ) mMoves = new ArrayList<>();
70
    else               mMoves.clear();
71
    }
72

    
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74

    
75
  public void toggleLock(TwistyActivity act)
76
    {
77
    act.toggleLock();
78
    mLockButton.setImageResource(getLockIcon(act,false));
79
    }
80

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82

    
83
  public int getLockIcon(TwistyActivity act, boolean red)
84
    {
85
    if( act.retLocked() )
86
      {
87
      if( red )
88
        {
89
        return RubikActivity.getDrawable(R.drawable.ui_small_locked_red,
90
                                         R.drawable.ui_medium_locked_red,
91
                                         R.drawable.ui_big_locked_red,
92
                                         R.drawable.ui_huge_locked_red);
93
        }
94
      else
95
        {
96
        return RubikActivity.getDrawable(R.drawable.ui_small_locked,
97
                                         R.drawable.ui_medium_locked,
98
                                         R.drawable.ui_big_locked,
99
                                         R.drawable.ui_huge_locked);
100
        }
101
      }
102
    else
103
      {
104
      return RubikActivity.getDrawable(R.drawable.ui_small_unlocked,
105
                                       R.drawable.ui_medium_unlocked,
106
                                       R.drawable.ui_big_unlocked,
107
                                       R.drawable.ui_huge_unlocked);
108
      }
109
    }
110

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112

    
113
  public int getPrevIcon(boolean on)
114
    {
115
    if( on )
116
      {
117
      return RubikActivity.getDrawable(R.drawable.ui_small_cube_back,
118
                                       R.drawable.ui_medium_cube_back,
119
                                       R.drawable.ui_big_cube_back,
120
                                       R.drawable.ui_huge_cube_back);
121
      }
122
    else
123
      {
124
      return RubikActivity.getDrawable(R.drawable.ui_small_cube_grey,
125
                                       R.drawable.ui_medium_cube_grey,
126
                                       R.drawable.ui_big_cube_grey,
127
                                       R.drawable.ui_huge_cube_grey);
128
      }
129
    }
130

    
131
//////////////////////////////////////////////////////////////////////////////////////////////////
132

    
133
  public void backMove(TwistyActivity act)
134
    {
135
    if( mCanPrevMove )
136
      {
137
      int numMoves = mMoves.size();
138

    
139
      if( numMoves>0 )
140
        {
141
        Move move   = mMoves.remove(numMoves-1);
142
        int axis    = move.mAxis;
143
        int row     = (1<<move.mRow);
144
        int angle   = move.mAngle;
145
        int duration= Math.abs(angle)*MILLIS_PER_DEGREE;
146

    
147
        if( angle!=0 )
148
          {
149
          mCanPrevMove = false;
150
          mPre = act.getTwistyPreRender();
151
          mPre.blockTouch();
152
          mPre.addRotation(this, axis, row, -angle, duration);
153
          }
154
        else
155
          {
156
          android.util.Log.e("solution", "error: trying to back move of angle 0");
157
          }
158

    
159
        if( numMoves==1 ) changeBackMove(act, false);
160
        }
161
      }
162
    }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
  private void changeBackMove(TwistyActivity act, final boolean on)
167
    {
168
    act.runOnUiThread(new Runnable()
169
      {
170
      @Override
171
      public void run()
172
        {
173
        if( mPrevButton!=null )
174
          mPrevButton.setImageResource(getPrevIcon(on));
175
        }
176
      });
177
    }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

    
181
  private void changeLockIcon(TwistyActivity act, final boolean red)
182
    {
183
    act.runOnUiThread(new Runnable()
184
      {
185
      @Override
186
      public void run()
187
        {
188
        if( mLockButton!=null )
189
          mLockButton.setImageResource(getLockIcon(act,red));
190
        }
191
      });
192
    }
193

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

    
196
  public void reddenLock(final TwistyActivity act)
197
    {
198
    mLockTime = System.currentTimeMillis();
199

    
200
    if( !mTimerRunning )
201
      {
202
      changeLockIcon(act,true);
203

    
204
      mTimerRunning = true;
205
      mTimer = new Timer();
206

    
207
      mTimer.scheduleAtFixedRate(new TimerTask()
208
        {
209
        @Override
210
        public void run()
211
          {
212
          act.runOnUiThread(new Runnable()
213
            {
214
            @Override
215
            public void run()
216
              {
217
              if( System.currentTimeMillis()-mLockTime > LOCK_TIME )
218
                {
219
                changeLockIcon(act,false);
220
                mTimer.cancel();
221
                mTimer = null;
222
                mTimerRunning = false;
223
                }
224
              }
225
            });
226
          }
227
        }, 0, LOCK_TIME);
228
      }
229
    }
230

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

    
233
  public void addMove(TwistyActivity act, int axis, int row, int angle)
234
    {
235
    if( mMoves.isEmpty() ) changeBackMove(act,true);
236
    mMoves.add(new Move(axis,row,angle));
237
    }
238

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

    
241
  public void onActionFinished(final long effectID)
242
    {
243
    mCanPrevMove = true;
244
    mPre.unblockTouch();
245
    }
246

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248

    
249
  public void clearMoves(final TwistyActivity act)
250
    {
251
    mMoves.clear();
252
    changeBackMove(act,false);
253
    }
254

    
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256

    
257
  public int getNumMoves()
258
    {
259
    return mMoves.size();
260
    }
261

    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263

    
264
  public void setupPrevButton(final TwistyActivity act, final float width)
265
    {
266
    final int icon = getPrevIcon( !mMoves.isEmpty() );
267
    mPrevButton = new TransparentImageButton(act, icon, width, LinearLayout.LayoutParams.MATCH_PARENT);
268

    
269
    mPrevButton.setOnClickListener( new View.OnClickListener()
270
      {
271
      @Override
272
      public void onClick(View v)
273
        {
274
        backMove(act);
275
        }
276
      });
277
    }
278

    
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280

    
281
  public void setupLockButton(final TwistyActivity act, final float width)
282
    {
283
    final int icon = getLockIcon(act,false);
284
    mLockButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
285

    
286
    mLockButton.setOnClickListener( new View.OnClickListener()
287
      {
288
      @Override
289
      public void onClick(View v)
290
        {
291
        toggleLock(act);
292
        }
293
      });
294
    }
295

    
296
///////////////////////////////////////////////////////////////////////////////////////////////////
297

    
298
  public void setLockState(final TwistyActivity act)
299
    {
300
    act.runOnUiThread(new Runnable()
301
      {
302
      @Override
303
      public void run()
304
        {
305
        if( mLockButton!=null )
306
          mLockButton.setImageResource(getLockIcon(act,false));
307
        }
308
      });
309
    }
310

    
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312

    
313
  public ImageButton getPrevButton()
314
    {
315
    return mPrevButton;
316
    }
317

    
318
///////////////////////////////////////////////////////////////////////////////////////////////////
319

    
320
  public ImageButton getLockButton()
321
    {
322
    return mLockButton;
323
    }
324
  }
(5-5/10)