Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2021 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// 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
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.helpers;
11

    
12
import java.util.ArrayList;
13

    
14
import android.app.Activity;
15
import android.content.SharedPreferences;
16
import android.view.View;
17
import android.widget.LinearLayout;
18

    
19
import org.distorted.objectlib.helpers.MovesFinished;
20
import org.distorted.objectlib.main.ObjectControl;
21

    
22
import org.distorted.main.R;
23

    
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25

    
26
public class MovesController implements MovesFinished
27
  {
28
  private static final int MOVES_PLACE_0 = 100;
29
  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
  private final ArrayList<Move> mMoves;
44
  private boolean mCanPrevMove;
45
  private ObjectControl mControl;
46
  private TransparentImageButton mPrevButton;
47

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49
// PUBLIC API
50

    
51
  public MovesController()
52
    {
53
    mCanPrevMove = true;
54
    mMoves       = new ArrayList<>();
55
    }
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

    
59
  private int getPrevIcon(boolean on)
60
    {
61
    return on ? R.drawable.ui_cube_back : R.drawable.ui_cube_grey;
62
    }
63

    
64
//////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
  public void backMove(Activity act, ObjectControl control)
67
    {
68
    if( mCanPrevMove )
69
      {
70
      int numMoves = mMoves.size();
71

    
72
      if( numMoves>0 )
73
        {
74
        Move move = mMoves.remove(numMoves-1);
75
        int axis  = move.mAxis;
76
        int angle = move.mAngle;
77

    
78
        if( angle!=0 )
79
          {
80
          mCanPrevMove = false;
81
          mControl = control;
82
          mControl.blockTouch(MOVES_PLACE_0);
83
          mControl.addRotation(this, axis, (1<<move.mRow), -angle, MILLIS_PER_DEGREE);
84
          }
85
        else
86
          {
87
          android.util.Log.e("backMove", "error: trying to back move of angle 0");
88
          }
89

    
90
        if( numMoves==1 ) changeBackMove(act, false);
91
        }
92
      }
93
    }
94

    
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96

    
97
  private void changeBackMove(Activity act, final boolean on)
98
    {
99
    act.runOnUiThread(new Runnable()
100
      {
101
      @Override
102
      public void run()
103
        {
104
        if( mPrevButton!=null )
105
          mPrevButton.setImageResource(getPrevIcon(on));
106
        }
107
      });
108
    }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

    
112
  public void addMove(Activity act, int axis, int row, int angle)
113
    {
114
    if( mMoves.isEmpty() ) changeBackMove(act,true);
115
    mMoves.add(new Move(axis,row,angle));
116
    }
117

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

    
120
  public void onActionFinished(final long effectID)
121
    {
122
    mCanPrevMove = true;
123
    mControl.unblockRotation();
124
    }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127

    
128
  public void clearMoves(final Activity act)
129
    {
130
    mMoves.clear();
131
    changeBackMove(act,false);
132
    }
133

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

    
136
  public int getNumMoves()
137
    {
138
    return mMoves.size();
139
    }
140

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

    
143
  public void setupButton(final Activity act, ObjectControl control)
144
    {
145
    final int icon = getPrevIcon( !mMoves.isEmpty() );
146
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
147
    mPrevButton = new TransparentImageButton(act,icon,params);
148

    
149
    mPrevButton.setOnClickListener( new View.OnClickListener()
150
      {
151
      @Override
152
      public void onClick(View v)
153
        {
154
        if( control.isScramblingAndSolvingNotBlocked() ) backMove(act,control);
155
        }
156
      });
157
    }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

    
161
    public void savePreferences(String key, SharedPreferences.Editor editor)
162
      {
163
      StringBuilder moves = new StringBuilder();
164
      int numMoves = getNumMoves();
165

    
166

    
167
      android.util.Log.e("D", "saving moves: "+numMoves);
168

    
169
      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
      editor.putString( key, moves.toString() );
182
      editor.apply();
183
      }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186

    
187
    public void restorePreferences(Activity act, String key, SharedPreferences preferences)
188
      {
189
      String objects = preferences.getString( key,"");
190

    
191
      if( objects.length()>0 )
192
        {
193
        String[] tokens = objects.split(" ");
194
        int length = tokens.length/3;
195

    
196
        android.util.Log.e("D", "restoring moves: "+length);
197

    
198
        for(int m=0; m<length; m++)
199
          {
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
///////////////////////////////////////////////////////////////////////////////////////////////////
222

    
223
  public TransparentImageButton getButton()
224
    {
225
    return mPrevButton;
226
    }
227
  }
(2-2/5)