Project

General

Profile

Download (14.4 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorScreen.java @ 39176a1f

1 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2 da56b12f Leszek Koltunski
// Copyright 2022 Leszek Koltunski                                                               //
3 9530f6b0 Leszek Koltunski
//                                                                                               //
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 b9d062cf Leszek Koltunski
import java.lang.ref.WeakReference;
23 d3d639b1 Leszek Koltunski
import java.util.ArrayList;
24
25 20b60ad9 Leszek Koltunski
import android.app.Activity;
26 b9d062cf Leszek Koltunski
import android.content.SharedPreferences;
27 7cb8d4b0 Leszek Koltunski
import android.graphics.Bitmap;
28 39176a1f Leszek Koltunski
import android.util.TypedValue;
29 9530f6b0 Leszek Koltunski
import android.view.View;
30 b9d062cf Leszek Koltunski
import android.view.ViewGroup;
31
import android.widget.AdapterView;
32
import android.widget.ArrayAdapter;
33 7cb8d4b0 Leszek Koltunski
import android.widget.ImageView;
34 9530f6b0 Leszek Koltunski
import android.widget.LinearLayout;
35 b9d062cf Leszek Koltunski
import android.widget.Spinner;
36 39176a1f Leszek Koltunski
import android.widget.TextView;
37 9530f6b0 Leszek Koltunski
38 a41e3c94 Leszek Koltunski
import org.distorted.external.RubikFiles;
39 9530f6b0 Leszek Koltunski
import org.distorted.helpers.TransparentImageButton;
40
import org.distorted.main.R;
41
import org.distorted.main.RubikActivity;
42
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44
45 b9d062cf Leszek Koltunski
public class BandagedCreatorScreen implements AdapterView.OnItemSelectedListener
46 9530f6b0 Leszek Koltunski
{
47 b9d062cf Leszek Koltunski
  public static final int MAX_SUPPORTED_SIZE = 5;
48
49
  private WeakReference<BandagedCreatorActivity> mAct;
50 9530f6b0 Leszek Koltunski
  private TransparentImageButton mBackButton, mResetButton, mDoneButton;
51 e48ad1af Leszek Koltunski
  private LinearLayout mObjectView;
52 b9d062cf Leszek Koltunski
  private int mNumObjects, mX, mY, mZ;
53
  private final ArrayList<BandagedCreatorObjectView> mViews;
54
  private Spinner mSpinnerX, mSpinnerY, mSpinnerZ;
55
  private boolean mSpinnersReady;
56 39176a1f Leszek Koltunski
  private float mTextSize;
57 9530f6b0 Leszek Koltunski
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59
60
  public BandagedCreatorScreen()
61
    {
62 b9d062cf Leszek Koltunski
    mSpinnersReady = false;
63 e48ad1af Leszek Koltunski
    mNumObjects = 0;
64 d3d639b1 Leszek Koltunski
    mViews = new ArrayList<>();
65 9530f6b0 Leszek Koltunski
    }
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 f802924b Leszek Koltunski
    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 9530f6b0 Leszek Koltunski
    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 e0b71e6e Leszek Koltunski
        BandagedCreatorRenderer renderer = act.getRenderer();
99 72e386ef Leszek Koltunski
        if( !renderer.isBusy() ) renderer.displaySavingDialog();
100 9530f6b0 Leszek Koltunski
        }
101
      });
102
    }
103
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105
106
  private void setupResetButton(final BandagedCreatorActivity act)
107
    {
108 1b185a91 Leszek Koltunski
    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 9530f6b0 Leszek Koltunski
    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 50ec342b Leszek Koltunski
        BandagedCreatorRenderer renderer = act.getRenderer();
118 e0b71e6e Leszek Koltunski
        if( !renderer.isBusy() ) renderer.setupReset();
119 9530f6b0 Leszek Koltunski
        }
120
      });
121
    }
122
123 b9d062cf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
150
151 bebd7af5 Leszek Koltunski
  void onAttachedToWindow(final BandagedCreatorActivity act)
