Project

General

Profile

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

magiccube / src / main / java / org / distorted / solverui / ScreenSolution.java @ 99c2e327

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

    
28
public class ScreenSolution extends ScreenAbstract implements MovesFinished
29
  {
30
  private static final int MILLIS_PER_DEGREE = 6;
31

    
32
  private TransparentImageButton mPrevButton, mNextButton, mBackButton;
33
  private TextView mMovesText;
34
  private int[][] mMoves;
35
  private int mCurrMove, mNumMoves;
36
  private boolean mCanRotate;
37
  private float mButtonSize;
38

    
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40

    
41
  void leaveScreen(SolverActivity act)
42
    {
43
    ObjectControl control = act.getControl();
44
    control.solveOnly();
45
    }
46

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

    
49
  void enterScreen(final SolverActivity act)
50
    {
51
    float width = act.getScreenWidthInPixels();
52
    mButtonSize = width*SolverActivity.BUTTON_TEXT_SIZE;
53
    float titleSize  = width*SolverActivity.TITLE_TEXT_SIZE;
54

    
55
    LayoutInflater inflater = act.getLayoutInflater();
56

    
57
    // TOP ////////////////////////////
58
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
59
    layoutTop.removeAllViews();
60

    
61
    final TextView text = (TextView)inflater.inflate(R.layout.upper_text, null);
62
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
63
    text.setText(R.string.solution);
64
    layoutTop.addView(text);
65

    
66
    // BOT ////////////////////////////
67
    LinearLayout layoutBot = act.findViewById(R.id.lowerBar);
68
    layoutBot.removeAllViews();
69

    
70
    LinearLayout.LayoutParams paramsL = new LinearLayout.LayoutParams((int)(width/2),LinearLayout.LayoutParams.MATCH_PARENT);
71
    LinearLayout.LayoutParams paramsM = new LinearLayout.LayoutParams((int)(width/6),LinearLayout.LayoutParams.MATCH_PARENT);
72
    LinearLayout.LayoutParams paramsR = new LinearLayout.LayoutParams((int)(width/3),LinearLayout.LayoutParams.MATCH_PARENT);
73

    
74
    LinearLayout layoutLeft = new LinearLayout(act);
75
    layoutLeft.setLayoutParams(paramsL);
76
    LinearLayout layoutMid = new LinearLayout(act);
77
    layoutMid.setLayoutParams(paramsM);
78
    LinearLayout layoutRight = new LinearLayout(act);
79
    layoutRight.setLayoutParams(paramsR);
80

    
81
    setupPrevButton(act);
82
    setupNextButton(act);
83
    setupTextView(act,width);
84

    
85
    layoutLeft.addView(mPrevButton);
86
    layoutLeft.addView(mMovesText);
87
    layoutLeft.addView(mNextButton);
88

    
89
    setupBackButton(act);
90

    
91
    layoutRight.addView(mBackButton);
92

    
93
    layoutBot.addView(layoutLeft);
94
    layoutBot.addView(layoutMid);
95
    layoutBot.addView(layoutRight);
96
    }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

    
100
  private void setupPrevButton(final SolverActivity act)
101
    {
102
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
103
    mPrevButton = new TransparentImageButton(act,R.drawable.ui_left,params);
104

    
105
    mPrevButton.setOnClickListener( new View.OnClickListener()
106
      {
107
      @Override
108
      public void onClick(View v)
109
        {
110
        ObjectControl control = act.getControl();
111
        backMove(control);
112
        mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
113
        }
114
      });
115
    }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

    
119
  private void setupNextButton(final SolverActivity act)
120
    {
121
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
122
    mNextButton = new TransparentImageButton(act,R.drawable.ui_right,params);
123

    
124
    mNextButton.setOnClickListener( new View.OnClickListener()
125
      {
126
      @Override
127
      public void onClick(View v)
128
        {
129
        ObjectControl control = act.getControl();
130
        makeMove(control);
131
        mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
132
        }
133
      });
134
    }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137

    
138
  private void setupTextView(final SolverActivity act, final float width)
139
    {
140
    int padding = (int)(width*SolverActivity.PADDING);
141
    int margin  = (int)(width*SolverActivity.SMALL_MARGIN);
142
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,2.0f);
143
    params.topMargin    = margin;
144
    params.bottomMargin = margin;
145
    params.leftMargin   = margin;
146
    params.rightMargin  = margin;
147

    
148
    mMovesText = new TextView(act);
149
    mMovesText.setTextSize(20);
150
    mMovesText.setLayoutParams(params);
151
    mMovesText.setPadding(padding,0,padding,0);
152
    mMovesText.setGravity(Gravity.CENTER);
153
    mMovesText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
154
    mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
155
    }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

    
159
  private void setupBackButton(final SolverActivity act)
160
    {
161
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
162
    mBackButton = new TransparentImageButton(act,R.drawable.ui_back,params);
163

    
164
    mBackButton.setOnClickListener( new View.OnClickListener()
165
      {
166
      @Override
167
      public void onClick(View v)
168
        {
169
        ScreenList.goBack(act);
170
        }
171
      });
172
    }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175

    
176
  private void makeMove(ObjectControl control)
177
    {
178
    if( mCanRotate )
179
      {
180
      mCurrMove++;
181

    
182
      if( mCurrMove>mNumMoves )
183
        {
184
        mCurrMove= 0;
185
        control.initializeObject(null);
186
        }
187
      else
188
        {
189
        int axis      = mMoves[mCurrMove-1][0];
190
		    int rowBitmap = mMoves[mCurrMove-1][1];
191
		    int bareAngle = mMoves[mCurrMove-1][2];
192

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

    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

    
212
  private void backMove(ObjectControl control)
213
    {
214
    if( mCanRotate )
215
      {
216
      mCurrMove--;
217

    
218
      if( mCurrMove<0 )
219
        {
220
        mCurrMove=mNumMoves;
221
        control.initializeObject(mMoves);
222
        }
223
      else
224
        {
225
        int axis      = mMoves[mCurrMove][0];
226
		    int rowBitmap = mMoves[mCurrMove][1];
227
		    int bareAngle = mMoves[mCurrMove][2];
228

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

    
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247

    
248
  void setupMoves(final SolverActivity act, String moves)
249
    {
250
    mCanRotate= true;
251
    mCurrMove = 0;
252
    mNumMoves = moves.length()/4;
253
    mMoves    = new int[mNumMoves][3];
254

    
255
    RubikPattern.parseMoves(mMoves,mNumMoves,moves);
256

    
257
    mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
258
    }
259

    
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261

    
262
  void setupMoves(final SolverActivity act, int[][] moves)
263
    {
264
    mCanRotate= true;
265
    mCurrMove = 0;
266
    mNumMoves = moves==null ? 0 : moves.length;
267
    mMoves    = moves;
268

    
269
    mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
270
    }
271

    
272
///////////////////////////////////////////////////////////////////////////////////////////////////
273

    
274
  public void savePreferences(SharedPreferences.Editor editor)
275
    {
276

    
277
    }
278

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

    
281
  public void restorePreferences(SharedPreferences preferences)
282
    {
283

    
284
    }
285

    
286
///////////////////////////////////////////////////////////////////////////////////////////////////
287

    
288
  public void onActionFinished(final long effectID)
289
    {
290
    mCanRotate = true;
291
    }
292
  }
(3-3/8)