Project

General

Profile

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

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

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