Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenSolution.java @ 1f3bc259

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
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
116
    mPrevButton = new TransparentImageButton(act,icon,TransparentImageButton.GRAVITY_MIDDLE,params);
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)
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
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
136
    mNextButton = new TransparentImageButton(act,icon,TransparentImageButton.GRAVITY_MIDDLE,params);
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.SMALL_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)
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
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
177
    mBackButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE,params);
178

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

    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190

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

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

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

    
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226

    
227
  private void backMove(ObjectControl control)
228
    {
229
    if( mCanRotate )
230
      {
231
      mCurrMove--;
232

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

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