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.screens;
|
21
|
|
22
|
import java.util.Random;
|
23
|
|
24
|
import android.content.SharedPreferences;
|
25
|
import android.os.Bundle;
|
26
|
import android.view.View;
|
27
|
import android.widget.LinearLayout;
|
28
|
|
29
|
import org.distorted.dialogs.RubikDialogFreePlaySettings;
|
30
|
import org.distorted.helpers.TransparentImageButton;
|
31
|
import org.distorted.main.R;
|
32
|
import org.distorted.main.RubikActivity;
|
33
|
import org.distorted.objectlib.main.ObjectControl;
|
34
|
import org.distorted.objectlib.main.TwistyObject;
|
35
|
|
36
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
37
|
|
38
|
public class RubikScreenFreePlay extends RubikScreenBase
|
39
|
{
|
40
|
public static final int[] DEPTHS = new int[] {20,50,100,200,500,1000};
|
41
|
public static final int ANIMATION_ON = 0;
|
42
|
public static final int ANIMATION_OFF = 1;
|
43
|
|
44
|
private TransparentImageButton mBackButton, mScrambleButton, mSolveButton, mSettingsButton;
|
45
|
private int[][] mMoves;
|
46
|
private Random mRnd;
|
47
|
private int mAnimationMode;
|
48
|
private int mScrambleDepth;
|
49
|
|
50
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
51
|
|
52
|
void leaveScreen(final RubikActivity act)
|
53
|
{
|
54
|
|
55
|
}
|
56
|
|
57
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
58
|
|
59
|
void enterScreen(final RubikActivity act)
|
60
|
{
|
61
|
int width = act.getScreenWidthInPixels();
|
62
|
|
63
|
LinearLayout.LayoutParams paramsL = new LinearLayout.LayoutParams(width/4, LinearLayout.LayoutParams.MATCH_PARENT);
|
64
|
LinearLayout.LayoutParams paramsM = new LinearLayout.LayoutParams(width/2, LinearLayout.LayoutParams.MATCH_PARENT);
|
65
|
LinearLayout.LayoutParams paramsR = new LinearLayout.LayoutParams(width/4, LinearLayout.LayoutParams.MATCH_PARENT);
|
66
|
|
67
|
// UPPER PANE /////////////
|
68
|
|
69
|
setupSolveButton(act);
|
70
|
setupSettingsButton(act);
|
71
|
setupScrambleButton(act);
|
72
|
|
73
|
LinearLayout layoutUpper = act.findViewById(R.id.upperBar);
|
74
|
|
75
|
LinearLayout layoutLeftU = new LinearLayout(act);
|
76
|
layoutLeftU.setLayoutParams(paramsL);
|
77
|
LinearLayout layoutMidU = new LinearLayout(act);
|
78
|
layoutMidU.setLayoutParams(paramsM);
|
79
|
LinearLayout layoutRightU= new LinearLayout(act);
|
80
|
layoutRightU.setLayoutParams(paramsR);
|
81
|
|
82
|
layoutLeftU.addView(mSolveButton);
|
83
|
layoutMidU.addView(mSettingsButton);
|
84
|
layoutRightU.addView(mScrambleButton);
|
85
|
|
86
|
layoutUpper.removeAllViews();
|
87
|
layoutUpper.addView(layoutLeftU);
|
88
|
layoutUpper.addView(layoutMidU);
|
89
|
layoutUpper.addView(layoutRightU);
|
90
|
|
91
|
// LOWER PANE /////////////
|
92
|
|
93
|
setupBackButton(act);
|
94
|
createBottomPane(act,mBackButton,null);
|
95
|
}
|
96
|
|
97
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
98
|
|
99
|
private void setupBackButton(final RubikActivity act)
|
100
|
{
|
101
|
int icon = RubikActivity.getDrawable(R.drawable.ui_small_smallback,R.drawable.ui_medium_smallback, R.drawable.ui_big_smallback, R.drawable.ui_huge_smallback);
|
102
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
|
103
|
mBackButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
|
104
|
|
105
|
mBackButton.setOnClickListener( new View.OnClickListener()
|
106
|
{
|
107
|
@Override
|
108
|
public void onClick(View v)
|
109
|
{
|
110
|
if(act.getControl().isUINotBlocked()) ScreenList.goBack(act);
|
111
|
}
|
112
|
});
|
113
|
}
|
114
|
|
115
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
116
|
|
117
|
private void setupSettingsButton(final RubikActivity act)
|
118
|
{
|
119
|
int icon = RubikActivity.getDrawable(R.drawable.ui_small_settings,R.drawable.ui_medium_settings, R.drawable.ui_big_settings, R.drawable.ui_huge_settings);
|
120
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
|
121
|
mSettingsButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
|
122
|
|
123
|
mSettingsButton.setOnClickListener( new View.OnClickListener()
|
124
|
{
|
125
|
@Override
|
126
|
public void onClick(View v)
|
127
|
{
|
128
|
if(act.getControl().isUINotBlocked())
|
129
|
{
|
130
|
Bundle bundle = new Bundle();
|
131
|
bundle.putInt("scraPos", mScrambleDepth );
|
132
|
bundle.putInt("animPos", mAnimationMode );
|
133
|
|
134
|
RubikDialogFreePlaySettings setDiag = new RubikDialogFreePlaySettings();
|
135
|
setDiag.setArguments(bundle);
|
136
|
setDiag.show(act.getSupportFragmentManager(), null);
|
137
|
}
|
138
|
}
|
139
|
});
|
140
|
}
|
141
|
|
142
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
143
|
|
144
|
private void setupSolveButton(final RubikActivity act)
|
145
|
{
|
146
|
int icon = RubikActivity.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);
|
147
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
|
148
|
mSolveButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE,params);
|
149
|
|
150
|
mSolveButton.setOnClickListener( new View.OnClickListener()
|
151
|
{
|
152
|
@Override
|
153
|
public void onClick(View v)
|
154
|
{
|
155
|
ObjectControl control = act.getControl();
|
156
|
if( mAnimationMode==ANIMATION_OFF ) control.solveOnly();
|
157
|
if( mAnimationMode==ANIMATION_ON ) control.solveObject();
|
158
|
mMovesController.clearMoves(act);
|
159
|
}
|
160
|
});
|
161
|
}
|
162
|
|
163
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
164
|
|
165
|
private void setupScrambleButton(final RubikActivity act)
|
166
|
{
|
167
|
int icon = RubikActivity.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);
|
168
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
|
169
|
mScrambleButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
|
170
|
|
171
|
mScrambleButton.setOnClickListener( new View.OnClickListener()
|
172
|
{
|
173
|
@Override
|
174
|
public void onClick(View v)
|
175
|
{
|
176
|
ObjectControl control = act.getControl();
|
177
|
TwistyObject object = control.getObject();
|
178
|
|
179
|
int depth = DEPTHS[mScrambleDepth];
|
180
|
|
181
|
if( mAnimationMode==ANIMATION_OFF )
|
182
|
{
|
183
|
if( mMoves==null || mMoves.length<depth ) mMoves = new int[depth][3];
|
184
|
if( mRnd==null ) mRnd = new Random();
|
185
|
|
186
|
for(int move=0; move<depth; move++)
|
187
|
{
|
188
|
object.randomizeNewScramble(mMoves, mRnd, move, depth);
|
189
|
}
|
190
|
for(int move=0; move<depth; move++)
|
191
|
{
|
192
|
int row = mMoves[move][1];
|
193
|
mMoves[move][1] = (1<<row);
|
194
|
}
|
195
|
|
196
|
control.initializeObject(mMoves);
|
197
|
}
|
198
|
|
199
|
if( mAnimationMode==ANIMATION_ON )
|
200
|
{
|
201
|
control.scrambleObject(depth);
|
202
|
}
|
203
|
}
|
204
|
});
|
205
|
}
|
206
|
|
207
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
208
|
|
209
|
public void savePreferences(SharedPreferences.Editor editor)
|
210
|
{
|
211
|
editor.putInt("freePlayScreen_scramble" , mScrambleDepth);
|
212
|
editor.putInt("freePlayScreen_animation", mAnimationMode);
|
213
|
}
|
214
|
|
215
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
216
|
|
217
|
public void restorePreferences(SharedPreferences preferences)
|
218
|
{
|
219
|
mScrambleDepth = preferences.getInt("freePlayScreen_scramble" ,3);
|
220
|
mAnimationMode = preferences.getInt("freePlayScreen_animation",ANIMATION_OFF);
|
221
|
}
|
222
|
|
223
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
224
|
|
225
|
public void setAnimationMode(int mode)
|
226
|
{
|
227
|
mAnimationMode = mode;
|
228
|
}
|
229
|
|
230
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
231
|
|
232
|
public void setScrambleDepth(int depth)
|
233
|
{
|
234
|
mScrambleDepth = depth;
|
235
|
}
|
236
|
}
|