1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2022 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Magic Cube. //
|
5
|
// //
|
6
|
// 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
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
9
|
|
10
|
package org.distorted.bandaged;
|
11
|
|
12
|
import java.lang.ref.WeakReference;
|
13
|
import java.util.ArrayList;
|
14
|
|
15
|
import android.annotation.SuppressLint;
|
16
|
import android.app.Activity;
|
17
|
import android.content.SharedPreferences;
|
18
|
import android.graphics.Bitmap;
|
19
|
import android.util.TypedValue;
|
20
|
import android.view.View;
|
21
|
import android.view.ViewGroup;
|
22
|
import android.widget.AdapterView;
|
23
|
import android.widget.ArrayAdapter;
|
24
|
import android.widget.ImageView;
|
25
|
import android.widget.LinearLayout;
|
26
|
import android.widget.Spinner;
|
27
|
import android.widget.TextView;
|
28
|
|
29
|
import org.distorted.external.RubikFiles;
|
30
|
import org.distorted.helpers.TransparentImageButton;
|
31
|
import org.distorted.main.R;
|
32
|
import org.distorted.objectlib.bandaged.LocallyBandagedList;
|
33
|
|
34
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
35
|
|
36
|
public class BandagedScreen implements AdapterView.OnItemSelectedListener
|
37
|
{
|
38
|
private WeakReference<BandagedActivity> mAct;
|
39
|
private TransparentImageButton mBackButton, mResetButton, mDoneButton;
|
40
|
private LinearLayout mObjectView;
|
41
|
private int mNumObjects, mX, mY, mZ;
|
42
|
private final ArrayList<BandagedObjectView> mViews;
|
43
|
private Spinner mSpinnerX, mSpinnerY, mSpinnerZ;
|
44
|
private boolean mSpinnersReady;
|
45
|
private float mTextSize;
|
46
|
private final int mOrdinal;
|
47
|
private final int mDimension;
|
48
|
private final int mMaxSupported;
|
49
|
private final int mMinSupported;
|
50
|
|
51
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
52
|
|
53
|
public BandagedScreen(int ordinal)
|
54
|
{
|
55
|
mOrdinal = ordinal;
|
56
|
mMinSupported = LocallyBandagedList.getMinSupported(ordinal);
|
57
|
mMaxSupported = LocallyBandagedList.getMaxSupported(ordinal);
|
58
|
mDimension = LocallyBandagedList.getDimension(ordinal);
|
59
|
mSpinnersReady= false;
|
60
|
mNumObjects = 0;
|
61
|
mViews = new ArrayList<>();
|
62
|
}
|
63
|
|
64
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
65
|
|
66
|
private void setupBackButton(final BandagedActivity act)
|
67
|
{
|
68
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
|
69
|
mBackButton = new TransparentImageButton(act,R.drawable.ui_smallback,params);
|
70
|
|
71
|
mBackButton.setOnClickListener( new View.OnClickListener()
|
72
|
{
|
73
|
@Override
|
74
|
public void onClick(View v)
|
75
|
{
|
76
|
act.finish();
|
77
|
}
|
78
|
});
|
79
|
}
|
80
|
|
81
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
82
|
|
83
|
private void setupDoneButton(final BandagedActivity act)
|
84
|
{
|
85
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
|
86
|
mDoneButton = new TransparentImageButton(act,R.drawable.ui_done,params);
|
87
|
|
88
|
mDoneButton.setOnClickListener( new View.OnClickListener()
|
89
|
{
|
90
|
@Override
|
91
|
public void onClick(View v)
|
92
|
{
|
93
|
BandagedRenderer renderer = act.getRenderer();
|
94
|
if( !renderer.isBusy() ) renderer.displaySavingDialog();
|
95
|
}
|
96
|
});
|
97
|
}
|
98
|
|
99
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
100
|
|
101
|
private void setupResetButton(final BandagedActivity act)
|
102
|
{
|
103
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
|
104
|
mResetButton = new TransparentImageButton(act,R.drawable.ui_reset,params);
|
105
|
|
106
|
mResetButton.setOnClickListener( new View.OnClickListener()
|
107
|
{
|
108
|
@Override
|
109
|
public void onClick(View v)
|
110
|
{
|
111
|
BandagedRenderer renderer = act.getRenderer();
|
112
|
if( !renderer.isBusy() ) renderer.setupReset();
|
113
|
}
|
114
|
});
|
115
|
}
|
116
|
|
117
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
118
|
|
119
|
private String[] createSizes(String mark)
|
120
|
{
|
121
|
int num = mMaxSupported-mMinSupported+1;
|
122
|
String[] ret = new String[num];
|
123
|
|
124
|
for(int i=1; i<=num; i++)
|
125
|
{
|
126
|
ret[i-1] = mark+" : "+(mMinSupported+i-1);
|
127
|
}
|
128
|
|
129
|
return ret;
|
130
|
}
|
131
|
|
132
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
133
|
|
134
|
private void changeObject()
|
135
|
{
|
136
|
if( mSpinnersReady )
|
137
|
{
|
138
|
BandagedActivity act = mAct.get();
|
139
|
if( act!=null ) act.changeObject(mX+mMinSupported,mY+mMinSupported,mZ+mMinSupported);
|
140
|
}
|
141
|
}
|
142
|
|
143
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
144
|
|
145
|
void onAttachedToWindow(final BandagedActivity act)
|
146
|
{
|
147
|
mAct = new WeakReference<>(act);
|
148
|
mObjectView = act.findViewById(R.id.bandagedCreatorView);
|
149
|
mTextSize = act.getScreenHeightInPixels()*BandagedActivity.SPINNER_TEXT_SIZE;
|
150
|
|
151
|
int width = act.getScreenWidthInPixels();
|
152
|
int height = act.getScreenHeightInPixels();
|
153
|
int padding= (int)(height*BandagedActivity.PADDING/3);
|
154
|
|
155
|
LinearLayout.LayoutParams paramsL = new LinearLayout.LayoutParams(width/4, LinearLayout.LayoutParams.MATCH_PARENT);
|
156
|
LinearLayout.LayoutParams paramsM = new LinearLayout.LayoutParams(width/2, LinearLayout.LayoutParams.MATCH_PARENT);
|
157
|
LinearLayout.LayoutParams paramsR = new LinearLayout.LayoutParams(width/4, LinearLayout.LayoutParams.MATCH_PARENT);
|
158
|
|
159
|
LinearLayout layoutLeft = new LinearLayout(act);
|
160
|
layoutLeft.setLayoutParams(paramsL);
|
161
|
LinearLayout layoutMid = new LinearLayout(act);
|
162
|
layoutMid.setLayoutParams(paramsM);
|
163
|
LinearLayout layoutRight= new LinearLayout(act);
|
164
|
layoutRight.setLayoutParams(paramsR);
|
165
|
|
166
|
setupBackButton(act);
|
167
|
layoutRight.addView(mBackButton);
|
168
|
setupDoneButton(act);
|
169
|
layoutMid.addView(mDoneButton);
|
170
|
setupResetButton(act);
|
171
|
layoutLeft.addView(mResetButton);
|
172
|
|
173
|
LinearLayout layout = act.findViewById(R.id.lowerBar);
|
174
|
layout.removeAllViews();
|
175
|
layout.addView(layoutLeft);
|
176
|
layout.addView(layoutMid);
|
177
|
layout.addView(layoutRight);
|
178
|
|
179
|
mSpinnerX = act.findViewById(R.id.bandagedCreatorX);
|
180
|
mSpinnerY = act.findViewById(R.id.bandagedCreatorY);
|
181
|
mSpinnerZ = act.findViewById(R.id.bandagedCreatorZ);
|
182
|
|
183
|
if( mDimension>=1 )
|
184
|
{
|
185
|
mSpinnerX.setOnItemSelectedListener(this);
|
186
|
ArrayAdapter<String> adX=new ArrayAdapter<>(act, R.layout.spinner_item, createSizes("X"));
|
187
|
mSpinnerX.setAdapter(adX);
|
188
|
ViewGroup.MarginLayoutParams paramsX=(ViewGroup.MarginLayoutParams) mSpinnerX.getLayoutParams();
|
189
|
paramsX.setMargins(0, 0, 2*padding, 0);
|
190
|
}
|
191
|
else
|
192
|
{
|
193
|
mSpinnerX.setVisibility(View.INVISIBLE);
|
194
|
}
|
195
|
|
196
|
if( mDimension>=2 )
|
197
|
{
|
198
|
mSpinnerY.setOnItemSelectedListener(this);
|
199
|
ArrayAdapter<String> adY=new ArrayAdapter<>(act, R.layout.spinner_item, createSizes("Y"));
|
200
|
mSpinnerY.setAdapter(adY);
|
201
|
ViewGroup.MarginLayoutParams paramsY=(ViewGroup.MarginLayoutParams) mSpinnerY.getLayoutParams();
|
202
|
paramsY.setMargins(padding, 0, padding, 0);
|
203
|
}
|
204
|
else
|
205
|
{
|
206
|
mSpinnerY.setVisibility(View.INVISIBLE);
|
207
|
}
|
208
|
|
209
|
if( mDimension>=3 )
|
210
|
{
|
211
|
mSpinnerZ.setOnItemSelectedListener(this);
|
212
|
ArrayAdapter<String> adZ=new ArrayAdapter<>(act, R.layout.spinner_item, createSizes("Z"));
|
213
|
mSpinnerZ.setAdapter(adZ);
|
214
|
ViewGroup.MarginLayoutParams paramsZ=(ViewGroup.MarginLayoutParams) mSpinnerZ.getLayoutParams();
|
215
|
paramsZ.setMargins(2*padding, 0, 0, 0);
|
216
|
}
|
217
|
else
|
218
|
{
|
219
|
mSpinnerZ.setVisibility(View.INVISIBLE);
|
220
|
}
|
221
|
}
|
222
|
|
223
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
224
|
|
225
|
boolean objectDoesntExist(String name)
|
226
|
{
|
227
|
for( BandagedObjectView view : mViews )
|
228
|
{
|
229
|
String viewName = view.getName();
|
230
|
if( viewName.equals(name) ) return false;
|
231
|
}
|
232
|
|
233
|
return true;
|
234
|
}
|
235
|
|
236
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
237
|
|
238
|
private void addObjects(BandagedActivity act, String objectString)
|
239
|
{
|
240
|
if( objectString.length()>0 )
|
241
|
{
|
242
|
String[] objects = objectString.split(" ");
|
243
|
RubikFiles files = RubikFiles.getInstance();
|
244
|
|
245
|
for(String object : objects)
|
246
|
{
|
247
|
if( objectDoesntExist(object) )
|
248
|
{
|
249
|
addObject(act, object);
|
250
|
Bitmap bmp = files.getIcon(act, object + ".png");
|
251
|
iconCreationDone(act, bmp);
|
252
|
}
|
253
|
}
|
254
|
}
|
255
|
}
|
256
|
|
257
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
258
|
|
259
|
void addObject(BandagedActivity act, String name)
|
260
|
{
|
261
|
BandagedObjectView view = new BandagedObjectView(act,name,mNumObjects==0);
|
262
|
LinearLayout pane = view.getPane();
|
263
|
mObjectView.addView(pane);
|
264
|
mNumObjects++;
|
265
|
mViews.add(view);
|
266
|
}
|
267
|
|
268
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
269
|
|
270
|
void deleteObject(BandagedActivity act, String name)
|
271
|
{
|
272
|
for(int v=0; v<mNumObjects; v++)
|
273
|
{
|
274
|
BandagedObjectView view = mViews.get(v);
|
275
|
String viewName = view.getName();
|
276
|
|
277
|
if( viewName.equals(name) )
|
278
|
{
|
279
|
LinearLayout pane = view.getPane();
|
280
|
mObjectView.removeView(pane);
|
281
|
mViews.remove(view);
|
282
|
mNumObjects--;
|
283
|
|
284
|
if( v==0 && mNumObjects>0 )
|
285
|
{
|
286
|
BandagedObjectView v2 = mViews.get(v);
|
287
|
LinearLayout p2 = v2.getPane();
|
288
|
int height = act.getScreenHeightInPixels();
|
289
|
LinearLayout.LayoutParams params = BandagedObjectView.createPaneParams(height,true,act.isRTL());
|
290
|
p2.setLayoutParams(params);
|
291
|
}
|
292
|
|
293
|
break;
|
294
|
}
|
295
|
}
|
296
|
}
|
297
|
|
298
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
299
|
|
300
|
void iconCreationDone(Activity act, Bitmap bmp)
|
301
|
{
|
302
|
int numChildren = mObjectView.getChildCount();
|
303
|
|
304
|
if( numChildren>0 )
|
305
|
{
|
306
|
LinearLayout pane = (LinearLayout)mObjectView.getChildAt(numChildren-1);
|
307
|
ImageView view = pane.findViewById(R.id.bandagedCreatorObjectIcon);
|
308
|
|
309
|
if( view!=null )
|
310
|
{
|
311
|
act.runOnUiThread(new Runnable()
|
312
|
{
|
313
|
@Override
|
314
|
public void run()
|
315
|
{
|
316
|
view.setImageBitmap(bmp);
|
317
|
}
|
318
|
});
|
319
|
}
|
320
|
else
|
321
|
{
|
322
|
android.util.Log.e("D", "ImageView not found!");
|
323
|
}
|
324
|
}
|
325
|
}
|
326
|
|
327
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
328
|
|
329
|
private String generateObjectStrings()
|
330
|
{
|
331
|
StringBuilder result = new StringBuilder();
|
332
|
int numViews = mViews.size();
|
333
|
|
334
|
for( int v=0; v<numViews; v++ )
|
335
|
{
|
336
|
BandagedObjectView view = mViews.get(v);
|
337
|
String name = view.getName();
|
338
|
if( v>0 ) result.append(' ');
|
339
|
result.append(name);
|
340
|
}
|
341
|
|
342
|
return result.toString();
|
343
|
}
|
344
|
|
345
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
346
|
|
347
|
@SuppressLint("SuspiciousIndentation")
|
348
|
@Override
|
349
|
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
|
350
|
{
|
351
|
int spinnerID = parent.getId();
|
352
|
|
353
|
if( view!=null ) ((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_PX,mTextSize);
|
354
|
|
355
|
if( spinnerID==R.id.bandagedCreatorX ) mX = pos;
|
356
|
else if( spinnerID==R.id.bandagedCreatorY ) mY = pos;
|
357
|
else if( spinnerID==R.id.bandagedCreatorZ ) mZ = pos;
|
358
|
|
359
|
changeObject();
|
360
|
}
|
361
|
|
362
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
363
|
|
364
|
@Override
|
365
|
public void onNothingSelected(AdapterView<?> parent) { }
|
366
|
|
367
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
368
|
|
369
|
private String generateObjectKey()
|
370
|
{
|
371
|
return mOrdinal==0 ? "bandagedObjects" : "bandagedObjects"+mOrdinal;
|
372
|
}
|
373
|
|
374
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
375
|
|
376
|
public void savePreferences(SharedPreferences.Editor editor)
|
377
|
{
|
378
|
String objects = generateObjectStrings();
|
379
|
editor.putString(generateObjectKey(), objects );
|
380
|
|
381
|
editor.putInt("bandagedX"+mOrdinal, mX);
|
382
|
editor.putInt("bandagedY"+mOrdinal, mY);
|
383
|
editor.putInt("bandagedZ"+mOrdinal, mZ);
|
384
|
}
|
385
|
|
386
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
387
|
|
388
|
public void restorePreferences(BandagedActivity act, SharedPreferences preferences)
|
389
|
{
|
390
|
String objects = preferences.getString(generateObjectKey(),"");
|
391
|
addObjects(act,objects);
|
392
|
|
393
|
mX = preferences.getInt("bandagedX"+mOrdinal, 2);
|
394
|
mY = preferences.getInt("bandagedY"+mOrdinal, 2);
|
395
|
mZ = preferences.getInt("bandagedZ"+mOrdinal, 2);
|
396
|
|
397
|
int max = mMaxSupported-mMinSupported+1;
|
398
|
|
399
|
if( mX>=max ) mX = 0;//max-1;
|
400
|
if( mY>=max ) mY = 0;//max-1;
|
401
|
if( mZ>=max ) mZ = 0;//max-1;
|
402
|
|
403
|
mSpinnerX.setSelection(mX);
|
404
|
mSpinnerY.setSelection(mY);
|
405
|
mSpinnerZ.setSelection(mZ);
|
406
|
|
407
|
mSpinnersReady = true;
|
408
|
changeObject();
|
409
|
}
|
410
|
}
|