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