Project

General

Profile

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

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

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
    setupPrevButton(act,width);
81
    setupNextButton(act,width);
82
    setupTextView(act,width);
83

    
84
    LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
85
    layoutLeft.removeAllViews();
86
    layoutLeft.addView(mPrevButton);
87
    layoutLeft.addView(mMovesText);
88
    layoutLeft.addView(mNextButton);
89

    
90
    setupBackButton(act,width);
91

    
92
    LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
93
    layoutRight.removeAllViews();
94
    layoutRight.addView(mBackButton);
95
    }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98

    
99
  private void setupPrevButton(final RubikActivity act, final float width)
100
    {
101
    int padding = (int)(width*RubikActivity.PADDING);
102
    int margin  = (int)(width*RubikActivity.MARGIN);
103
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
104
    params.topMargin    = margin;
105
    params.bottomMargin = margin;
106
    params.leftMargin   = margin;
107
    params.rightMargin  = margin;
108

    
109
    mPrevButton = new ImageButton(act);
110
    mPrevButton.setLayoutParams(params);
111
    mPrevButton.setPadding(padding,0,padding,0);
112
    mPrevButton.setImageResource(R.drawable.left);
113

    
114
    mPrevButton.setOnClickListener( new View.OnClickListener()
115
      {
116
      @Override
117
      public void onClick(View v)
118
        {
119
        RubikPreRender pre = act.getPreRender();
120
        backMove(pre);
121
        mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
122
        }
123
      });
124
    }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127

    
128
  private void setupNextButton(final RubikActivity act, final float width)
129
    {
130
    int padding = (int)(width*RubikActivity.PADDING);
131
    int margin  = (int)(width*RubikActivity.MARGIN);
132
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
133
    params.topMargin    = margin;
134
    params.bottomMargin = margin;
135
    params.leftMargin   = margin;
136
    params.rightMargin  = margin;
137

    
138
    mNextButton = new ImageButton(act);
139
    mNextButton.setLayoutParams(params);
140
    mNextButton.setPadding(padding,0,padding,0);
141
    mNextButton.setImageResource(R.drawable.right);
142

    
143
    mNextButton.setOnClickListener( new View.OnClickListener()
144
      {
145
      @Override
146
      public void onClick(View v)
147
        {
148
        RubikPreRender pre = act.getPreRender();
149
        makeMove(pre);
150
        mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
151
        }
152
      });
153
    }
154

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156

    
157
  private void setupTextView(final RubikActivity act, final float width)
158
    {
159
    int padding = (int)(width*RubikActivity.PADDING);
160
    int margin  = (int)(width*RubikActivity.MARGIN);
161
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,2.0f);
162
    params.topMargin    = margin;
163
    params.bottomMargin = margin;
164
    params.leftMargin   = margin;
165
    params.rightMargin  = margin;
166

    
167
    mMovesText = new TextView(act);
168
    mMovesText.setTextSize(20);
169
    mMovesText.setLayoutParams(params);
170
    mMovesText.setPadding(padding,0,padding,0);
171
    mMovesText.setGravity(Gravity.CENTER);
172
    mMovesText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
173
    mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
174
    }
175

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177

    
178
  private void setupBackButton(final RubikActivity act, final float width)
179
    {
180
    int padding = (int)(width*RubikActivity.PADDING);
181
    int margin  = (int)(width*RubikActivity.MARGIN);
182
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
183
    params.topMargin    = margin;
184
    params.bottomMargin = margin;
185
    params.leftMargin   = margin;
186
    params.rightMargin  = margin;
187

    
188
    mBackButton = new Button(act);
189
    mBackButton.setLayoutParams(params);
190
    mBackButton.setPadding(padding,0,padding,0);
191
    mBackButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
192
    mBackButton.setText(R.string.back);
193

    
194
    mBackButton.setOnClickListener( new View.OnClickListener()
195
      {
196
      @Override
197
      public void onClick(View v)
198
        {
199
        RubikState.goBack(act);
200
        }
201
      });
202
    }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

    
206
  private void makeMove(RubikPreRender pre)
207
    {
208
    if( mCanRotate )
209
      {
210
      mCurrMove++;
211

    
212
      if( mCurrMove>mNumMoves )
213
        {
214
        mCurrMove= 0;
215
        pre.initializeObject(null);
216
        }
217
      else
218
        {
219
        int axis     = mMoves[mCurrMove-1][0];
220
		    int rowBitmap= mMoves[mCurrMove-1][1];
221
		    int bareAngle= mMoves[mCurrMove-1][2];
222
        int angle    = bareAngle*(360/pre.getObject().getBasicAngle());
223
        int numRot   = Math.abs(bareAngle);
224

    
225
        if( angle!=0 )
226
          {
227
          mCanRotate = false;
228
          pre.addRotation(this, axis, rowBitmap, angle, numRot*DURATION_MILLIS);
229
          }
230
        else
231
          {
232
          android.util.Log.e("solution", "error: solution contains angle 0");
233
          }
234
        }
235
      }
236
    else
237
      {
238
      android.util.Log.e("solution", "failed to make move!");
239
      }
240
    }
241

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

    
244
  private void backMove(RubikPreRender pre)
245
    {
246
    if( mCanRotate )
247
      {
248
      mCurrMove--;
249

    
250
      if( mCurrMove<0 )
251
        {
252
        mCurrMove=mNumMoves;
253
        pre.initializeObject(mMoves);
254
        }
255
      else
256
        {
257
        int axis     = mMoves[mCurrMove][0];
258
		    int rowBitmap= mMoves[mCurrMove][1];
259
		    int bareAngle= mMoves[mCurrMove][2];
260
        int angle    = bareAngle*(360/pre.getObject().getBasicAngle());
261
        int numRot   = Math.abs(bareAngle);
262

    
263
        if( angle!=0 )
264
          {
265
          mCanRotate = false;
266
          pre.addRotation(this, axis, rowBitmap, -angle, numRot*DURATION_MILLIS);
267
          }
268
        else
269
          {
270
          android.util.Log.e("solution", "error: solution contains angle 0");
271
          }
272
        }
273
      }
274
    else
275
      {
276
      android.util.Log.e("solution", "failed to back move!");
277
      }
278
    }
279

    
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281

    
282
  void setupMoves(final RubikActivity act, String moves)
283
    {
284
    mCanRotate= true;
285
    mCurrMove = 0;
286
    mNumMoves = moves.length()/4;
287
    mMoves    = new int[mNumMoves][3];
288

    
289
    RubikPattern.parseMoves(mMoves,mNumMoves,moves);
290

    
291
    mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
292
    }
293

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

    
296
  public void savePreferences(SharedPreferences.Editor editor)
297
    {
298

    
299
    }
300

    
301
///////////////////////////////////////////////////////////////////////////////////////////////////
302

    
303
  public void restorePreferences(SharedPreferences preferences)
304
    {
305

    
306
    }
307

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

    
310
  public void onActionFinished(final long effectID)
311
    {
312
    mCanRotate = true;
313
    }
314
  }
(7-7/9)