Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenSolution.java @ 588ace55

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