152 9530f6b0 Leszek Koltunski
    {
153 b9d062cf Leszek Koltunski
    mAct = new WeakReference<>(act);
154 e48ad1af Leszek Koltunski
    mObjectView = act.findViewById(R.id.bandagedCreatorView);
155 39176a1f Leszek Koltunski
    mTextSize = act.getScreenHeightInPixels()*BandagedCreatorActivity.SPINNER_TEXT_SIZE;
156 e48ad1af Leszek Koltunski
157 b9d062cf Leszek Koltunski
    int width  = act.getScreenWidthInPixels();
158
    int height = act.getScreenHeightInPixels();
159
    int padding= (int)(height*RubikActivity.PADDING/3);
160 9530f6b0 Leszek Koltunski
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 b9d062cf Leszek Koltunski
185
    mSpinnerX = act.findViewById(R.id.bandagedCreatorX);
186
    mSpinnerX.setOnItemSelectedListener(this);
187 39176a1f Leszek Koltunski
    ArrayAdapter<String> adX = new ArrayAdapter<>(act, R.layout.spinner_item, createSizes("X") );
188 b9d062cf Leszek Koltunski
    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 39176a1f Leszek Koltunski
    ArrayAdapter<String> adY = new ArrayAdapter<>(act, R.layout.spinner_item, createSizes("Y") );
194 b9d062cf Leszek Koltunski
    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 39176a1f Leszek Koltunski
    ArrayAdapter<String> adZ = new ArrayAdapter<>(act, R.layout.spinner_item, createSizes("Z") );
200 b9d062cf Leszek Koltunski
    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 9530f6b0 Leszek Koltunski
    }
210 e48ad1af Leszek Koltunski
211 a41e3c94 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 b9d062cf Leszek Koltunski
  private void addObjects(BandagedCreatorActivity act, String objectString)
227 a41e3c94 Leszek Koltunski
    {
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 e48ad1af Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 d3d639b1 Leszek Koltunski
    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 66cbab36 Leszek Koltunski
      String viewName = view.getName();
264 d3d639b1 Leszek Koltunski
265 66cbab36 Leszek Koltunski
      if( viewName.equals(name) )
266 d3d639b1 Leszek Koltunski
        {
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 66cbab36 Leszek Koltunski
          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 d3d639b1 Leszek Koltunski
285
          p2.setLayoutParams(params);
286
          }
287
288
        break;
289
        }
290
      }
291 e48ad1af Leszek Koltunski
    }
292 7cb8d4b0 Leszek Koltunski
293
///////////////////////////////////////////////////////////////////////////////////////////////////
294
295 20b60ad9 Leszek Koltunski
  void iconCreationDone(Activity act, Bitmap bmp)
296 7cb8d4b0 Leszek Koltunski
    {
297
    int numChildren = mObjectView.getChildCount();
298
299 83e021c5 Leszek Koltunski
    if( numChildren>0 )
300 7cb8d4b0 Leszek Koltunski
      {
301 83e021c5 Leszek Koltunski
      LinearLayout pane = (LinearLayout)mObjectView.getChildAt(numChildren-1);
302
      ImageView view = pane.findViewById(R.id.bandagedCreatorObjectIcon);
303
304
      if( view!=null )
305 20b60ad9 Leszek Koltunski
        {
306 83e021c5 Leszek Koltunski
        act.runOnUiThread(new Runnable()
307
          {
308
          @Override
309
          public void run()
310 20b60ad9 Leszek Koltunski
          {
311
          view.setImageBitmap(bmp);
312
          }
313 83e021c5 Leszek Koltunski
          });
314
        }
315
      else
316
        {
317
        android.util.Log.e("D", "ImageView not found!");
318
        }
319 7cb8d4b0 Leszek Koltunski
      }
320
    }
321 a41e3c94 Leszek Koltunski
322
///////////////////////////////////////////////////////////////////////////////////////////////////
323
324 b9d062cf Leszek Koltunski
  private String generateObjectStrings()
325 a41e3c94 Leszek Koltunski
    {
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 b9d062cf Leszek Koltunski
341
///////////////////////////////////////////////////////////////////////////////////////////////////
342
343
    @Override
344
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
345
      {
346
      int spinnerID = parent.getId();
347
348 39176a1f Leszek Koltunski
      ((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_PX,mTextSize);
349
350 b9d062cf Leszek Koltunski
      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 9530f6b0 Leszek Koltunski
}