Project

General

Profile

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

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

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.DisplayMetrics;
24
import android.util.TypedValue;
25
import android.view.Gravity;
26
import android.view.LayoutInflater;
27
import android.view.View;
28
import android.widget.Button;
29
import android.widget.ImageButton;
30
import android.widget.LinearLayout;
31
import android.widget.TextView;
32

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

    
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40

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

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

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

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

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

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

    
69
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
70
    final float scale = metrics.density;
71
    LayoutInflater inflater = act.getLayoutInflater();
72

    
73
    // TOP ////////////////////////////
74
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
75
    layoutTop.removeAllViews();
76

    
77
    final TextView text = (TextView)inflater.inflate(R.layout.upper_text, null);
78
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTitleSize);
79
    text.setText(R.string.solution);
80
    layoutTop.addView(text);
81

    
82
    // BOT ////////////////////////////
83
    setupPrevButton(act,scale);
84
    setupNextButton(act,scale);
85
    setupTextView(act,scale);
86

    
87
    LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
88
    layoutLeft.removeAllViews();
89
    layoutLeft.addView(mPrevButton);
90
    layoutLeft.addView(mMovesText);
91
    layoutLeft.addView(mNextButton);
92

    
93
    setupBackButton(act,scale);
94

    
95
    LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
96
    layoutRight.removeAllViews();
97
    layoutRight.addView(mBackButton);
98
    }
99

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101

    
102
  private void setupPrevButton(final RubikActivity act, final float scale)
103
    {
104
    int padding = (int)(3*scale + 0.5f);
105
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
106
    mPrevButton = new ImageButton(act);
107
    mPrevButton.setLayoutParams(params);
108
    mPrevButton.setPadding(padding,0,padding,0);
109
    mPrevButton.setImageResource(R.drawable.left);
110

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

    
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124

    
125
  private void setupNextButton(final RubikActivity act, final float scale)
126
    {
127
    int padding = (int)( 3*scale + 0.5f);
128
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
129
    mNextButton = new ImageButton(act);
130
    mNextButton.setLayoutParams(params);
131
    mNextButton.setPadding(padding,0,padding,0);
132
    mNextButton.setImageResource(R.drawable.right);
133

    
134
    mNextButton.setOnClickListener( new View.OnClickListener()
135
      {
136
      @Override
137
      public void onClick(View v)
138
        {
139
        RubikPreRender pre = act.getPreRender();
140
        makeMove(pre);
141
        mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
142
        }
143
      });
144
    }
145

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

    
148
  private void setupTextView(final RubikActivity act, final float scale)
149
    {
150
    int padding = (int)( 3*scale + 0.5f);
151
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,2.0f);
152

    
153
    mMovesText = new TextView(act);
154
    mMovesText.setTextSize(20);
155
    mMovesText.setLayoutParams(params);
156
    mMovesText.setPadding(padding,0,padding,0);
157
    mMovesText.setGravity(Gravity.CENTER);
158
    mMovesText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
159
    mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
160
    }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163

    
164
  private void setupBackButton(final RubikActivity act, final float scale)
165
    {
166
    int padding = (int)(3*scale + 0.5f);
167
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
168
    mBackButton = new Button(act);
169
    mBackButton.setLayoutParams(backParams);
170
    mBackButton.setPadding(padding,0,padding,0);
171
    mBackButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
172
    mBackButton.setText(R.string.back);
173

    
174
    mBackButton.setOnClickListener( new View.OnClickListener()
175
      {
176
      @Override
177
      public void onClick(View v)
178
        {
179
        RubikState.goBack(act);
180
        }
181
      });
182
    }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

    
186
  private void makeMove(RubikPreRender pre)
187
    {
188
    if( mCanRotate )
189
      {
190
      mCurrMove++;
191

    
192
      if( mCurrMove>mNumMoves )
193
        {
194
        mCurrMove= 0;
195
        pre.initializeObject(null);
196
        }
197
      else
198
        {
199
        int axis     = mMoves[mCurrMove-1][0];
200
		    int rowBitmap= mMoves[mCurrMove-1][1];
201
		    int bareAngle= mMoves[mCurrMove-1][2];
202
        int angle    = bareAngle*(360/pre.getObject().getBasicAngle());
203
        int numRot   = Math.abs(bareAngle);
204

    
205
        if( angle!=0 )
206
          {
207
          mCanRotate = false;
208
          pre.addRotation(this, axis, rowBitmap, angle, numRot*DURATION_MILLIS);
209
          }
210
        else
211
          {
212
          android.util.Log.e("solution", "error: solution contains angle 0");
213
          }
214
        }
215
      }
216
    else
217
      {
218
      android.util.Log.e("solution", "failed to make move!");
219
      }
220
    }
221

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

    
224
  private void backMove(RubikPreRender pre)
225
    {
226
    if( mCanRotate )
227
      {
228
      mCurrMove--;
229

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

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

    
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261

    
262
  void setupMoves(final RubikActivity act, String moves)
263
    {
264
    mCanRotate= true;
265
    mCurrMove = 0;
266
    mNumMoves = moves.length()/4;
267
    mMoves    = new int[mNumMoves][3];
268

    
269
    RubikPattern.parseMoves(mMoves,mNumMoves,moves);
270

    
271
    mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
272
    }
273

    
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275

    
276
  public void savePreferences(SharedPreferences.Editor editor)
277
    {
278

    
279
    }
280

    
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282

    
283
  public void restorePreferences(SharedPreferences preferences)
284
    {
285

    
286
    }
287

    
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289

    
290
  public void onActionFinished(final long effectID)
291
    {
292
    mCanRotate = true;
293
    }
294
  }
(8-8/10)