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.states;
|
21
|
|
22
|
import android.content.SharedPreferences;
|
23
|
import android.os.Bundle;
|
24
|
import androidx.fragment.app.FragmentManager;
|
25
|
import android.util.TypedValue;
|
26
|
import android.view.Gravity;
|
27
|
import android.view.LayoutInflater;
|
28
|
import android.view.View;
|
29
|
import android.widget.Button;
|
30
|
import android.widget.ImageButton;
|
31
|
import android.widget.LinearLayout;
|
32
|
import android.widget.TextView;
|
33
|
|
34
|
import org.distorted.dialogs.RubikDialogPattern;
|
35
|
import org.distorted.main.R;
|
36
|
import org.distorted.main.RubikActivity;
|
37
|
import org.distorted.main.RubikPreRender;
|
38
|
import org.distorted.objects.RubikObjectList;
|
39
|
import org.distorted.patterns.RubikPattern;
|
40
|
import org.distorted.patterns.RubikPatternList;
|
41
|
|
42
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
43
|
|
44
|
public class RubikStatePattern extends RubikStateAbstract
|
45
|
{
|
46
|
private TextView mText;
|
47
|
private Button mBackButton;
|
48
|
private ImageButton mPrevButton, mNextButton;
|
49
|
private TextView mMovesText;
|
50
|
private int mNumMoves;
|
51
|
private int mPatternOrdinal, mCategory, mPattern;
|
52
|
private float mButtonSize, mTitleSize;
|
53
|
|
54
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
55
|
|
56
|
RubikStatePattern()
|
57
|
{
|
58
|
|
59
|
}
|
60
|
|
61
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
62
|
|
63
|
void leaveState(RubikActivity act)
|
64
|
{
|
65
|
RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
|
66
|
|
67
|
RubikObjectList object = RubikPatternList.getObject(mPatternOrdinal);
|
68
|
int size = RubikPatternList.getSize(mPatternOrdinal);
|
69
|
|
70
|
if( !play.setObjectAndSize(act,object,size) )
|
71
|
{
|
72
|
int objectPlay= play.getObject();
|
73
|
int sizePlay = play.getSize();
|
74
|
|
75
|
act.changeObject(RubikObjectList.getObject(objectPlay),sizePlay, false);
|
76
|
}
|
77
|
}
|
78
|
|
79
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
80
|
|
81
|
void enterState(final RubikActivity act)
|
82
|
{
|
83
|
float width = act.getScreenWidthInPixels();
|
84
|
mButtonSize = width*RubikActivity.BUTTON_TEXT_SIZE;
|
85
|
mTitleSize = width*RubikActivity.TITLE_TEXT_SIZE;
|
86
|
|
87
|
RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
|
88
|
int obj = play.getObject();
|
89
|
int size = play.getSize();
|
90
|
|
91
|
mPatternOrdinal = RubikPatternList.getOrdinal(obj,size);
|
92
|
|
93
|
if( mPatternOrdinal<0 )
|
94
|
{
|
95
|
mPatternOrdinal = RubikObjectList.getSizeIndex(RubikStatePlay.DEF_OBJECT,RubikStatePlay.DEF_SIZE);
|
96
|
}
|
97
|
|
98
|
FragmentManager mana = act.getSupportFragmentManager();
|
99
|
RubikDialogPattern diag = (RubikDialogPattern) mana.findFragmentByTag(RubikDialogPattern.getDialogTag());
|
100
|
|
101
|
if( diag==null ) showDialog(mana);
|
102
|
|
103
|
LayoutInflater inflater = act.getLayoutInflater();
|
104
|
|
105
|
// TOP ////////////////////////////
|
106
|
LinearLayout layoutTop = act.findViewById(R.id.upperBar);
|
107
|
layoutTop.removeAllViews();
|
108
|
mText = (TextView)inflater.inflate(R.layout.upper_pattern_text, null);
|
109
|
mText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTitleSize);
|
110
|
mText.setText(R.string.patterns);
|
111
|
layoutTop.addView(mText);
|
112
|
|
113
|
// BOT ////////////////////////////
|
114
|
LinearLayout layoutBot = act.findViewById(R.id.lowerBar);
|
115
|
layoutBot.removeAllViews();
|
116
|
|
117
|
setupPrevButton(act,width);
|
118
|
setupNextButton(act,width);
|
119
|
setupTextView(act,width);
|
120
|
|
121
|
setTrioState(false);
|
122
|
|
123
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1);
|
124
|
|
125
|
LinearLayout layoutLeft = new LinearLayout(act);
|
126
|
layoutLeft.setLayoutParams(params);
|
127
|
|
128
|
layoutLeft.addView(mPrevButton);
|
129
|
layoutLeft.addView(mMovesText);
|
130
|
layoutLeft.addView(mNextButton);
|
131
|
|
132
|
setupBackButton(act,width);
|
133
|
|
134
|
LinearLayout layoutRight = new LinearLayout(act);
|
135
|
layoutRight.setLayoutParams(params);
|
136
|
|
137
|
layoutRight.addView(mBackButton);
|
138
|
|
139
|
layoutBot.addView(layoutLeft);
|
140
|
layoutBot.addView(layoutRight);
|
141
|
}
|
142
|
|
143
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
144
|
|
145
|
private void showDialog(FragmentManager manager)
|
146
|
{
|
147
|
RubikDialogPattern diag = new RubikDialogPattern();
|
148
|
Bundle bundle = new Bundle();
|
149
|
bundle.putInt("tab", mPatternOrdinal );
|
150
|
diag.setArguments(bundle);
|
151
|
diag.show( manager, RubikDialogPattern.getDialogTag() );
|
152
|
}
|
153
|
|
154
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
155
|
|
156
|
private void setTrioState(boolean enable)
|
157
|
{
|
158
|
int state = enable ? View.VISIBLE : View.INVISIBLE;
|
159
|
|
160
|
if( mPrevButton!=null ) mPrevButton.setVisibility(state);
|
161
|
if( mNextButton!=null ) mNextButton.setVisibility(state);
|
162
|
if( mMovesText !=null ) mMovesText.setVisibility(state);
|
163
|
}
|
164
|
|
165
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
166
|
|
167
|
private void setupBackButton(final RubikActivity act, final float width)
|
168
|
{
|
169
|
int padding = (int)(width*RubikActivity.PADDING);
|
170
|
int margin = (int)(width*RubikActivity.MARGIN);
|
171
|
|
172
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
|
173
|
params.topMargin = margin;
|
174
|
params.bottomMargin = margin;
|
175
|
params.leftMargin = margin;
|
176
|
params.rightMargin = margin;
|
177
|
|
178
|
mBackButton = new Button(act);
|
179
|
mBackButton.setLayoutParams(params);
|
180
|
mBackButton.setPadding(padding,0,padding,0);
|
181
|
mBackButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
|
182
|
mBackButton.setText(R.string.back);
|
183
|
|
184
|
mBackButton.setOnClickListener( new View.OnClickListener()
|
185
|
{
|
186
|
@Override
|
187
|
public void onClick(View v)
|
188
|
{
|
189
|
FragmentManager mana = act.getSupportFragmentManager();
|
190
|
RubikDialogPattern diag = (RubikDialogPattern) mana.findFragmentByTag(RubikDialogPattern.getDialogTag());
|
191
|
|
192
|
if( diag==null )
|
193
|
{
|
194
|
showDialog(mana);
|
195
|
setTrioState(false);
|
196
|
}
|
197
|
else
|
198
|
{
|
199
|
diag.rememberState();
|
200
|
diag.dismiss();
|
201
|
RubikState.goBack(act);
|
202
|
}
|
203
|
}
|
204
|
});
|
205
|
}
|
206
|
|
207
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
208
|
|
209
|
private void setupPrevButton(final RubikActivity act, final float width)
|
210
|
{
|
211
|
int padding = (int)(width*RubikActivity.PADDING);
|
212
|
int margin = (int)(width*RubikActivity.MARGIN);
|
213
|
int icon = RubikActivity.getDrawable(R.drawable.ui_small_left,R.drawable.ui_medium_left, R.drawable.ui_big_left, R.drawable.ui_huge_left);
|
214
|
|
215
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
|
216
|
params.topMargin = margin;
|
217
|
params.bottomMargin = margin;
|
218
|
params.leftMargin = margin;
|
219
|
params.rightMargin = margin;
|
220
|
|
221
|
mPrevButton = new ImageButton(act);
|
222
|
mPrevButton.setLayoutParams(params);
|
223
|
mPrevButton.setPadding(padding,0,padding,0);
|
224
|
mPrevButton.setImageResource(icon);
|
225
|
|
226
|
mPrevButton.setOnClickListener( new View.OnClickListener()
|
227
|
{
|
228
|
@Override
|
229
|
public void onClick(View v)
|
230
|
{
|
231
|
RubikPattern pattern = RubikPattern.getInstance();
|
232
|
RubikPreRender pre = act.getPreRender();
|
233
|
pattern.backMove( pre, mPatternOrdinal, mCategory, mPattern);
|
234
|
int currMove = pattern.getCurMove(mPatternOrdinal, mCategory, mPattern);
|
235
|
mMovesText.setText(act.getString(R.string.mo_placeholder,currMove,mNumMoves));
|
236
|
}
|
237
|
});
|
238
|
}
|
239
|
|
240
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
241
|
|
242
|
private void setupNextButton(final RubikActivity act, final float width)
|
243
|
{
|
244
|
int padding = (int)(width*RubikActivity.PADDING);
|
245
|
int margin = (int)(width*RubikActivity.MARGIN);
|
246
|
int icon = RubikActivity.getDrawable(R.drawable.ui_small_right,R.drawable.ui_medium_right, R.drawable.ui_big_right, R.drawable.ui_huge_right);
|
247
|
|
248
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
|
249
|
params.topMargin = margin;
|
250
|
params.bottomMargin = margin;
|
251
|
params.leftMargin = margin;
|
252
|
params.rightMargin = margin;
|
253
|
|
254
|
mNextButton = new ImageButton(act);
|
255
|
mNextButton.setLayoutParams(params);
|
256
|
mNextButton.setPadding(padding,0,padding,0);
|
257
|
mNextButton.setImageResource(icon);
|
258
|
|
259
|
mNextButton.setOnClickListener( new View.OnClickListener()
|
260
|
{
|
261
|
@Override
|
262
|
public void onClick(View v)
|
263
|
{
|
264
|
RubikPattern pattern = RubikPattern.getInstance();
|
265
|
RubikPreRender pre = act.getPreRender();
|
266
|
pattern.makeMove( pre, mPatternOrdinal, mCategory, mPattern);
|
267
|
int currMove = pattern.getCurMove(mPatternOrdinal, mCategory, mPattern);
|
268
|
mMovesText.setText(act.getString(R.string.mo_placeholder,currMove,mNumMoves));
|
269
|
}
|
270
|
});
|
271
|
}
|
272
|
|
273
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
274
|
|
275
|
private void setupTextView(final RubikActivity act, final float width)
|
276
|
{
|
277
|
int padding = (int)(width*RubikActivity.PADDING);
|
278
|
int margin = (int)(width*RubikActivity.MARGIN);
|
279
|
|
280
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,2.0f);
|
281
|
params.topMargin = margin;
|
282
|
params.bottomMargin = margin;
|
283
|
params.leftMargin = margin;
|
284
|
params.rightMargin = margin;
|
285
|
|
286
|
mMovesText = new TextView(act);
|
287
|
mMovesText.setTextSize(20);
|
288
|
mMovesText.setLayoutParams(params);
|
289
|
mMovesText.setPadding(padding,0,padding,0);
|
290
|
mMovesText.setGravity(Gravity.CENTER);
|
291
|
mMovesText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
|
292
|
mMovesText.setText(act.getString(R.string.mo_placeholder,0,0));
|
293
|
}
|
294
|
|
295
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
296
|
|
297
|
public void setPattern(final RubikActivity act, int ordinal, int category, int pattern)
|
298
|
{
|
299
|
mPatternOrdinal = ordinal;
|
300
|
mCategory = category;
|
301
|
mPattern = pattern;
|
302
|
|
303
|
setTrioState(true);
|
304
|
|
305
|
RubikPattern patt = RubikPattern.getInstance();
|
306
|
String patternName = patt.getPatternName(ordinal,category,pattern);
|
307
|
mText.setText(patternName);
|
308
|
|
309
|
mNumMoves = patt.getNumMoves(ordinal,category,pattern);
|
310
|
int currMove= patt.getCurMove(ordinal,category,pattern);
|
311
|
|
312
|
mMovesText.setText(act.getString(R.string.mo_placeholder,currMove,mNumMoves));
|
313
|
}
|
314
|
|
315
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
316
|
|
317
|
public void savePreferences(SharedPreferences.Editor editor)
|
318
|
{
|
319
|
|
320
|
}
|
321
|
|
322
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
323
|
|
324
|
public void restorePreferences(SharedPreferences preferences)
|
325
|
{
|
326
|
|
327
|
}
|
328
|
}
|