Project

General

Profile

« Previous | Next » 

Revision 15846fe4

Added by Leszek Koltunski over 4 years ago

cube_back and cube_solve buttons.

View differences:

src/main/java/org/distorted/main/RubikSurfaceView.java
34 34
import org.distorted.objects.RubikObjectMovement;
35 35
import org.distorted.solvers.SolverMain;
36 36
import org.distorted.states.RubikState;
37
import org.distorted.states.RubikStatePlay;
37 38
import org.distorted.states.RubikStateSolver;
38 39
import org.distorted.states.RubikStateSolving;
39 40

  
......
393 394
      int angle = mPreRender.getObject().computeNearestAngle(mCurrentAngle, mCurrRotSpeed);
394 395
      mPreRender.finishRotation(angle);
395 396

  
396
      if( RubikState.getCurrentState()==RubikState.SOLV && angle!=0 )
397
      if( angle!=0 )
397 398
        {
398
        RubikStateSolving solving = (RubikStateSolving)RubikState.SOLV.getStateClass();
399
        solving.addMove(mCurrentAxis, mCurrentRow, angle);
399
        if( RubikState.getCurrentState()==RubikState.SOLV )
400
          {
401
          RubikStateSolving solving = (RubikStateSolving)RubikState.SOLV.getStateClass();
402
          solving.addMove(mCurrentAxis, mCurrentRow, angle);
403
          }
404
        if( RubikState.getCurrentState()==RubikState.PLAY )
405
          {
406
          RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
407
          play.addMove(mCurrentAxis, mCurrentRow, angle);
408
          }
400 409
        }
401 410

  
402 411
      mContinuingRotation = false;
src/main/java/org/distorted/states/RubikStatePlay.java
45 45
import org.distorted.dialogs.RubikDialogScores;
46 46
import org.distorted.main.R;
47 47
import org.distorted.main.RubikActivity;
48
import org.distorted.main.RubikPreRender;
49
import org.distorted.objects.RubikObject;
48 50
import org.distorted.objects.RubikObjectList;
49 51

  
52
import java.util.ArrayList;
53

  
50 54
///////////////////////////////////////////////////////////////////////////////////////////////////
51 55

  
52
public class RubikStatePlay extends RubikStateAbstract implements AdapterView.OnItemSelectedListener
56
public class RubikStatePlay extends RubikStateAbstract implements AdapterView.OnItemSelectedListener,
57
                                                                  RubikPreRender.ActionFinishedListener
53 58
  {
59
  private static final int DURATION_MILLIS = 750;
54 60
  private static final int DEF_LEVEL =  1;
55 61
  public  static final int DEF_OBJECT= RubikObjectList.CUBE.ordinal();
56 62
  public  static final int DEF_SIZE  =  3;
......
58 64
  private static int[] BUTTON_LABELS = { R.string.scores, R.string.patterns, R.string.solver, R.string.about };
59 65
  private static final int NUM_BUTTONS = BUTTON_LABELS.length;
60 66

  
61
  private ImageButton mObjButton, mMenuButton;
62
  private Button mBackButton, mSolveButton, mPlayButton;
67
  private ImageButton mObjButton, mMenuButton, mPrevButton, mSolveButton;
68
  private Button mPlayButton;
63 69
  private PopupWindow mObjectPopup, mMenuPopup;
64 70
  private int mObject = DEF_OBJECT;
65 71
  private int mSize   = DEF_SIZE;
......
70 76
  private int mLevelValue;
71 77
  private float mButtonSize, mTitleSize;
72 78

  
79
  private ArrayList<Move> mMoves;
80
  private boolean mCanPrevMove;
81

  
82
  private static class Move
83
    {
84
    private int mAxis, mRow, mAngle;
85

  
86
    Move(int axis, int row, int angle)
87
      {
88
      mAxis = axis;
89
      mRow  = row;
90
      mAngle= angle;
91
      }
92
    }
93

  
73 94
///////////////////////////////////////////////////////////////////////////////////////////////////
74 95

  
75 96
  void leaveState(RubikActivity act)
......
88 109
    mButtonSize = width*RubikActivity.BUTTON_TEXT_SIZE;
89 110
    mTitleSize  = width*RubikActivity.TITLE_TEXT_SIZE;
90 111

  
112
    mCanPrevMove = true;
113

  
114
    if( mMoves==null ) mMoves = new ArrayList<>();
115
    else               mMoves.clear();
116

  
91 117
    // TOP ////////////////////////////
92 118
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
93 119
    layoutTop.removeAllViews();
......
106 132
    LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
107 133
    layoutLeft.removeAllViews();
108 134

  
109
    setupMenuButton(act,scale,width);
110
    layoutLeft.addView(mMenuButton);
111
    setupSolveButton(act,scale);
135
    setupPrevButton(act,scale,width);
136
    layoutLeft.addView(mPrevButton);
137
    setupSolveButton(act,scale,width);
112 138
    layoutLeft.addView(mSolveButton);
113 139

  
114 140
    LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
115 141
    layoutRight.removeAllViews();
116 142

  
117
    setupBackButton(act,scale);
118
    layoutRight.addView(mBackButton);
143
    setupMenuButton(act,scale);
144
    layoutRight.addView(mMenuButton);
119 145

  
120 146
    setupMenuWindow(act, scale);
121 147
    }
