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/screens/RubikScreenBase.java
19 19

  
20 20
package org.distorted.screens;
21 21

  
22
import android.view.View;
23 22
import android.widget.ImageButton;
24 23
import android.widget.LinearLayout;
25 24

  
25
import org.distorted.helpers.MovesAndLockController;
26 26
import org.distorted.main.R;
27 27
import org.distorted.main.RubikActivity;
28
import org.distorted.main.RubikPreRender;
29

  
30
import java.util.ArrayList;
31 28

  
32 29
///////////////////////////////////////////////////////////////////////////////////////////////////
33 30

  
34
abstract class RubikScreenBase extends RubikScreenAbstract implements RubikPreRender.ActionFinishedListener
31
abstract class RubikScreenBase extends RubikScreenAbstract
35 32
  {
36
  private static final int MILLIS_PER_DEGREE = 6;
37

  
38
  private ImageButton mPrevButton, mLockButton;
39
  private boolean mCanPrevMove;
40
  private RubikPreRender mPre;
41

  
42
  private static class Move
43
    {
44
    private final int mAxis, mRow, mAngle;
45

  
46
    Move(int axis, int row, int angle)
47
      {
48
      mAxis = axis;
49
      mRow  = row;
50
      mAngle= angle;
51
      }
52
    }
53

  
54
  ArrayList<Move> mMoves;
55

  
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

  
58
  private void backMove(RubikPreRender pre)
59
    {
60
    if( mCanPrevMove )
61
      {
62
      int numMoves = mMoves.size();
63

  
64
      if( numMoves>0 )
65
        {
66
        RubikScreenBase.Move move = mMoves.remove(numMoves-1);
67
        int axis  = move.mAxis;
68
        int row   = (1<<move.mRow);
69
        int angle = move.mAngle;
70
        int duration= Math.abs(angle)*MILLIS_PER_DEGREE;
71

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

  
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88

  
89
  private void toggleLock(RubikActivity act)
90
    {
91
    act.toggleLock();
92
    mLockButton.setImageResource(getLockIcon(act));
93
    }
94

  
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96

  
97
  private int getLockIcon(RubikActivity act)
98
    {
99
    if( act.retLocked() )
100
      {
101
      return RubikActivity.getDrawable(R.drawable.ui_small_locked,R.drawable.ui_medium_locked, R.drawable.ui_big_locked, R.drawable.ui_huge_locked);
102
      }
103
    else
104
      {
105
      return RubikActivity.getDrawable(R.drawable.ui_small_unlocked,R.drawable.ui_medium_unlocked, R.drawable.ui_big_unlocked, R.drawable.ui_huge_unlocked);
106
      }
107
    }
33
  MovesAndLockController mController;
108 34

  
109 35
///////////////////////////////////////////////////////////////////////////////////////////////////
110 36

  
111 37
  void createBottomPane(final RubikActivity act, float width, ImageButton button)
112 38
    {
113
    mCanPrevMove = true;
114

  
115
    if( mMoves==null ) mMoves = new ArrayList<>();
116
    else               mMoves.clear();
39
    if( mController==null ) mController = new MovesAndLockController();
117 40

  
118 41
    LinearLayout layoutBot = act.findViewById(R.id.lowerBar);
119 42
    layoutBot.removeAllViews();
......
127 50
    LinearLayout layoutRight = new LinearLayout(act);
128 51
    layoutRight.setLayoutParams(params);
129 52

  
130
    setupPrevButton(act,width);
131
    layoutLeft.addView(mPrevButton);
132
    setupLockButton(act,width);
133
    layoutMid.addView(mLockButton);
53
    mController.setupPrevButton(act,width);
54
    layoutLeft.addView(mController.getPrevButton());
55
    mController.setupLockButton(act,width);
56
    layoutMid.addView(mController.getLockButton());
134 57
    layoutRight.addView(button);
135 58

  
136 59
    layoutBot.addView(layoutLeft);
......
138 61
    layoutBot.addView(layoutRight);
139 62
    }
140 63

  
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142

  
143
  void setupLockButton(final RubikActivity act, final float width)
144
    {
145
    final int icon = getLockIcon(act);
146
    mLockButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
147

  
148
    mLockButton.setOnClickListener( new View.OnClickListener()
149
      {
150
      @Override
151
      public void onClick(View v)
152
        {
153
        toggleLock(act);
154
        }
155
      });
156
    }
157

  
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159

  
160
  void setupPrevButton(final RubikActivity act, final float width)
161
    {
162
    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);
163
    mPrevButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
164

  
165
    mPrevButton.setOnClickListener( new View.OnClickListener()
166
      {
167
      @Override
168
      public void onClick(View v)
169
        {
170
        RubikPreRender pre = act.getPreRender();
171
        backMove(pre);
172
        }
173
      });
174
    }
175

  
176 64
///////////////////////////////////////////////////////////////////////////////////////////////////
177 65

  
178 66
  public void setLockState(final RubikActivity act)
179 67
    {
180
    act.runOnUiThread(new Runnable()
181
      {
182
      @Override
183
      public void run()
184
        {
185
        if( mLockButton!=null )
186
          mLockButton.setImageResource(getLockIcon(act));
187
        }
188
      });
68
    mController.setLockState(act);
189 69
    }
190 70

  
191 71
///////////////////////////////////////////////////////////////////////////////////////////////////
192 72

  
193 73
  public void addMove(int axis, int row, int angle)
194 74
    {
195
    mMoves.add(new Move(axis,row,angle));
196
    }
197

  
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

  
200
  public void onActionFinished(final long effectID)
201
    {
202
    mCanPrevMove = true;
203
    mPre.unblockTouch();
75
    mController.addMove(axis,row,angle);
204 76
    }
205 77
  }

Also available in: Unified diff