Project

General

Profile

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

magiccube / src / main / java / org / distorted / helpers / MovesController.java @ 7bb30586

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
24 55e6be1d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
25
26 dd1a65c1 Leszek Koltunski
public class MovesController implements MovesFinished
27 55e6be1d Leszek Koltunski
  {
28 c65a5efe Leszek Koltunski
  private static final int MOVES_PLACE_0 = 100;
29 55e6be1d Leszek Koltunski
  private static final int MILLIS_PER_DEGREE = 6;
30
31
  private static class Move
32
    {
33
    private final int mAxis, mRow, mAngle;
34
35
    Move(int axis, int row, int angle)
36
      {
37
      mAxis = axis;
38
      mRow  = row;
39
      mAngle= angle;
40
      }
41
    }
42
43 9d591756 Leszek Koltunski
  private final ArrayList<Move> mMoves;
44 55e6be1d Leszek Koltunski
  private boolean mCanPrevMove;
45 2afc6754 Leszek Koltunski
  private ObjectControl mControl;
46 dd874ae8 Leszek Koltunski
  private TransparentImageButton mPrevButton;
47 55e6be1d Leszek Koltunski
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49
// PUBLIC API
50
51 dd1a65c1 Leszek Koltunski
  public MovesController()
52 55e6be1d Leszek Koltunski
    {
53
    mCanPrevMove = true;
54 9d591756 Leszek Koltunski
    mMoves       = new ArrayList<>();
55 55e6be1d Leszek Koltunski
    }
56
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58
59 dd1a65c1 Leszek Koltunski
  private int getPrevIcon(boolean on)
60 146661ec Leszek Koltunski
    {
61 70688a23 leszek
    return on ? R.drawable.ui_cube_back : R.drawable.ui_cube_grey;
62 55e6be1d Leszek Koltunski
    }
63
64
//////////////////////////////////////////////////////////////////////////////////////////////////
65
66 e019c70b Leszek Koltunski
  public void backMove(Activity act, ObjectControl control)
67 55e6be1d Leszek Koltunski
    {
68
    if( mCanPrevMove )
69
      {
70
      int numMoves = mMoves.size();
71
72
      if( numMoves>0 )
73
        {
74 f781325a Leszek Koltunski
        Move move = mMoves.remove(numMoves-1);
75
        int axis  = move.mAxis;
76
        int angle = move.mAngle;
77 55e6be1d Leszek Koltunski
78
        if( angle!=0 )
79
          {
80
          mCanPrevMove = false;
81 e019c70b Leszek Koltunski
          mControl = control;
82 c65a5efe Leszek Koltunski
          mControl.blockTouch(MOVES_PLACE_0);
83 e4f656d1 Leszek Koltunski
          mControl.addRotation(this, axis, (1<<move.mRow), -angle, MILLIS_PER_DEGREE);
84 55e6be1d Leszek Koltunski
          }
85
        else
86
          {
87 f781325a Leszek Koltunski
          android.util.Log.e("backMove", "error: trying to back move of angle 0");
88 55e6be1d Leszek Koltunski
          }
89 146661ec Leszek Koltunski
90
        if( numMoves==1 ) changeBackMove(act, false);
91 55e6be1d Leszek Koltunski
        }
92
      }
93
    }
94
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96
97 e019c70b Leszek Koltunski
  private void changeBackMove(Activity act, final boolean on)
98 146661ec Leszek Koltunski
    {
99
    act.runOnUiThread(new Runnable()
100
      {
101
      @Override
102
      public void run()
103
        {
104
        if( mPrevButton!=null )
105 464ade4a leszek
          mPrevButton.setImageResource(getPrevIcon(on));
106 146661ec Leszek Koltunski
        }
107
      });
108
    }
109
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111
112 e019c70b Leszek Koltunski
  public void addMove(Activity act, int axis, int row, int angle)
113 55e6be1d Leszek Koltunski
    {
114 9f006481 Leszek Koltunski
    if( mMoves.isEmpty() ) changeBackMove(act,true);
115 55e6be1d Leszek Koltunski
    mMoves.add(new Move(axis,row,angle));
116
    }
117
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119
120
  public void onActionFinished(final long effectID)
121
    {
122
    mCanPrevMove = true;
123 919b830e Leszek Koltunski
    mControl.unblockRotation();
124 55e6be1d Leszek Koltunski
    }
125
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127
128 e019c70b Leszek Koltunski
  public void clearMoves(final Activity act)
129 55e6be1d Leszek Koltunski
    {
130
    mMoves.clear();
131 1ae92052 Leszek Koltunski
    changeBackMove(act,false);
132 55e6be1d Leszek Koltunski
    }
133
134 9f006481 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
135
136
  public int getNumMoves()
137
    {
138
    return mMoves.size();
139
    }
140
141 55e6be1d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
142
143 dd874ae8 Leszek Koltunski
  public void setupButton(final Activity act, ObjectControl control)
144 55e6be1d Leszek Koltunski
    {
145 146661ec Leszek Koltunski
    final int icon = getPrevIcon( !mMoves.isEmpty() );
146 b600ccd9 Leszek Koltunski
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
147 464ade4a leszek
    mPrevButton = new TransparentImageButton(act,icon,params);
148 55e6be1d Leszek Koltunski
149
    mPrevButton.setOnClickListener( new View.OnClickListener()
150
      {
151
      @Override
152
      public void onClick(View v)
153
        {
154 c95b37a4 Leszek Koltunski
        if( control.isScramblingAndSolvingNotBlocked() ) backMove(act,control);
155 55e6be1d Leszek Koltunski
        }
156
      });
157
    }
158
159 2e3488f6 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
160
161 972f9eae Leszek Koltunski
    public void savePreferences(String key, SharedPreferences.Editor editor)
162 2e3488f6 Leszek Koltunski
      {
163
      StringBuilder moves = new StringBuilder();
164
      int numMoves = getNumMoves();
165
166 7bb30586 leszek
167
      android.util.Log.e("D", "saving moves: "+numMoves);
168
169 2e3488f6 Leszek Koltunski
      for(int m=0; m<numMoves; m++)
170
        {
171
        Move move = mMoves.get(m);
172
173
        if( m>0 ) moves.append(' ');
174
        moves.append(move.mAxis);
175
        moves.append(' ');
176
        moves.append(move.mRow);
177
        moves.append(' ');
178
        moves.append(move.mAngle);
179
        }
180
181 972f9eae Leszek Koltunski
      editor.putString( key, moves.toString() );
182 2e3488f6 Leszek Koltunski
      editor.apply();
183
      }
184
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186
187 972f9eae Leszek Koltunski
    public void restorePreferences(Activity act, String key, SharedPreferences preferences)
188 2e3488f6 Leszek Koltunski
      {
189 972f9eae Leszek Koltunski
      String objects = preferences.getString( key,"");
190 2e3488f6 Leszek Koltunski
191
      if( objects.length()>0 )
192
        {
193
        String[] tokens = objects.split(" ");
194 5305fdc8 Leszek Koltunski
        int length = tokens.length/3;
195 2e3488f6 Leszek Koltunski
196 7bb30586 leszek
        android.util.Log.e("D", "restoring moves: "+length);
197
198 5305fdc8 Leszek Koltunski
        for(int m=0; m<length; m++)
199 2e3488f6 Leszek Koltunski
          {
200
          String axis  = tokens[3*m  ];
201
          String row   = tokens[3*m+1];
202
          String angle = tokens[3*m+2];
203
204
          try
205
            {
206
            int axisI = Integer.parseInt(axis);
207
            int rowI  = Integer.parseInt(row);
208
            int angleI= Integer.parseInt(angle);
209
210
            addMove(act,axisI,rowI,angleI);
211
            }
212
          catch(NumberFormatException ex)
213
            {
214
            // ignore
215
            android.util.Log.e("D", "exception: "+ex.getMessage());
216
            }
217
          }
218
        }
219
      }
220
221 55e6be1d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
222
223 dd874ae8 Leszek Koltunski
  public TransparentImageButton getButton()
224 55e6be1d Leszek Koltunski
    {
225
    return mPrevButton;
226
    }
227
  }