Project

General

Profile

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

magiccube / src / main / java / org / distorted / helpers / MovesAndLockController.java @ 3f7a4363

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 java.util.ArrayList;
23
import java.util.Timer;
24
import java.util.TimerTask;
25

    
26
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

    
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 final 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
    mMoves       = new ArrayList<>();
69
    }
70

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72

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

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80

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

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

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

    
129
//////////////////////////////////////////////////////////////////////////////////////////////////
130

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

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

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

    
157
        if( numMoves==1 ) changeBackMove(act, false);
158
        }
159
      }
160
    }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163

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

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

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

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193

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

    
198
    if( !mTimerRunning )
199
      {
200
      changeLockIcon(act,true);
201

    
202
      mTimerRunning = true;
203
      mTimer = new Timer();
204

    
205
      mTimer.scheduleAtFixedRate(new TimerTask()
206
        {
207
        @Override
208
        public void run()
209
          {
210
          act.runOnUiThread(new Runnable()
211
            {
212
            @Override
213
            public void run()
214
              {
215
              if( System.currentTimeMillis()-mLockTime > LOCK_TIME )
216
                {
217
                changeLockIcon(act,false);
218

    
219
                if( mTimer!=null )
220
                  {
221
                  mTimer.cancel();
222
                  mTimer = null;
223
                  }
224

    
225
                mTimerRunning = false;
226
                }
227
              }
228
            });
229
          }
230
        }, 0, LOCK_TIME);
231
      }
232
    }
233

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

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

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

    
244
  public void onActionFinished(final long effectID)
245
    {
246
    mCanPrevMove = true;
247
    mPre.unblockTouch();
248
    }
249

    
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251

    
252
  public void clearMoves(final TwistyActivity act)
253
    {
254
    mMoves.clear();
255
    changeBackMove(act,false);
256
    }
257

    
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259

    
260
  public int getNumMoves()
261
    {
262
    return mMoves.size();
263
    }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266

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

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

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283

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

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

    
299
///////////////////////////////////////////////////////////////////////////////////////////////////
300

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

    
314
///////////////////////////////////////////////////////////////////////////////////////////////////
315

    
316
  public ImageButton getPrevButton()
317
    {
318
    return mPrevButton;
319
    }
320

    
321
///////////////////////////////////////////////////////////////////////////////////////////////////
322

    
323
  public ImageButton getLockButton()
324
    {
325
    return mLockButton;
326
    }
327
  }
(2-2/7)