Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikStateSolution.java @ 92843d3b

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 Button mBackButton;
45
  private ImageButton mPrevButton, mNextButton;
46
  private TextView mMovesText;
47
  private int[][] mMoves;
48
  private int mCurrMove, mNumMoves;
49
  private boolean mCanRotate;
50
  private float mButtonSize, mTitleSize;
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

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

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

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

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

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

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

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

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

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

    
96
    setupBackButton(act,width);
97

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

    
101
    layoutRight.addView(mBackButton);
102

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

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108

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

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

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

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

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

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

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

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

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

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170

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

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

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191

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

    
202
    mBackButton = new Button(act);
203
    mBackButton.setLayoutParams(params);
204
    mBackButton.setPadding(padding,0,padding,0);
205
    mBackButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
206
    mBackButton.setText(R.string.back);
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)