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.Context;
|
23
|
import android.content.SharedPreferences;
|
24
|
import android.graphics.drawable.BitmapDrawable;
|
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.PopupWindow;
|
33
|
import android.widget.TextView;
|
34
|
|
35
|
import org.distorted.magic.R;
|
36
|
import org.distorted.magic.RubikActivity;
|
37
|
import org.distorted.object.RubikObjectList;
|
38
|
import org.distorted.patterns.RubikPattern;
|
39
|
|
40
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
41
|
|
42
|
public class RubikStatePattern extends RubikStateAbstract
|
43
|
{
|
44
|
private ImageButton mObjButton;
|
45
|
private Button mBackButton;
|
46
|
private PopupWindow mPopup;
|
47
|
private int mLayoutWidth, mLayoutHeight;
|
48
|
private LinearLayout mLayout;
|
49
|
private int mSize;
|
50
|
|
51
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
52
|
|
53
|
RubikStatePattern()
|
54
|
{
|
55
|
|
56
|
}
|
57
|
|
58
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
59
|
|
60
|
void leaveState(RubikActivity act)
|
61
|
{
|
62
|
RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
|
63
|
|
64
|
if( !play.setObjectAndSize(RubikObjectList.CUBE, mSize) )
|
65
|
{
|
66
|
int object= play.getObject();
|
67
|
int size = play.getSize();
|
68
|
|
69
|
act.changeObject(RubikObjectList.getObject(object),size);
|
70
|
}
|
71
|
}
|
72
|
|
73
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
74
|
|
75
|
void enterState(final RubikActivity act)
|
76
|
{
|
77
|
RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
|
78
|
int obj = play.getObject();
|
79
|
int size = play.getSize();
|
80
|
|
81
|
if( size>=RubikPattern.MIN_CUBE && size<=RubikPattern.MAX_CUBE && obj==RubikObjectList.CUBE.ordinal() )
|
82
|
{
|
83
|
mSize = size;
|
84
|
}
|
85
|
else
|
86
|
{
|
87
|
mSize = RubikStatePlay.DEF_SIZE;
|
88
|
act.changeObject(RubikObjectList.CUBE,mSize);
|
89
|
}
|
90
|
|
91
|
LayoutInflater inflater = act.getLayoutInflater();
|
92
|
|
93
|
// TOP ////////////////////////////
|
94
|
LinearLayout layoutTop = act.findViewById(R.id.mainTitle);
|
95
|
layoutTop.removeAllViews();
|
96
|
TextView text = (TextView)inflater.inflate(R.layout.upper_text, null);
|
97
|
text.setText(R.string.patterns);
|
98
|
layoutTop.addView(text);
|
99
|
|
100
|
// BOT ////////////////////////////
|
101
|
DisplayMetrics metrics = act.getResources().getDisplayMetrics();
|
102
|
final float scale = metrics.density;
|
103
|
|
104
|
if( mObjButton==null ) setupObjectButton(act,scale);
|
105
|
|
106
|
LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
|
107
|
layoutLeft.removeAllViews();
|
108
|
layoutLeft.addView(mObjButton);
|
109
|
|
110
|
if( mBackButton==null ) setupBackButton(act,scale);
|
111
|
|
112
|
LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
|
113
|
layoutRight.removeAllViews();
|
114
|
layoutRight.addView(mBackButton);
|
115
|
|
116
|
if( mPopup==null ) setupPopupWindow(act, scale);
|
117
|
}
|
118
|
|
119
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
120
|
|
121
|
private void setupObjectButton(final RubikActivity act, final float scale)
|
122
|
{
|
123
|
int padding = (int)(3*scale + 0.5f);
|
124
|
LinearLayout.LayoutParams objectParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT);
|
125
|
mObjButton = new ImageButton(act);
|
126
|
mObjButton.setLayoutParams(objectParams);
|
127
|
mObjButton.setPadding(padding,0,padding,0);
|
128
|
mObjButton.setImageResource(R.drawable.cube_menu);
|
129
|
|
130
|
mObjButton.setOnClickListener( new View.OnClickListener()
|
131
|
{
|
132
|
@Override
|
133
|
public void onClick(View view)
|
134
|
{
|
135
|
int total = RubikPattern.NUM_CUBES;
|
136
|
boolean vertical = act.isVertical();
|
137
|
mLayout.setOrientation(vertical ? LinearLayout.VERTICAL:LinearLayout.HORIZONTAL);
|
138
|
|
139
|
int height = view.getHeight();
|
140
|
int width = view.getWidth();
|
141
|
int laywid = mLayoutWidth * (vertical? 1:total);
|
142
|
int layhei = mLayoutHeight* (vertical? total:1);
|
143
|
|
144
|
mPopup.showAsDropDown(view, (width-laywid)/2, -height-layhei, Gravity.LEFT);
|
145
|
}
|
146
|
});
|
147
|
}
|
148
|
|
149
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
150
|
|
151
|
private void setupBackButton(final RubikActivity act, final float scale)
|
152
|
{
|
153
|
int padding = (int)(3*scale + 0.5f);
|
154
|
LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
|
155
|
mBackButton = new Button(act);
|
156
|
mBackButton.setLayoutParams(backParams);
|
157
|
mBackButton.setId(BUTTON_ID_BACK);
|
158
|
mBackButton.setPadding(padding,0,padding,0);
|
159
|
mBackButton.setText(R.string.back);
|
160
|
|
161
|
mBackButton.setOnClickListener( new View.OnClickListener()
|
162
|
{
|
163
|
@Override
|
164
|
public void onClick(View v)
|
165
|
{
|
166
|
RubikState.goBack(act);
|
167
|
}
|
168
|
});
|
169
|
}
|
170
|
|
171
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
172
|
|
173
|
private void setupPopupWindow(final RubikActivity act, final float scale)
|
174
|
{
|
175
|
LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
176
|
final View layout = layoutInflater.inflate(R.layout.popup_objects, null);
|
177
|
mLayout = layout.findViewById(R.id.popup);
|
178
|
|
179
|
mPopup = new PopupWindow(act);
|
180
|
mPopup.setContentView(layout);
|
181
|
mPopup.setFocusable(true);
|
182
|
int margin = (int)(5*scale + 0.5f);
|
183
|
|
184
|
BitmapDrawable bd = (BitmapDrawable) act.getResources().getDrawable(R.drawable.cube2);
|
185
|
int cubeWidth = bd.getIntrinsicWidth();
|
186
|
int cubeHeight = bd.getIntrinsicHeight();
|
187
|
|
188
|
mLayoutWidth = (int)(cubeWidth + 2*margin + 0.5f);
|
189
|
mLayoutHeight= (int)(cubeHeight+ 2*margin + 0.5f);
|
190
|
|
191
|
final int[] icons = {R.drawable.cube2, R.drawable.cube3, R.drawable.cube4, R.drawable.cube5 };
|
192
|
final int[] sizes = {2,3,4,5};
|
193
|
int len = icons.length;
|
194
|
|
195
|
for(int i=0; i<len; i++)
|
196
|
{
|
197
|
final int size = i;
|
198
|
|
199
|
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
|
200
|
p.setMargins(margin, margin, margin, margin);
|
201
|
|
202
|
ImageButton button = new ImageButton(act);
|
203
|
button.setLayoutParams(p);
|
204
|
|
205
|
button.setBackgroundResource(icons[i]);
|
206
|
button.setOnClickListener( new View.OnClickListener()
|
207
|
{
|
208
|
@Override
|
209
|
public void onClick(View v)
|
210
|
{
|
211
|
mSize = sizes[size];
|
212
|
act.changeObject(RubikObjectList.CUBE,sizes[size]);
|
213
|
mPopup.dismiss();
|
214
|
}
|
215
|
});
|
216
|
|
217
|
mLayout.addView(button);
|
218
|
}
|
219
|
}
|
220
|
|
221
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
222
|
|
223
|
public void savePreferences(SharedPreferences.Editor editor)
|
224
|
{
|
225
|
mObjButton = null;
|
226
|
mBackButton= null;
|
227
|
|
228
|
if( mPopup!=null )
|
229
|
{
|
230
|
mPopup.dismiss();
|
231
|
mPopup = null;
|
232
|
}
|
233
|
}
|
234
|
|
235
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
236
|
|
237
|
public void restorePreferences(SharedPreferences preferences)
|
238
|
{
|
239
|
|
240
|
}
|
241
|
}
|