Project

General

Profile

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

magiccube / src / main / java / org / distorted / helpers / MovesController.java @ 2cf21fdc

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
import org.distorted.main.RubikActivity;
24

    
25
///////////////////////////////////////////////////////////////////////////////////////////////////
26

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

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50
// PUBLIC API
51

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

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
  private int getPrevIcon(boolean on)
61
    {
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
      }
76
    }
77

    
78
//////////////////////////////////////////////////////////////////////////////////////////////////
79

    
80
  public void backMove(Activity act, ObjectControl control)
81
    {
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
          mControl = control;
96
          mControl.blockTouch(MOVES_PLACE_0);
97
          mControl.addRotation(this, axis, (1<<move.mRow), -angle, MILLIS_PER_DEGREE);
98
          }
99
        else
100
          {
101
          android.util.Log.e("solution", "error: trying to back move of angle 0");
102
          }
103

    
104
        if( numMoves==1 ) changeBackMove(act, false);
105
        }
106
      }
107
    }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

    
111
  private void changeBackMove(Activity act, final boolean on)
112
    {
113
    act.runOnUiThread(new Runnable()
114
      {
115
      @Override
116
      public void run()
117
        {
118
        if( mPrevButton!=null )
119
          mPrevButton.setIconResource(getPrevIcon(on));
120
        }
121
      });
122
    }
123

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

    
126
  public void addMove(Activity act, int axis, int row, int angle)
127
    {
128
    if( mMoves.isEmpty() ) changeBackMove(act,true);
129
    mMoves.add(new Move(axis,row,angle));
130
    }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

    
134
  public void onActionFinished(final long effectID)
135
    {
136
    mCanPrevMove = true;
137
    mControl.unblockTouch();
138
    }
139

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141

    
142
  public void clearMoves(final Activity act)
143
    {
144
    mMoves.clear();
145
    changeBackMove(act,false);
146
    }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

    
150
  public int getNumMoves()
151
    {
152
    return mMoves.size();
153
    }
154

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156

    
157
  public void setupButton(final Activity act, ObjectControl control)
158
    {
159
    final int icon = getPrevIcon( !mMoves.isEmpty() );
160
    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

    
163
    mPrevButton.setOnClickListener( new View.OnClickListener()
164
      {
165
      @Override
166
      public void onClick(View v)
167
        {
168
        backMove(act,control);
169
        }
170
      });
171
    }
172

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

    
175
    public void savePreferences(String key, SharedPreferences.Editor editor)
176
      {
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
      editor.putString( key, moves.toString() );
193
      editor.apply();
194
      }
195

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197

    
198
    public void restorePreferences(Activity act, String key, SharedPreferences preferences)
199
      {
200
      String objects = preferences.getString( key,"");
201

    
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
///////////////////////////////////////////////////////////////////////////////////////////////////
231

    
232
  public TransparentImageButton getButton()
233
    {
234
    return mPrevButton;
235
    }
236
  }
(2-2/4)