Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenSolution.java @ 588ace55

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

    
31
import org.distorted.helpers.MovesFinished;
32
import org.distorted.helpers.TransparentImageButton;
33
import org.distorted.main.R;
34
import org.distorted.main.RubikActivity;
35
import org.distorted.main.RubikPreRender;
36
import org.distorted.objectlib.TwistyObject;
37
import org.distorted.patterns.RubikPattern;
38

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

    
41
public class RubikScreenSolution extends RubikScreenAbstract implements MovesFinished
42
  {
43
  private static final int MILLIS_PER_DEGREE = 6;
44

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

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

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

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

    
62
  void enterScreen(final RubikActivity act)
63
    {
64
    float width = act.getScreenWidthInPixels();
65
    mButtonSize = width*RubikActivity.BUTTON_TEXT_SIZE;
66
    float titleSize  = 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, titleSize);
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 paramsL = new LinearLayout.LayoutParams((int)(width/2),LinearLayout.LayoutParams.MATCH_PARENT);
84
    LinearLayout.LayoutParams paramsM = new LinearLayout.LayoutParams((int)(width/6),LinearLayout.LayoutParams.MATCH_PARENT);
85
    LinearLayout.LayoutParams paramsR = new LinearLayout.LayoutParams((int)(width/3),LinearLayout.LayoutParams.MATCH_PARENT);
86

    
87
    LinearLayout layoutLeft = new LinearLayout(act);
88
    layoutLeft.setLayoutParams(paramsL);
89
    LinearLayout layoutMid = new LinearLayout(act);
90
    layoutMid.setLayoutParams(paramsM);
91
    LinearLayout layoutRight = new LinearLayout(act);
92
    layoutRight.setLayoutParams(paramsR);
93

    
94
    setupPrevButton(act,width);
95
    setupNextButton(act,width);
96
    setupTextView(act,width);
97

    
98
    layoutLeft.addView(mPrevButton);
99
    layoutLeft.addView(mMovesText);
100
    layoutLeft.addView(mNextButton);
101

    
102
    setupBackButton(act,width);
103

    
104
    layoutRight.addView(mBackButton);
105

    
106
    layoutBot.addView(layoutLeft);
107
    layoutBot.addView(layoutMid);
108
    layoutBot.addView(layoutRight);
109
    }
110

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112

    
113
  private void setupPrevButton(final RubikActivity act, final float width)
114
    {
115
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_left,R.drawable.ui_medium_left, R.drawable.ui_big_left, R.drawable.ui_huge_left);
116
    mPrevButton = new TransparentImageButton(act,icon,width,0);
117

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

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131

    
132
  private void setupNextButton(final RubikActivity act, final float width)
133
    {
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
    mNextButton = new TransparentImageButton(act,icon,width,0);
136

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

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

    
151
  private void setupTextView(final RubikActivity act, final float width)
152
    {
153
    int padding = (int)(width*RubikActivity.PADDING);
154
    int margin  = (int)(width*RubikActivity.MARGIN);
155
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,2.0f);
156
    params.topMargin    = margin;
157
    params.bottomMargin = margin;
158
    params.leftMargin   = margin;
159
    params.rightMargin  = margin;
160

    
161
    mMovesText = new TextView(act);
162
    mMovesText.setTextSize(20);
163
    mMovesText.setLayoutParams(params);
164
    mMovesText.setPadding(padding,0,padding,0);
165
    mMovesText.setGravity(Gravity.CENTER);
166
    mMovesText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
167
    mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
168
    }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

    
172
  private void setupBackButton(final RubikActivity act, final float width)
173
    {
174
    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);
175
    mBackButton = new TransparentImageButton(act, icon, width, LinearLayout.LayoutParams.MATCH_PARENT);
176

    
177
    mBackButton.setOnClickListener( new View.OnClickListener()
178
      {
179
      @Override
180
      public void onClick(View v)
181
        {
182
        ScreenList.goBack(act);
183
        }
184
      });
185
    }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

    
189
  private void makeMove(RubikPreRender pre)
190
    {
191
    if( mCanRotate )
192
      {
193
      mCurrMove++;
194

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

    
209
        if( angle!=0 )
210
          {
211
          mCanRotate = false;
212
          pre.addRotation(this, axis, rowBitmap, angle, duration);
213
          }
214
        else
215
          {
216
          android.util.Log.e("solution", "error: solution contains angle 0");
217
          }
218
        }
219
      }
220
    else
221
      {
222
      android.util.Log.e("solution", "failed to make move!");
223
      }
224
    }
225

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227

    
228
  private void backMove(RubikPreRender pre)
229
    {
230
    if( mCanRotate )
231
      {
232
      mCurrMove--;
233

    
234
      if( mCurrMove<0 )
235
        {
236
        mCurrMove=mNumMoves;
237
        pre.initializeObject(mMoves);
238
        }
239
      else
240
        {
241
        int axis      = mMoves[mCurrMove][0];
242
		    int rowBitmap = mMoves[mCurrMove][1];
243
		    int bareAngle = mMoves[mCurrMove][2];
244
        int basicAngle= pre.getObject().getBasicAngle()[axis];
245
        int angle     = bareAngle*(360/basicAngle);
246
        int duration  = Math.abs(angle)*MILLIS_PER_DEGREE;
247

    
248
        if( angle!=0 )
249
          {
250
          mCanRotate = false;
251
          pre.addRotation(this, axis, rowBitmap, -angle, duration);
252
          }
253
        else
254
          {
255
          android.util.Log.e("solution", "error: solution contains angle 0");
256
          }
257
        }
258
      }
259
    else
260
      {
261
      android.util.Log.e("solution", "failed to back move!");
262
      }
263
    }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266

    
267
  void setupMoves(final RubikActivity act, String moves)
268
    {
269
    mCanRotate= true;
270
    mCurrMove = 0;
271
    mNumMoves = moves.length()/4;
272
    mMoves    = new int[mNumMoves][3];
273

    
274
    RubikPattern.parseMoves(mMoves,mNumMoves,moves);
275

    
276
    mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
277
    }
278

    
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280

    
281
  public void savePreferences(SharedPreferences.Editor editor)
282
    {
283

    
284
    }
285

    
286
///////////////////////////////////////////////////////////////////////////////////////////////////
287

    
288
  public void restorePreferences(SharedPreferences preferences)
289
    {
290

    
291
    }
292

    
293
///////////////////////////////////////////////////////////////////////////////////////////////////
294

    
295
  public void onActionFinished(final long effectID)
296
    {
297
    mCanRotate = true;
298
    }
299
  }
(7-7/10)