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