Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenSolution.java @ 925ed78f

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.main.R;
32
import org.distorted.main.RubikActivity;
33
import org.distorted.main.RubikPreRender;
34
import org.distorted.objects.TwistyObject;
35
import org.distorted.patterns.RubikPattern;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

    
39
public class RubikScreenSolution extends RubikScreenAbstract implements RubikPreRender.ActionFinishedListener
40
  {
41
  private static final int DURATION_MILLIS = 750;
42

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

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

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

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
  void enterScreen(final RubikActivity act)
61
    {
62
    float width = act.getScreenWidthInPixels();
63
    mButtonSize = width*RubikActivity.BUTTON_TEXT_SIZE;
64
    float titleSize  = width*RubikActivity.TITLE_TEXT_SIZE;
65

    
66
    LayoutInflater inflater = act.getLayoutInflater();
67

    
68
    // TOP ////////////////////////////
69
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
70
    layoutTop.removeAllViews();
71

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

    
77
    // BOT ////////////////////////////
78
    LinearLayout layoutBot = act.findViewById(R.id.lowerBar);
79
    layoutBot.removeAllViews();
80

    
81
    LinearLayout.LayoutParams paramsL = new LinearLayout.LayoutParams((int)(width/2),LinearLayout.LayoutParams.MATCH_PARENT);
82
    LinearLayout.LayoutParams paramsM = new LinearLayout.LayoutParams((int)(width/6),LinearLayout.LayoutParams.MATCH_PARENT);
83
    LinearLayout.LayoutParams paramsR = new LinearLayout.LayoutParams((int)(width/3),LinearLayout.LayoutParams.MATCH_PARENT);
84

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

    
92
    setupPrevButton(act,width);
93
    setupNextButton(act,width);
94
    setupTextView(act,width);
95

    
96
    layoutLeft.addView(mPrevButton);
97
    layoutLeft.addView(mMovesText);
98
    layoutLeft.addView(mNextButton);
99

    
100
    setupBackButton(act,width);
101

    
102
    layoutRight.addView(mBackButton);
103

    
104
    layoutBot.addView(layoutLeft);
105
    layoutBot.addView(layoutMid);
106
    layoutBot.addView(layoutRight);
107
    }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

    
111
  private void setupPrevButton(final RubikActivity act, final float width)
112
    {
113
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_left,R.drawable.ui_medium_left, R.drawable.ui_big_left, R.drawable.ui_huge_left);
114
    mPrevButton = new TransparentImageButton(act,icon,width,0);
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 icon = RubikActivity.getDrawable(R.drawable.ui_small_right,R.drawable.ui_medium_right, R.drawable.ui_big_right, R.drawable.ui_huge_right);
133
    mNextButton = new TransparentImageButton(act,icon,width,0);
134

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

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

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

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

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

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

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

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186

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

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

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

    
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225

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

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

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

    
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264

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

    
272
    RubikPattern.parseMoves(mMoves,mNumMoves,moves);
273

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

    
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278

    
279
  public void savePreferences(SharedPreferences.Editor editor)
280
    {
281

    
282
    }
283

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

    
286
  public void restorePreferences(SharedPreferences preferences)
287
    {
288

    
289
    }
290

    
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292

    
293
  public void onActionFinished(final long effectID)
294
    {
295
    mCanRotate = true;
296
    }
297
  }
(7-7/12)