Project

General

Profile

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

magiccube / src / main / java / org / distorted / helpers / MovesController.java @ 70688a23

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
      for(int m=0; m<numMoves; m++)
167
        {
168
        Move move = mMoves.get(m);
169
170
        if( m>0 ) moves.append(' ');
171
        moves.append(move.mAxis);
172
        moves.append(' ');
173
        moves.append(move.mRow);
174
        moves.append(' ');
175
        moves.append(move.mAngle);
176
        }
177
178 972f9eae Leszek Koltunski
      editor.putString( key, moves.toString() );
179 2e3488f6 Leszek Koltunski
      editor.apply();
180
      }
181
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183
184 972f9eae Leszek Koltunski
    public void restorePreferences(Activity act, String key, SharedPreferences preferences)
185 2e3488f6 Leszek Koltunski
      {
186 972f9eae Leszek Koltunski
      String objects = preferences.getString( key,"");
187 2e3488f6 Leszek Koltunski
188
      if( objects.length()>0 )
189
        {
190
        String[] tokens = objects.split(" ");
191 5305fdc8 Leszek Koltunski
        int length = tokens.length/3;
192 2e3488f6 Leszek Koltunski
193 5305fdc8 Leszek Koltunski
        for(int m=0; m<length; m++)
194 2e3488f6 Leszek Koltunski
          {
195
          String axis  = tokens[3*m  ];
196
          String row   = tokens[3*m+1];
197
          String angle = tokens[3*m+2];
198
199
          try
200
            {
201
            int axisI = Integer.parseInt(axis);
202
            int rowI  = Integer.parseInt(row);
203
            int angleI= Integer.parseInt(angle);
204
205
            addMove(act,axisI,rowI,angleI);
206
            }
207
          catch(NumberFormatException ex)
208
            {
209
            // ignore
210
            android.util.Log.e("D", "exception: "+ex.getMessage());
211
            }
212
          }
213
        }
214
      }
215
216 55e6be1d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
217
218 dd874ae8 Leszek Koltunski
  public TransparentImageButton getButton()
219 55e6be1d Leszek Koltunski
    {
220
    return mPrevButton;
221
    }
222
  }