Project

General

Profile

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

magiccube / src / main / java / org / distorted / helpers / MovesController.java @ 1f3bc259

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