Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenSolution.java @ 88a3e972

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.objectlib.main.TwistyObject;
32

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

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

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

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

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

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

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

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

    
69
    LayoutInflater inflater = act.getLayoutInflater();
70

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

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

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

    
84
    LinearLayout.LayoutParams paramsL = new LinearLayout.LayoutParams((int)(width/2),LinearLayout.LayoutParams.MATCH_PARENT);
85
    LinearLayout.LayoutParams paramsM = new LinearLayout.LayoutParams((int)(width/6),LinearLayout.LayoutParams.MATCH_PARENT);
86
    LinearLayout.LayoutParams paramsR = new LinearLayout.LayoutParams((int)(width/3),LinearLayout.LayoutParams.MATCH_PARENT);
87

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

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

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

    
103
    setupBackButton(act,width);
104

    
105
    layoutRight.addView(mBackButton);
106

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

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

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

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

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132

    
133
  private void setupNextButton(final RubikActivity act, final float width)
134
    {
135
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_right,R.drawable.ui_medium_right, R.drawable.ui_big_right, R.drawable.ui_huge_right);
136
    mNextButton = new TransparentImageButton(act,icon,width,0);
137

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

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151

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

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

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172

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

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

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189

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

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

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

    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228

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

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

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

    
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267

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

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

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

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

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

    
285
    }
286

    
287
///////////////////////////////////////////////////////////////////////////////////////////////////
288

    
289
  public void restorePreferences(SharedPreferences preferences)
290
    {
291

    
292
    }
293

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

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