Project

General

Profile

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

magiccube / src / main / java / org / distorted / helpers / MovesAndLockController.java @ 88a3e972

1 55e6be1d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 3f7a4363 Leszek Koltunski
import java.util.ArrayList;
23
import java.util.Timer;
24
import java.util.TimerTask;
25
26 55e6be1d Leszek Koltunski
import android.view.View;
27
import android.widget.ImageButton;
28
import android.widget.LinearLayout;
29
30
import org.distorted.main.R;
31
import org.distorted.main.RubikActivity;
32 88a3e972 Leszek Koltunski
import org.distorted.objectlib.helpers.BlockController;
33
import org.distorted.objectlib.helpers.MovesFinished;
34
import org.distorted.objectlib.helpers.TwistyActivity;
35
import org.distorted.objectlib.helpers.TwistyPreRender;
36 55e6be1d Leszek Koltunski
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38
39
public class MovesAndLockController implements MovesFinished
40
  {
41
  private static final int MILLIS_PER_DEGREE = 6;
42 cc88f2fa Leszek Koltunski
  private static final int LOCK_TIME = 300;
43 55e6be1d Leszek Koltunski
44
  private static class Move
45
    {
46
    private final int mAxis, mRow, mAngle;
47
48
    Move(int axis, int row, int angle)
49
      {
50
      mAxis = axis;
51
      mRow  = row;
52
      mAngle= angle;
53
      }
54
    }
55
56 9d591756 Leszek Koltunski
  private final ArrayList<Move> mMoves;
57 55e6be1d Leszek Koltunski
  private boolean mCanPrevMove;
58
  private TwistyPreRender mPre;
59
  private ImageButton mPrevButton, mLockButton;
60 cc88f2fa Leszek Koltunski
  private long mLockTime;
61
  private Timer mTimer;
62
  private boolean mTimerRunning;
63 55e6be1d Leszek Koltunski
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65
// PUBLIC API
66
67
  public MovesAndLockController()
68
    {
69 9d591756 Leszek Koltunski
    mTimerRunning= false;
70
    mLockTime    = 0;
71 55e6be1d Leszek Koltunski
    mCanPrevMove = true;
72 9d591756 Leszek Koltunski
    mMoves       = new ArrayList<>();
73 55e6be1d Leszek Koltunski
    }
74
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76
77
  public void toggleLock(TwistyActivity act)
78
    {
79
    act.toggleLock();
80 cc88f2fa Leszek Koltunski
    mLockButton.setImageResource(getLockIcon(act,false));
81 55e6be1d Leszek Koltunski
    }
82
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84
85 cc88f2fa Leszek Koltunski
  public int getLockIcon(TwistyActivity act, boolean red)
86 55e6be1d Leszek Koltunski
    {
87
    if( act.retLocked() )
88
      {
89 cc88f2fa Leszek Koltunski
      if( red )
90
        {
91
        return RubikActivity.getDrawable(R.drawable.ui_small_locked_red,
92
                                         R.drawable.ui_medium_locked_red,
93
                                         R.drawable.ui_big_locked_red,
94
                                         R.drawable.ui_huge_locked_red);
95
        }
96
      else
97
        {
98
        return RubikActivity.getDrawable(R.drawable.ui_small_locked,
99
                                         R.drawable.ui_medium_locked,
100
                                         R.drawable.ui_big_locked,
101
                                         R.drawable.ui_huge_locked);
102
        }
103 55e6be1d Leszek Koltunski
      }
104
    else
105
      {
106 146661ec Leszek Koltunski
      return RubikActivity.getDrawable(R.drawable.ui_small_unlocked,
107
                                       R.drawable.ui_medium_unlocked,
108
                                       R.drawable.ui_big_unlocked,
109
                                       R.drawable.ui_huge_unlocked);
110
      }
111
    }
112
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114
115
  public int getPrevIcon(boolean on)
116
    {
117
    if( on )
118
      {
119
      return RubikActivity.getDrawable(R.drawable.ui_small_cube_back,
120
                                       R.drawable.ui_medium_cube_back,
121
                                       R.drawable.ui_big_cube_back,
122
                                       R.drawable.ui_huge_cube_back);
123
      }
124
    else
125
      {
126
      return RubikActivity.getDrawable(R.drawable.ui_small_cube_grey,
127
                                       R.drawable.ui_medium_cube_grey,
128
                                       R.drawable.ui_big_cube_grey,
129
                                       R.drawable.ui_huge_cube_grey);
130 55e6be1d Leszek Koltunski
      }
131
    }
132
133
//////////////////////////////////////////////////////////////////////////////////////////////////
134
135 146661ec Leszek Koltunski
  public void backMove(TwistyActivity act)
136 55e6be1d Leszek Koltunski
    {
137
    if( mCanPrevMove )
138
      {
139
      int numMoves = mMoves.size();
140
141
      if( numMoves>0 )
142
        {
143
        Move move   = mMoves.remove(numMoves-1);
144
        int axis    = move.mAxis;
145
        int row     = (1<<move.mRow);
146
        int angle   = move.mAngle;
147
        int duration= Math.abs(angle)*MILLIS_PER_DEGREE;
148
149
        if( angle!=0 )
150
          {
151
          mCanPrevMove = false;
152 146661ec Leszek Koltunski
          mPre = act.getTwistyPreRender();
153 809c3432 Leszek Koltunski
          mPre.blockTouch(BlockController.MOVES_PLACE_0);
154 146661ec Leszek Koltunski
          mPre.addRotation(this, axis, row, -angle, duration);
155 55e6be1d Leszek Koltunski
          }
156
        else
157
          {
158
          android.util.Log.e("solution", "error: trying to back move of angle 0");
159
          }
160 146661ec Leszek Koltunski
161
        if( numMoves==1 ) changeBackMove(act, false);
162 55e6be1d Leszek Koltunski
        }
163
      }
164
    }
165
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167
168 146661ec Leszek Koltunski
  private void changeBackMove(TwistyActivity act, final boolean on)
169
    {
170
    act.runOnUiThread(new Runnable()
171
      {
172
      @Override
173
      public void run()
174
        {
175
        if( mPrevButton!=null )
176
          mPrevButton.setImageResource(getPrevIcon(on));
177
        }
178
      });
179
    }
180
181 cc88f2fa Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
182
183
  private void changeLockIcon(TwistyActivity act, final boolean red)
184
    {
185
    act.runOnUiThread(new Runnable()
186
      {
187
      @Override
188
      public void run()
189
        {
190
        if( mLockButton!=null )
191
          mLockButton.setImageResource(getLockIcon(act,red));
192
        }
193
      });
194
    }
195
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197
198
  public void reddenLock(final TwistyActivity act)
199
    {
200
    mLockTime = System.currentTimeMillis();
201
202
    if( !mTimerRunning )
203
      {
204
      changeLockIcon(act,true);
205
206
      mTimerRunning = true;
207
      mTimer = new Timer();
208
209
      mTimer.scheduleAtFixedRate(new TimerTask()
210
        {
211
        @Override
212
        public void run()
213
          {
214
          act.runOnUiThread(new Runnable()
215
            {
216
            @Override
217
            public void run()
218
              {
219
              if( System.currentTimeMillis()-mLockTime > LOCK_TIME )
220
                {
221
                changeLockIcon(act,false);
222 a84c3a25 Leszek Koltunski
223
                if( mTimer!=null )
224
                  {
225
                  mTimer.cancel();
226
                  mTimer = null;
227
                  }
228
229 cc88f2fa Leszek Koltunski
                mTimerRunning = false;
230
                }
231
              }
232
            });
233
          }
234
        }, 0, LOCK_TIME);
235
      }
236
    }
237
238 146661ec Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
239
240
  public void addMove(TwistyActivity act, int axis, int row, int angle)
241 55e6be1d Leszek Koltunski
    {
242 9f006481 Leszek Koltunski
    if( mMoves.isEmpty() ) changeBackMove(act,true);
243 55e6be1d Leszek Koltunski
    mMoves.add(new Move(axis,row,angle));
244
    }
245
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247
248
  public void onActionFinished(final long effectID)
249
    {
250
    mCanPrevMove = true;
251
    mPre.unblockTouch();
252
    }
253
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255
256 1ae92052 Leszek Koltunski
  public void clearMoves(final TwistyActivity act)
257 55e6be1d Leszek Koltunski
    {
258
    mMoves.clear();
259 1ae92052 Leszek Koltunski
    changeBackMove(act,false);
260 55e6be1d Leszek Koltunski
    }
261
262 9f006481 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
263
264
  public int getNumMoves()
265
    {
266
    return mMoves.size();
267
    }
268
269 55e6be1d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
270
271
  public void setupPrevButton(final TwistyActivity act, final float width)
272
    {
273 146661ec Leszek Koltunski
    final int icon = getPrevIcon( !mMoves.isEmpty() );
274 55e6be1d Leszek Koltunski
    mPrevButton = new TransparentImageButton(act, icon, width, LinearLayout.LayoutParams.MATCH_PARENT);
275
276
    mPrevButton.setOnClickListener( new View.OnClickListener()
277
      {
278
      @Override
279
      public void onClick(View v)
280
        {
281 146661ec Leszek Koltunski
        backMove(act);
282 55e6be1d Leszek Koltunski
        }
283
      });
284
    }
285
286
///////////////////////////////////////////////////////////////////////////////////////////////////
287
288
  public void setupLockButton(final TwistyActivity act, final float width)
289
    {
290 cc88f2fa Leszek Koltunski
    final int icon = getLockIcon(act,false);
291 55e6be1d Leszek Koltunski
    mLockButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
292
293
    mLockButton.setOnClickListener( new View.OnClickListener()
294
      {
295
      @Override
296
      public void onClick(View v)
297
        {
298
        toggleLock(act);
299
        }
300
      });
301
    }
302
303
///////////////////////////////////////////////////////////////////////////////////////////////////
304
305
  public void setLockState(final TwistyActivity act)
306
    {
307
    act.runOnUiThread(new Runnable()
308
      {
309
      @Override
310
      public void run()
311
        {
312
        if( mLockButton!=null )
313 cc88f2fa Leszek Koltunski
          mLockButton.setImageResource(getLockIcon(act,false));
314 55e6be1d Leszek Koltunski
        }
315
      });
316
    }
317
318
///////////////////////////////////////////////////////////////////////////////////////////////////
319
320
  public ImageButton getPrevButton()
321
    {
322
    return mPrevButton;
323
    }
324
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326
327
  public ImageButton getLockButton()
328
    {
329
    return mLockButton;
330
    }
331
  }