Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenSolution.java @ 88a3e972

1 264af0ad Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20 fcd5b990 Leszek Koltunski
package org.distorted.screens;
21 264af0ad Leszek Koltunski
22
import android.content.SharedPreferences;
23 e3c74c0f Leszek Koltunski
import android.util.TypedValue;
24 264af0ad Leszek Koltunski
import android.view.Gravity;
25
import android.view.LayoutInflater;
26
import android.view.View;
27
import android.widget.ImageButton;
28
import android.widget.LinearLayout;
29
import android.widget.TextView;
30
31 3f7a4363 Leszek Koltunski
import org.distorted.objectlib.main.TwistyObject;
32
33 88a3e972 Leszek Koltunski
import org.distorted.objectlib.helpers.MovesFinished;
34 55e6be1d Leszek Koltunski
import org.distorted.helpers.TransparentImageButton;
35 1f9772f3 Leszek Koltunski
import org.distorted.main.R;
36
import org.distorted.main.RubikActivity;
37 5a4d4fba Leszek Koltunski
import org.distorted.main.RubikPreRender;
38 7f84a768 Leszek Koltunski
import org.distorted.patterns.RubikPattern;
39 264af0ad Leszek Koltunski
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41
42 55e6be1d Leszek Koltunski
public class RubikScreenSolution extends RubikScreenAbstract implements MovesFinished
43 264af0ad Leszek Koltunski
  {
44 0594c61f Leszek Koltunski
  private static final int MILLIS_PER_DEGREE = 6;
45 7f84a768 Leszek Koltunski
46 4fb1fc0d Leszek Koltunski
  private ImageButton mPrevButton, mNextButton, mBackButton;
47 264af0ad Leszek Koltunski
  private TextView mMovesText;
48 7f84a768 Leszek Koltunski
  private int[][] mMoves;
49
  private int mCurrMove, mNumMoves;
50
  private boolean mCanRotate;
51 da768c35 Leszek Koltunski
  private float mButtonSize;
52 264af0ad Leszek Koltunski
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54
55 f5da732a Leszek Koltunski
  void leaveScreen(RubikActivity act)
56 264af0ad Leszek Koltunski
    {
57 9c2f0c91 Leszek Koltunski
    TwistyObject object = act.getObject();
58 1f9772f3 Leszek Koltunski
    object.solve();
59 264af0ad Leszek Koltunski
    }
60
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62
63 f5da732a Leszek Koltunski
  void enterScreen(final RubikActivity act)
64 264af0ad Leszek Koltunski
    {
65 e3c74c0f Leszek Koltunski
    float width = act.getScreenWidthInPixels();
66
    mButtonSize = width*RubikActivity.BUTTON_TEXT_SIZE;
67 da768c35 Leszek Koltunski
    float titleSize  = width*RubikActivity.TITLE_TEXT_SIZE;
68 e3c74c0f Leszek Koltunski
69 264af0ad Leszek Koltunski
    LayoutInflater inflater = act.getLayoutInflater();
70
71
    // TOP ////////////////////////////
72
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
73
    layoutTop.removeAllViews();
74
75
    final TextView text = (TextView)inflater.inflate(R.layout.upper_text, null);
76 da768c35 Leszek Koltunski
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
77 264af0ad Leszek Koltunski
    text.setText(R.string.solution);
78
    layoutTop.addView(text);
79
80
    // BOT ////////////////////////////
81 92843d3b Leszek Koltunski
    LinearLayout layoutBot = act.findViewById(R.id.lowerBar);
82
    layoutBot.removeAllViews();
83
84 af133d41 Leszek Koltunski
    LinearLayout.LayoutParams paramsL = new LinearLayout.LayoutParams((int)(width/2),LinearLayout.LayoutParams.MATCH_PARENT);
85
    LinearLayout.LayoutParams paramsM = new LinearLayout.LayoutParams((int)(width/6),LinearLayout.LayoutParams.MATCH_PARENT);
86
    LinearLayout.LayoutParams paramsR = new LinearLayout.LayoutParams((int)(width/3),LinearLayout.LayoutParams.MATCH_PARENT);
87
88
    LinearLayout layoutLeft = new LinearLayout(act);
89
    layoutLeft.setLayoutParams(paramsL);
90
    LinearLayout layoutMid = new LinearLayout(act);
91
    layoutMid.setLayoutParams(paramsM);
92
    LinearLayout layoutRight = new LinearLayout(act);
93
    layoutRight.setLayoutParams(paramsR);
94 92843d3b Leszek Koltunski
95 ad0c8e0e Leszek Koltunski
    setupPrevButton(act,width);
96
    setupNextButton(act,width);
97
    setupTextView(act,width);
98 264af0ad Leszek Koltunski
99
    layoutLeft.addView(mPrevButton);
100
    layoutLeft.addView(mMovesText);
101
    layoutLeft.addView(mNextButton);
102
103 ad0c8e0e Leszek Koltunski
    setupBackButton(act,width);
104 264af0ad Leszek Koltunski
105
    layoutRight.addView(mBackButton);
106 92843d3b Leszek Koltunski
107
    layoutBot.addView(layoutLeft);
108 af133d41 Leszek Koltunski
    layoutBot.addView(layoutMid);
109 92843d3b Leszek Koltunski
    layoutBot.addView(layoutRight);
110 264af0ad Leszek Koltunski
    }
111
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113
114 ad0c8e0e Leszek Koltunski
  private void setupPrevButton(final RubikActivity act, final float width)
115 264af0ad Leszek Koltunski
    {
116 31a9f38d Leszek Koltunski
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_left,R.drawable.ui_medium_left, R.drawable.ui_big_left, R.drawable.ui_huge_left);
117 da768c35 Leszek Koltunski
    mPrevButton = new TransparentImageButton(act,icon,width,0);
118 264af0ad Leszek Koltunski
119
    mPrevButton.setOnClickListener( new View.OnClickListener()
120
      {
121
      @Override
122
      public void onClick(View v)
123
        {
124 5a4d4fba Leszek Koltunski
        RubikPreRender pre = act.getPreRender();
125
        backMove(pre);
126 7f84a768 Leszek Koltunski
        mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
127 264af0ad Leszek Koltunski
        }
128
      });
129
    }
