Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikStateSolution.java @ eaf87d1d

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