Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedPlayScreen.java @ 39176a1f

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 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.bandaged;
21

    
22
import java.util.Random;
23

    
24
import android.app.Activity;
25
import android.content.SharedPreferences;
26
import android.os.Bundle;
27
import android.view.View;
28
import android.widget.LinearLayout;
29

    
30
import org.distorted.dialogs.RubikDialogBandagedSettings;
31
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
import org.distorted.objectlib.main.TwistyObject;
37

    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

    
40
public class BandagedPlayScreen
41
{
42
  public static final int[] DEPTHS = new int[] {20,50,100,200,500,1000};
43
  public static final int ANIMATION_ON  = 0;
44
  public static final int ANIMATION_OFF = 1;
45

    
46
  private TransparentImageButton mBackButton, mScrambleButton, mSolveButton, mSettingsButton;
47
  private final LockController mLockController;
48
  private final MovesController mMovesController;
49
  private String mKey;
50
  private int[][] mMoves;
51
  private Random mRnd;
52

    
53
  private int mAnimationMode;
54
  private int mScrambleDepth;
55

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

    
58
  public BandagedPlayScreen()
59
    {
60
    mLockController = new LockController();
61
    mMovesController= new MovesController();
62
    }
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
  private void setupBackButton(final BandagedPlayActivity act)
67
    {
68
    int icon = BandagedPlayActivity.getDrawable(R.drawable.ui_small_smallback,R.drawable.ui_medium_smallback, R.drawable.ui_big_smallback, R.drawable.ui_huge_smallback);
69
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
70
    mBackButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
71

    
72
    mBackButton.setOnClickListener( new View.OnClickListener()
73
      {
74
      @Override
75
      public void onClick(View v)
76
        {
77
        ObjectControl control = act.getControl();
78
        if( control!=null ) control.unblockEverything();
79
        act.finish();
80
        }
81
      });
82
    }
83

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

    
86
  private void setupSettingsButton(final BandagedPlayActivity act)
87
    {
88
    int icon = BandagedPlayActivity.getDrawable(R.drawable.ui_small_settings,R.drawable.ui_medium_settings, R.drawable.ui_big_settings, R.drawable.ui_huge_settings);
89
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
90
    mSettingsButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
91

    
92
    mSettingsButton.setOnClickListener( new View.OnClickListener()
93
      {
94
      @Override
95
      public void onClick(View v)
96
        {
97
        Bundle bundle = new Bundle();
98
        bundle.putInt("scraPos", mScrambleDepth );
99
        bundle.putInt("animPos", mAnimationMode );
100

    
101
        RubikDialogBandagedSettings setDiag = new RubikDialogBandagedSettings();
102
        setDiag.setArguments(bundle);
103
        setDiag.show(act.getSupportFragmentManager(), null);
104
        }
105
      });
106
    }
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109

    
110
  private void setupSolveButton(final BandagedPlayActivity act)
111
    {
112
    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);
113
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
114
    mSolveButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE,params);
115

    
116
    mSolveButton.setOnClickListener( new View.OnClickListener()
117
      {
118
      @Override
119
      public void onClick(View v)
120
        {
121
        ObjectControl control = act.getControl();
122
        if( mAnimationMode==ANIMATION_OFF ) control.solveOnly();
123
        if( mAnimationMode==ANIMATION_ON  ) control.solveObject();
124
        mMovesController.clearMoves(act);
125
        }
126
      });
127
    }
128

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130

    
131
  private void setupScrambleButton(final BandagedPlayActivity act)
132
    {
133
    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);
134
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
135
    mScrambleButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
136

    
137
    mScrambleButton.setOnClickListener( new View.OnClickListener()
138
      {
139
      @Override
140
      public void onClick(View v)
141
        {
142
        ObjectControl control = act.getControl();
143
        TwistyObject object = control.getObject();
144

    
145
        int depth = DEPTHS[mScrambleDepth];
146

    
147
        if( mAnimationMode==ANIMATION_OFF )
148
          {
149
          if( mMoves==null || mMoves.length<depth ) mMoves = new int[depth][3];
150
          if( mRnd==null ) mRnd = new Random();
151

    
152
          for(int move=0; move<depth; move++)
153
            {
154
            object.randomizeNewScramble(mMoves, mRnd, move, depth);
155
            }
156
          for(int move=0; move<depth; move++)
157
            {
158
            int row = mMoves[move][1];
159
            mMoves[move][1] = (1<<row);
160
            }
161

    
162
          control.initializeObject(mMoves);
163
          }
164

    
165
        if( mAnimationMode==ANIMATION_ON )
166
          {
167
          control.scrambleObject(depth);
168
          }
169
        }
170
      });
