Project

General

Profile

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

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

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.ObjectControl;
32
import org.distorted.objectlib.main.TwistyObject;
33
import org.distorted.objectlib.helpers.MovesFinished;
34

    
35
import org.distorted.helpers.TransparentImageButton;
36
import org.distorted.main.R;
37
import org.distorted.main.RubikActivity;
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
    ObjectControl control = act.getControl();
58
    control.solveOnly();
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
        ObjectControl control = act.getControl();
125
        backMove(control);
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
        ObjectControl control = act.getControl();
144
        makeMove(control);
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(ObjectControl control)
191
    {
192
    if( mCanRotate )
193
      {
194
      mCurrMove++;
195

    
196
      if( mCurrMove>mNumMoves )
197
        {
198
        mCurrMove= 0;
199
        control.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

    
207
        if( bareAngle!=0 )
208
          {
209
          mCanRotate = false;
210
          control.addRotation(this, axis, rowBitmap, bareAngle, MILLIS_PER_DEGREE);
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(ObjectControl control)
227
    {
228
    if( mCanRotate )
229
      {
230
      mCurrMove--;
231

    
232
      if( mCurrMove<0 )
233
        {
234
        mCurrMove=mNumMoves;
235
        control.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

    
243
        if( bareAngle!=0 )
244
          {
245
          mCanRotate = false;
246
          control.addRotation(this, axis, rowBitmap, -bareAngle, MILLIS_PER_DEGREE);
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
  }
(7-7/10)