Project

General

Profile

Download (8.17 KB) Statistics
| Branch: | Revision:

phasedsolver / src / main / java / org / distorted / phasedsolver / SolverLowerPane.java @ 78843d15

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2024 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.phasedsolver;
11

    
12
import android.widget.TextView;
13

    
14
import org.distorted.objectlib.helpers.MovesFinished;
15
import org.distorted.objectlib.main.ObjectControl;
16

    
17
///////////////////////////////////////////////////////////////////////////////////////////////////
18

    
19
public class SolverLowerPane implements MovesFinished
20
{
21
  private static final int MOVES_PLACE_0 = 100;
22
  private static final int MOVES_PLACE_1 = 101;
23
  private static final int MILLIS_PER_DEGREE = 6;
24

    
25
  private final String[] mPhaseNames;
26
  private final TextView mText, mPhase;
27

    
28
  private int[][][] mMoves;
29
  private int mNumMoves,mCurrMove;
30
  private int mNumPhases,mCurrPhase;
31
  private boolean mCanMove;
32
  private ObjectControl mControl;
33

    
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35

    
36
  SolverLowerPane(SolverActivity act, String[] names)
37
    {
38
    mText  = act.findViewById(R.id.solverText);
39
    mPhase = act.findViewById(R.id.solverPhaseName);
40

    
41
    mPhaseNames = names;
42
    mNumPhases = names.length;
43

    
44
    mCanMove = true;
45
    setMoves(act,null);
46
    }
47

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

    
50
  void backPhase(SolverActivity act)
51
    {
52
    if( mCurrMove>0 )
53
      {
54
      int[][] moves = transformMoves(mMoves[mCurrPhase],0,mCurrMove, false);
55
      mControl = act.getControl();
56
      mControl.applyScrambles(moves);
57
      mCurrMove = 0;
58
      }
59
    else if( mCurrPhase>0 )
60
      {
61
      mCurrPhase--;
62
      mNumMoves = mMoves[mCurrPhase].length;
63
      int[][] moves = transformMoves(mMoves[mCurrPhase],0,mNumMoves, false);
64
      mControl = act.getControl();
65
      mControl.applyScrambles(moves);
66
      }
67
    else
68
      {
69
      mCurrPhase = mNumPhases-1;
70
      mNumMoves = mMoves[mCurrPhase].length;
71
      mCurrMove = mNumMoves;
72
      mControl = act.getControl();
73

    
74
      int[][] moves = transformMoves(mMoves, true);
75
      mControl.applyScrambles(moves);
76
      }
77

    
78
    setText(act);
79
    }
80

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82

    
83
  void nextPhase(SolverActivity act)
84
    {
85
    if( mCurrMove<mNumMoves )
86
      {
87
      int[][] moves = transformMoves(mMoves[mCurrPhase],mCurrMove,mNumMoves, true);
88
      mCurrMove = mNumMoves;
89
      mControl = act.getControl();
90
      mControl.applyScrambles(moves);
91
      }
92
    else if( mCurrPhase<mNumPhases-1 )
93
      {
94
      mCurrPhase++;
95
      mNumMoves = mMoves[mCurrPhase].length;
96
      mCurrMove = mNumMoves;
97
      int[][] moves = transformMoves(mMoves[mCurrPhase],0,mNumMoves, true);
98
      mControl = act.getControl();
99
      mControl.applyScrambles(moves);
100
      }
101
    else
102
      {
103
      mCurrPhase = 0;
104
      mNumMoves = mMoves[mCurrPhase].length;
105
      mCurrMove = 0;
106
      mControl = act.getControl();
107
      int[][] moves = transformMoves(mMoves, false);
108
      mControl.applyScrambles(moves);
109
      }
110

    
111
    setText(act);
112
    }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

    
116
  private void setText(SolverActivity act)
117
    {
118
    int currMove = 0;
119
    int totalMove = 0;
120

    
121
    if( mMoves!=null )
122
      {
123
      currMove = mCurrMove;
124
      for(int p=0; p<mCurrPhase; p++) currMove  += (mMoves[p]==null ? 0: mMoves[p].length);
125
      for(int p=0; p<mNumPhases; p++) totalMove += (mMoves[p]==null ? 0: mMoves[p].length);
126
      }
127

    
128
    final int cMove = currMove;
129
    final int tMove = totalMove;
130

    
131
    act.runOnUiThread(new Runnable()
132
      {
133
      @Override
134
      public void run()
135
        {
136
        mPhase.setText(mPhaseNames[mCurrPhase]+" "+mCurrMove+"/"+mNumMoves);
137
        mText.setText(cMove+"/"+tMove);
138
        }
139
      });
140
    }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143

    
144
  private int[][] transformMoves(int[][] moves, int start, int end, boolean front)
145
    {
146
    int mult = front ? 1:-1;
147
    int len = end-start;
148
    int[][] ret = new int[len][];
149

    
150
    for(int m=0; m<len; m++)
151
      {
152
      int[] mv = moves[front ? start+m : end-1-m];
153
      int[] rt = new int[3];
154
      rt[0] = mv[0];
155
      rt[1] = (1<<mv[1]);
156
      rt[2] = mult*mv[2];
157
      ret[m] = rt;
158
      }
159

    
160
    return ret;
161
    }
162

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164

    
165
  private int[][] transformMoves(int[][][] moves, boolean front)
166
    {
167
    int len = moves.length;
168
    int totalLen = 0;
169
    for (int[][] move : moves) totalLen += (move==null ? 0 : move.length);
170

    
171
    int[][] ret = new int[totalLen][];
172
    int mult = front ? 1:-1;
173
    int index = 0;
174

    
175
    for(int m=0; m<len; m++)
176
      {
177
      int[][] mv = moves[front ? m : len-1-m];
178
      int l = (mv==null ? 0 : mv.length);
179

    
180
      for(int p=0; p<l; p++)
181
        {
182
        int[] mve = mv[front ? p : l-1-p];
183
        int[] rt = new int[3];
184
        rt[0] = mve[0];
185
        rt[1] = (1<<mve[1]);
186
        rt[2] = mult*mve[2];
187
        ret[index++] = rt;
188
        }
189
      }
190

    
191
    return ret;
192
    }
193

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

    
196
  void setMoves(SolverActivity act, int[][][] moves)
197
    {
198
    mMoves    = moves;
199
    mCurrPhase= 0;
200
    mNumMoves = moves==null || moves[0]==null ? 0 : moves[0].length;
201
    mCurrMove = 0;
202

    
203
    setText(act);
204
    }
205

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

    
208
  void clearMoves(SolverActivity act)
209
    {
210
    setMoves(act,null);
211
    }
212

    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214

    
215
  void backMove(SolverActivity act)
216
    {
217
    if( mMoves!=null && mCanMove )
218
      {
219
      if( mCurrMove>0 )
220
        {
221
        mCanMove = false;
222
        int[] move = mMoves[mCurrPhase][--mCurrMove];
223
        mControl = act.getControl();
224
        mControl.blockTouch(MOVES_PLACE_0);
225
        mControl.addRotation(this, move[0], (1<<move[1]), -move[2], MILLIS_PER_DEGREE);
226
        }
227
      else if( mCurrPhase>0 )
228
        {
229
        mCurrPhase--;
230
        mNumMoves = mMoves[mCurrPhase].length;
231
        mCurrMove = mNumMoves;
232
        }
233
      else
234
        {
235
        mCurrPhase = mNumPhases-1;
236
        mNumMoves = mMoves[mCurrPhase].length;
237
        mCurrMove = mNumMoves;
238
        mControl = act.getControl();
239

    
240
        int[][] moves = transformMoves(mMoves, true);
241
        mControl.applyScrambles(moves);
242
        }
243

    
244
      setText(act);
245
      }
246
    }
247

    
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249

    
250
  void nextMove(SolverActivity act)
251
    {
252
    if( mMoves!=null && mCanMove )
253
      {
254
      if( mCurrMove<mNumMoves )
255
        {
256
        mCanMove = false;
257
        int[] move = mMoves[mCurrPhase][mCurrMove++];
258
        mControl = act.getControl();
259
        mControl.blockTouch(MOVES_PLACE_1);
260
        mControl.addRotation(this, move[0], (1<<move[1]), move[2], MILLIS_PER_DEGREE);
261
        }
262
      else if( mCurrPhase<mNumPhases-1 )
263
        {
264
        mCurrPhase++;
265
        mNumMoves = mMoves[mCurrPhase].length;
266
        mCurrMove = 0;
267
        }
268
      else
269
        {
270
        mCurrPhase = 0;
271
        mNumMoves = mMoves[mCurrPhase].length;
272
        mCurrMove = 0;
273
        mControl = act.getControl();
274

    
275
        int[][] moves = transformMoves(mMoves, false);
276
        mControl.applyScrambles(moves);
277
        }
278

    
279
      setText(act);
280
      }
281
    }
282

    
283
///////////////////////////////////////////////////////////////////////////////////////////////////
284

    
285
  public void onActionFinished(final long effectID)
286
    {
287
    mCanMove = true;
288
    mControl.unblockRotation();
289
    }
290
}
(2-2/5)