171
    }
172

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

    
175
  void onAttachedToWindow(final BandagedPlayActivity act, String objectName)
176
    {
177
    mKey = "moveController_"+objectName;
178

    
179
    int width = act.getScreenWidthInPixels();
180

    
181
    LinearLayout.LayoutParams paramsL = new LinearLayout.LayoutParams(width/4, LinearLayout.LayoutParams.MATCH_PARENT);
182
    LinearLayout.LayoutParams paramsM = new LinearLayout.LayoutParams(width/2, LinearLayout.LayoutParams.MATCH_PARENT);
183
    LinearLayout.LayoutParams paramsR = new LinearLayout.LayoutParams(width/4, LinearLayout.LayoutParams.MATCH_PARENT);
184

    
185
    LinearLayout layoutLeft = new LinearLayout(act);
186
    layoutLeft.setLayoutParams(paramsL);
187
    LinearLayout layoutMid  = new LinearLayout(act);
188
    layoutMid.setLayoutParams(paramsM);
189
    LinearLayout layoutRight= new LinearLayout(act);
190
    layoutRight.setLayoutParams(paramsR);
191

    
192
    setupBackButton(act);
193
    ObjectControl control = act.getControl();
194
    mMovesController.setupButton(act,control);
195
    layoutLeft.addView(mMovesController.getButton());
196
    mLockController.setupButton(act,control);
197
    layoutMid.addView(mLockController.getButton());
198

    
199
    layoutRight.addView(mBackButton);
200

    
201
    LinearLayout layoutLower = act.findViewById(R.id.lowerBar);
202
    layoutLower.removeAllViews();
203
    layoutLower.addView(layoutLeft);
204
    layoutLower.addView(layoutMid);
205
    layoutLower.addView(layoutRight);
206

    
207
    setupSolveButton(act);
208
    setupSettingsButton(act);
209
    setupScrambleButton(act);
210

    
211
    LinearLayout layoutUpper = act.findViewById(R.id.upperBar);
212

    
213
    LinearLayout layoutLeftU = new LinearLayout(act);
214
    layoutLeftU.setLayoutParams(paramsL);
215
    LinearLayout layoutMidU  = new LinearLayout(act);
216
    layoutMidU.setLayoutParams(paramsM);
217
    LinearLayout layoutRightU= new LinearLayout(act);
218
    layoutRightU.setLayoutParams(paramsR);
219

    
220
    layoutLeftU.addView(mSolveButton);
221
    layoutMidU.addView(mSettingsButton);
222
    layoutRightU.addView(mScrambleButton);
223

    
224
    layoutUpper.removeAllViews();
225
    layoutUpper.addView(layoutLeftU);
226
    layoutUpper.addView(layoutMidU);
227
    layoutUpper.addView(layoutRightU);
228
    }
229

    
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231

    
232
  public void setLockState(final BandagedPlayActivity act)
233
    {
234
    boolean locked = act.getControl().retLocked();
235
    mLockController.setState(act,locked);
236
    }
237

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239

    
240
  public void reddenLock(final BandagedPlayActivity act)
241
    {
242
    ObjectControl control = act.getControl();
243
    mLockController.reddenLock(act,control);
244
    }
245

    
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247

    
248
  public void addMove(Activity act, int axis, int row, int angle)
249
    {
250
    mMovesController.addMove(act,axis,row,angle);
251
    }
252

    
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254

    
255
  public void savePreferences(BandagedPlayActivity act, SharedPreferences.Editor editor)
256
    {
257
    mMovesController.savePreferences(mKey,editor);
258
    ObjectControl control = act.getControl();
259
    control.savePreferences(editor);
260

    
261
    editor.putInt("playScreen_scramble" , mScrambleDepth);
262
    editor.putInt("playScreen_animation", mAnimationMode);
263
    }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266

    
267
  public void restorePreferences(BandagedPlayActivity act, SharedPreferences preferences)
268
    {
269
    mMovesController.restorePreferences(act,mKey,preferences);
270
    ObjectControl control = act.getControl();
271
    control.restorePreferences(preferences);
272

    
273
    mScrambleDepth = preferences.getInt("playScreen_scramble" ,3);
274
    mAnimationMode = preferences.getInt("playScreen_animation",ANIMATION_OFF);
275
    }
276

    
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278

    
279
  public void setAnimationMode(int mode)
280
    {
281
    mAnimationMode = mode;
282
    }
283

    
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285

    
286
  public void setScrambleDepth(int depth)
287
    {
288
    mScrambleDepth = depth;
289
    }
290
}
(12-12/13)