......
161 187

  
162 188
  private void setupLevelSpinner(final RubikActivity act, final float scale)
163 189
    {
164
    int spinnerPadding = (int)(scale* 10 + 0.5f);
165
    int spinnerMargin  = (int)(scale*  3 + 0.5f);
190
    int padding = (int)(scale* 10 + 0.5f);
191
    int margin  = (int)(scale*  3 + 0.5f);
166 192
    LinearLayout.LayoutParams spinnerLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
167
    spinnerLayoutParams.topMargin    =   spinnerMargin;
168
    spinnerLayoutParams.bottomMargin =   spinnerMargin;
169
    spinnerLayoutParams.leftMargin   =   spinnerMargin;
170
    spinnerLayoutParams.rightMargin  = 2*spinnerMargin;
193
    spinnerLayoutParams.topMargin    = margin;
194
    spinnerLayoutParams.bottomMargin = margin;
195
    spinnerLayoutParams.leftMargin   = margin;
196
    spinnerLayoutParams.rightMargin  = margin;
171 197

  
172 198
    mLevelSpinner = new AppCompatSpinner(act);
173 199
    mLevelSpinner.setLayoutParams(spinnerLayoutParams);
174
    mLevelSpinner.setPadding(spinnerPadding,0,spinnerPadding,0);
200
    mLevelSpinner.setPadding(padding,0,padding,0);
175 201
    mLevelSpinner.setBackgroundResource(R.drawable.spinner);
176 202
    mLevelSpinner.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
177 203

  
......
228 254

  
229 255
///////////////////////////////////////////////////////////////////////////////////////////////////
230 256

  
231
  private void setupMenuButton(final RubikActivity act, final float scale, final float width)
257
  private void setupMenuButton(final RubikActivity act, final float scale)
232 258
    {
233 259
    int padding = (int)(3*scale + 0.5f);
234
    int widthBut = (int)(width/6);
235
    LinearLayout.LayoutParams objectParams = new LinearLayout.LayoutParams(widthBut,LinearLayout.LayoutParams.MATCH_PARENT);
260
    LinearLayout.LayoutParams objectParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
236 261
    mMenuButton = new ImageButton(act);
237 262
    mMenuButton.setLayoutParams(objectParams);
238 263
    mMenuButton.setPadding(padding,0,padding,0);
......
258 283

  
259 284
///////////////////////////////////////////////////////////////////////////////////////////////////
260 285

  
261
  private void setupSolveButton(final RubikActivity act, final float scale)
286
  private void setupSolveButton(final RubikActivity act, final float scale, final float width)
262 287
    {
263 288
    int padding = (int)(3*scale + 0.5f);
264
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
265
    mSolveButton = new Button(act);
289
    int widthBut = (int)(width/6);
290
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(widthBut, LinearLayout.LayoutParams.MATCH_PARENT);
291
    mSolveButton = new ImageButton(act);
266 292
    mSolveButton.setLayoutParams(backParams);
267 293
    mSolveButton.setPadding(padding,0,padding,0);
268
    mSolveButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
269

  
270
    mSolveButton.setText(R.string.solve);
294
    mSolveButton.setImageResource(R.drawable.cube_solve);
271 295

  
272 296
    mSolveButton.setOnClickListener( new View.OnClickListener()
273 297
      {
......
281 305

  
282 306
///////////////////////////////////////////////////////////////////////////////////////////////////
283 307

  
284
  private void setupBackButton(final RubikActivity act, final float scale)
308
  private void setupPrevButton(final RubikActivity act, final float scale, final float width)
285 309
    {
286 310
    int padding = (int)(3*scale + 0.5f);
287
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
288
    mBackButton = new Button(act);
289
    mBackButton.setLayoutParams(backParams);
290
    mBackButton.setPadding(padding,0,padding,0);
291
    mBackButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
292
    mBackButton.setText(R.string.exit);
293

  
294
    mBackButton.setOnClickListener( new View.OnClickListener()
311
    int widthBut = (int)(width/6);
312
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(widthBut, LinearLayout.LayoutParams.MATCH_PARENT);
313
    mPrevButton = new ImageButton(act);
314
    mPrevButton.setLayoutParams(backParams);
315
    mPrevButton.setPadding(padding,0,padding,0);
316
    mPrevButton.setImageResource(R.drawable.cube_back);
317

  
318
    mPrevButton.setOnClickListener( new View.OnClickListener()
295 319
      {
296 320
      @Override
297 321
      public void onClick(View v)
298 322
        {
299
        if( act.getPreRender().canPlay() ) RubikState.goBack(act);
323
        RubikPreRender pre = act.getPreRender();
324
        backMove(pre);
300 325
        }
301 326
      });
302 327
    }
......
398 423
    mMenuLayoutHeight= (int)(margin + NUM_BUTTONS*(mButtonSize+margin));
399 424
    }
400 425

  
426
///////////////////////////////////////////////////////////////////////////////////////////////////
427

  
428
  private void backMove(RubikPreRender pre)
429
    {
430
    if( mCanPrevMove )
431
      {
432
      int numMoves = mMoves.size();
433

  
434
      if( numMoves>0 )
435
        {
436
        Move move = mMoves.remove(numMoves-1);
437
        RubikObject object = pre.getObject();
438

  
439
        int axis  = move.mAxis;
440
        int row   = (1<<move.mRow);
441
        int angle = move.mAngle;
442
        int numRot= Math.abs(angle*object.getBasicAngle()/360);
443

  
444
        if( angle!=0 )
445
          {
446
          mCanPrevMove = false;
447
          pre.addRotation(this, axis, row, -angle, numRot*DURATION_MILLIS);
448
          }
449
        else
450
          {
451
          android.util.Log.e("solution", "error: trying to back move of angle 0");
452
          }
453
        }
454
      else
455
        {
456
        android.util.Log.e("solv", "error: no moves to back!");
457
        }
458
      }
459
    }
401 460
///////////////////////////////////////////////////////////////////////////////////////////////////
402 461

  
403 462
  private void Action(RubikActivity act, int button)
......
522 581
    return mLevelValue;
523 582
    }
524 583

  
584
///////////////////////////////////////////////////////////////////////////////////////////////////
585

  
586
  public void addMove(int axis, int row, int angle)
587
    {
588
    mMoves.add(new Move(axis,row,angle));
589
    }
590

  
525 591
///////////////////////////////////////////////////////////////////////////////////////////////////
526 592

  
527 593
  public int getObject()
......
546 612
///////////////////////////////////////////////////////////////////////////////////////////////////
547 613

  
548 614
  public void onNothingSelected(AdapterView<?> parent) { }
615

  
616
///////////////////////////////////////////////////////////////////////////////////////////////////
617

  
618
  public void onActionFinished(final long effectID)
619
    {
620
    mCanPrevMove = true;
621
    }
549 622
  }
src/main/java/org/distorted/states/RubikStateReady.java
25 25
import android.view.LayoutInflater;
26 26
import android.view.View;
27 27
import android.widget.Button;
28
import android.widget.ImageButton;
28 29
import android.widget.LinearLayout;
29 30
import android.widget.TextView;
30 31

  
......
35 36

  
36 37
public class RubikStateReady extends RubikStateAbstract
37 38
  {
39
  private ImageButton mPrevButton;
40

  
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

  
38 43
  void leaveState(RubikActivity act)
39 44
    {
40 45

  
......
64 69
    LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
65 70
    layoutLeft.removeAllViews();
66 71

  
72
    setupPrevMoveButtom(act,scale,width);
73
    layoutLeft.addView(mPrevButton);
74

  
67 75
    LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
68 76
    layoutRight.removeAllViews();
69 77

  
......
88 96
    layoutRight.addView(back);
89 97
    }
90 98

  
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

  
101
  private void setupPrevMoveButtom(final RubikActivity act, float scale, float width)
102
    {
103
    int padding = (int)( 3*scale + 0.5f);
104
    int widthBut= (int)(width/6);
105

  
106
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(widthBut,LinearLayout.LayoutParams.MATCH_PARENT);
107
    mPrevButton = new ImageButton(act);
108
    mPrevButton.setLayoutParams(params);
109
    mPrevButton.setPadding(padding,0,padding,0);
110
    mPrevButton.setImageResource(R.drawable.cube_back);
111

  
112
    mPrevButton.setOnClickListener( new View.OnClickListener()
113
      {
114
      @Override
115
      public void onClick(View v)
116
        {
117
        // empty
118
        }
119
      });
120
    }
121

  
91 122
///////////////////////////////////////////////////////////////////////////////////////////////////
92 123

  
93 124
  public void savePreferences(SharedPreferences.Editor editor)
src/main/java/org/distorted/states/RubikStateSolving.java
152 152
    mPrevButton = new ImageButton(act);
153 153
    mPrevButton.setLayoutParams(params);
154 154
    mPrevButton.setPadding(padding,0,padding,0);
155
    mPrevButton.setImageResource(R.drawable.left);
156
    mPrevButton.setVisibility(View.INVISIBLE);
155
    mPrevButton.setImageResource(R.drawable.cube_back);
157 156

  
158 157
    mPrevButton.setOnClickListener( new View.OnClickListener()
159 158
      {
......
188 187
          {
189 188
          mCanPrevMove = false;
190 189
          pre.addRotation(this, axis, row, -angle, numRot*DURATION_MILLIS);
191

  
192
          if( numMoves==1 )
193
            {
194
            mPrevButton.setVisibility(View.INVISIBLE);
195
            }
196 190
          }
197 191
        else
198 192
          {
......
211 205
  public void addMove(int axis, int row, int angle)
212 206
    {
213 207
    mMoves.add(new Move(axis,row,angle));
214

  
215
    if( mMoves.size()==1 )
216
      {
217
      mPrevButton.setVisibility(View.VISIBLE);
218
      }
219 208
    }
220 209

  
221 210
///////////////////////////////////////////////////////////////////////////////////////////////////

Also available in: Unified diff