Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenSolution.java @ 70688a23

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 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.screens;
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.objectlib.main.ObjectControl;
21
import org.distorted.objectlib.helpers.MovesFinished;
22

    
23
import org.distorted.helpers.TransparentImageButton;
24
import org.distorted.main.R;
25
import org.distorted.main.RubikActivity;
26
import org.distorted.objectlib.patterns.RubikPattern;
27

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

    
30
public class RubikScreenSolution extends RubikScreenAbstract 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

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

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

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

    
51
  void enterScreen(final RubikActivity act)
52
    {
53
    float width = act.getScreenWidthInPixels();
54
    mButtonSize = width*RubikActivity.BUTTON_TEXT_SIZE;
55
    float titleSize  = width*RubikActivity.TITLE_TEXT_SIZE;
56

    
57
    LayoutInflater inflater = act.getLayoutInflater();
58

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

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

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

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

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

    
83
    setupPrevButton(act);
84
    setupNextButton(act);
85
    setupTextView(act,width);
86

    
87
    layoutLeft.addView(mPrevButton);
88
    layoutLeft.addView(mMovesText);
89
    layoutLeft.addView(mNextButton);
90

    
91
    setupBackButton(act);
92

    
93
    layoutRight.addView(mBackButton);
94

    
95
    layoutBot.addView(layoutLeft);
96
    layoutBot.addView(layoutMid);
97
    layoutBot.addView(layoutRight);
98
    }
99

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101

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

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

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

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

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

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

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

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

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

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

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

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177

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

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

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

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

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

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

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

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

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

    
257
    RubikPattern.parseMoves(mMoves,mNumMoves,moves);
258

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

    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263

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

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

    
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275

    
276
  public void savePreferences(SharedPreferences.Editor editor)
277
    {
278

    
279
    }
280

    
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282

    
283
  public void restorePreferences(SharedPreferences preferences)
284
    {
285

    
286
    }
287

    
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289

    
290
  public void onActionFinished(final long effectID)
291
    {
292
    mCanRotate = true;
293
    }
294
  }
(7-7/10)