Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikStateSolution.java @ 6d4d56cb

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
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_left,R.drawable.ui_medium_left, R.drawable.ui_big_left, R.drawable.ui_huge_left);
104

    
105
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
106
    params.topMargin    = margin;
107
    params.bottomMargin = margin;
108
    params.leftMargin   = margin;
109
    params.rightMargin  = margin;
110

    
111
    mPrevButton = new ImageButton(act);
112
    mPrevButton.setLayoutParams(params);
113
    mPrevButton.setPadding(padding,0,padding,0);
114
    mPrevButton.setImageResource(icon);
115

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

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

    
130
  private void setupNextButton(final RubikActivity act, final float width)
131
    {
132
    int padding = (int)(width*RubikActivity.PADDING);
133
    int margin  = (int)(width*RubikActivity.MARGIN);
134
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_right,R.drawable.ui_medium_right, R.drawable.ui_big_right, R.drawable.ui_huge_right);
135

    
136
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
137
    params.topMargin    = margin;
138
    params.bottomMargin = margin;
139
    params.leftMargin   = margin;
140
    params.rightMargin  = margin;
141

    
142
    mNextButton = new ImageButton(act);
143
    mNextButton.setLayoutParams(params);
144
    mNextButton.setPadding(padding,0,padding,0);
145
    mNextButton.setImageResource(icon);
146

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

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

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

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

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

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

    
192
    mBackButton = new Button(act);
193
    mBackButton.setLayoutParams(params);
194
    mBackButton.setPadding(padding,0,padding,0);
195
    mBackButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
196
    mBackButton.setText(R.string.back);
197

    
198
    mBackButton.setOnClickListener( new View.OnClickListener()
199
      {
200
      @Override
201
      public void onClick(View v)
202
        {
203
        RubikState.goBack(act);
204
        }
205
      });
206
    }
207

    
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209

    
210
  private void makeMove(RubikPreRender pre)
211
    {
212
    if( mCanRotate )
213
      {
214
      mCurrMove++;
215

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

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

    
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247

    
248
  private void backMove(RubikPreRender pre)
249
    {
250
    if( mCanRotate )
251
      {
252
      mCurrMove--;
253

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

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

    
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285

    
286
  void setupMoves(final RubikActivity act, String moves)
287
    {
288
    mCanRotate= true;
289
    mCurrMove = 0;
290
    mNumMoves = moves.length()/4;
291
    mMoves    = new int[mNumMoves][3];
292

    
293
    RubikPattern.parseMoves(mMoves,mNumMoves,moves);
294

    
295
    mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
296
    }
297

    
298
///////////////////////////////////////////////////////////////////////////////////////////////////
299

    
300
  public void savePreferences(SharedPreferences.Editor editor)
301
    {
302

    
303
    }
304

    
305
///////////////////////////////////////////////////////////////////////////////////////////////////
306

    
307
  public void restorePreferences(SharedPreferences preferences)
308
    {
309

    
310
    }
311

    
312
///////////////////////////////////////////////////////////////////////////////////////////////////
313

    
314
  public void onActionFinished(final long effectID)
315
    {
316
    mCanRotate = true;
317
    }
318
  }
(7-7/9)