Project

General

Profile

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

magiccube / src / main / java / org / distorted / tutorials / TutorialState.java @ eaf87d1d

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.tutorials;
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.ObjectList;
30
import org.distorted.objects.TwistyObject;
31
import org.distorted.states.RubikStatePlay;
32
import org.distorted.states.StateList;
33
import org.distorted.states.TransparentImageButton;
34

    
35
import java.util.ArrayList;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

    
39
public class TutorialState implements RubikPreRender.ActionFinishedListener
40
{
41
  private static final int DURATION_MILLIS = 750;
42

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

    
45
  private boolean mCanPrevMove;
46

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

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

    
59
  ArrayList<Move> mMoves;
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

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

    
69
      if( numMoves>0 )
70
        {
71
        Move move = mMoves.remove(numMoves-1);
72
        TwistyObject object = pre.getObject();
73

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

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

    
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93

    
94
  private void toggleLock(TutorialActivity act)
95
    {
96
    act.toggleLock();
97
    mLockButton.setImageResource(getLockIcon(act));
98
    }
99

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101

    
102
  private int getLockIcon(TutorialActivity act)
103
    {
104
    if( act.retLocked() )
105
      {
106
      return RubikActivity.getDrawable(R.drawable.ui_small_locked,R.drawable.ui_medium_locked, R.drawable.ui_big_locked, R.drawable.ui_huge_locked);
107
      }
108
    else
109
      {
110
      return RubikActivity.getDrawable(R.drawable.ui_small_unlocked,R.drawable.ui_medium_unlocked, R.drawable.ui_big_unlocked, R.drawable.ui_huge_unlocked);
111
      }
112
    }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

    
116
  private void setupSolveButton(final TutorialActivity act, final float width)
117
    {
118
    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);
119
    mSolveButton = new TransparentImageButton(act, icon, width, LinearLayout.LayoutParams.MATCH_PARENT);
120

    
121
    mSolveButton.setOnClickListener( new View.OnClickListener()
122
      {
123
      @Override
124
      public void onClick(View v)
125
        {
126
        act.getPreRender().solveObject();
127
        mMoves.clear();
128
        }
129
      });
130
    }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

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

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

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

    
151
  private void setupPrevButton(final TutorialActivity act, final float width)
152
    {
153
    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);
154
    mPrevButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
155

    
156
    mPrevButton.setOnClickListener( new View.OnClickListener()
157
      {
158
      @Override
159
      public void onClick(View v)
160
        {
161
        TutorialPreRender pre = act.getPreRender();
162
        backMove(pre);
163
        }
164
      });
165
    }
166

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168

    
169
  private void setupScrambleButton(final TutorialActivity act, final float width)
170
    {
171
    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);
172
    mScrambleButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
173

    
174
    mScrambleButton.setOnClickListener( new View.OnClickListener()
175
      {
176
      @Override
177
      public void onClick(View v)
178
        {
179
        RubikStatePlay play = (RubikStatePlay) StateList.PLAY.getStateClass();
180
        int size = play.getSize();
181
        int object= play.getObject();
182
        int sizeIndex = ObjectList.getSizeIndex(object,size);
183
        int maxLevel = ObjectList.getMaxLevel(object, sizeIndex);
184

    
185
        act.getPreRender().scrambleObject(maxLevel);
186
        }
187
      });
188
    }
189

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191

    
192
  private void setupBackButton(final TutorialActivity act, final float width)
193
    {
194
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_smallback,R.drawable.ui_medium_smallback, R.drawable.ui_big_smallback, R.drawable.ui_huge_smallback);
195
    mBackButton = new TransparentImageButton(act, icon, width, LinearLayout.LayoutParams.MATCH_PARENT);
196

    
197
    mBackButton.setOnClickListener( new View.OnClickListener()
198
      {
199
      @Override
200
      public void onClick(View v)
201
        {
202
        act.finish();
203
        }
204
      });
205
    }
206

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

    
209
  void createRightPane(final TutorialActivity act, float width)
210
    {
211
    mCanPrevMove = true;
212

    
213
    if( mMoves==null ) mMoves = new ArrayList<>();
214
    else               mMoves.clear();
215

    
216
    LinearLayout layout = act.findViewById(R.id.tutorialRightBar);
217
    layout.removeAllViews();
218

    
219
    setupPrevButton(act,width);
220
    setupLockButton(act,width);
221
    setupSolveButton(act,width);
222
    setupScrambleButton(act,width);
223
    setupBackButton(act,width);
224

    
225
    layout.addView(mSolveButton);
226
    layout.addView(mPrevButton);
227
    layout.addView(mScrambleButton);
228
    layout.addView(mLockButton);
229
    layout.addView(mBackButton);
230
    }
231

    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233

    
234
  public void addMove(int axis, int row, int angle)
235
    {
236
    mMoves.add(new Move(axis,row,angle));
237
    }
238

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

    
241
  public void onActionFinished(final long effectID)
242
    {
243
    mCanPrevMove = true;
244
    }
245
}
(5-5/7)