Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedPlayScreen.java @ b72b71a1

1 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2 da56b12f Leszek Koltunski
// Copyright 2022 Leszek Koltunski                                                               //
3 9530f6b0 Leszek Koltunski
//                                                                                               //
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.bandaged;
21
22 b72b71a1 Leszek Koltunski
import java.util.Random;
23
24 88b94310 Leszek Koltunski
import android.app.Activity;
25
import android.content.SharedPreferences;
26 9530f6b0 Leszek Koltunski
import android.view.View;
27
import android.widget.LinearLayout;
28
29
import org.distorted.helpers.LockController;
30
import org.distorted.helpers.MovesController;
31
import org.distorted.helpers.TransparentImageButton;
32
import org.distorted.main.R;
33
import org.distorted.objectlib.main.ObjectControl;
34 4844152a Leszek Koltunski
import org.distorted.objectlib.main.TwistyObject;
35
import org.distorted.objectlib.scrambling.ObjectScrambler;
36 9530f6b0 Leszek Koltunski
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38
39
public class BandagedPlayScreen
40
{
41 b72b71a1 Leszek Koltunski
  public static final int ANIMATION_ON  = 0;
42
  public static final int ANIMATION_OFF = 1;
43
44
  private static final int NUM_SCRAMBLES = 2;
45 f3563327 Leszek Koltunski
46 9530f6b0 Leszek Koltunski
  private TransparentImageButton mBackButton, mScrambleButton, mSolveButton;
47
  private final LockController mLockController;
48 b72b71a1 Leszek Koltunski
  private final MovesController mMovesController;
49 972f9eae Leszek Koltunski
  private String mKey;
50 b72b71a1 Leszek Koltunski
  private int[][] mMoves;
51
  private Random mRnd;
52
53
  private int mAnimationMode;
54
  private int mScrambleDepth;
55 9530f6b0 Leszek Koltunski
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57
58
  public BandagedPlayScreen()
59
    {
60
    mLockController = new LockController();
61
    mMovesController= new MovesController();
62 b72b71a1 Leszek Koltunski
63
    mAnimationMode = ANIMATION_OFF;
64
    mScrambleDepth = NUM_SCRAMBLES;
65 9530f6b0 Leszek Koltunski
    }
66
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68
69
  private void setupBackButton(final BandagedPlayActivity act)
70
    {
71 88b94310 Leszek Koltunski
    int icon = BandagedPlayActivity.getDrawable(R.drawable.ui_small_smallback,R.drawable.ui_medium_smallback, R.drawable.ui_big_smallback, R.drawable.ui_huge_smallback);
72 9530f6b0 Leszek Koltunski
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
73
    mBackButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
74
75
    mBackButton.setOnClickListener( new View.OnClickListener()
76
      {
77
      @Override
78
      public void onClick(View v)
79
        {
80
        ObjectControl control = act.getControl();
81
        if( control!=null ) control.unblockEverything();
82
        act.finish();
83
        }
84
      });
85
    }
86
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88
89 f3563327 Leszek Koltunski
  private void setupSolveButton(final BandagedPlayActivity act)
90 9530f6b0 Leszek Koltunski
    {
91 f3563327 Leszek Koltunski
    int icon = BandagedPlayActivity.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);
92
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
93
    mSolveButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE,params);
94
95
    mSolveButton.setOnClickListener( new View.OnClickListener()
96
      {
97
      @Override
98
      public void onClick(View v)
99
        {
100 b72b71a1 Leszek Koltunski
        ObjectControl control = act.getControl();
101
        if( mAnimationMode==ANIMATION_OFF ) control.solveOnly();
102
        if( mAnimationMode==ANIMATION_ON  ) control.solveObject();
103 f3563327 Leszek Koltunski
        mMovesController.clearMoves(act);
104
        }
105
      });
106
    }
107
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109 9530f6b0 Leszek Koltunski
110 f3563327 Leszek Koltunski
  private void setupScrambleButton(final BandagedPlayActivity act)
111
    {
112
    int icon = BandagedPlayActivity.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);
113
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
114
    mScrambleButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
115
116
    mScrambleButton.setOnClickListener( new View.OnClickListener()
117
      {
118
      @Override
119
      public void onClick(View v)
120
        {
121 4844152a Leszek Koltunski
        ObjectControl control = act.getControl();
122
        TwistyObject object = control.getObject();
123
        ObjectScrambler.setSignature(object.getSignature());
124 b72b71a1 Leszek Koltunski
125
        if( mAnimationMode==ANIMATION_OFF )
126
          {
127
          if( mMoves==null || mMoves.length<mScrambleDepth ) mMoves = new int[mScrambleDepth][3];
128
          if( mRnd==null ) mRnd = new Random();
129
130
          for(int move=0; move<mScrambleDepth; move++)
131
            {
132
            object.randomizeNewScramble(mMoves, mRnd, move, mScrambleDepth);
133
            }
134
          for(int move=0; move<mScrambleDepth; move++)
135
            {
136
            int row = mMoves[move][1];
137
            mMoves[move][1] = (1<<row);
138
            }
139
140
          control.initializeObject(mMoves);
141
          }
142
143
        if( mAnimationMode==ANIMATION_ON )
144
          {
145
          control.scrambleObject(mScrambleDepth);
146
          }
147 f3563327 Leszek Koltunski
        }
148
      });