130
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132
133 ad0c8e0e Leszek Koltunski
  private void setupNextButton(final RubikActivity act, final float width)
134 264af0ad Leszek Koltunski
    {
135 31a9f38d Leszek Koltunski
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_right,R.drawable.ui_medium_right, R.drawable.ui_big_right, R.drawable.ui_huge_right);
136 da768c35 Leszek Koltunski
    mNextButton = new TransparentImageButton(act,icon,width,0);
137 264af0ad Leszek Koltunski
138
    mNextButton.setOnClickListener( new View.OnClickListener()
139
      {
140
      @Override
141
      public void onClick(View v)
142
        {
143 5a4d4fba Leszek Koltunski
        RubikPreRender pre = act.getPreRender();
144
        makeMove(pre);
145 7f84a768 Leszek Koltunski
        mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
146 264af0ad Leszek Koltunski
        }
147
      });
148
    }
149
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151
152 ad0c8e0e Leszek Koltunski
  private void setupTextView(final RubikActivity act, final float width)
153 264af0ad Leszek Koltunski
    {
154 ad0c8e0e Leszek Koltunski
    int padding = (int)(width*RubikActivity.PADDING);
155
    int margin  = (int)(width*RubikActivity.MARGIN);
156 264af0ad Leszek Koltunski
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,2.0f);
157 ad0c8e0e Leszek Koltunski
    params.topMargin    = margin;
158
    params.bottomMargin = margin;
159
    params.leftMargin   = margin;
160
    params.rightMargin  = margin;
161 264af0ad Leszek Koltunski
162
    mMovesText = new TextView(act);
163
    mMovesText.setTextSize(20);
164
    mMovesText.setLayoutParams(params);
165
    mMovesText.setPadding(padding,0,padding,0);
166
    mMovesText.setGravity(Gravity.CENTER);
167 e3c74c0f Leszek Koltunski
    mMovesText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
168 7f84a768 Leszek Koltunski
    mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
169 264af0ad Leszek Koltunski
    }
170
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172
173 ad0c8e0e Leszek Koltunski
  private void setupBackButton(final RubikActivity act, final float width)
174 264af0ad Leszek Koltunski
    {
175 4fb1fc0d Leszek Koltunski
    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);
176 da768c35 Leszek Koltunski
    mBackButton = new TransparentImageButton(act, icon, width, LinearLayout.LayoutParams.MATCH_PARENT);
177 264af0ad Leszek Koltunski
178
    mBackButton.setOnClickListener( new View.OnClickListener()
179
      {
180
      @Override
181
      public void onClick(View v)
182
        {
183 fcd5b990 Leszek Koltunski
        ScreenList.goBack(act);
184 264af0ad Leszek Koltunski
        }
185
      });
