Project

General

Profile

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

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

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

    
24
import android.app.Activity;
25
import android.view.View;
26
import android.widget.ImageButton;
27
import android.widget.LinearLayout;
28

    
29
import org.distorted.objectlib.helpers.MovesFinished;
30
import org.distorted.objectlib.main.ObjectControl;
31

    
32
import org.distorted.main.R;
33
import org.distorted.main.RubikActivity;
34

    
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

    
37
public class MovesController implements MovesFinished
38
  {
39
  private static final int MOVES_PLACE_0 = 100;
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
  private final ArrayList<Move> mMoves;
55
  private boolean mCanPrevMove;
56
  private ObjectControl mControl;
57
  private ImageButton mPrevButton;
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60
// PUBLIC API
61

    
62
  public MovesController()
63
    {
64
    mCanPrevMove = true;
65
    mMoves       = new ArrayList<>();
66
    }
67

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

    
70
  private int getPrevIcon(boolean on)
71
    {
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
      }
86
    }
87

    
88
//////////////////////////////////////////////////////////////////////////////////////////////////
89

    
90
  public void backMove(Activity act, ObjectControl control)
91
    {
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
          mControl = control;
106
          mControl.blockTouch(MOVES_PLACE_0);
107
          mControl.addRotation(this, axis, (1<<move.mRow), -angle, MILLIS_PER_DEGREE);
108
          }
109
        else
110
          {
111
          android.util.Log.e("solution", "error: trying to back move of angle 0");
112
          }
113

    
114
        if( numMoves==1 ) changeBackMove(act, false);
115
        }
116
      }
117
    }
118

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

    
121
  private void changeBackMove(Activity act, final boolean on)
122
    {
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
  public void addMove(Activity act, int axis, int row, int angle)
137
    {
138
    if( mMoves.isEmpty() ) changeBackMove(act,true);
139
    mMoves.add(new Move(axis,row,angle));
140
    }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143

    
144
  public void onActionFinished(final long effectID)
145
    {
146
    mCanPrevMove = true;
147
    mControl.unblockTouch();
148
    }
149

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151

    
152
  public void clearMoves(final Activity act)
153
    {
154
    mMoves.clear();
155
    changeBackMove(act,false);
156
    }
157

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159

    
160
  public int getNumMoves()
161
    {
162
    return mMoves.size();
163
    }
164

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166

    
167
  public void setupButton(final Activity act, ObjectControl control, final float width)
168
    {
169
    final int icon = getPrevIcon( !mMoves.isEmpty() );
170
    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
        backMove(act,control);
178
        }
179
      });
180
    }
181

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

    
184
  public ImageButton getButton()
185
    {
186
    return mPrevButton;
187
    }
188
  }
(2-2/4)