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.uistate;
|
21
|
|
22
|
import android.content.SharedPreferences;
|
23
|
import android.os.Bundle;
|
24
|
import android.support.v4.app.FragmentManager;
|
25
|
import android.util.DisplayMetrics;
|
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.dialog.RubikDialogPattern;
|
35
|
import org.distorted.magic.R;
|
36
|
import org.distorted.magic.RubikActivity;
|
37
|
import org.distorted.magic.RubikPostRender;
|
38
|
import org.distorted.object.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
|
|
53
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
54
|
|
55
|
RubikStatePattern()
|
56
|
{
|
57
|
|
58
|
}
|
59
|
|
60
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
61
|
|
62
|
void leaveState(RubikActivity act)
|
63
|
{
|
64
|
RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
|
65
|
|
66
|
RubikObjectList object = RubikPatternList.getObject(mPatternOrdinal);
|
67
|
int size = RubikPatternList.getSize(mPatternOrdinal);
|
68
|
|
69
|
if( !play.setObjectAndSize(object,size) )
|
70
|
{
|
71
|
int objectPlay= play.getObject();
|
72
|
int sizePlay = play.getSize();
|
73
|
|
74
|
act.changeObject(RubikObjectList.getObject(objectPlay),sizePlay,null);
|
75
|
}
|
76
|
}
|
77
|
|
78
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
79
|
|
80
|
void enterState(final RubikActivity act)
|
81
|
{
|
82
|
RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
|
83
|
int obj = play.getObject();
|
84
|
int size = play.getSize();
|
85
|
|
86
|
mPatternOrdinal = RubikPatternList.getOrdinal(obj,size);
|
87
|
|
88
|
if( mPatternOrdinal<0 )
|
89
|
{
|
90
|
mPatternOrdinal = RubikObjectList.getSizeIndex(RubikStatePlay.DEF_OBJECT,RubikStatePlay.DEF_SIZE);
|
91
|
}
|
92
|
|
93
|
FragmentManager mana = act.getSupportFragmentManager();
|
94
|
RubikDialogPattern diag = (RubikDialogPattern) mana.findFragmentByTag(RubikDialogPattern.getDialogTag());
|
95
|
|
96
|
if( diag==null ) showDialog(mana);
|
97
|
|
98
|
LayoutInflater inflater = act.getLayoutInflater();
|
99
|
|
100
|
// TOP ////////////////////////////
|
101
|
LinearLayout layoutTop = act.findViewById(R.id.upperBar);
|
102
|
layoutTop.removeAllViews();
|
103
|
mText = (TextView)inflater.inflate(R.layout.upper_pattern_text, null);
|
104
|
mText.setText(R.string.patterns);
|
105
|
layoutTop.addView(mText);
|
106
|
|
107
|
// BOT ////////////////////////////
|
108
|
DisplayMetrics metrics = act.getResources().getDisplayMetrics();
|
109
|
final float scale = metrics.density;
|
110
|
|
111
|
if( mPrevButton==null ) setupPrevButton(act,scale);
|
112
|
if( mNextButton==null ) setupNextButton(act,scale);
|
113
|
if( mMovesText ==null ) setupTextView(act,scale);
|
114
|
|
115
|
LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
|
116
|
layoutLeft.removeAllViews();
|
117
|
layoutLeft.addView(mPrevButton);
|
118
|
layoutLeft.addView(mMovesText);
|
119
|
layoutLeft.addView(mNextButton);
|
120
|
|
121
|
if( mBackButton==null ) setupBackButton(act,scale);
|
122
|
|
123
|
LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
|
124
|
layoutRight.removeAllViews();
|
125
|
layoutRight.addView(mBackButton);
|
126
|
}
|
127
|
|
128
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
129
|
|
130
|
private void showDialog(FragmentManager manager)
|
131
|
{
|
132
|
RubikDialogPattern diag = new RubikDialogPattern();
|
133
|
Bundle bundle = new Bundle();
|
134
|
bundle.putInt("tab", mPatternOrdinal );
|
135
|
diag.setArguments(bundle);
|
136
|
diag.show( manager, RubikDialogPattern.getDialogTag() );
|
137
|
}
|
138
|
|
139
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
140
|
|
141
|
private void setupBackButton(final RubikActivity act, final float scale)
|
142
|
{
|
143
|
int padding = (int)(3*scale + 0.5f);
|
144
|
LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
|
145
|
mBackButton = new Button(act);
|
146
|
mBackButton.setLayoutParams(backParams);
|
147
|
mBackButton.setPadding(padding,0,padding,0);
|
148
|
mBackButton.setText(R.string.back);
|
149
|
|
150
|
mBackButton.setOnClickListener( new View.OnClickListener()
|
151
|
{
|
152
|
@Override
|
153
|
public void onClick(View v)
|
154
|
{
|
155
|
FragmentManager mana = act.getSupportFragmentManager();
|
156
|
RubikDialogPattern diag = (RubikDialogPattern) mana.findFragmentByTag(RubikDialogPattern.getDialogTag());
|
157
|
|
158
|
if( diag==null )
|
159
|
{
|
160
|
showDialog(mana);
|
161
|
}
|
162
|
else
|
163
|
{
|
164
|
diag.rememberState();
|
165
|
diag.dismiss();
|
166
|
RubikState.goBack(act);
|
167
|
}
|
168
|
}
|
169
|
});
|
170
|
}
|
171
|
|
172
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
173
|
|
174
|
private void setupPrevButton(final RubikActivity act, final float scale)
|
175
|
{
|
176
|
int padding = (int)(3*scale + 0.5f);
|
177
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
|
178
|
mPrevButton = new ImageButton(act);
|
179
|
mPrevButton.setLayoutParams(params);
|
180
|
mPrevButton.setPadding(padding,0,padding,0);
|
181
|
mPrevButton.setImageResource(R.drawable.left);
|
182
|
|
183
|
mPrevButton.setOnClickListener( new View.OnClickListener()
|
184
|
{
|
185
|
@Override
|
186
|
public void onClick(View v)
|
187
|
{
|
188
|
RubikPattern pattern = RubikPattern.getInstance();
|
189
|
RubikPostRender post = act.getPostRender();
|
190
|
pattern.backMove( post, mPatternOrdinal, mCategory, mPattern);
|
191
|
int currMove = pattern.getCurMove(mPatternOrdinal, mCategory, mPattern);
|
192
|
mMovesText.setText(act.getString(R.string.mo_placeholder,currMove,mNumMoves));
|
193
|
}
|
194
|
});
|
195
|
}
|
196
|
|
197
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
198
|
|
199
|
private void setupNextButton(final RubikActivity act, final float scale)
|
200
|
{
|
201
|
int padding = (int)( 3*scale + 0.5f);
|
202
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
|
203
|
mNextButton = new ImageButton(act);
|
204
|
mNextButton.setLayoutParams(params);
|
205
|
mNextButton.setPadding(padding,0,padding,0);
|
206
|
mNextButton.setImageResource(R.drawable.right);
|
207
|
|
208
|
mNextButton.setOnClickListener( new View.OnClickListener()
|
209
|
{
|
210
|
@Override
|
211
|
public void onClick(View v)
|
212
|
{
|
213
|
RubikPattern pattern = RubikPattern.getInstance();
|
214
|
RubikPostRender post = act.getPostRender();
|
215
|
pattern.makeMove( post, mPatternOrdinal, mCategory, mPattern);
|
216
|
int currMove = pattern.getCurMove(mPatternOrdinal, mCategory, mPattern);
|
217
|
mMovesText.setText(act.getString(R.string.mo_placeholder,currMove,mNumMoves));
|
218
|
}
|
219
|
});
|
220
|
}
|
221
|
|
222
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
223
|
|
224
|
private void setupTextView(final RubikActivity act, final float scale)
|
225
|
{
|
226
|
int padding = (int)( 3*scale + 0.5f);
|
227
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,2.0f);
|
228
|
|
229
|
mMovesText = new TextView(act);
|
230
|
mMovesText.setTextSize(20);
|
231
|
mMovesText.setLayoutParams(params);
|
232
|
mMovesText.setPadding(padding,0,padding,0);
|
233
|
mMovesText.setGravity(Gravity.CENTER);
|
234
|
mMovesText.setText(act.getString(R.string.mo_placeholder,0,0));
|
235
|
}
|
236
|
|
237
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
238
|
|
239
|
public void setPattern(final RubikActivity act, int ordinal, int category, int pattern)
|
240
|
{
|
241
|
mPatternOrdinal = ordinal;
|
242
|
mCategory = category;
|
243
|
mPattern = pattern;
|
244
|
|
245
|
RubikPattern patt = RubikPattern.getInstance();
|
246
|
String patternName = patt.getPatternName(ordinal,category,pattern);
|
247
|
mText.setText(patternName);
|
248
|
|
249
|
mNumMoves = patt.getNumMoves(ordinal,category,pattern);
|
250
|
int currMove= patt.getCurMove(ordinal,category,pattern);
|
251
|
|
252
|
mMovesText.setText(act.getString(R.string.mo_placeholder,currMove,mNumMoves));
|
253
|
}
|
254
|
|
255
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
256
|
|
257
|
public void savePreferences(SharedPreferences.Editor editor)
|
258
|
{
|
259
|
mBackButton= null;
|
260
|
mPrevButton= null;
|
261
|
mNextButton= null;
|
262
|
mMovesText = null;
|
263
|
}
|
264
|
|
265
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
266
|
|
267
|
public void restorePreferences(SharedPreferences preferences)
|
268
|
{
|
269
|
|
270
|
}
|
271
|
}
|