Project

General

Profile

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

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

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

    
30
import org.distorted.objectlib.main.ObjectControl;
31
import org.distorted.objectlib.helpers.MovesFinished;
32

    
33
import org.distorted.helpers.TransparentImageButton;
34
import org.distorted.main.R;
35
import org.distorted.main.RubikActivity;
36
import org.distorted.patterns.RubikPattern;
37

    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

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

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

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
  void leaveScreen(RubikActivity act)
54
    {
55
    ObjectControl control = act.getControl();
56
    control.solveOnly();
57
    }
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

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

    
67
    LayoutInflater inflater = act.getLayoutInflater();
68

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

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

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

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

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

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

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

    
101
    setupBackButton(act);
102

    
103
    layoutRight.addView(mBackButton);
104

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

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

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

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

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130

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

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

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

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

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

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170

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

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

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187

    
188
  private void makeMove(ObjectControl control)
189
    {
190
    if( mCanRotate )
191
      {
192
      mCurrMove++;
193

    
194
      if( mCurrMove>mNumMoves )
195
        {
196
        mCurrMove= 0;
197
        control.initializeObject(null);
198
        }
199
      else
200
        {
201
        int axis      = mMoves[mCurrMove-1][0];
202
		    int rowBitmap = mMoves[mCurrMove-1][1];
203
		    int bareAngle = mMoves[mCurrMove-1][2];
204

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

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

    
224
  private void backMove(ObjectControl control)
225
    {
226
    if( mCanRotate )
227
      {
228
      mCurrMove--;
229

    
230
      if( mCurrMove<0 )
231
        {
232
        mCurrMove=mNumMoves;
233
        control.initializeObject(mMoves);
234
        }
235
      else
236
        {
237
        int axis      = mMoves[mCurrMove][0];
238
		    int rowBitmap = mMoves[mCurrMove][1];
239
		    int bareAngle = mMoves[mCurrMove][2];
240

    
241
        if( bareAngle!=0 )
242
          {
243
          mCanRotate = false;
244
          control.addRotation(this, axis, rowBitmap, -bareAngle, MILLIS_PER_DEGREE);
245
          }
246
        else
247
          {
248
          android.util.Log.e("solution", "error: solution contains angle 0");
249
          }
250
        }
251
      }
252
    else
253
      {
254
      android.util.Log.e("solution", "failed to back move!");
255
      }
256
    }
257

    
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259

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

    
267
    RubikPattern.parseMoves(mMoves,mNumMoves,moves);
268

    
269
    mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
270
    }
271

    
272
///////////////////////////////////////////////////////////////////////////////////////////////////
273

    
274
  public void savePreferences(SharedPreferences.Editor editor)
275
    {
276

    
277
    }
278

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

    
281
  public void restorePreferences(SharedPreferences preferences)
282
    {
283

    
284
    }
285

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

    
288
  public void onActionFinished(final long effectID)
289
    {
290
    mCanRotate = true;
291
    }
292
  }
(7-7/10)