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.LayoutInflater;
|
27
|
import android.view.View;
|
28
|
import android.widget.Button;
|
29
|
import android.widget.ImageButton;
|
30
|
import android.widget.LinearLayout;
|
31
|
import android.widget.TextView;
|
32
|
|
33
|
import org.distorted.dialog.RubikDialogPattern;
|
34
|
import org.distorted.magic.R;
|
35
|
import org.distorted.magic.RubikActivity;
|
36
|
import org.distorted.object.RubikObjectList;
|
37
|
import org.distorted.patterns.RubikPattern;
|
38
|
|
39
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
40
|
|
41
|
public class RubikStatePattern extends RubikStateAbstract
|
42
|
{
|
43
|
// TODO: read this from upper_text.xml; this is the height of the TextView there.
|
44
|
private static final int DEFAULT_TEXT_SIZE = 30;
|
45
|
|
46
|
private TextView mText;
|
47
|
private Button mBackButton;
|
48
|
private ImageButton mPrevButton, mNextButton;
|
49
|
private TextView mMovesText;
|
50
|
private int mSize;
|
51
|
|
52
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
53
|
|
54
|
RubikStatePattern()
|
55
|
{
|
56
|
|
57
|
}
|
58
|
|
59
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
60
|
|
61
|
void leaveState(RubikActivity act)
|
62
|
{
|
63
|
RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
|
64
|
|
65
|
if( !play.setObjectAndSize(RubikObjectList.CUBE, mSize) )
|
66
|
{
|
67
|
int object= play.getObject();
|
68
|
int size = play.getSize();
|
69
|
|
70
|
act.changeObject(RubikObjectList.getObject(object),size);
|
71
|
}
|
72
|
}
|
73
|
|
74
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
75
|
|
76
|
void enterState(final RubikActivity act)
|
77
|
{
|
78
|
RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
|
79
|
int obj = play.getObject();
|
80
|
int size = play.getSize();
|
81
|
|
82
|
if( size>=RubikPattern.MIN_CUBE && size<=RubikPattern.MAX_CUBE && obj==RubikObjectList.CUBE.ordinal() )
|
83
|
{
|
84
|
mSize = size;
|
85
|
}
|
86
|
else
|
87
|
{
|
88
|
mSize = RubikStatePlay.DEF_SIZE;
|
89
|
act.changeObject(RubikObjectList.CUBE,mSize);
|
90
|
}
|
91
|
|
92
|
FragmentManager mana = act.getSupportFragmentManager();
|
93
|
RubikDialogPattern diag = (RubikDialogPattern) mana.findFragmentByTag(RubikDialogPattern.getDialogTag());
|
94
|
|
95
|
if( diag==null ) showDialog(mana);
|
96
|
|
97
|
LayoutInflater inflater = act.getLayoutInflater();
|
98
|
|
99
|
// TOP ////////////////////////////
|
100
|
LinearLayout layoutTop = act.findViewById(R.id.mainTitle);
|
101
|
layoutTop.removeAllViews();
|
102
|
mText = (TextView)inflater.inflate(R.layout.upper_text, null);
|
103
|
mText.setText(R.string.patterns);
|
104
|
layoutTop.addView(mText);
|
105
|
|
106
|
// BOT ////////////////////////////
|
107
|
DisplayMetrics metrics = act.getResources().getDisplayMetrics();
|
108
|
final float scale = metrics.density;
|
109
|
|
110
|
if( mPrevButton==null ) setupPrevButton(act,scale);
|
111
|
if( mNextButton==null ) setupNextButton(act,scale);
|
112
|
if( mMovesText ==null ) setupTextView(act,scale);
|
113
|
|
114
|
LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
|
115
|
layoutLeft.removeAllViews();
|
116
|
layoutLeft.addView(mPrevButton);
|
117
|
layoutLeft.addView(mMovesText);
|
118
|
layoutLeft.addView(mNextButton);
|
119
|
|
120
|
if( mBackButton==null ) setupBackButton(act,scale);
|
121
|
|
122
|
LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
|
123
|
layoutRight.removeAllViews();
|
124
|
layoutRight.addView(mBackButton);
|
125
|
}
|
126
|
|
127
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
128
|
|
129
|
private void showDialog(FragmentManager manager)
|
130
|
{
|
131
|
Bundle bundle = new Bundle();
|
132
|
int object = RubikObjectList.CUBE.ordinal();
|
133
|
int sizeIndex = RubikObjectList.getSizeIndex(object,mSize);
|
134
|
bundle.putInt("tab", RubikObjectList.pack(object,sizeIndex) );
|
135
|
|
136
|
RubikDialogPattern diag = new RubikDialogPattern();
|
137
|
diag.setArguments(bundle);
|
138
|
diag.show( manager, RubikDialogPattern.getDialogTag() );
|
139
|
}
|
140
|
|
141
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
142
|
|
143
|
private void setupBackButton(final RubikActivity act, final float scale)
|
144
|
{
|
145
|
int padding = (int)(3*scale + 0.5f);
|
146
|
LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
|
147
|
mBackButton = new Button(act);
|
148
|
mBackButton.setLayoutParams(backParams);
|
149
|
mBackButton.setPadding(padding,0,padding,0);
|
150
|
mBackButton.setText(R.string.back);
|
151
|
|
152
|
mBackButton.setOnClickListener( new View.OnClickListener()
|
153
|
{
|
154
|
@Override
|
155
|
public void onClick(View v)
|
156
|
{
|
157
|
FragmentManager mana = act.getSupportFragmentManager();
|
158
|
RubikDialogPattern diag = (RubikDialogPattern) mana.findFragmentByTag(RubikDialogPattern.getDialogTag());
|
159
|
|
160
|
if( diag==null )
|
161
|
{
|
162
|
mText.setTextSize(DEFAULT_TEXT_SIZE);
|
163
|
mText.setText(R.string.patterns);
|
164
|
showDialog(mana);
|
165
|
}
|
166
|
else
|
167
|
{
|
168
|
diag.rememberCategories();
|
169
|
diag.dismiss();
|
170
|
RubikState.goBack(act);
|
171
|
}
|
172
|
}
|
173
|
});
|
174
|
}
|
175
|
|
176
|
|
177
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
178
|
|
179
|
private void setupPrevButton(final RubikActivity act, final float scale)
|
180
|
{
|
181
|
int padding = (int)(3*scale + 0.5f);
|
182
|
int length = (int)(40*scale + 0.5f);
|
183
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(length,LinearLayout.LayoutParams.MATCH_PARENT);
|
184
|
mPrevButton = new ImageButton(act);
|
185
|
mPrevButton.setLayoutParams(params);
|
186
|
mPrevButton.setPadding(padding,0,padding,0);
|
187
|
mPrevButton.setImageResource(R.drawable.left);
|
188
|
|
189
|
mPrevButton.setOnClickListener( new View.OnClickListener()
|
190
|
{
|
191
|
@Override
|
192
|
public void onClick(View v)
|
193
|
{
|
194
|
android.util.Log.e("patt", "prev button clicked!");
|
195
|
}
|
196
|
});
|
197
|
}
|
198
|
|
199
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
200
|
|
201
|
private void setupNextButton(final RubikActivity act, final float scale)
|
202
|
{
|
203
|
int padding = (int)( 3*scale + 0.5f);
|
204
|
int length = (int)(40*scale + 0.5f);
|
205
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(length,LinearLayout.LayoutParams.MATCH_PARENT);
|
206
|
mNextButton = new ImageButton(act);
|
207
|
mNextButton.setLayoutParams(params);
|
208
|
mNextButton.setPadding(padding,0,padding,0);
|
209
|
mNextButton.setImageResource(R.drawable.right);
|
210
|
|
211
|
mNextButton.setOnClickListener( new View.OnClickListener()
|
212
|
{
|
213
|
@Override
|
214
|
public void onClick(View v)
|
215
|
{
|
216
|
android.util.Log.e("patt", "next button clicked!");
|
217
|
}
|
218
|
});
|
219
|
}
|
220
|
|
221
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
222
|
|
223
|
private void setupTextView(final RubikActivity act, final float scale)
|
224
|
{
|
225
|
int padding = (int)( 3*scale + 0.5f);
|
226
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT);
|
227
|
|
228
|
mMovesText = new TextView(act);
|
229
|
mMovesText.setLayoutParams(params);
|
230
|
mMovesText.setPadding(padding,0,padding,0);
|
231
|
|
232
|
mMovesText.setText("aaa");
|
233
|
}
|
234
|
|
235
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
236
|
|
237
|
public void setPattern(int sizeIndex, int category, int pattern)
|
238
|
{
|
239
|
mSize = RubikObjectList.CUBE.getSizes()[sizeIndex];
|
240
|
|
241
|
RubikPattern patt = RubikPattern.getInstance();
|
242
|
String patternName = patt.getPatternName(sizeIndex,category,pattern);
|
243
|
mText.setText(patternName);
|
244
|
}
|
245
|
|
246
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
247
|
|
248
|
public void savePreferences(SharedPreferences.Editor editor)
|
249
|
{
|
250
|
mBackButton= null;
|
251
|
mPrevButton= null;
|
252
|
mNextButton= null;
|
253
|
mMovesText = null;
|
254
|
}
|
255
|
|
256
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
257
|
|
258
|
public void restorePreferences(SharedPreferences preferences)
|
259
|
{
|
260
|
|
261
|
}
|
262
|
}
|