149
    }
150
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152
153 972f9eae Leszek Koltunski
  void onAttachedToWindow(final BandagedPlayActivity act, String objectName)
154 f3563327 Leszek Koltunski
    {
155 972f9eae Leszek Koltunski
    mKey = "moveController_"+objectName;
156
157 f3563327 Leszek Koltunski
    int width = act.getScreenWidthInPixels();
158 9530f6b0 Leszek Koltunski
159
    LinearLayout.LayoutParams paramsL = new LinearLayout.LayoutParams(width/4, LinearLayout.LayoutParams.MATCH_PARENT);
160
    LinearLayout.LayoutParams paramsM = new LinearLayout.LayoutParams(width/2, LinearLayout.LayoutParams.MATCH_PARENT);
161
    LinearLayout.LayoutParams paramsR = new LinearLayout.LayoutParams(width/4, LinearLayout.LayoutParams.MATCH_PARENT);
162
163
    LinearLayout layoutLeft = new LinearLayout(act);
164
    layoutLeft.setLayoutParams(paramsL);
165
    LinearLayout layoutMid  = new LinearLayout(act);
166
    layoutMid.setLayoutParams(paramsM);
167
    LinearLayout layoutRight= new LinearLayout(act);
168
    layoutRight.setLayoutParams(paramsR);
169
170
    setupBackButton(act);
171
    ObjectControl control = act.getControl();
172
    mMovesController.setupButton(act,control);
173
    layoutLeft.addView(mMovesController.getButton());
174
    mLockController.setupButton(act,control);
175
    layoutMid.addView(mLockController.getButton());
176
177
    layoutRight.addView(mBackButton);
178
179 f3563327 Leszek Koltunski
    LinearLayout layoutLower = act.findViewById(R.id.lowerBar);
180
    layoutLower.removeAllViews();
181
    layoutLower.addView(layoutLeft);
182
    layoutLower.addView(layoutMid);
183
    layoutLower.addView(layoutRight);
184
185
    setupScrambleButton(act);
186
    setupSolveButton(act);
187
188
    LinearLayout layoutUpper = act.findViewById(R.id.upperBar);
189
190
    LinearLayout layoutLeftU = new LinearLayout(act);
191
    layoutLeftU.setLayoutParams(paramsL);
192
    LinearLayout layoutMidU  = new LinearLayout(act);
193
    layoutMidU.setLayoutParams(paramsM);
194
    LinearLayout layoutRightU= new LinearLayout(act);
195
    layoutRightU.setLayoutParams(paramsR);
196
197
    layoutLeftU.addView(mSolveButton);
198
    layoutRightU.addView(mScrambleButton);
199
200 775d5361 Leszek Koltunski
    layoutUpper.removeAllViews();
201 f3563327 Leszek Koltunski
    layoutUpper.addView(layoutLeftU);
202
    layoutUpper.addView(layoutMidU);
203
    layoutUpper.addView(layoutRightU);
204 9530f6b0 Leszek Koltunski
    }
205
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207
208 88b94310 Leszek Koltunski
  public void setLockState(final BandagedPlayActivity act)
209 9530f6b0 Leszek Koltunski
    {
210
    boolean locked = act.getControl().retLocked();
211
    mLockController.setState(act,locked);
212
    }
213
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215
216 88b94310 Leszek Koltunski
  public void reddenLock(final BandagedPlayActivity act)
217 9530f6b0 Leszek Koltunski
    {
218
    ObjectControl control = act.getControl();
219
    mLockController.reddenLock(act,control);
220
    }
221 88b94310 Leszek Koltunski
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223
224
  public void addMove(Activity act, int axis, int row, int angle)
225
    {
226
    mMovesController.addMove(act,axis,row,angle);
227
    }
228
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230
231 972f9eae Leszek Koltunski
  public void savePreferences(BandagedPlayActivity act, SharedPreferences.Editor editor)
232 88b94310 Leszek Koltunski
    {
233 972f9eae Leszek Koltunski
    mMovesController.savePreferences(mKey,editor);
234
    ObjectControl control = act.getControl();
235
    control.savePreferences(editor);
236 88b94310 Leszek Koltunski
    }
237
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239
240 972f9eae Leszek Koltunski
  public void restorePreferences(BandagedPlayActivity act, SharedPreferences preferences)
241 88b94310 Leszek Koltunski
    {
242 972f9eae Leszek Koltunski
    mMovesController.restorePreferences(act,mKey,preferences);
243
    ObjectControl control = act.getControl();
244
    control.restorePreferences(preferences);
245 88b94310 Leszek Koltunski
    }
246 b72b71a1 Leszek Koltunski
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248
249
  public void setAnimationMode(int mode)
250
    {
251
    mAnimationMode = mode;
252
    }
253
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255
256
  public void setScrambleDepth(int depth)
257
    {
258
    mScrambleDepth = depth;
259
    }
260 9530f6b0 Leszek Koltunski
}