Project

General

Profile

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

magiccube / src / main / java / org / distorted / solverui / ScreenSolutionSinglephased.java @ 8477cf44

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2023 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.solverui;
11

    
12
import android.content.SharedPreferences;
13
import android.util.TypedValue;
14
import android.view.Gravity;
15
import android.view.LayoutInflater;
16
import android.view.View;
17
import android.widget.LinearLayout;
18
import android.widget.TextView;
19

    
20
import org.distorted.helpers.TransparentImageButton;
21
import org.distorted.main.R;
22
import org.distorted.objectlib.helpers.MovesFinished;
23
import org.distorted.objectlib.main.ObjectControl;
24
import org.distorted.objectlib.patterns.RubikPattern;
25

    
26
import java.lang.ref.WeakReference;
27

    
28
///////////////////////////////////////////////////////////////////////////////////////////////////
29

    
30
public class ScreenSolutionSinglephased extends ScreenAbstract implements MovesFinished
31
  {
32
  private static final int MILLIS_PER_DEGREE = 6;
33

    
34
  private TransparentImageButton mPrevButton, mNextButton, mBackButton;
35
  private TextView mMovesText;
36
  private int[][] mMoves;
37
  private int mCurrMove, mNumMoves;
38
  private boolean mCanRotate;
39
  private float mButtonSize;
40
  private WeakReference<SolverActivity> mAct;
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

    
44
  void leaveScreen(SolverActivity act)
45
    {
46
    ObjectControl control = act.getControl();
47
    control.solveOnly();
48
    }
49

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
  void enterScreen(final SolverActivity act)
53
    {
54
    mAct = new WeakReference<>(act);
55

    
56
    float width = act.getScreenWidthInPixels();
57
    mButtonSize = width*SolverActivity.BUTTON_TEXT_SIZE;
58
    float titleSize  = width*SolverActivity.TITLE_TEXT_SIZE;
59

    
60
    LayoutInflater inflater = act.getLayoutInflater();
61

    
62
    // TOP ////////////////////////////
63
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
64
    layoutTop.removeAllViews();
65

    
66
    final TextView text = (TextView)inflater.inflate(R.layout.upper_text, null);
67
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
68
    text.setText(R.string.solution);
69
    layoutTop.addView(text);
70

    
71
    // BOT ////////////////////////////
72
    LinearLayout layoutBot = act.findViewById(R.id.lowerBar);
73
    layoutBot.removeAllViews();
74

    
75
    LinearLayout.LayoutParams paramsL = new LinearLayout.LayoutParams((int)(width/2),LinearLayout.LayoutParams.MATCH_PARENT);
76
    LinearLayout.LayoutParams paramsM = new LinearLayout.LayoutParams((int)(width/6),LinearLayout.LayoutParams.MATCH_PARENT);
77
    LinearLayout.LayoutParams paramsR = new LinearLayout.LayoutParams((int)(width/3),LinearLayout.LayoutParams.MATCH_PARENT);
78

    
79
    LinearLayout layoutLeft = new LinearLayout(act);
80
    layoutLeft.setLayoutParams(paramsL);
81
    LinearLayout layoutMid = new LinearLayout(act);
82
    layoutMid.setLayoutParams(paramsM);
83
    LinearLayout layoutRight = new LinearLayout(act);
84
    layoutRight.setLayoutParams(paramsR);
85

    
86
    setupPrevButton(act);
87
    setupNextButton(act);
88
    setupTextView(act,width);
89

    
90
    layoutLeft.addView(mPrevButton);
91
    layoutLeft.addView(mMovesText);
92
    layoutLeft.addView(mNextButton);
93

    
94
    setupBackButton(act);
95

    
96
    layoutRight.addView(mBackButton);
97

    
98
    layoutBot.addView(layoutLeft);
99
    layoutBot.addView(layoutMid);
100
    layoutBot.addView(layoutRight);
101
    }
102

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104

    
105
  private void setupPrevButton(final SolverActivity act)
106
    {
107
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
108
    mPrevButton = new TransparentImageButton(act,R.drawable.ui_left,params);
109

    
110
    mPrevButton.setOnClickListener( new View.OnClickListener()
111
      {
112
      @Override
113
      public void onClick(View v)
114
        {
115
        ObjectControl control = act.getControl();
116
        backMove(control);
117
        mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
118
        }
119
      });
120
    }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

    
124
  private void setupNextButton(final SolverActivity act)
125
    {
126
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
127
    mNextButton = new TransparentImageButton(act,R.drawable.ui_right,params);
128

    
129
    mNextButton.setOnClickListener( new View.OnClickListener()
130
      {
131
      @Override
132
      public void onClick(View v)
133
        {
134
        ObjectControl control = act.getControl();
135
        makeMove(control);
136
        mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
137
        }
138
      });
139
    }
140

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

    
143
  private void setupTextView(final SolverActivity act, final float width)
144
    {
145
    int padding = (int)(width*SolverActivity.PADDING);
146
    int margin  = (int)(width*SolverActivity.SMALL_MARGIN);
147
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,2.0f);
148
    params.topMargin    = margin;
149
    params.bottomMargin = margin;
150
    params.leftMargin   = margin;
151
    params.rightMargin  = margin;
152

    
153
    mMovesText = new TextView(act);
154
    mMovesText.setTextSize(20);
155
    mMovesText.setLayoutParams(params);
156
    mMovesText.setPadding(padding,0,padding,0);
157
    mMovesText.setGravity(Gravity.CENTER);
158
    mMovesText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
159
    mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
160
    }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163

    