186
    }
187
188 7f84a768 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
189
190 5a4d4fba Leszek Koltunski
  private void makeMove(RubikPreRender pre)
191 7f84a768 Leszek Koltunski
    {
192
    if( mCanRotate )
193
      {
194
      mCurrMove++;
195
196
      if( mCurrMove>mNumMoves )
197
        {
198
        mCurrMove= 0;
199 5a4d4fba Leszek Koltunski
        pre.initializeObject(null);
200 7f84a768 Leszek Koltunski
        }
201
      else
202
        {
203 925ed78f Leszek Koltunski
        int axis      = mMoves[mCurrMove-1][0];
204
		    int rowBitmap = mMoves[mCurrMove-1][1];
205
		    int bareAngle = mMoves[mCurrMove-1][2];
206
		    int basicAngle= pre.getObject().getBasicAngle()[axis];
207
        int angle     = bareAngle*(360/basicAngle);
208 0594c61f Leszek Koltunski
        int duration  = Math.abs(angle)*MILLIS_PER_DEGREE;
209 7f84a768 Leszek Koltunski
210
        if( angle!=0 )
211
          {
212
          mCanRotate = false;
213 0594c61f Leszek Koltunski
          pre.addRotation(this, axis, rowBitmap, angle, duration);
214 7f84a768 Leszek Koltunski
          }
215
        else
216
          {
217
          android.util.Log.e("solution", "error: solution contains angle 0");
218
          }
219
        }
220
      }
221
    else
222
      {
223
      android.util.Log.e("solution", "failed to make move!");
224
      }
225
    }
226
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228
229 5a4d4fba Leszek Koltunski
  private void backMove(RubikPreRender pre)
230 7f84a768 Leszek Koltunski
    {
231
    if( mCanRotate )
232
      {
233
      mCurrMove--;
234
235
      if( mCurrMove<0 )
236
        {
237
        mCurrMove=mNumMoves;
238 5a4d4fba Leszek Koltunski
        pre.initializeObject(mMoves);
239 7f84a768 Leszek Koltunski
        }
240
      else
241
        {
242 925ed78f Leszek Koltunski
        int axis      = mMoves[mCurrMove][0];
243
		    int rowBitmap = mMoves[mCurrMove][1];
244
		    int bareAngle = mMoves[mCurrMove][2];
245
        int basicAngle= pre.getObject().getBasicAngle()[axis];
246
        int angle     = bareAngle*(360/basicAngle);
247 0594c61f Leszek Koltunski
        int duration  = Math.abs(angle)*MILLIS_PER_DEGREE;
248 7f84a768 Leszek Koltunski
249
        if( angle!=0 )
250
          {
251
          mCanRotate = false;
252 0594c61f Leszek Koltunski
          pre.addRotation(this, axis, rowBitmap, -angle, duration);
253 7f84a768 Leszek Koltunski
          }
254
        else
255
          {
256
          android.util.Log.e("solution", "error: solution contains angle 0");
257
          }
258
        }
259
      }
260
    else
261
      {
262
      android.util.Log.e("solution", "failed to back move!");
263
      }
264
    }
265
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267
268 373fa45f Leszek Koltunski
  void setupMoves(final RubikActivity act, String moves)
269 7f84a768 Leszek Koltunski
    {
270
    mCanRotate= true;
271
    mCurrMove = 0;
272 373fa45f Leszek Koltunski
    mNumMoves = moves.length()/4;
273 7f84a768 Leszek Koltunski
    mMoves    = new int[mNumMoves][3];
274
275 373fa45f Leszek Koltunski
    RubikPattern.parseMoves(mMoves,mNumMoves,moves);
276 7f84a768 Leszek Koltunski
277
    mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
278
    }
279
280 264af0ad Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
281
282
  public void savePreferences(SharedPreferences.Editor editor)
283
    {
284 4f36e418 Leszek Koltunski
285 264af0ad Leszek Koltunski
    }
286
287
///////////////////////////////////////////////////////////////////////////////////////////////////
288
289
  public void restorePreferences(SharedPreferences preferences)
290
    {
291
292
    }
293 7f84a768 Leszek Koltunski
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295
296
  public void onActionFinished(final long effectID)
297
    {
298
    mCanRotate = true;
299
    }
300 264af0ad Leszek Koltunski
  }