Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
package org.distorted.states;
21

    
22
import android.content.SharedPreferences;
23
import android.util.TypedValue;
24
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
import org.distorted.main.R;
32
import org.distorted.main.RubikActivity;
33
import org.distorted.main.RubikPreRender;
34
import org.distorted.objects.TwistyObject;
35
import org.distorted.patterns.RubikPattern;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

    
39
public class RubikStateSolution extends RubikStateAbstract implements RubikPreRender.ActionFinishedListener
40
  {
41
  private static final int DURATION_MILLIS = 750;
42

    
43
  private ImageButton mPrevButton, mNextButton, mBackButton;
44
  private TextView mMovesText;
45
  private int[][] mMoves;
46
  private int mCurrMove, mNumMoves;
47
  private boolean mCanRotate;
48
  private float mButtonSize, mTitleSize;
49

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
  void leaveState(RubikActivity act)
53
    {
54
    TwistyObject object = act.getObject();
55
    object.solve();
56
    }
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
  void enterState(final RubikActivity act)
61
    {
62
    float width = act.getScreenWidthInPixels();
63
    mButtonSize = width*RubikActivity.BUTTON_TEXT_SIZE;
64
    mTitleSize  = width*RubikActivity.TITLE_TEXT_SIZE;
65

    
66
    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
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTitleSize);
74
    text.setText(R.string.solution);
75
    layoutTop.addView(text);
76

    
77
    // BOT ////////////////////////////
78
    LinearLayout layoutBot = act.findViewById(R.id.lowerBar);
79
    layoutBot.removeAllViews();
80

    
81
    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

    
92
    setupPrevButton(act,width);
93
    setupNextButton(act,width);
94
    setupTextView(act,width);
95

    
96
    layoutLeft.addView(mPrevButton);
97
    layoutLeft.addView(mMovesText);
98
    layoutLeft.addView(mNextButton);
99

    
100
    setupBackButton(act,width);
101

    
102
    layoutRight.addView(mBackButton);
103

    
104
    layoutBot.addView(layoutLeft);
105
    layoutBot.addView(layoutMid);
106
    layoutBot.addView(layoutRight);
107
    }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

    
111
  private void setupPrevButton(final RubikActivity act, final float width)
112
    {
113
    int padding = (int)(width*RubikActivity.PADDING);
114
    int margin  = (int)(width*RubikActivity.MARGIN);
115
    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

    
117
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
118
    params.topMargin    = margin;
119
    params.bottomMargin = margin;
120
    params.leftMargin   = margin;
121
    params.rightMargin  = margin;
122

    
123
    mPrevButton = new ImageButton(act);
124
    mPrevButton.setLayoutParams(params);
125
    mPrevButton.setPadding(padding,0,padding,0);
126
    mPrevButton.setImageResource(icon);
127

    
128
    mPrevButton.setOnClickListener( new View.OnClickListener()
129
      {
130
      @Override
131
      public void onClick(View v)
132
        {
133
        RubikPreRender pre = act.getPreRender();
134
        backMove(pre);
135
        mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
136
        }
137
      });
138
    }
139

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

    
142
  private void setupNextButton(final RubikActivity act, final float width)
143
    {
144
    int padding = (int)(width*RubikActivity.PADDING);
145
    int margin  = (int)(width*RubikActivity.MARGIN);
146
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_right,R.drawable.ui_medium_right, R.drawable.ui_big_right, R.drawable.ui_huge_right);
147

    
148
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
149
    params.topMargin    = margin;
150
    params.bottomMargin = margin;
151
    params.leftMargin   = margin;
152
    params.rightMargin  = margin;
153

    
154
    mNextButton = new ImageButton(act);
155
    mNextButton.setLayoutParams(params);
156
    mNextButton.setPadding(padding,0,padding,0);
157
    mNextButton.setImageResource(icon);
158

    
159
    mNextButton.setOnClickListener( new View.OnClickListener()
160
      {
161
      @Override
162
      public void onClick(View v)
163
        {
164
        RubikPreRender pre = act.getPreRender();
165
        makeMove(pre);
166
        mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
167
        }
168
      });
169
    }
170

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172

    
173
  private void setupTextView(final RubikActivity act, final float width)
174
    {
175
    int padding = (int)(width*RubikActivity.PADDING);
176
    int margin  = (int)(width*RubikActivity.MARGIN);
177
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,2.0f);
178
    params.topMargin    = margin;
179
    params.bottomMargin = margin;
180
    params.leftMargin   = margin;
