Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikStateBase.java @ d85e1397

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

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

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

    
32
import java.util.ArrayList;
33

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

    
36
abstract class RubikStateBase extends RubikStateAbstract implements RubikPreRender.ActionFinishedListener
37
  {
38
  private static final int DURATION_MILLIS = 750;
39

    
40
  private ImageButton mPrevButton, mLockButton, mInfoButton;
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(RubikPreRender pre)
61
    {
62
    if( mCanPrevMove )
63
      {
64
      int numMoves = mMoves.size();
65

    
66
      if( numMoves>0 )
67
        {
68
        RubikStateBase.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(RubikActivity act)
92
    {
93
    act.toggleLock();
94
    mLockButton.setImageResource(getLockIcon(act));
95
    }
96

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

    
99
  private int getLockIcon(RubikActivity 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
  void createBottomPane(final RubikActivity act, float width, ImageButton button)
114
    {
115
    mCanPrevMove = true;
116

    
117
    if( mMoves==null ) mMoves = new ArrayList<>();
118
    else               mMoves.clear();
119

    
120
    LinearLayout layoutBot = act.findViewById(R.id.lowerBar);
121
    layoutBot.removeAllViews();
122

    
123
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1);
124

    
125
    LinearLayout layoutLeft = new LinearLayout(act);
126
    layoutLeft.setLayoutParams(params);
127
    LinearLayout layoutMid = new LinearLayout(act);
128
    layoutMid.setLayoutParams(params);
129
    LinearLayout layoutRight = new LinearLayout(act);
130
    layoutRight.setLayoutParams(params);
131

    
132
    setupPrevButton(act,width);
133
    layoutLeft.addView(mPrevButton);
134
    setupLockButton(act,width);
135
    layoutMid.addView(mLockButton);
136
    setupInfoButton(act,width);
137
    layoutMid.addView(mInfoButton);
138
    layoutRight.addView(button);
139

    
140
    layoutBot.addView(layoutLeft);
141
    layoutBot.addView(layoutMid);
142
    layoutBot.addView(layoutRight);
143
    }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

    
147
  void setupLockButton(final RubikActivity act, final float width)
148
    {
149
    final int icon = getLockIcon(act);
150
    mLockButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
151

    
152
    mLockButton.setOnClickListener( new View.OnClickListener()
153
      {
154
      @Override
155
      public void onClick(View v)
156
        {
157
        toggleLock(act);
158
        }
159
      });
160
    }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163

    
164
  void setupInfoButton(final RubikActivity act, final float width)
165
    {
166
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_info,R.drawable.ui_medium_info, R.drawable.ui_big_info, R.drawable.ui_huge_info);
167
    mInfoButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
168

    
169
    mInfoButton.setOnClickListener( new View.OnClickListener()
170
      {
171
      @Override
172
      public void onClick(View v)
173
        {
174
        RubikDialogInfo infoDiag = new RubikDialogInfo();
175
        infoDiag.show(act.getSupportFragmentManager(), null);
176
        }
177
      });
178
    }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

    
182
  void setupPrevButton(final RubikActivity act, final float width)
183
    {
184
    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);
185
    mPrevButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
186

    
187
    mPrevButton.setOnClickListener( new View.OnClickListener()
188
      {
189
      @Override
190
      public void onClick(View v)
191
        {
192
        RubikPreRender pre = act.getPreRender();
193
        backMove(pre);
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

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

    
207
  public void onActionFinished(final long effectID)
208
    {
209
    mCanPrevMove = true;
210
    }
211
  }
(2-2/10)