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