Project

General

Profile

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

phasedsolver / src / main / java / org / distorted / phasedsolver / SolverLowerPane.java @ be803eb3

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 TextView mText, mPhase;
26
  private String[] mPhaseNames;
27
  private int mNumPhases;
28
  private int[][][] mMoves;
29
  private int mNumMoves,mCurrMove,mCurrPhase;
30
  private boolean mCanMove;
31
  private ObjectControl mControl;
32

    
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34

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

    
40
    mPhaseNames = names;
41
    mNumPhases = names.length;
42
    mMoves = new int[mNumPhases][][];
43
    mCanMove = true;
44
    setMoves(act,null,0);
45
    }
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

    
49
  void updateNames(SolverActivity act, String[] names)
50
    {
51
    mPhaseNames = names;
52
    mNumPhases = names.length;
53
    mMoves = new int[mNumPhases][][];
54
    mCanMove = true;
55
    setMoves(act,null,0);
56
    }
57

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

    
60
  void backPhase(SolverActivity act)
61
    {
62
    if( mCurrMove>0 )
63
      {
64
      int[][] moves = transformMoves(mMoves[mCurrPhase],0,mCurrMove, false);
65
      mControl = act.getControl();
66
      mControl.applyScrambles(moves);
67
      mCurrMove = 0;
68
      }
69
    else if( mCurrPhase>0 )
70
      {
71
      mCurrPhase--;
72
      mNumMoves = mMoves[mCurrPhase]==null ? 0 : mMoves[mCurrPhase].length;
73
      int[][] moves = transformMoves(mMoves[mCurrPhase],0,mNumMoves, false);
74
      mControl = act.getControl();
75
      mControl.applyScrambles(moves);
76
      }
77
    else
78
      {
79
      mCurrPhase = mNumPhases-1;
80
      mNumMoves = mMoves[mCurrPhase]==null ? 0 : mMoves[mCurrPhase].length;
81
      mCurrMove = mNumMoves;
82
      mControl = act.getControl();
83

    
84
      int[][] moves = transformMoves(mMoves, true);
85
      mControl.applyScrambles(moves);
86
      }
87

    
88
    setText(act);
89
    }
90

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

    
93
  void nextPhase(SolverActivity act)
94
    {
95
    if( mCurrMove<mNumMoves )
96
      {
97
      int[][] moves = transformMoves(mMoves[mCurrPhase],mCurrMove,mNumMoves, true);
98
      mCurrMove = mNumMoves;
99
      mControl = act.getControl();
100
      mControl.applyScrambles(moves);
101
      }
102
    else if( mCurrPhase<mNumPhases-1 )
103
      {
104
      mCurrPhase++;
105
      mNumMoves = mMoves[mCurrPhase]==null ? 0 : mMoves[mCurrPhase].length;
106
      mCurrMove = mNumMoves;
107
      int[][] moves = transformMoves(mMoves[mCurrPhase],0,mNumMoves, true);
108
      mControl = act.getControl();
109
      mControl.applyScrambles(moves);
110
      }
111
    else
112
      {
113
      mCurrPhase = 0;
114
      mNumMoves = mMoves[mCurrPhase]==null ? 0 : mMoves[mCurrPhase].length;
115
      mCurrMove = 0;
116
      mControl = act.getControl();
117
      int[][] moves = transformMoves(mMoves, false);
118
      mControl.applyScrambles(moves);
119
      }
120

    
121
    setText(act);
122
    }
123

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

    
126
  private void setText(SolverActivity act)
127
    {
128
    int currMove = 0;
129
    int totalMove = 0;
130

    
131
    if( mMoves!=null )
132
      {
133
      currMove = mCurrMove;
134
      for(int p=0; p<mCurrPhase; p++) currMove  += (mMoves[p]==null ? 0: mMoves[p].length);
135
      for(int p=0; p<mNumPhases; p++) totalMove += (mMoves[p]==null ? 0: mMoves[p].length);
136
      }
137

    
138
    final int cMove = currMove;
139
    final int tMove = totalMove;
140

    
141
    act.runOnUiThread(new Runnable()
142
      {
143
      @Override
144
      public void run()
145
        {
146
        mPhase.setText(mPhaseNames[mCurrPhase]+" "+mCurrMove+"/"+mNumMoves);
147
        mText.setText(cMove+"/"+tMove);
148
        }
149
      });
150
    }
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153

    
154
  private int[][] transformMoves(int[][] moves, int start, int end, boolean front)
155
    {
156
    int mult = front ? 1:-1;
157
    int len = end-start;
158
    int[][] ret = new int[len][];
159

    
160
    for(int m=0; m<len; m++)
161
      {
162
      int[] mv = moves[front ? start+m : end-1-m];
163
      int[] rt = new int[3];
164
      rt[0] = mv[0];
165
      rt[1] = (1<<mv[1]);
166
      rt[2] = mult*mv[2];
167
      ret[m] = rt;
168
      }
169

    
170
    return ret;
171
    }