164
  private void setupBackButton(final SolverActivity act)
165
    {
166
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
167
    mBackButton = new TransparentImageButton(act,R.drawable.ui_back,params);
168

    
169
    mBackButton.setOnClickListener( new View.OnClickListener()
170
      {
171
      @Override
172
      public void onClick(View v)
173
        {
174
        ScreenList.goBack(act);
175
        }
176
      });
177
    }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

    
181
  private void makeMove(ObjectControl control)
182
    {
183
    if( mCanRotate )
184
      {
185
      mCurrMove++;
186

    
187
      if( mCurrMove>mNumMoves )
188
        {
189
        mCurrMove= 0;
190
        control.initializeObject(null);
191
        }
192
      else
193
        {
194
        int axis      = mMoves[mCurrMove-1][0];
195
		int rowBitmap = mMoves[mCurrMove-1][1];
196
		int bareAngle = mMoves[mCurrMove-1][2];
197

    
198
        if( bareAngle!=0 )
199
          {
200
          mCanRotate = false;
201
          control.addRotation(this, axis, rowBitmap, bareAngle, MILLIS_PER_DEGREE);
202
          }
203
        else
204
          {
205
          android.util.Log.e("solution", "error: solution contains angle 0");
206
          }
207
        }
208
      }
209
    else
210
      {
211
      android.util.Log.e("solution", "failed to make move!");
212
      }
213
    }
214

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

    
217
  private void backMove(ObjectControl control)
218
    {
219
    if( mCanRotate )
220
      {
221
      mCurrMove--;
222

    
223
      if( mCurrMove<0 )
224
        {
225
        mCurrMove=mNumMoves;
226
        control.initializeObject(mMoves);
227
        }
228
      else
229
        {
230
        int axis      = mMoves[mCurrMove][0];
231
		int rowBitmap = mMoves[mCurrMove][1];
232
		int bareAngle = mMoves[mCurrMove][2];
233

    
234
        if( bareAngle!=0 )
235
          {
236
          mCanRotate = false;
237
          control.addRotation(this, axis, rowBitmap, -bareAngle, MILLIS_PER_DEGREE);
238
          }
239
        else
240
          {
241
          android.util.Log.e("solution", "error: solution contains angle 0");
242
          }
243
        }
244
      }
245
    else
246
      {
247
      android.util.Log.e("solution", "failed to back move!");
248
      }
249
    }
250

    
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252

    
253
  void setSolution(String moves)
254
    {
255
    mCanRotate= true;
256
    mCurrMove = 0;
257
    mNumMoves = moves.length()/4;
258
    mMoves    = new int[mNumMoves][3];
259

    
260
    RubikPattern.parseMoves(mMoves,mNumMoves,moves);
261

    
262
    SolverActivity act = mAct.get();
263
    mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
264
    }
265

    
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267

    
268
  void setSolution(int[][] moves)
269
    {
270
    mCanRotate= true;
271
    mCurrMove = 0;
272
    mNumMoves = moves==null ? 0 : moves.length;
273
    mMoves    = moves;
274

    
275
    SolverActivity act = mAct.get();
276
    mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
277
    }
278

    
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280

    
281
  public void savePreferences(SharedPreferences.Editor editor) { }
282
  public void restorePreferences(SharedPreferences preferences) { }
283
  public void onActionFinished(final long effectID) { mCanRotate = true; }
284
  }
(5-5/9)