Project

General

Profile

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

magiccube / src / main / java / org / distorted / helpers / MovesController.java @ 2afc6754

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
24 55e6be1d Leszek Koltunski
import android.view.View;
25
import android.widget.ImageButton;
26
import android.widget.LinearLayout;
27
28 88a3e972 Leszek Koltunski
import org.distorted.objectlib.helpers.BlockController;
29
import org.distorted.objectlib.helpers.MovesFinished;
30
import org.distorted.objectlib.helpers.TwistyActivity;
31 2afc6754 Leszek Koltunski
import org.distorted.objectlib.main.ObjectControl;
32 55e6be1d Leszek Koltunski
33 dd1a65c1 Leszek Koltunski
import org.distorted.main.R;
34
import org.distorted.main.RubikActivity;
35
36 55e6be1d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
37
38 dd1a65c1 Leszek Koltunski
public class MovesController implements MovesFinished
39 55e6be1d Leszek Koltunski
  {
40
  private static final int MILLIS_PER_DEGREE = 6;
41
42
  private static class Move
43
    {
44
    private final int mAxis, mRow, mAngle;
45
46
    Move(int axis, int row, int angle)
47
      {
48
      mAxis = axis;
49
      mRow  = row;
50
      mAngle= angle;
51
      }
52
    }
53
54 9d591756 Leszek Koltunski
  private final ArrayList<Move> mMoves;
55 55e6be1d Leszek Koltunski
  private boolean mCanPrevMove;
56 2afc6754 Leszek Koltunski
  private ObjectControl mControl;
57 dd1a65c1 Leszek Koltunski
  private ImageButton mPrevButton;
58 55e6be1d Leszek Koltunski
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60
// PUBLIC API
61
62 dd1a65c1 Leszek Koltunski
  public MovesController()
63 55e6be1d Leszek Koltunski
    {
64
    mCanPrevMove = true;
65 9d591756 Leszek Koltunski
    mMoves       = new ArrayList<>();
66 55e6be1d Leszek Koltunski
    }
67
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69
70 dd1a65c1 Leszek Koltunski
  private int getPrevIcon(boolean on)
71 146661ec Leszek Koltunski
    {
72
    if( on )
73
      {
74
      return RubikActivity.getDrawable(R.drawable.ui_small_cube_back,
75
                                       R.drawable.ui_medium_cube_back,
76
                                       R.drawable.ui_big_cube_back,
77
                                       R.drawable.ui_huge_cube_back);
78
      }
79
    else
80
      {
81
      return RubikActivity.getDrawable(R.drawable.ui_small_cube_grey,
82
                                       R.drawable.ui_medium_cube_grey,
83
                                       R.drawable.ui_big_cube_grey,
84
                                       R.drawable.ui_huge_cube_grey);
85 55e6be1d Leszek Koltunski
      }
86
    }
87
88
//////////////////////////////////////////////////////////////////////////////////////////////////
89
90 146661ec Leszek Koltunski
  public void backMove(TwistyActivity act)
91 55e6be1d Leszek Koltunski
    {
92
    if( mCanPrevMove )
93
      {
94
      int numMoves = mMoves.size();
95
96
      if( numMoves>0 )
97
        {
98
        Move move   = mMoves.remove(numMoves-1);
99
        int axis    = move.mAxis;
100
        int row     = (1<<move.mRow);
101
        int angle   = move.mAngle;
102
        int duration= Math.abs(angle)*MILLIS_PER_DEGREE;
103
104
        if( angle!=0 )
105
          {
106
          mCanPrevMove = false;
107 2afc6754 Leszek Koltunski
          mControl = act.getControl();
108
          mControl.blockTouch(BlockController.MOVES_PLACE_0);
109
          mControl.addRotation(this, axis, row, -angle, duration);
110 55e6be1d Leszek Koltunski
          }
111
        else
112
          {
113
          android.util.Log.e("solution", "error: trying to back move of angle 0");
114
          }
115 146661ec Leszek Koltunski
116
        if( numMoves==1 ) changeBackMove(act, false);
117 55e6be1d Leszek Koltunski
        }
118
      }
119
    }
120
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122
123 146661ec Leszek Koltunski
  private void changeBackMove(TwistyActivity act, final boolean on)
124
    {
125
    act.runOnUiThread(new Runnable()
126
      {
127
      @Override
128
      public void run()
129
        {
130
        if( mPrevButton!=null )
131
          mPrevButton.setImageResource(getPrevIcon(on));
132
        }
133
      });
134
    }
135
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
138
  public void addMove(TwistyActivity act, int axis, int row, int angle)
139 55e6be1d Leszek Koltunski
    {
140 9f006481 Leszek Koltunski
    if( mMoves.isEmpty() ) changeBackMove(act,true);
141 55e6be1d Leszek Koltunski
    mMoves.add(new Move(axis,row,angle));
142
    }
143
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145
146
  public void onActionFinished(final long effectID)
147
    {
148
    mCanPrevMove = true;
149 2afc6754 Leszek Koltunski
    mControl.unblockTouch();
150 55e6be1d Leszek Koltunski
    }
151
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153
154 1ae92052 Leszek Koltunski
  public void clearMoves(final TwistyActivity act)
155 55e6be1d Leszek Koltunski
    {
156
    mMoves.clear();
157 1ae92052 Leszek Koltunski
    changeBackMove(act,false);
158 55e6be1d Leszek Koltunski
    }
159
160 9f006481 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
161
162
  public int getNumMoves()
163
    {
164
    return mMoves.size();
165
    }
166
167 55e6be1d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
168
169 dd1a65c1 Leszek Koltunski
  public void setupButton(final TwistyActivity act, final float width)
170 55e6be1d Leszek Koltunski
    {
171 146661ec Leszek Koltunski
    final int icon = getPrevIcon( !mMoves.isEmpty() );
172 55e6be1d Leszek Koltunski
    mPrevButton = new TransparentImageButton(act, icon, width, LinearLayout.LayoutParams.MATCH_PARENT);
173
174
    mPrevButton.setOnClickListener( new View.OnClickListener()
175
      {
176
      @Override
177
      public void onClick(View v)
178
        {
179 146661ec Leszek Koltunski
        backMove(act);
180 55e6be1d Leszek Koltunski
        }
181
      });
182
    }
183
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185
186 dd1a65c1 Leszek Koltunski
  public ImageButton getButton()
187 55e6be1d Leszek Koltunski
    {
188
    return mPrevButton;
189
    }
190
  }