172

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

    
175
  private int[][] transformMoves(int[][][] moves, boolean front)
176
    {
177
    int len = moves.length;
178
    int totalLen = 0;
179
    for (int[][] move : moves) totalLen += (move==null ? 0 : move.length);
180

    
181
    int[][] ret = new int[totalLen][];
182
    int mult = front ? 1:-1;
183
    int index = 0;
184

    
185
    for(int m=0; m<len; m++)
186
      {
187
      int[][] mv = moves[front ? m : len-1-m];
188
      int l = (mv==null ? 0 : mv.length);
189

    
190
      for(int p=0; p<l; p++)
191
        {
192
        int[] mve = mv[front ? p : l-1-p];
193
        int[] rt = new int[3];
194
        rt[0] = mve[0];
195
        rt[1] = (1<<mve[1]);
196
        rt[2] = mult*mve[2];
197
        ret[index++] = rt;
198
        }
199
      }
200

    
201
    return ret;
202
    }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

    
206
  void setMoves(SolverActivity act, int[][] moves, int phase)
207
    {
208
    mMoves[phase] = moves;
209
    if( phase==0 ) mNumMoves = (moves==null ? 0 : moves.length);
210
    mCurrPhase = 0;
211
    mCurrMove = 0;
212

    
213
    setText(act);
214
    }
215

    
216
///////////////////////////////////////////////////////////////////////////////////////////////////
217

    
218
  void clearMoves(SolverActivity act)
219
    {
220
    for(int p=0; p<mNumPhases; p++) mMoves[p] = null;
221
    mNumMoves = 0;
222
    mCurrPhase= 0;
223
    mCurrMove = 0;
224

    
225
    setText(act);
226
    }
227

    
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229

    
230
  void backMove(SolverActivity act)
231
    {
232
    if( mMoves!=null && mCanMove )
233
      {
234
      if( mCurrMove>0 )
235
        {
236
        mCanMove = false;
237
        int[] move = mMoves[mCurrPhase][--mCurrMove];
238
        mControl = act.getControl();
239
        mControl.blockTouch(MOVES_PLACE_0);
240
        mControl.addRotation(this, move[0], (1<<move[1]), -move[2], MILLIS_PER_DEGREE);
241
        }
242
      else if( mCurrPhase>0 )
243
        {
244
        mCurrPhase--;
245
        mNumMoves = mMoves[mCurrPhase]==null ? 0 : mMoves[mCurrPhase].length;
246
        mCurrMove = mNumMoves;
247
        }
248
      else
249
        {
250
        mCurrPhase = mNumPhases-1;
251
        mNumMoves = mMoves[mCurrPhase]==null ? 0 : mMoves[mCurrPhase].length;
252
        mCurrMove = mNumMoves;
253
        mControl = act.getControl();
254

    
255
        int[][] moves = transformMoves(mMoves, true);
256
        mControl.applyScrambles(moves);
257
        }
258

    
259
      setText(act);
260
      }
261
    }
262

    
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264

    
265
  void nextMove(SolverActivity act)
266
    {
267
    if( mMoves!=null && mCanMove )
268
      {
269
      if( mCurrMove<mNumMoves )
270
        {
271
        mCanMove = false;
272
        int[] move = mMoves[mCurrPhase][mCurrMove++];
273
        mControl = act.getControl();
274
        mControl.blockTouch(MOVES_PLACE_1);
275
        mControl.addRotation(this, move[0], (1<<move[1]), move[2], MILLIS_PER_DEGREE);
276
        }
277
      else if( mCurrPhase<mNumPhases-1 )
278
        {
279
        mCurrPhase++;
280
        mNumMoves = mMoves[mCurrPhase]==null ? 0 : mMoves[mCurrPhase].length;
281
        mCurrMove = 0;
282
        }
283
      else
284
        {
285
        mCurrPhase = 0;
286
        mNumMoves = mMoves[mCurrPhase]==null ? 0 : mMoves[mCurrPhase].length;
287
        mCurrMove = 0;
288
        mControl = act.getControl();
289

    
290
        int[][] moves = transformMoves(mMoves, false);
291
        mControl.applyScrambles(moves);
292
        }
293

    
294
      setText(act);
295
      }
296
    }
297

    
298
///////////////////////////////////////////////////////////////////////////////////////////////////
299

    
300
  public void onActionFinished(final long effectID)
301
    {
302
    mCanMove = true;
303
    mControl.unblockRotation();
304
    }
305
}
(2-2/5)