Project

General

Profile

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

magiccube / src / main / java / org / distorted / tutorial / TutorialState.java @ 344f290c

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.tutorial;
21

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

    
26
import org.distorted.main.R;
27
import org.distorted.main.RubikActivity;
28
import org.distorted.main.RubikPreRender;
29
import org.distorted.objects.TwistyObject;
30
import org.distorted.states.TransparentImageButton;
31

    
32
import java.util.ArrayList;
33

    
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35

    
36
public class TutorialState implements RubikPreRender.ActionFinishedListener
37
{
38
  private static final int DURATION_MILLIS = 750;
39

    
40
  private ImageButton mPrevButton, mLockButton, mSolveButton, mScrambleButton, mBackButton;
41

    
42
  private boolean mCanPrevMove;
43

    
44
  private static class Move
45
    {
46
    private int mAxis, mRow, mAngle;
47

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

    
56
  ArrayList<Move> mMoves;
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

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

    
66
      if( numMoves>0 )
67
        {
68
        Move move = mMoves.remove(numMoves-1);
69
        TwistyObject object = pre.getObject();
70

    
71
        int axis  = move.mAxis;
72
        int row   = (1<<move.mRow);
73
        int angle = move.mAngle;
74
        int numRot= Math.abs(angle*object.getBasicAngle()/360);
75

    
76
        if( angle!=0 )
77
          {
78
          mCanPrevMove = false;
79
          pre.addRotation(this, axis, row, -angle, numRot*DURATION_MILLIS);
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
    }
110

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112

    
113
  private void setupSolveButton(final TutorialActivity act, final float width)
114
    {
115
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube_solve,R.drawable.ui_medium_cube_solve, R.drawable.ui_big_cube_solve, R.drawable.ui_huge_cube_solve);
116
    mSolveButton = new TransparentImageButton(act, icon, width, LinearLayout.LayoutParams.MATCH_PARENT);
117

    
118
    mSolveButton.setOnClickListener( new View.OnClickListener()
119
      {
120
      @Override
121
      public void onClick(View v)
122
        {
123
        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);
160
        }
161
      });
162
    }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
  private void setupScrambleButton(final TutorialActivity act, final float width)
167
    {
168
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube_scramble,R.drawable.ui_medium_cube_scramble, R.drawable.ui_big_cube_scramble, R.drawable.ui_huge_cube_scramble);
169
    mScrambleButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
170

    
171
    mScrambleButton.setOnClickListener( new View.OnClickListener()
172
      {
173
      @Override
174
      public void onClick(View v)
175
        {
176
        TutorialPreRender pre = act.getPreRender();
177
        // TODO
178
        }
179
      });
180
    }
181

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

    
184
  private void setupBackButton(final TutorialActivity act, final float width)
185
    {
186
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_back,R.drawable.ui_medium_back, R.drawable.ui_big_back, R.drawable.ui_huge_back);
187
    mBackButton = new TransparentImageButton(act, icon, width, LinearLayout.LayoutParams.MATCH_PARENT);
188

    
189
    mBackButton.setOnClickListener( new View.OnClickListener()
190
      {
191
      @Override
192
      public void onClick(View v)
193
        {
194
        act.finish();
195
        }
196
      });
197
    }
198

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200

    
201
  void createRightPane(final TutorialActivity act, float width)
202
    {
203
    mCanPrevMove = true;
204

    
205
    if( mMoves==null ) mMoves = new ArrayList<>();
206
    else               mMoves.clear();
207

    
208
    LinearLayout layout = act.findViewById(R.id.rightBar);
209
    layout.removeAllViews();
210

    
211
    setupPrevButton(act,width);
212
    setupLockButton(act,width);
213
    setupSolveButton(act,width);
214
    setupScrambleButton(act,width);
215
    setupBackButton(act,width);
216

    
217
    layout.addView(mSolveButton);
218
    layout.addView(mPrevButton);
219
    layout.addView(mScrambleButton);
220
    layout.addView(mLockButton);
221
    layout.addView(mBackButton);
222
    }
223

    
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225

    
226
  public void addMove(int axis, int row, int angle)
227
    {
228
    mMoves.add(new Move(axis,row,angle));
229
    }
230

    
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232

    
233
  public void onActionFinished(final long effectID)
234
    {
235
    mCanPrevMove = true;
236
    }
237
}
(4-4/5)