Project

General

Profile

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

magiccube / src / main / java / org / distorted / helpers / MovesController.java @ 8feb68c2

1 55e6be1d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2021 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6 1c327853 Leszek Koltunski
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8 55e6be1d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
9
10
package org.distorted.helpers;
11
12 3f7a4363 Leszek Koltunski
import java.util.ArrayList;
13
14 e019c70b Leszek Koltunski
import android.app.Activity;
15 2e3488f6 Leszek Koltunski
import android.content.SharedPreferences;
16 55e6be1d Leszek Koltunski
import android.view.View;
17
import android.widget.LinearLayout;
18
19 88a3e972 Leszek Koltunski
import org.distorted.objectlib.helpers.MovesFinished;
20 2afc6754 Leszek Koltunski
import org.distorted.objectlib.main.ObjectControl;
21 55e6be1d Leszek Koltunski
22 dd1a65c1 Leszek Koltunski
import org.distorted.main.R;
23
import org.distorted.main.RubikActivity;
24
25 55e6be1d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
26
27 dd1a65c1 Leszek Koltunski
public class MovesController implements MovesFinished
28 55e6be1d Leszek Koltunski
  {
29 c65a5efe Leszek Koltunski
  private static final int MOVES_PLACE_0 = 100;
30 55e6be1d Leszek Koltunski
  private static final int MILLIS_PER_DEGREE = 6;
31
32
  private static class Move
33
    {
34
    private final int mAxis, mRow, mAngle;
35
36
    Move(int axis, int row, int angle)
37
      {
38
      mAxis = axis;
39
      mRow  = row;
40
      mAngle= angle;
41
      }
42
    }
43
44 9d591756 Leszek Koltunski
  private final ArrayList<Move> mMoves;
45 55e6be1d Leszek Koltunski
  private boolean mCanPrevMove;
46 2afc6754 Leszek Koltunski
  private ObjectControl mControl;
47 dd874ae8 Leszek Koltunski
  private TransparentImageButton mPrevButton;
48 55e6be1d Leszek Koltunski
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50
// PUBLIC API
51
52 dd1a65c1 Leszek Koltunski
  public MovesController()
53 55e6be1d Leszek Koltunski
    {
54
    mCanPrevMove = true;
55 9d591756 Leszek Koltunski
    mMoves       = new ArrayList<>();
56 55e6be1d Leszek Koltunski
    }
57
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59
60 dd1a65c1 Leszek Koltunski
  private int getPrevIcon(boolean on)
61 146661ec Leszek Koltunski
    {
62
    if( on )
63
      {
64
      return RubikActivity.getDrawable(R.drawable.ui_small_cube_back,
65
                                       R.drawable.ui_medium_cube_back,
66
                                       R.drawable.ui_big_cube_back,
67
                                       R.drawable.ui_huge_cube_back);
68
      }
69
    else
70
      {
71
      return RubikActivity.getDrawable(R.drawable.ui_small_cube_grey,
72
                                       R.drawable.ui_medium_cube_grey,
73
                                       R.drawable.ui_big_cube_grey,
74
                                       R.drawable.ui_huge_cube_grey);
75 55e6be1d Leszek Koltunski
      }
76
    }
77
78
//////////////////////////////////////////////////////////////////////////////////////////////////
79
80 e019c70b Leszek Koltunski
  public void backMove(Activity act, ObjectControl control)
81 55e6be1d Leszek Koltunski
    {
82
    if( mCanPrevMove )
83
      {
84
      int numMoves = mMoves.size();
85
86
      if( numMoves>0 )
87
        {
88
        Move move   = mMoves.remove(numMoves-1);
89
        int axis    = move.mAxis;
90
        int angle   = move.mAngle;
91
92
        if( angle!=0 )
93
          {
94
          mCanPrevMove = false;
95 e019c70b Leszek Koltunski
          mControl = control;
96 c65a5efe Leszek Koltunski
          mControl.blockTouch(MOVES_PLACE_0);
97 e4f656d1 Leszek Koltunski
          mControl.addRotation(this, axis, (1<<move.mRow), -angle, MILLIS_PER_DEGREE);
98 55e6be1d Leszek Koltunski
          }
99
        else
100
          {
101
          android.util.Log.e("solution", "error: trying to back move of angle 0");
102
          }
103 146661ec Leszek Koltunski
104
        if( numMoves==1 ) changeBackMove(act, false);
105 55e6be1d Leszek Koltunski
        }
106
      }
107
    }
108
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
111 e019c70b Leszek Koltunski
  private void changeBackMove(Activity act, final boolean on)
112 146661ec Leszek Koltunski
    {
113
    act.runOnUiThread(new Runnable()
114
      {
115
      @Override
116
      public void run()
117
        {
118
        if( mPrevButton!=null )
119 dd874ae8 Leszek Koltunski
          mPrevButton.setIconResource(getPrevIcon(on));
120 146661ec Leszek Koltunski
        }
121
      });
122
    }
123
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125
126 e019c70b Leszek Koltunski
  public void addMove(Activity act, int axis, int row, int angle)
127 55e6be1d Leszek Koltunski
    {
128 9f006481 Leszek Koltunski
    if( mMoves.isEmpty() ) changeBackMove(act,true);
129 55e6be1d Leszek Koltunski
    mMoves.add(new Move(axis,row,angle));
130
    }
131
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133
134
  public void onActionFinished(final long effectID)
135
    {
136
    mCanPrevMove = true;
137 2afc6754 Leszek Koltunski
    mControl.unblockTouch();
138 55e6be1d Leszek Koltunski
    }
139
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141
142 e019c70b Leszek Koltunski
  public void clearMoves(final Activity act)
143 55e6be1d Leszek Koltunski
    {
144
    mMoves.clear();
145 1ae92052 Leszek Koltunski
    changeBackMove(act,false);
146 55e6be1d Leszek Koltunski
    }
147
148 9f006481 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
149
150
  public int getNumMoves()
151
    {
152
    return mMoves.size();
153
    }
154
155 55e6be1d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
156
157 dd874ae8 Leszek Koltunski
  public void setupButton(final Activity act, ObjectControl control)
158 55e6be1d Leszek Koltunski
    {
159 146661ec Leszek Koltunski
    final int icon = getPrevIcon( !mMoves.isEmpty() );
160 b600ccd9 Leszek Koltunski
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
161
    mPrevButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
162 55e6be1d Leszek Koltunski
163
    mPrevButton.setOnClickListener( new View.OnClickListener()
164
      {
165
      @Override
166
      public void onClick(View v)
167
        {
168 e019c70b Leszek Koltunski
        backMove(act,control);
169 55e6be1d Leszek Koltunski
        }
170
      });
171
    }
172
173 2e3488f6 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
174
175 972f9eae Leszek Koltunski
    public void savePreferences(String key, SharedPreferences.Editor editor)
176 2e3488f6 Leszek Koltunski
      {
177
      StringBuilder moves = new StringBuilder();
178
      int numMoves = getNumMoves();
179
180
      for(int m=0; m<numMoves; m++)
181
        {
182
        Move move = mMoves.get(m);
183
184
        if( m>0 ) moves.append(' ');
185
        moves.append(move.mAxis);
186
        moves.append(' ');
187
        moves.append(move.mRow);
188
        moves.append(' ');
189
        moves.append(move.mAngle);
190
        }
191
192 972f9eae Leszek Koltunski
      editor.putString( key, moves.toString() );
193 2e3488f6 Leszek Koltunski
      editor.apply();
194
      }
195
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197
198 972f9eae Leszek Koltunski
    public void restorePreferences(Activity act, String key, SharedPreferences preferences)
199 2e3488f6 Leszek Koltunski
      {
200 972f9eae Leszek Koltunski
      String objects = preferences.getString( key,"");
201 2e3488f6 Leszek Koltunski
202
      if( objects.length()>0 )
203
        {
204
        String[] tokens = objects.split(" ");
205
        int length = tokens.length;
206
207
        for(int m=0; m<length/3; m++)
208
          {
209
          String axis  = tokens[3*m  ];
210
          String row   = tokens[3*m+1];
211
          String angle = tokens[3*m+2];
212
213
          try
214
            {
215
            int axisI = Integer.parseInt(axis);
216
            int rowI  = Integer.parseInt(row);
217
            int angleI= Integer.parseInt(angle);
218
219
            addMove(act,axisI,rowI,angleI);
220
            }
221
          catch(NumberFormatException ex)
222
            {
223
            // ignore
224
            android.util.Log.e("D", "exception: "+ex.getMessage());
225
            }
226
          }
227
        }
228
      }
229
230 55e6be1d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
231
232 dd874ae8 Leszek Koltunski
  public TransparentImageButton getButton()
233 55e6be1d Leszek Koltunski
    {
234
    return mPrevButton;
235
    }
236
  }