Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenSolution.java @ fcd5b990

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 leaveState(RubikActivity act)
53
    {
54
    TwistyObject object = act.getObject();
55
    object.solve();
56
    }
57

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

    
60
  void enterState(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 angle    = bareAngle*(360/pre.getObject().getBasicAngle());
204
        int numRot   = Math.abs(bareAngle);
205

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

    
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224

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

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

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

    
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262

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

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

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

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

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

    
280
    }
281

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283

    
284
  public void restorePreferences(SharedPreferences preferences)
285
    {
286

    
287
    }
288

    
289
///////////////////////////////////////////////////////////////////////////////////////////////////
290

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