Revision 58fd2ec0
Added by Leszek Koltunski 3 months ago
src/main/java/org/distorted/info/InfoScreen.java | ||
---|---|---|
9 | 9 |
|
10 | 10 |
package org.distorted.info; |
11 | 11 |
|
12 |
import android.os.Build; |
|
13 |
import android.util.TypedValue; |
|
14 |
import android.view.Gravity; |
|
15 | 12 |
import android.view.View; |
16 |
import android.widget.GridLayout; |
|
17 |
import android.widget.ImageButton; |
|
18 | 13 |
import android.widget.LinearLayout; |
19 |
import android.widget.PopupWindow; |
|
20 |
import android.widget.ScrollView; |
|
21 |
import android.widget.TextView; |
|
22 |
|
|
23 |
import org.distorted.helpers.ObjectGridCreator; |
|
24 | 14 |
|
25 | 15 |
import org.distorted.helpers.TransparentImageButton; |
26 |
import org.distorted.main.MainActivity; |
|
27 | 16 |
import org.distorted.main.R; |
28 |
import org.distorted.objects.RubikObject; |
|
29 |
import org.distorted.objects.RubikObjectList; |
|
30 |
|
|
31 |
import static android.view.View.inflate; |
|
32 | 17 |
|
33 | 18 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
34 | 19 |
|
35 | 20 |
public class InfoScreen |
36 | 21 |
{ |
37 |
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 |
private static final int[] mLocation = new int[2]; |
|
41 |
private static final float POPUP_W = 0.8f; |
|
42 |
|
|
43 |
private TransparentImageButton mBackButton, mObjectButton, mPrevButton, mNextButton; |
|
44 |
private TextView mMovesText; |
|
45 |
private PopupWindow mObjectPopup; |
|
22 |
private TransparentImageButton mBackButton; |
|
46 | 23 |
private InfoScreenPane mPane; |
47 |
private int mObjectOrdinal; |
|
48 |
private float mButtonSize; |
|
49 |
|
|
50 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
51 |
|
|
52 |
private void setupObjectWindow(final InfoActivity act, final float width, final float height) |
|
53 |
{ |
|
54 |
int numObjects= RubikObjectList.getNumObjects(); |
|
55 |
ScrollView view = (ScrollView)inflate( act, R.layout.popup_object_simple, null); |
|
56 |
GridLayout objectGrid = view.findViewById(R.id.objectGrid); |
|
57 |
|
|
58 |
int[] objects = new int[numObjects]; |
|
59 |
for(int i=0; i<numObjects; i++) objects[i] = i; |
|
60 |
|
|
61 |
ObjectGridCreator creator = new ObjectGridCreator(width); |
|
62 |
creator.createObjectGridClassic(objectGrid,act,objects); |
|
63 |
|
|
64 |
for(int child=0; child<numObjects; child++) |
|
65 |
{ |
|
66 |
final RubikObject obj = RubikObjectList.getObject(child); |
|
67 |
View v = objectGrid.getChildAt(child); |
|
68 |
ImageButton button = creator.getButton(obj,v); |
|
69 |
final int ordinal = child; |
|
70 |
|
|
71 |
button.setOnClickListener( new View.OnClickListener() |
|
72 |
{ |
|
73 |
@Override |
|
74 |
public void onClick(View v) |
|
75 |
{ |
|
76 |
if( mObjectOrdinal!=ordinal ) |
|
77 |
{ |
|
78 |
mObjectOrdinal = ordinal; |
|
79 |
act.changeObject(mObjectOrdinal); |
|
80 |
mMovesText.setText(act.getString(R.string.mo_placeholder,mObjectOrdinal+1,numObjects)); |
|
81 |
mPane.updatePane(act,mObjectOrdinal); |
|
82 |
} |
|
83 |
|
|
84 |
mObjectPopup.dismiss(); |
|
85 |
} |
|
86 |
}); |
|
87 |
} |
|
88 |
|
|
89 |
mObjectPopup = new PopupWindow(act); |
|
90 |
mObjectPopup.setFocusable(true); |
|
91 |
mObjectPopup.setContentView(view); |
|
92 |
} |
|
93 | 24 |
|
94 | 25 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
95 | 26 |
|
... | ... | |
108 | 39 |
}); |
109 | 40 |
} |
110 | 41 |
|
111 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
112 |
|
|
113 |
private void setupObjectButton(final InfoActivity act, final int width, final int height) |
|
114 |
{ |
|
115 |
final int margin= (int)(height*MARGIN); |
|
116 |
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT); |
|
117 |
mObjectButton = new TransparentImageButton(act,R.drawable.ui_cube_menu,params); |
|
118 |
|
|
119 |
mObjectButton.setOnClickListener( new View.OnClickListener() |
|
120 |
{ |
|
121 |
@Override |
|
122 |
public void onClick(View view) |
|
123 |
{ |
|
124 |
if( mObjectPopup==null ) setupObjectWindow(act,width,height); |
|
125 |
View popupView = mObjectPopup.getContentView(); |
|
126 |
popupView.setSystemUiVisibility(MainActivity.FLAGS); |
|
127 |
displayPopup(act,view,mObjectPopup,width,height,margin,margin); |
|
128 |
} |
|
129 |
}); |
|
130 |
} |
|
131 |
|
|
132 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
133 |
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes |
|
134 |
|
|
135 |
private void displayPopup(InfoActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff) |
|
136 |
{ |
|
137 |
View topLayout = act.findViewById(R.id.mainLayout); |
|
138 |
boolean isFullScreen; |
|
139 |
|
|
140 |
if( topLayout!=null ) |
|
141 |
{ |
|
142 |
topLayout.getLocationOnScreen(mLocation); |
|
143 |
isFullScreen = (mLocation[1]==0); |
|
144 |
} |
|
145 |
else |
|
146 |
{ |
|
147 |
isFullScreen = true; |
|
148 |
} |
|
149 |
|
|
150 |
try |
|
151 |
{ |
|
152 |
// if on Android 11 or we are fullscreen |
|
153 |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R || isFullScreen ) |
|
154 |
{ |
|
155 |
window.showAsDropDown(view, xoff, yoff, Gravity.CENTER); |
|
156 |
window.update(view, w, h); |
|
157 |
} |
|
158 |
else // Android 10 or below in pop-up mode or split-screen mode |
|
159 |
{ |
|
160 |
view.getLocationOnScreen(mLocation); |
|
161 |
int width = view.getWidth(); |
|
162 |
int height = view.getHeight(); |
|
163 |
int x = mLocation[0]+(width-w)/2; |
|
164 |
int y = mLocation[1]+height+yoff; |
|
165 |
|
|
166 |
window.showAsDropDown(view); |
|
167 |
window.update(x,y,w,h); |
|
168 |
} |
|
169 |
} |
|
170 |
catch( IllegalArgumentException iae ) |
|
171 |
{ |
|
172 |
// ignore, this means window is 'not attached to window manager' - |
|
173 |
// which most probably is because we are already exiting the app. |
|
174 |
} |
|
175 |
} |
|
176 |
|
|
177 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
178 |
|
|
179 |
private void prevObject(InfoActivity act, int numObjects) |
|
180 |
{ |
|
181 |
mObjectOrdinal--; |
|
182 |
if( mObjectOrdinal<0 ) mObjectOrdinal=numObjects-1; |
|
183 |
|
|
184 |
act.changeObject(mObjectOrdinal); |
|
185 |
mPane.updatePane(act,mObjectOrdinal); |
|
186 |
} |
|
187 |
|
|
188 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
189 |
|
|
190 |
private void nextObject(InfoActivity act, int numObjects) |
|
191 |
{ |
|
192 |
mObjectOrdinal++; |
|
193 |
if( mObjectOrdinal>=numObjects ) mObjectOrdinal=0; |
|
194 |
|
|
195 |
act.changeObject(mObjectOrdinal); |
|
196 |
mPane.updatePane(act,mObjectOrdinal); |
|
197 |
} |
|
198 |
|
|
199 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
200 |
|
|
201 |
private void setupPrevButton(final InfoActivity act) |
|
202 |
{ |
|
203 |
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f); |
|
204 |
mPrevButton = new TransparentImageButton(act,R.drawable.ui_left,params); |
|
205 |
|
|
206 |
mPrevButton.setOnClickListener( new View.OnClickListener() |
|
207 |
{ |
|
208 |
@Override |
|
209 |
public void onClick(View v) |
|
210 |
{ |
|
211 |
int numObjects = RubikObjectList.getNumObjects(); |
|
212 |
prevObject(act,numObjects); |
|
213 |
mMovesText.setText(act.getString(R.string.mo_placeholder,mObjectOrdinal+1,numObjects)); |
|
214 |
} |
|
215 |
}); |
|
216 |
} |
|
217 |
|
|
218 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
219 |
|
|
220 |
private void setupNextButton(final InfoActivity act) |
|
221 |
{ |
|
222 |
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f); |
|
223 |
mNextButton = new TransparentImageButton(act,R.drawable.ui_right,params); |
|
224 |
|
|
225 |
mNextButton.setOnClickListener( new View.OnClickListener() |
|
226 |
{ |
|
227 |
@Override |
|
228 |
public void onClick(View v) |
|
229 |
{ |
|
230 |
int numObjects = RubikObjectList.getNumObjects(); |
|
231 |
nextObject(act,numObjects); |
|
232 |
mMovesText.setText(act.getString(R.string.mo_placeholder,mObjectOrdinal+1,numObjects)); |
|
233 |
} |
|
234 |
}); |
|
235 |
} |
|
236 |
|
|
237 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
238 |
|
|
239 |
private void setupTextView(final InfoActivity act, final float height, int numObjects) |
|
240 |
{ |
|
241 |
int padding = (int)(height*PADDING); |
|
242 |
int margin = (int)(height*MARGIN); |
|
243 |
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,2.0f); |
|
244 |
params.topMargin = margin; |
|
245 |
params.bottomMargin = margin; |
|
246 |
params.leftMargin = margin; |
|
247 |
params.rightMargin = margin; |
|
248 |
|
|
249 |
mMovesText = new TextView(act); |
|
250 |
mMovesText.setTextSize(20); |
|
251 |
mMovesText.setLayoutParams(params); |
|
252 |
mMovesText.setPadding(padding,0,padding,0); |
|
253 |
mMovesText.setGravity(Gravity.CENTER); |
|
254 |
mMovesText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize); |
|
255 |
mMovesText.setText(act.getString(R.string.mo_placeholder,mObjectOrdinal+1,numObjects)); |
|
256 |
} |
|
257 |
|
|
258 | 42 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
259 | 43 |
|
260 | 44 |
void onAttachedToWindow(final InfoActivity act, final int objectOrdinal) |
261 | 45 |
{ |
262 |
int numObjects = RubikObjectList.getNumObjects(); |
|
263 | 46 |
int width = act.getScreenWidthInPixels(); |
264 |
int height= act.getScreenHeightInPixels(); |
|
265 |
int barHeight = act.getHeightBar(); |
|
266 |
mButtonSize = height*TEXT_SIZE; |
|
267 |
mObjectOrdinal = objectOrdinal; |
|
268 | 47 |
|
269 | 48 |
LinearLayout.LayoutParams paramsL = new LinearLayout.LayoutParams(width/4, LinearLayout.LayoutParams.MATCH_PARENT); |
270 | 49 |
LinearLayout.LayoutParams paramsM = new LinearLayout.LayoutParams(width/2, LinearLayout.LayoutParams.MATCH_PARENT); |
... | ... | |
277 | 56 |
LinearLayout layoutRight= new LinearLayout(act); |
278 | 57 |
layoutRight.setLayoutParams(paramsR); |
279 | 58 |
|
280 |
int popupW = (int)(POPUP_W*width); |
|
281 |
int popupH = (int)(height-barHeight); |
|
282 |
|
|
283 |
setupObjectButton(act,popupW,popupH); |
|
284 |
setupPrevButton(act); |
|
285 |
setupNextButton(act); |
|
286 |
setupTextView(act,height,numObjects); |
|
287 | 59 |
setupBackButton(act); |
288 |
|
|
289 |
layoutLeft.addView(mObjectButton); |
|
290 |
layoutMid.addView(mPrevButton); |
|
291 |
layoutMid.addView(mMovesText); |
|
292 |
layoutMid.addView(mNextButton); |
|
293 | 60 |
layoutRight.addView(mBackButton); |
294 | 61 |
|
295 | 62 |
LinearLayout layout = act.findViewById(R.id.lowerBar); |
... | ... | |
298 | 65 |
layout.addView(layoutMid); |
299 | 66 |
layout.addView(layoutRight); |
300 | 67 |
|
301 |
mPane = new InfoScreenPane(act,mObjectOrdinal);
|
|
68 |
mPane = new InfoScreenPane(act,objectOrdinal);
|
|
302 | 69 |
} |
303 | 70 |
} |
Also available in: Unified diff
Initial support for configuring the stickers.