Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenBase.java @ fcd5b990

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.screens;
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

    
31
import java.util.ArrayList;
32

    
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34

    
35
abstract class RubikScreenBase extends RubikScreenAbstract implements RubikPreRender.ActionFinishedListener
36
  {
37
  private static final int DURATION_MILLIS = 750;
38

    
39
  private ImageButton mPrevButton, mLockButton;
40

    
41
  private boolean mCanPrevMove;
42

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

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

    
55
  ArrayList<Move> mMoves;
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

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

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

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

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

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89

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

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97

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

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

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

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

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

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

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

    
131
    setupPrevButton(act,width);
132
    layoutLeft.addView(mPrevButton);
133
    setupLockButton(act,width);
134
    layoutMid.addView(mLockButton);
135
    layoutRight.addView(button);
136

    
137
    layoutBot.addView(layoutLeft);
138
    layoutBot.addView(layoutMid);
139
    layoutBot.addView(layoutRight);
140
    }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143

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

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

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

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

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

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

    
179
  public void addMove(int axis, int row, int angle)
180
    {
181
    mMoves.add(new Move(axis,row,angle));
182
    }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

    
186
  public void onActionFinished(final long effectID)
187
    {
188
    mCanPrevMove = true;
189
    }
190
  }
(2-2/12)