Project

General

Profile

« Previous | Next » 

Revision 55e6be1d

Added by Leszek Koltunski almost 3 years ago

Abstract the part that controls the 'Locked' and 'Back Moves' buttons from the two activities: the main one and the tutorial one.
This code had been duplicated there.

View differences:

src/main/java/org/distorted/tutorials/TutorialState.java
23 23
import android.widget.ImageButton;
24 24
import android.widget.LinearLayout;
25 25

  
26
import org.distorted.helpers.MovesAndLockController;
26 27
import org.distorted.main.R;
27 28
import org.distorted.main.RubikActivity;
28
import org.distorted.main.RubikPreRender;
29 29
import org.distorted.objects.ObjectList;
30 30
import org.distorted.screens.RubikScreenPlay;
31 31
import org.distorted.screens.ScreenList;
32
import org.distorted.screens.TransparentImageButton;
33

  
34
import java.util.ArrayList;
32
import org.distorted.helpers.TransparentImageButton;
35 33

  
36 34
///////////////////////////////////////////////////////////////////////////////////////////////////
37 35

  
38
public class TutorialState implements RubikPreRender.ActionFinishedListener
36
public class TutorialState
39 37
{
40
  private static final int MILLIS_PER_DEGREE = 6;
41

  
42
  private ImageButton mPrevButton, mLockButton, mSolveButton, mScrambleButton, mBackButton;
43

  
44
  private boolean mCanPrevMove;
45

  
46
  private static class Move
47
    {
48
    private final int mAxis, mRow, mAngle;
49

  
50
    Move(int axis, int row, int angle)
51
      {
52
      mAxis = axis;
53
      mRow  = row;
54
      mAngle= angle;
55
      }
56
    }
57

  
58
  ArrayList<Move> mMoves;
59

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

  
62
  private void backMove(TutorialPreRender pre)
63
    {
64
    if( mCanPrevMove )
65
      {
66
      int numMoves = mMoves.size();
67

  
68
      if( numMoves>0 )
69
        {
70
        Move move   = mMoves.remove(numMoves-1);
71
        int axis    = move.mAxis;
72
        int row     = (1<<move.mRow);
73
        int angle   = move.mAngle;
74
        int duration= Math.abs(angle)*MILLIS_PER_DEGREE;
75

  
76
        if( angle!=0 )
77
          {
78
          mCanPrevMove = false;
79
          pre.addRotation(this, axis, row, -angle, duration);
80
          }
81
        else
82
          {
83
          android.util.Log.e("solution", "error: trying to back move of angle 0");
84
          }
85
        }
86
      }
87
    }
88

  
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

  
91
  private void toggleLock(TutorialActivity act)
92
    {
93
    act.toggleLock();
94
    mLockButton.setImageResource(getLockIcon(act));
95
    }
96

  
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98

  
99
  private int getLockIcon(TutorialActivity act)
100
    {
101
    if( act.retLocked() )
102
      {
103
      return RubikActivity.getDrawable(R.drawable.ui_small_locked,R.drawable.ui_medium_locked, R.drawable.ui_big_locked, R.drawable.ui_huge_locked);
104
      }
105
    else
106
      {
107
      return RubikActivity.getDrawable(R.drawable.ui_small_unlocked,R.drawable.ui_medium_unlocked, R.drawable.ui_big_unlocked, R.drawable.ui_huge_unlocked);
108
      }
109
    }
38
  private ImageButton mSolveButton, mScrambleButton, mBackButton;
39
  private MovesAndLockController mController;
110 40

  
111 41
///////////////////////////////////////////////////////////////////////////////////////////////////
112 42

  
......
121 51
      public void onClick(View v)
122 52
        {
123 53
        act.getPreRender().solveObject();
124
        mMoves.clear();
125
        }
126
      });
127
    }
128

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

  
131
  private void setupLockButton(final TutorialActivity act, final float width)
132
    {
133
    final int icon = getLockIcon(act);
134
    mLockButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
135

  
136
    mLockButton.setOnClickListener( new View.OnClickListener()
137
      {
138
      @Override
139
      public void onClick(View v)
140
        {
141
        toggleLock(act);
142
        }
143
      });
144
    }
145

  
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

  
148
  private void setupPrevButton(final TutorialActivity act, final float width)
149
    {
150
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube_back,R.drawable.ui_medium_cube_back, R.drawable.ui_big_cube_back, R.drawable.ui_huge_cube_back);
151
    mPrevButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
152

  
153
    mPrevButton.setOnClickListener( new View.OnClickListener()
154
      {
155
      @Override
156
      public void onClick(View v)
157
        {
158
        TutorialPreRender pre = act.getPreRender();
159
        backMove(pre);
54
        mController.clearMoves();
160 55
        }
161 56
      });
162 57
    }
......
205 100

  
206 101
  void createRightPane(final TutorialActivity act, float width)
207 102
    {
208
    mCanPrevMove = true;
209

  
210
    if( mMoves==null ) mMoves = new ArrayList<>();
211
    else               mMoves.clear();
103
    if( mController==null ) mController = new MovesAndLockController();
212 104

  
213 105
    LinearLayout layout = act.findViewById(R.id.tutorialRightBar);
214 106
    layout.removeAllViews();
215 107

  
216
    setupPrevButton(act,width);
217
    setupLockButton(act,width);
108
    mController.setupPrevButton(act,width);
109
    mController.setupLockButton(act,width);
218 110
    setupSolveButton(act,width);
219 111
    setupScrambleButton(act,width);
220 112
    setupBackButton(act,width);
221 113

  
222 114
    layout.addView(mSolveButton);
223
    layout.addView(mPrevButton);
115
    layout.addView(mController.getPrevButton());
224 116
    layout.addView(mScrambleButton);
225
    layout.addView(mLockButton);
117
    layout.addView(mController.getLockButton());
226 118
    layout.addView(mBackButton);
227 119
    }
228 120

  
229 121
///////////////////////////////////////////////////////////////////////////////////////////////////
230 122

  
231
  public void addMove(int axis, int row, int angle)
232
    {
233
    mMoves.add(new Move(axis,row,angle));
234
    }
235

  
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

  
238
  public void onActionFinished(final long effectID)
123
  void addMove(int axis, int row, int angle)
239 124
    {
240
    mCanPrevMove = true;
125
    mController.addMove(axis,row,angle);
241 126
    }
242 127
}

Also available in: Unified diff