Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikStateSolution.java @ 4fb1fc0d

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.Button;
28
import android.widget.ImageButton;
29
import android.widget.LinearLayout;
30
import android.widget.TextView;
31

    
32
import org.distorted.main.R;
33
import org.distorted.main.RubikActivity;
34
import org.distorted.main.RubikPreRender;
35
import org.distorted.objects.RubikObject;
36
import org.distorted.patterns.RubikPattern;
37

    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

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

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

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

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

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

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

    
67
    LayoutInflater inflater = act.getLayoutInflater();
68

    
69
    // TOP ////////////////////////////
70
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
71
    layoutTop.removeAllViews();
72

    
73
    final TextView text = (TextView)inflater.inflate(R.layout.upper_text, null);
74
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTitleSize);
75
    text.setText(R.string.solution);
76
    layoutTop.addView(text);
77

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

    
82
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1);
83

    
84
    setupPrevButton(act,width);
85
    setupNextButton(act,width);
86
    setupTextView(act,width);
87

    
88
    LinearLayout layoutLeft = new LinearLayout(act);
89
    layoutLeft.setLayoutParams(params);
90

    
91
    layoutLeft.addView(mPrevButton);
92
    layoutLeft.addView(mMovesText);
93
    layoutLeft.addView(mNextButton);
94

    
95
    setupBackButton(act,width);
96

    
97
    LinearLayout layoutRight = new LinearLayout(act);
98
    layoutRight.setLayoutParams(params);
99

    
100
    layoutRight.addView(mBackButton);
101

    
102
    layoutBot.addView(layoutLeft);
103
    layoutBot.addView(layoutRight);
104
    }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

    
108
  private void setupPrevButton(final RubikActivity act, final float width)
109
    {
110
    int padding = (int)(width*RubikActivity.PADDING);
111
    int margin  = (int)(width*RubikActivity.MARGIN);
112
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_left,R.drawable.ui_medium_left, R.drawable.ui_big_left, R.drawable.ui_huge_left);
113

    
114
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
115
    params.topMargin    = margin;
116
    params.bottomMargin = margin;
117
    params.leftMargin   = margin;
118
    params.rightMargin  = margin;
119

    
120
    mPrevButton = new ImageButton(act);
121
    mPrevButton.setLayoutParams(params);
122
    mPrevButton.setPadding(padding,0,padding,0);
123
    mPrevButton.setImageResource(icon);
124

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

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138

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

    
145
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
146
    params.topMargin    = margin;
147
    params.bottomMargin = margin;
148
    params.leftMargin   = margin;
149
    params.rightMargin  = margin;
150

    
151
    mNextButton = new ImageButton(act);
152
    mNextButton.setLayoutParams(params);
153
    mNextButton.setPadding(padding,0,padding,0);
154
    mNextButton.setImageResource(icon);
155

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

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

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

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

    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190

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

    
201
    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);
202

    
203
    mBackButton = new ImageButton(act);
204
    mBackButton.setLayoutParams(params);
205
    mBackButton.setPadding(padding,0,padding,0);
206
    mBackButton.setImageResource(icon);
207

    
208
    mBackButton.setOnClickListener( new View.OnClickListener()
209
      {
210
      @Override
211
      public void onClick(View v)
212
        {
213
        RubikState.goBack(act);
214
        }
215
      });
216
    }
217

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

    
220
  private void makeMove(RubikPreRender pre)
221
    {
222
    if( mCanRotate )
223
      {
224
      mCurrMove++;
225

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

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

    
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257

    
258
  private void backMove(RubikPreRender pre)
259
    {
260
    if( mCanRotate )
261
      {
262
      mCurrMove--;
263

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

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

    
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295

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

    
303
    RubikPattern.parseMoves(mMoves,mNumMoves,moves);
304

    
305
    mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
306
    }
307

    
308
///////////////////////////////////////////////////////////////////////////////////////////////////
309

    
310
  public void savePreferences(SharedPreferences.Editor editor)
311
    {
312

    
313
    }
314

    
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316

    
317
  public void restorePreferences(SharedPreferences preferences)
318
    {
319

    
320
    }
321

    
322
///////////////////////////////////////////////////////////////////////////////////////////////////
323

    
324
  public void onActionFinished(final long effectID)
325
    {
326
    mCanRotate = true;
327
    }
328
  }
(7-7/9)