181
    params.rightMargin  = margin;
182

    
183
    mMovesText = new TextView(act);
184
    mMovesText.setTextSize(20);
185
    mMovesText.setLayoutParams(params);
186
    mMovesText.setPadding(padding,0,padding,0);
187
    mMovesText.setGravity(Gravity.CENTER);
188
    mMovesText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
189
    mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
190
    }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193

    
194
  private void setupBackButton(final RubikActivity act, final float width)
195
    {
196
    int padding = (int)(width*RubikActivity.PADDING);
197
    int margin  = (int)(width*RubikActivity.MARGIN);
198
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
199
    params.topMargin    = margin;
200
    params.bottomMargin = margin;
201
    params.leftMargin   = margin;
202
    params.rightMargin  = margin;
203

    
204
    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);
205

    
206
    mBackButton = new ImageButton(act);
207
    mBackButton.setLayoutParams(params);
208
    mBackButton.setPadding(padding,0,padding,0);
209
    mBackButton.setImageResource(icon);
210

    
211
    mBackButton.setOnClickListener( new View.OnClickListener()
212
      {
213
      @Override
214
      public void onClick(View v)
215
        {
216
        StateList.goBack(act);
217
        }
218
      });
219
    }
220

    
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222

    
223
  private void makeMove(RubikPreRender pre)
224
    {
225
    if( mCanRotate )
226
      {
227
      mCurrMove++;
228

    
229
      if( mCurrMove>mNumMoves )
230
        {
231
        mCurrMove= 0;
232
        pre.initializeObject(null);
233
        }
234
      else
235
        {
236
        int axis     = mMoves[mCurrMove-1][0];
237
		    int rowBitmap= mMoves[mCurrMove-1][1];
238
		    int bareAngle= mMoves[mCurrMove-1][2];
239
        int angle    = bareAngle*(360/pre.getObject().getBasicAngle());
240
        int numRot   = Math.abs(bareAngle);
241

    
242
        if( angle!=0 )
243
          {
244
          mCanRotate = false;
245
          pre.addRotation(this, axis, rowBitmap, angle, numRot*DURATION_MILLIS);
246
          }
247
        else
248
          {
249
          android.util.Log.e("solution", "error: solution contains angle 0");
250
          }
251
        }
252
      }
253
    else
254
      {
255
      android.util.Log.e("solution", "failed to make move!");
256
      }
257
    }
258

    
259
///////////////////////////////////////////////////////////////////////////////////////////////////
260

    
261
  private void backMove(RubikPreRender pre)
262
    {
263
    if( mCanRotate )
264
      {
265
      mCurrMove--;
266

    
267
      if( mCurrMove<0 )
268
        {
269
        mCurrMove=mNumMoves;
270
        pre.initializeObject(mMoves);
271
        }
272
      else
273
        {
274
        int axis     = mMoves[mCurrMove][0];
275
		    int rowBitmap= mMoves[mCurrMove][1];
276
		    int bareAngle= mMoves[mCurrMove][2];
277
        int angle    = bareAngle*(360/pre.getObject().getBasicAngle());
278
        int numRot   = Math.abs(bareAngle);
279

    
280
        if( angle!=0 )
281
          {
282
          mCanRotate = false;
283
          pre.addRotation(this, axis, rowBitmap, -angle, numRot*DURATION_MILLIS);
284
          }
285
        else
286
          {
287
          android.util.Log.e("solution", "error: solution contains angle 0");
288
          }
289
        }
290
      }
291
    else
292
      {
293
      android.util.Log.e("solution", "failed to back move!");
294
      }
295
    }
296

    
297
///////////////////////////////////////////////////////////////////////////////////////////////////
298

    
299
  void setupMoves(final RubikActivity act, String moves)
300
    {
301
    mCanRotate= true;
302
    mCurrMove = 0;
303
    mNumMoves = moves.length()/4;
304
    mMoves    = new int[mNumMoves][3];
305

    
306
    RubikPattern.parseMoves(mMoves,mNumMoves,moves);
307

    
308
    mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
309
    }
310

    
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312

    
313
  public void savePreferences(SharedPreferences.Editor editor)
314
    {
315

    
316
    }
317

    
318
///////////////////////////////////////////////////////////////////////////////////////////////////
319

    
320
  public void restorePreferences(SharedPreferences preferences)
321
    {
322

    
323
    }
324

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326

    
327
  public void onActionFinished(final long effectID)
328
    {
329
    mCanRotate = true;
330
    }
331
  }
(6-6/9)