Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenSolution.java @ 1ba56d95

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.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
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_left,R.drawable.ui_medium_left, R.drawable.ui_big_left, R.drawable.ui_huge_left);
105
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
106
    mPrevButton = new TransparentImageButton(act,icon,TransparentImageButton.GRAVITY_MIDDLE,params);
107

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

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

    
122
  private void setupNextButton(final RubikActivity act)
123
    {
124
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_right,R.drawable.ui_medium_right, R.drawable.ui_big_right, R.drawable.ui_huge_right);
125
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
126
    mNextButton = new TransparentImageButton(act,icon,TransparentImageButton.GRAVITY_MIDDLE,params);
127

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

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141

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

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

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162

    
163
  private void setupBackButton(final RubikActivity act)
164
    {
165
    final int icon = RubikActivity.getDrawable(R.drawable.ui_small_back,R.drawable.ui_medium_back, R.drawable.ui_big_back, R.drawable.ui_huge_back);
166
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
167
    mBackButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE,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 setupMoves(final RubikActivity act, 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
    mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
263
    }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266

    
267
  public void savePreferences(SharedPreferences.Editor editor)
268
    {
269

    
270
    }
271

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

    
274
  public void restorePreferences(SharedPreferences preferences)
275
    {
276

    
277
    }
278

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

    
281
  public void onActionFinished(final long effectID)
282
    {
283
    mCanRotate = true;
284
    }
285
  }
(7-7/10)