Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenSolution.java @ f4f784ad

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 2afc6754 Leszek Koltunski
import org.distorted.objectlib.main.ObjectControl;
32 3f7a4363 Leszek Koltunski
import org.distorted.objectlib.main.TwistyObject;
33 88a3e972 Leszek Koltunski
import org.distorted.objectlib.helpers.MovesFinished;
34 2afc6754 Leszek Koltunski
35 55e6be1d Leszek Koltunski
import org.distorted.helpers.TransparentImageButton;
36 1f9772f3 Leszek Koltunski
import org.distorted.main.R;
37
import org.distorted.main.RubikActivity;
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 f4f784ad Leszek Koltunski
    ObjectControl control = act.getControl();
58
    control.solveOnly();
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 2afc6754 Leszek Koltunski
        ObjectControl control = act.getControl();
125
        backMove(control);
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 2afc6754 Leszek Koltunski
        ObjectControl control = act.getControl();
144
        makeMove(control);
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 2afc6754 Leszek Koltunski
  private void makeMove(ObjectControl control)
191 7f84a768 Leszek Koltunski
    {
192
    if( mCanRotate )
193
      {
194
      mCurrMove++;
195
196
      if( mCurrMove>mNumMoves )
197
        {
198
        mCurrMove= 0;
199 2afc6754 Leszek Koltunski
        control.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 7f84a768 Leszek Koltunski
207 e4f656d1 Leszek Koltunski
        if( bareAngle!=0 )
208 7f84a768 Leszek Koltunski
          {
209
          mCanRotate = false;
210 e4f656d1 Leszek Koltunski
          control.addRotation(this, axis, rowBitmap, bareAngle, MILLIS_PER_DEGREE);
211 7f84a768 Leszek Koltunski
          }
212
        else
213
          {
214
          android.util.Log.e("solution", "error: solution contains angle 0");
215
          }
216
        }
217
      }
218
    else
219
      {
220
      android.util.Log.e("solution", "failed to make move!");
221
      }
222
    }
223
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225
226 2afc6754 Leszek Koltunski
  private void backMove(ObjectControl control)
227 7f84a768 Leszek Koltunski
    {
228
    if( mCanRotate )
229
      {
230
      mCurrMove--;
231
232
      if( mCurrMove<0 )
233
        {
234
        mCurrMove=mNumMoves;
235 2afc6754 Leszek Koltunski
        control.initializeObject(mMoves);
236 7f84a768 Leszek Koltunski
        }
237
      else
238
        {
239 925ed78f Leszek Koltunski
        int axis      = mMoves[mCurrMove][0];
240
		    int rowBitmap = mMoves[mCurrMove][1];
241
		    int bareAngle = mMoves[mCurrMove][2];
242 7f84a768 Leszek Koltunski
243 e4f656d1 Leszek Koltunski
        if( bareAngle!=0 )
244 7f84a768 Leszek Koltunski
          {
245
          mCanRotate = false;
246 e4f656d1 Leszek Koltunski
          control.addRotation(this, axis, rowBitmap, -bareAngle, MILLIS_PER_DEGREE);
247 7f84a768 Leszek Koltunski
          }
248
        else
249
          {
250
          android.util.Log.e("solution", "error: solution contains angle 0");
251
          }
252
        }
253
      }
254
    else
255
      {
256
      android.util.Log.e("solution", "failed to back move!");
257
      }
258
    }
259
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261
262 373fa45f Leszek Koltunski
  void setupMoves(final RubikActivity act, String moves)
263 7f84a768 Leszek Koltunski
    {
264
    mCanRotate= true;
265
    mCurrMove = 0;
266 373fa45f Leszek Koltunski
    mNumMoves = moves.length()/4;
267 7f84a768 Leszek Koltunski
    mMoves    = new int[mNumMoves][3];
268
269 373fa45f Leszek Koltunski
    RubikPattern.parseMoves(mMoves,mNumMoves,moves);
270 7f84a768 Leszek Koltunski
271
    mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
272
    }
273
274 264af0ad Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
275
276
  public void savePreferences(SharedPreferences.Editor editor)
277
    {
278 4f36e418 Leszek Koltunski
279 264af0ad Leszek Koltunski
    }
280
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282
283
  public void restorePreferences(SharedPreferences preferences)
284
    {
285
286
    }
287 7f84a768 Leszek Koltunski
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289
290
  public void onActionFinished(final long effectID)
291
    {
292
    mCanRotate = true;
293
    }
294 264af0ad Leszek Koltunski
  }