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