Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenSolution.java @ 3f287c05

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.helpers.MovesFinished;
33

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

    
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40

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

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

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

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

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

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

    
68
    LayoutInflater inflater = act.getLayoutInflater();
69

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

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

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

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

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

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

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

    
102
    setupBackButton(act,width);
103

    
104
    layoutRight.addView(mBackButton);
105

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

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112

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

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

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131

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

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

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

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

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

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

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

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

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

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

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

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

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

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

    
259
///////////////////////////////////////////////////////////////////////////////////////////////////
260

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

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

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

    
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274

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

    
278
    }
279

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

    
282
  public void restorePreferences(SharedPreferences preferences)
283
    {
284

    
285
    }
286

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

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