Project

General

Profile

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

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

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.LinearLayout;
27

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

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

    
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35

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

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59
// PUBLIC API
60

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

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

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

    
87
//////////////////////////////////////////////////////////////////////////////////////////////////
88

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

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

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

    
120
  private void changeBackMove(Activity act, final boolean on)
121
    {
122
    act.runOnUiThread(new Runnable()
123
      {
124
      @Override
125
      public void run()
126
        {
127
        if( mPrevButton!=null )
128
          mPrevButton.setIconResource(getPrevIcon(on));
129
        }
130
      });
131
    }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

    
135
  public void addMove(Activity act, int axis, int row, int angle)
136
    {
137
    if( mMoves.isEmpty() ) changeBackMove(act,true);
138
    mMoves.add(new Move(axis,row,angle));
139
    }
140

    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142

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

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

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

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

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

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
  public void setupButton(final Activity act, ObjectControl control)
167
    {
168
    final int icon = getPrevIcon( !mMoves.isEmpty() );
169
    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

    
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 TransparentImageButton getButton()
185
    {
186
    return mPrevButton;
187
    }
188
  }
(2-2/4)