Project

General

Profile

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

magiccube / src / main / java / org / distorted / helpers / MovesController.java @ c65a5efe

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 e019c70b Leszek Koltunski
import android.app.Activity;
25 55e6be1d Leszek Koltunski
import android.view.View;
26
import android.widget.ImageButton;
27
import android.widget.LinearLayout;
28
29 88a3e972 Leszek Koltunski
import org.distorted.objectlib.helpers.MovesFinished;
30 2afc6754 Leszek Koltunski
import org.distorted.objectlib.main.ObjectControl;
31 55e6be1d Leszek Koltunski
32 dd1a65c1 Leszek Koltunski
import org.distorted.main.R;
33
import org.distorted.main.RubikActivity;
34
35 55e6be1d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
36
37 dd1a65c1 Leszek Koltunski
public class MovesController implements MovesFinished
38 55e6be1d Leszek Koltunski
  {
39 c65a5efe Leszek Koltunski
  private static final int MOVES_PLACE_0 = 100;
40 55e6be1d Leszek Koltunski
  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 e019c70b Leszek Koltunski
  public void backMove(Activity act, ObjectControl control)
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 angle   = move.mAngle;
101
102
        if( angle!=0 )
103
          {
104
          mCanPrevMove = false;
105 e019c70b Leszek Koltunski
          mControl = control;
106 c65a5efe Leszek Koltunski
          mControl.blockTouch(MOVES_PLACE_0);
107 e4f656d1 Leszek Koltunski
          mControl.addRotation(this, axis, (1<<move.mRow), -angle, MILLIS_PER_DEGREE);
108 55e6be1d Leszek Koltunski
          }
109
        else
110
          {
111
          android.util.Log.e("solution", "error: trying to back move of angle 0");
112
          }
113 146661ec Leszek Koltunski
114
        if( numMoves==1 ) changeBackMove(act, false);
115 55e6be1d Leszek Koltunski
        }
116
      }
117
    }
118
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120
121 e019c70b Leszek Koltunski
  private void changeBackMove(Activity act, final boolean on)
122 146661ec Leszek Koltunski
    {
123
    act.runOnUiThread(new Runnable()
124
      {
125
      @Override
126
      public void run()
127
        {
128
        if( mPrevButton!=null )
129
          mPrevButton.setImageResource(getPrevIcon(on));
130
        }
131
      });
132
    }
133
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135
136 e019c70b Leszek Koltunski
  public void addMove(Activity act, int axis, int row, int angle)
137 55e6be1d Leszek Koltunski
    {
138 9f006481 Leszek Koltunski
    if( mMoves.isEmpty() ) changeBackMove(act,true);
139 55e6be1d Leszek Koltunski
    mMoves.add(new Move(axis,row,angle));
140
    }
141
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143
144
  public void onActionFinished(final long effectID)
145
    {
146
    mCanPrevMove = true;
147 2afc6754 Leszek Koltunski
    mControl.unblockTouch();
148 55e6be1d Leszek Koltunski
    }
149
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151
152 e019c70b Leszek Koltunski
  public void clearMoves(final Activity act)
153 55e6be1d Leszek Koltunski
    {
154
    mMoves.clear();
155 1ae92052 Leszek Koltunski
    changeBackMove(act,false);
156 55e6be1d Leszek Koltunski
    }
157
158 9f006481 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
159
160
  public int getNumMoves()
161
    {
162
    return mMoves.size();
163
    }
164
165 55e6be1d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
166
167 e019c70b Leszek Koltunski
  public void setupButton(final Activity act, ObjectControl control, final float width)
168 55e6be1d Leszek Koltunski
    {
169 146661ec Leszek Koltunski
    final int icon = getPrevIcon( !mMoves.isEmpty() );
170 55e6be1d Leszek Koltunski
    mPrevButton = new TransparentImageButton(act, icon, width, LinearLayout.LayoutParams.MATCH_PARENT);
171
172
    mPrevButton.setOnClickListener( new View.OnClickListener()
173
      {
174
      @Override
175
      public void onClick(View v)
176
        {
177 e019c70b Leszek Koltunski
        backMove(act,control);
178 55e6be1d Leszek Koltunski
        }
179
      });
180
    }
181
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183
184 dd1a65c1 Leszek Koltunski
  public ImageButton getButton()
185 55e6be1d Leszek Koltunski
    {
186
    return mPrevButton;
187
    }
188
  }