1 |
1237d25d
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
// Copyright 2020 Leszek Koltunski //
|
3 |
|
|
// //
|
4 |
|
|
// This file is part of Magic Cube. //
|
5 |
|
|
// //
|
6 |
9d51b9d6
|
Leszek Koltunski
|
// Magic Cube is proprietary software licensed under an EULA which you should have received //
|
7 |
|
|
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html //
|
8 |
1237d25d
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
9 |
|
|
|
10 |
|
|
package org.distorted.config;
|
11 |
|
|
|
12 |
97a4ae23
|
Leszek Koltunski
|
import android.os.Build;
|
13 |
|
|
import android.util.TypedValue;
|
14 |
|
|
import android.view.Gravity;
|
15 |
1237d25d
|
Leszek Koltunski
|
import android.view.View;
|
16 |
97a4ae23
|
Leszek Koltunski
|
import android.widget.GridLayout;
|
17 |
302c76a0
|
Leszek Koltunski
|
import android.widget.ImageButton;
|
18 |
1237d25d
|
Leszek Koltunski
|
import android.widget.LinearLayout;
|
19 |
97a4ae23
|
Leszek Koltunski
|
import android.widget.PopupWindow;
|
20 |
|
|
import android.widget.ScrollView;
|
21 |
|
|
import android.widget.TextView;
|
22 |
|
|
|
23 |
432a5f2c
|
Leszek Koltunski
|
import org.distorted.helpers.PopupCreator;
|
24 |
1237d25d
|
Leszek Koltunski
|
|
25 |
|
|
import org.distorted.helpers.TransparentImageButton;
|
26 |
|
|
import org.distorted.main.R;
|
27 |
|
|
import org.distorted.main.RubikActivity;
|
28 |
302c76a0
|
Leszek Koltunski
|
import org.distorted.objects.RubikObject;
|
29 |
a7d8c3cd
|
Leszek Koltunski
|
import org.distorted.objects.RubikObjectList;
|
30 |
97a4ae23
|
Leszek Koltunski
|
|
31 |
|
|
import static android.view.View.inflate;
|
32 |
1237d25d
|
Leszek Koltunski
|
|
33 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
34 |
|
|
|
35 |
|
|
public class ConfigScreen
|
36 |
|
|
{
|
37 |
27401bea
|
Leszek Koltunski
|
private static final float TEXT_SIZE = 0.0310f;
|
38 |
|
|
private static final float PADDING = 0.0060f;
|
39 |
|
|
private static final float MARGIN = 0.0024f;
|
40 |
6e3fcb91
|
Leszek Koltunski
|
private static final int NUM_COLUMNS = 5;
|
41 |
97a4ae23
|
Leszek Koltunski
|
private static final int[] mLocation = new int[2];
|
42 |
|
|
|
43 |
|
|
private TransparentImageButton mBackButton, mObjectButton, mPrevButton, mNextButton;
|
44 |
|
|
private TextView mMovesText;
|
45 |
|
|
private PopupWindow mObjectPopup;
|
46 |
74d088c3
|
Leszek Koltunski
|
private ConfigScreenPane mPane;
|
47 |
97a4ae23
|
Leszek Koltunski
|
private int mObjectOrdinal;
|
48 |
|
|
private int mColCount, mRowCount, mMaxRowCount;
|
49 |
|
|
private int mObjectSize;
|
50 |
|
|
private int mBarHeight;
|
51 |
|
|
private float mButtonSize;
|
52 |
|
|
|
53 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
54 |
|
|
|
55 |
|
|
private void setupObjectWindow(final ConfigActivity act, final float width, final float height)
|
56 |
|
|
{
|
57 |
432a5f2c
|
Leszek Koltunski
|
int numObjects= RubikObjectList.getNumObjects();
|
58 |
|
|
mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
|
59 |
|
|
mColCount = NUM_COLUMNS;
|
60 |
|
|
|
61 |
27401bea
|
Leszek Koltunski
|
int cubeSize = (int)( (Math.min(width,(int)(height*0.7f))) / 9 );
|
62 |
|
|
int margin = (int)(height*RubikActivity.POPUP_MARGIN);
|
63 |
|
|
int padding = (int)(height*RubikActivity.POPUP_PADDING);
|
64 |
432a5f2c
|
Leszek Koltunski
|
mObjectSize = (int)(cubeSize + 2*margin + 0.5f);
|
65 |
|
|
mMaxRowCount = (int)((height-mBarHeight)/mObjectSize);
|
66 |
97a4ae23
|
Leszek Koltunski
|
|
67 |
|
|
ScrollView view = (ScrollView)inflate( act, R.layout.popup_object_simple, null);
|
68 |
|
|
GridLayout objectGrid = view.findViewById(R.id.objectGrid);
|
69 |
|
|
|
70 |
a88b947f
|
Leszek Koltunski
|
PopupCreator.createObjectGrid(objectGrid,act,mRowCount,mColCount,numObjects,margin,cubeSize,padding);
|
71 |
a7d8c3cd
|
Leszek Koltunski
|
|
72 |
432a5f2c
|
Leszek Koltunski
|
for(int child=0; child<numObjects; child++)
|
73 |
97a4ae23
|
Leszek Koltunski
|
{
|
74 |
302c76a0
|
Leszek Koltunski
|
final RubikObject obj = RubikObjectList.getObject(child);
|
75 |
|
|
View v = objectGrid.getChildAt(child);
|
76 |
|
|
ImageButton button = PopupCreator.getButton(obj,v);
|
77 |
432a5f2c
|
Leszek Koltunski
|
final int ordinal = child;
|
78 |
314e9ff0
|
Leszek Koltunski
|
|
79 |
97a4ae23
|
Leszek Koltunski
|
button.setOnClickListener( new View.OnClickListener()
|
80 |
|
|
{
|
81 |
|
|
@Override
|
82 |
|
|
public void onClick(View v)
|
83 |
|
|
{
|
84 |
919b830e
|
Leszek Koltunski
|
if( mObjectOrdinal!=ordinal )
|
85 |
97a4ae23
|
Leszek Koltunski
|
{
|
86 |
|
|
mObjectOrdinal = ordinal;
|
87 |
280dc794
|
Leszek Koltunski
|
act.changeObject(mObjectOrdinal);
|
88 |
a7d8c3cd
|
Leszek Koltunski
|
mMovesText.setText(act.getString(R.string.mo_placeholder,mObjectOrdinal+1,numObjects));
|
89 |
74d088c3
|
Leszek Koltunski
|
mPane.updatePane(act,mObjectOrdinal);
|
90 |
97a4ae23
|
Leszek Koltunski
|
}
|
91 |
|
|
|
92 |
|
|
mObjectPopup.dismiss();
|
93 |
|
|
}
|
94 |
|
|
});
|
95 |
|
|
}
|
96 |
432a5f2c
|
Leszek Koltunski
|
|
97 |
|
|
mObjectPopup = new PopupWindow(act);
|
98 |
|
|
mObjectPopup.setFocusable(true);
|
99 |
|
|
mObjectPopup.setContentView(view);
|
100 |
97a4ae23
|
Leszek Koltunski
|
}
|
101 |
1237d25d
|
Leszek Koltunski
|
|
102 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
103 |
|
|
|
104 |
|
|
private void setupBackButton(final ConfigActivity act)
|
105 |
|
|
{
|
106 |
|
|
int icon = RubikActivity.getDrawable(R.drawable.ui_small_smallback,R.drawable.ui_medium_smallback, R.drawable.ui_big_smallback, R.drawable.ui_huge_smallback);
|
107 |
97a4ae23
|
Leszek Koltunski
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
|
108 |
1237d25d
|
Leszek Koltunski
|
mBackButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
|
109 |
|
|
|
110 |
|
|
mBackButton.setOnClickListener( new View.OnClickListener()
|
111 |
|
|
{
|
112 |
|
|
@Override
|
113 |
|
|
public void onClick(View v)
|
114 |
|
|
{
|
115 |
|
|
act.finish();
|
116 |
|
|
}
|
117 |
|
|
});
|
118 |
|
|
}
|
119 |
|
|
|
120 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
121 |
|
|
|
122 |
27401bea
|
Leszek Koltunski
|
private void setupObjectButton(final ConfigActivity act, final int width, final int height)
|
123 |
1237d25d
|
Leszek Koltunski
|
{
|
124 |
27401bea
|
Leszek Koltunski
|
final int margin= (int)(height*MARGIN);
|
125 |
97a4ae23
|
Leszek Koltunski
|
final int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube_menu,R.drawable.ui_medium_cube_menu, R.drawable.ui_big_cube_menu, R.drawable.ui_huge_cube_menu);
|
126 |
|
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
|
127 |
|
|
mObjectButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
|
128 |
|
|
|
129 |
|
|
mObjectButton.setOnClickListener( new View.OnClickListener()
|
130 |
|
|
{
|
131 |
|
|
@Override
|
132 |
|
|
public void onClick(View view)
|
133 |
|
|
{
|
134 |
|
|
if( mObjectPopup==null )
|
135 |
|
|
{
|
136 |
|
|
setupObjectWindow(act,width,height);
|
137 |
|
|
}
|
138 |
|
|
|
139 |
919b830e
|
Leszek Koltunski
|
int rowCount = Math.min(mMaxRowCount,mRowCount);
|
140 |
|
|
View popupView = mObjectPopup.getContentView();
|
141 |
|
|
popupView.setSystemUiVisibility(RubikActivity.FLAGS);
|
142 |
|
|
displayPopup(act,view,mObjectPopup,mObjectSize*mColCount,mObjectSize*rowCount+5*margin,margin,margin);
|
143 |
97a4ae23
|
Leszek Koltunski
|
}
|
144 |
|
|
});
|
145 |
|
|
}
|
146 |
|
|
|
147 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
148 |
|
|
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
|
149 |
|
|
|
150 |
|
|
private void displayPopup(ConfigActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
|
151 |
|
|
{
|
152 |
|
|
View topLayout = act.findViewById(R.id.mainLayout);
|
153 |
|
|
boolean isFullScreen;
|
154 |
|
|
|
155 |
|
|
if( topLayout!=null )
|
156 |
|
|
{
|
157 |
|
|
topLayout.getLocationOnScreen(mLocation);
|
158 |
|
|
isFullScreen = (mLocation[1]==0);
|
159 |
|
|
}
|
160 |
|
|
else
|
161 |
|
|
{
|
162 |
|
|
isFullScreen = true;
|
163 |
|
|
}
|
164 |
|
|
|
165 |
|
|
try
|
166 |
|
|
{
|
167 |
|
|
// if on Android 11 or we are fullscreen
|
168 |
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R || isFullScreen )
|
169 |
|
|
{
|
170 |
|
|
window.showAsDropDown(view, xoff, yoff, Gravity.CENTER);
|
171 |
|
|
window.update(view, w, h);
|
172 |
|
|
}
|
173 |
|
|
else // Android 10 or below in pop-up mode or split-screen mode
|
174 |
|
|
{
|
175 |
|
|
view.getLocationOnScreen(mLocation);
|
176 |
|
|
int width = view.getWidth();
|
177 |
|
|
int height = view.getHeight();
|
178 |
|
|
int x = mLocation[0]+(width-w)/2;
|
179 |
|
|
int y = mLocation[1]+height+yoff;
|
180 |
|
|
|
181 |
|
|
window.showAsDropDown(view);
|
182 |
|
|
window.update(x,y,w,h);
|
183 |
|
|
}
|
184 |
|
|
}
|
185 |
|
|
catch( IllegalArgumentException iae )
|
186 |
|
|
{
|
187 |
|
|
// ignore, this means window is 'not attached to window manager' -
|
188 |
|
|
// which most probably is because we are already exiting the app.
|
189 |
|
|
}
|
190 |
1237d25d
|
Leszek Koltunski
|
}
|
191 |
|
|
|
192 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
193 |
|
|
|
194 |
a7d8c3cd
|
Leszek Koltunski
|
private void prevObject(ConfigActivity act, int numObjects)
|
195 |
1237d25d
|
Leszek Koltunski
|
{
|
196 |
97a4ae23
|
Leszek Koltunski
|
mObjectOrdinal--;
|
197 |
a7d8c3cd
|
Leszek Koltunski
|
if( mObjectOrdinal<0 ) mObjectOrdinal=numObjects-1;
|
198 |
97a4ae23
|
Leszek Koltunski
|
|
199 |
280dc794
|
Leszek Koltunski
|
act.changeObject(mObjectOrdinal);
|
200 |
74d088c3
|
Leszek Koltunski
|
mPane.updatePane(act,mObjectOrdinal);
|
201 |
97a4ae23
|
Leszek Koltunski
|
}
|
202 |
|
|
|
203 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
204 |
|
|
|
205 |
a7d8c3cd
|
Leszek Koltunski
|
private void nextObject(ConfigActivity act, int numObjects)
|
206 |
97a4ae23
|
Leszek Koltunski
|
{
|
207 |
|
|
mObjectOrdinal++;
|
208 |
a7d8c3cd
|
Leszek Koltunski
|
if( mObjectOrdinal>=numObjects ) mObjectOrdinal=0;
|
209 |
97a4ae23
|
Leszek Koltunski
|
|
210 |
280dc794
|
Leszek Koltunski
|
act.changeObject(mObjectOrdinal);
|
211 |
74d088c3
|
Leszek Koltunski
|
mPane.updatePane(act,mObjectOrdinal);
|
212 |
97a4ae23
|
Leszek Koltunski
|
}
|
213 |
|
|
|
214 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
215 |
|
|
|
216 |
|
|
private void setupPrevButton(final ConfigActivity act)
|
217 |
|
|
{
|
218 |
|
|
int icon = RubikActivity.getDrawable(R.drawable.ui_small_left,R.drawable.ui_medium_left, R.drawable.ui_big_left, R.drawable.ui_huge_left);
|
219 |
|
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
|
220 |
|
|
mPrevButton = new TransparentImageButton(act,icon,TransparentImageButton.GRAVITY_MIDDLE,params);
|
221 |
|
|
|
222 |
|
|
mPrevButton.setOnClickListener( new View.OnClickListener()
|
223 |
|
|
{
|
224 |
|
|
@Override
|
225 |
|
|
public void onClick(View v)
|
226 |
|
|
{
|
227 |
a7d8c3cd
|
Leszek Koltunski
|
int numObjects = RubikObjectList.getNumObjects();
|
228 |
|
|
prevObject(act,numObjects);
|
229 |
|
|
mMovesText.setText(act.getString(R.string.mo_placeholder,mObjectOrdinal+1,numObjects));
|
230 |
97a4ae23
|
Leszek Koltunski
|
}
|
231 |
|
|
});
|
232 |
|
|
}
|
233 |
|
|
|
234 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
235 |
|
|
|
236 |
|
|
private void setupNextButton(final ConfigActivity act)
|
237 |
|
|
{
|
238 |
|
|
int icon = RubikActivity.getDrawable(R.drawable.ui_small_right,R.drawable.ui_medium_right, R.drawable.ui_big_right, R.drawable.ui_huge_right);
|
239 |
|
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
|
240 |
|
|
mNextButton = new TransparentImageButton(act,icon,TransparentImageButton.GRAVITY_MIDDLE,params);
|
241 |
|
|
|
242 |
|
|
mNextButton.setOnClickListener( new View.OnClickListener()
|
243 |
|
|
{
|
244 |
|
|
@Override
|
245 |
|
|
public void onClick(View v)
|
246 |
|
|
{
|
247 |
a7d8c3cd
|
Leszek Koltunski
|
int numObjects = RubikObjectList.getNumObjects();
|
248 |
|
|
nextObject(act,numObjects);
|
249 |
|
|
mMovesText.setText(act.getString(R.string.mo_placeholder,mObjectOrdinal+1,numObjects));
|
250 |
97a4ae23
|
Leszek Koltunski
|
}
|
251 |
|
|
});
|
252 |
|
|
}
|
253 |
|
|
|
254 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
255 |
|
|
|
256 |
27401bea
|
Leszek Koltunski
|
private void setupTextView(final ConfigActivity act, final float height, int numObjects)
|
257 |
97a4ae23
|
Leszek Koltunski
|
{
|
258 |
27401bea
|
Leszek Koltunski
|
int padding = (int)(height*PADDING);
|
259 |
|
|
int margin = (int)(height*MARGIN);
|
260 |
97a4ae23
|
Leszek Koltunski
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,2.0f);
|
261 |
|
|
params.topMargin = margin;
|
262 |
|
|
params.bottomMargin = margin;
|
263 |
|
|
params.leftMargin = margin;
|
264 |
|
|
params.rightMargin = margin;
|
265 |
|
|
|
266 |
|
|
mMovesText = new TextView(act);
|
267 |
|
|
mMovesText.setTextSize(20);
|
268 |
|
|
mMovesText.setLayoutParams(params);
|
269 |
|
|
mMovesText.setPadding(padding,0,padding,0);
|
270 |
|
|
mMovesText.setGravity(Gravity.CENTER);
|
271 |
|
|
mMovesText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
|
272 |
a7d8c3cd
|
Leszek Koltunski
|
mMovesText.setText(act.getString(R.string.mo_placeholder,mObjectOrdinal+1,numObjects));
|
273 |
97a4ae23
|
Leszek Koltunski
|
}
|
274 |
|
|
|
275 |
8bf8fe70
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
276 |
|
|
|
277 |
74d088c3
|
Leszek Koltunski
|
void onAttachedToWindow(final ConfigActivity act, final int objectOrdinal)
|
278 |
97a4ae23
|
Leszek Koltunski
|
{
|
279 |
a7d8c3cd
|
Leszek Koltunski
|
int numObjects = RubikObjectList.getNumObjects();
|
280 |
97a4ae23
|
Leszek Koltunski
|
int width = act.getScreenWidthInPixels();
|
281 |
27401bea
|
Leszek Koltunski
|
int height= act.getScreenHeightInPixels();
|
282 |
97a4ae23
|
Leszek Koltunski
|
mBarHeight = act.getHeightBar();
|
283 |
27401bea
|
Leszek Koltunski
|
mButtonSize = height*TEXT_SIZE;
|
284 |
97a4ae23
|
Leszek Koltunski
|
mObjectOrdinal = objectOrdinal;
|
285 |
|
|
|
286 |
74d088c3
|
Leszek Koltunski
|
LinearLayout.LayoutParams paramsL = new LinearLayout.LayoutParams(width/4, LinearLayout.LayoutParams.MATCH_PARENT);
|
287 |
|
|
LinearLayout.LayoutParams paramsM = new LinearLayout.LayoutParams(width/2, LinearLayout.LayoutParams.MATCH_PARENT);
|
288 |
|
|
LinearLayout.LayoutParams paramsR = new LinearLayout.LayoutParams(width/4, LinearLayout.LayoutParams.MATCH_PARENT);
|
289 |
97a4ae23
|
Leszek Koltunski
|
|
290 |
|
|
LinearLayout layoutLeft = new LinearLayout(act);
|
291 |
|
|
layoutLeft.setLayoutParams(paramsL);
|
292 |
|
|
LinearLayout layoutMid = new LinearLayout(act);
|
293 |
|
|
layoutMid.setLayoutParams(paramsM);
|
294 |
|
|
LinearLayout layoutRight= new LinearLayout(act);
|
295 |
|
|
layoutRight.setLayoutParams(paramsR);
|
296 |
|
|
|
297 |
27401bea
|
Leszek Koltunski
|
setupObjectButton(act,width,height);
|
298 |
97a4ae23
|
Leszek Koltunski
|
setupPrevButton(act);
|
299 |
|
|
setupNextButton(act);
|
300 |
27401bea
|
Leszek Koltunski
|
setupTextView(act,height,numObjects);
|
301 |
97a4ae23
|
Leszek Koltunski
|
setupBackButton(act);
|
302 |
|
|
|
303 |
|
|
layoutLeft.addView(mObjectButton);
|
304 |
|
|
layoutMid.addView(mPrevButton);
|
305 |
|
|
layoutMid.addView(mMovesText);
|
306 |
|
|
layoutMid.addView(mNextButton);
|
307 |
|
|
layoutRight.addView(mBackButton);
|
308 |
|
|
|
309 |
|
|
LinearLayout layout = act.findViewById(R.id.lowerBar);
|
310 |
|
|
layout.removeAllViews();
|
311 |
|
|
layout.addView(layoutLeft);
|
312 |
|
|
layout.addView(layoutMid);
|
313 |
|
|
layout.addView(layoutRight);
|
314 |
1237d25d
|
Leszek Koltunski
|
|
315 |
74d088c3
|
Leszek Koltunski
|
mPane = new ConfigScreenPane(act,mObjectOrdinal);
|
316 |
1237d25d
|
Leszek Koltunski
|
}
|
317 |
|
|
}
|