Project

General

Profile

« Previous | Next » 

Revision 0e5ad27c

Added by Leszek Koltunski about 4 years ago

Add a 'withdraw move' button to the Solving UI state.

View differences:

src/main/java/org/distorted/states/RubikStateSolving.java
24 24
import android.view.LayoutInflater;
25 25
import android.view.View;
26 26
import android.widget.Button;
27
import android.widget.ImageButton;
27 28
import android.widget.LinearLayout;
28 29
import android.widget.TextView;
29 30

  
30 31
import org.distorted.main.R;
31 32
import org.distorted.main.RubikActivity;
33
import org.distorted.main.RubikPostRender;
34
import org.distorted.objects.RubikObject;
32 35
import org.distorted.objects.RubikObjectList;
33 36
import org.distorted.scores.RubikScores;
34 37

  
38
import java.util.ArrayList;
35 39
import java.util.Timer;
36 40
import java.util.TimerTask;
37 41

  
38 42
///////////////////////////////////////////////////////////////////////////////////////////////////
39 43

  
40
public class RubikStateSolving extends RubikStateAbstract
44
public class RubikStateSolving extends RubikStateAbstract implements RubikPostRender.ActionFinishedListener
41 45
  {
46
  private static final int DURATION_MILLIS = 750;
47

  
42 48
  private TextView mTime;
43 49
  private Timer mTimer;
44 50
  private long mStartTime;
45 51
  private boolean mRunning;
46 52
  private RubikScores mScores;
47 53
  private Button mBack;
54
  private ImageButton mPrevButton;
55
  private boolean mCanPrevMove;
56
  private ArrayList<Move> mMoves;
57

  
58
  private static class Move
59
    {
60
    private int mAxis, mRow, mAngle;
61

  
62
    Move(int axis, int row, int angle)
63
      {
64
      mAxis = axis;
65
      mRow  = row;
66
      mAngle= angle;
67
      }
68
    }
48 69

  
49 70
///////////////////////////////////////////////////////////////////////////////////////////////////
50 71

  
......
64 85

  
65 86
  void enterState(final RubikActivity act)
66 87
    {
88
    mCanPrevMove = true;
89

  
90
    if( mMoves==null ) mMoves = new ArrayList<>();
91
    else               mMoves.clear();
92

  
67 93
    LayoutInflater inflater = act.getLayoutInflater();
94
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
95
    float scale = metrics.density;
68 96

  
69 97
    // TOP ////////////////////////////
70 98
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
......
76 104
    // BOT ////////////////////////////
77 105
    LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
78 106
    layoutLeft.removeAllViews();
107

  
108
    if( mPrevButton==null ) setupPrevMoveButtom(act,scale);
109
    layoutLeft.addView(mPrevButton);
110

  
79 111
    LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
80 112
    layoutRight.removeAllViews();
81 113

  
82
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
83
    float scale = metrics.density;
84 114
    int padding = (int)(5*scale + 0.5f);
85 115
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
86 116

  
......
101 131
    layoutRight.addView(mBack);
102 132
    }
103 133

  
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

  
136
  private void setupPrevMoveButtom(final RubikActivity act, float scale)
137
    {
138
    int padding = (int)( 3*scale + 0.5f);
139
    int width   = (int)(60*scale + 0.5f);
140
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width,LinearLayout.LayoutParams.MATCH_PARENT);
141
    mPrevButton = new ImageButton(act);
142
    mPrevButton.setLayoutParams(params);
143
    mPrevButton.setPadding(padding,0,padding,0);
144
    mPrevButton.setImageResource(R.drawable.left);
145
    mPrevButton.setEnabled(false);
146

  
147
    mPrevButton.setOnClickListener( new View.OnClickListener()
148
      {
149
      @Override
150
      public void onClick(View v)
151
        {
152
        RubikPostRender post = act.getPostRender();
153
        backMove(post);
154
        }
155
      });
156
    }
157

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

  
160
  private void backMove(RubikPostRender post)
161
    {
162
    if( mCanPrevMove )
163
      {
164
      int numMoves = mMoves.size();
165

  
166
      if( numMoves>0 )
167
        {
168
        Move move = mMoves.remove(numMoves-1);
169
        RubikObject object = post.getObject();
170

  
171
        int axis  = move.mAxis;
172
        int row   = (1<<move.mRow);
173
        int angle = move.mAngle;
174
        int numRot= Math.abs(angle*object.getBasicAngle()/360);
175

  
176
        if( angle!=0 )
177
          {
178
          mCanPrevMove = false;
179
          post.addRotation(this, axis, row, -angle, numRot*DURATION_MILLIS);
180

  
181
          if( numMoves==1 )
182
            {
183
            mPrevButton.setEnabled(false);
184
            }
185
          }
186
        else
187
          {
188
          android.util.Log.e("solution", "error: trying to back move of angle 0");
189
          }
190
        }
191
      else
192
        {
193
        android.util.Log.e("solv", "error: no moves to back!");
194
        }
195
      }
196
    }
197

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

  
200
  public void addMove(int axis, int row, int angle)
201
    {
202
    mMoves.add(new Move(axis,row,angle));
203

  
204
    if( mMoves.size()==1 )
205
      {
206
      mPrevButton.setEnabled(true);
207
      }
208
    }
209

  
104 210
///////////////////////////////////////////////////////////////////////////////////////////////////
105 211

  
106 212
  public void savePreferences(SharedPreferences.Editor editor)
......
182 288

  
183 289
    return 0;
184 290
    }
291

  
292
///////////////////////////////////////////////////////////////////////////////////////////////////
293

  
294
  public void onActionFinished(final long effectID)
295
    {
296
    mCanPrevMove = true;
297
    }
185 298
  }

Also available in: Unified diff