Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObjectList.java @ 5bda8973

1 a7d8c3cd Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2021 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6 f05e0259 Leszek Koltunski
// 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 a7d8c3cd Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
9
10
package org.distorted.objects;
11
12 804293f0 Leszek Koltunski
import java.util.ArrayList;
13 5e048300 Leszek Koltunski
import java.util.Locale;
14 804293f0 Leszek Koltunski
15 81493402 Leszek Koltunski
import android.content.Context;
16 400ff34d Leszek Koltunski
import android.content.SharedPreferences;
17
18 81493402 Leszek Koltunski
import org.distorted.external.RubikFiles;
19 400ff34d Leszek Koltunski
import org.distorted.main.RubikActivity;
20 e4733ed7 Leszek Koltunski
import org.distorted.objectlib.main.ObjectSignatures;
21 a7d8c3cd Leszek Koltunski
import org.distorted.objectlib.main.ObjectType;
22
23 09cf2a36 Leszek Koltunski
import static org.distorted.objectlib.main.TwistyObject.MESH_NICE;
24 a7d8c3cd Leszek Koltunski
import static org.distorted.objectlib.main.ObjectType.NUM_OBJECTS;
25 e847c553 Leszek Koltunski
import static org.distorted.main.RubikActivity.SHOW_DOWNLOADED_DEBUG;
26 a7d8c3cd Leszek Koltunski
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28
29
public class RubikObjectList
30
{
31 e4733ed7 Leszek Koltunski
  public static final int DEF_OBJECT= ObjectSignatures.CUBE_3;
32 506f7ceb Leszek Koltunski
  private static RubikObjectList mThis;
33 a7d8c3cd Leszek Koltunski
  private static int mNumObjects;
34 fcf7320f Leszek Koltunski
  private static int mNumExtras;
35 a7d8c3cd Leszek Koltunski
  private static ArrayList<RubikObject> mObjects;
36 400ff34d Leszek Koltunski
  private static int mObject = DEF_OBJECT;
37 a7d8c3cd Leszek Koltunski
38 314e9ff0 Leszek Koltunski
  public static class DownloadedObject
39 506f7ceb Leszek Koltunski
    {
40
    String shortName;
41
    boolean icon,object,extras;
42 84d746d7 Leszek Koltunski
    int numScrambles, objectMinor, extrasMinor;
43 506f7ceb Leszek Koltunski
44 84d746d7 Leszek Koltunski
    DownloadedObject(String sName, int scrambles, int oMinor, int eMinor, boolean i, boolean o, boolean e)
45 506f7ceb Leszek Koltunski
      {
46
      shortName = sName;
47 84d746d7 Leszek Koltunski
48
      numScrambles= scrambles;
49
      objectMinor = oMinor;
50
      extrasMinor = eMinor;
51
52 506f7ceb Leszek Koltunski
      icon   = i;
53
      object = o;
54
      extras = e;
55
      }
56
    }
57
58
  private static ArrayList<DownloadedObject> mDownloadedObjects;
59
60 a7d8c3cd Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
61
62
  private RubikObjectList()
63
    {
64 fcf7320f Leszek Koltunski
    mNumObjects= 0;
65
    mNumExtras = 0;
66 804293f0 Leszek Koltunski
67 506f7ceb Leszek Koltunski
    mObjects           = new ArrayList<>();
68
    mDownloadedObjects = new ArrayList<>();
69 a7d8c3cd Leszek Koltunski
70
    createBuiltinObjects();
71
    }
72
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74
75
  private void createBuiltinObjects()
76
    {
77
    for(int i=0; i<NUM_OBJECTS; i++)
78
      {
79
      ObjectType type = ObjectType.getObject(i);
80 d433b50e Leszek Koltunski
      RubikObject obj = new RubikObject(type);
81 a7d8c3cd Leszek Koltunski
      mObjects.add(obj);
82
      mNumObjects++;
83 804293f0 Leszek Koltunski
84 e847c553 Leszek Koltunski
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "creating local object "+type.name() );
85 84d746d7 Leszek Koltunski
86 314e9ff0 Leszek Koltunski
      if( obj.hasExtras() )
87 804293f0 Leszek Koltunski
        {
88 e847c553 Leszek Koltunski
        if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "has extras "+mNumExtras );
89 84d746d7 Leszek Koltunski
90 fcf7320f Leszek Koltunski
        obj.setExtrasOrdinal(mNumExtras);
91
        mNumExtras++;
92 84d746d7 Leszek Koltunski
        }
93
      else
94
        {
95 e847c553 Leszek Koltunski
        if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "no extras");
96 804293f0 Leszek Koltunski
        }
97 a7d8c3cd Leszek Koltunski
      }
98
    }
99
100 506f7ceb Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
101
102 84d746d7 Leszek Koltunski
  private static boolean internalAddDownloadedObject(DownloadedObject object)
103 506f7ceb Leszek Koltunski
    {
104 314e9ff0 Leszek Koltunski
    String name = object.shortName;
105
106
    for(RubikObject ro : mObjects )
107 84d746d7 Leszek Koltunski
      if( ro.getLowerName().equals(name) )
108 314e9ff0 Leszek Koltunski
        {
109 84d746d7 Leszek Koltunski
        return ro.updateObject(object);
110 314e9ff0 Leszek Koltunski
        }
111
112 84d746d7 Leszek Koltunski
    RubikObject obj = new RubikObject(object);
113
    mObjects.add(obj);
114
    mNumObjects++;
115
116 e847c553 Leszek Koltunski
    if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "creating downloaded object "+obj.getUpperName() );
117 84d746d7 Leszek Koltunski
118
    if( obj.hasExtras() )
119 314e9ff0 Leszek Koltunski
      {
120 e847c553 Leszek Koltunski
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "has extras "+mNumExtras );
121 506f7ceb Leszek Koltunski
122 84d746d7 Leszek Koltunski
      obj.setExtrasOrdinal(mNumExtras);
123
      mNumExtras++;
124
      }
125
    else
126
      {
127 e847c553 Leszek Koltunski
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "no extras");
128 314e9ff0 Leszek Koltunski
      }
129 84d746d7 Leszek Koltunski
130
    return true;
131 506f7ceb Leszek Koltunski
    }
132
133 a7d8c3cd Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
134
// PUBLIC API
135 806329e3 Leszek Koltunski
136 ac1900c3 Leszek Koltunski
  public static boolean addDownloadedObject(Context context, String shortName, int numScrambles, int objectMinor,
137 81493402 Leszek Koltunski
                                         int extrasMinor, boolean icon, boolean object, boolean extras)
138 806329e3 Leszek Koltunski
    {
139 c89c3b1b Leszek Koltunski
    if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "New downloaded object "+shortName+" icon="+icon+" object="+object+" extras="+extras);
140
141 506f7ceb Leszek Koltunski
    for( DownloadedObject obj : mDownloadedObjects )
142
      {
143
      if( obj.shortName.equals(shortName) )
144
        {
145
        obj.icon  |= icon;
146
        obj.object|= object;
147
        obj.extras|= extras;
148
149 c89c3b1b Leszek Koltunski
        if( !obj.object ) objectMinor=-1;
150
        if( !obj.extras ) extrasMinor=-1;
151
152
        obj.objectMinor = objectMinor;
153
        obj.extrasMinor = extrasMinor;
154
155
        if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Updating downloaded object "+shortName+" icon="+obj.icon+" object="+obj.object+" extras="+obj.extras);
156 506f7ceb Leszek Koltunski
157 41a5dd89 Leszek Koltunski
        try
158
          {
159
          RubikActivity ract = (RubikActivity)context;
160
          ract.reloadObject(shortName);
161
          }
162
        catch(Exception ex)
163
          {
164
          android.util.Log.e("D", "exception trying to reload object: "+ex.getMessage() );
165
          }
166
167 ac1900c3 Leszek Koltunski
        return false;
168 506f7ceb Leszek Koltunski
        }
169
      }
170
171 c89c3b1b Leszek Koltunski
    if( !object ) objectMinor=-1;
172
    if( !extras ) extrasMinor=-1;
173
174
    DownloadedObject obj = new DownloadedObject(shortName,numScrambles,objectMinor,extrasMinor,icon,object,extras);
175
    if ( internalAddDownloadedObject(obj) )
176 84d746d7 Leszek Koltunski
      {
177 c89c3b1b Leszek Koltunski
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Adding new downloaded object "+shortName+" icon="+obj.icon+" object="+obj.object+" extras="+obj.extras);
178
      mDownloadedObjects.add(obj);
179 ac1900c3 Leszek Koltunski
      return true;
180 84d746d7 Leszek Koltunski
      }
181 81493402 Leszek Koltunski
    else
182
      {
183
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "New downloaded object "+shortName+" is already built-in, deleting");
184
      RubikFiles files = RubikFiles.getInstance();
185
      files.deleteIcon(context,shortName);
186
      files.deleteJsonObject(context,shortName);
187
      files.deleteJsonExtras(context,shortName);
188 ac1900c3 Leszek Koltunski
      return false;
189 81493402 Leszek Koltunski
      }
190 806329e3 Leszek Koltunski
    }
191
192 f12e4de9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
193
194
  public static void setMeshState(int ordinal, int state)
195
    {
196
    if( ordinal>=0 && ordinal<mNumObjects ) mObjects.get(ordinal).setMeshState(state);
197
    }
198
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200
201
  public static int getMeshState(int ordinal)
202
    {
203
    return (ordinal>=0 && ordinal<mNumObjects) ? mObjects.get(ordinal).getMeshState() : MESH_NICE;
204
    }
205
206 400ff34d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
207
208
  public static void savePreferences(SharedPreferences.Editor editor)
209
    {
210 f12e4de9 Leszek Koltunski
    RubikObject obj = getObject(mObject);
211 84d746d7 Leszek Koltunski
    if( obj!=null ) editor.putString("rol_objName", obj.getUpperName() );
212 506f7ceb Leszek Koltunski
213
    int numDownloaded = mDownloadedObjects.size();
214
215
    if( numDownloaded>0 )
216
      {
217
      StringBuilder downloadedObjects = new StringBuilder();
218
219
      for(int i=0; i<numDownloaded; i++)
220
        {
221
        if( i>0 ) downloadedObjects.append(',');
222
223
        DownloadedObject object = mDownloadedObjects.get(i);
224
        downloadedObjects.append(object.shortName);
225
        downloadedObjects.append(' ');
226 84d746d7 Leszek Koltunski
        downloadedObjects.append(object.numScrambles);
227
        downloadedObjects.append(' ');
228
        downloadedObjects.append(object.objectMinor);
229
        downloadedObjects.append(' ');
230
        downloadedObjects.append(object.extrasMinor);
231
        downloadedObjects.append(' ');
232 506f7ceb Leszek Koltunski
        downloadedObjects.append(object.icon   ? "1":"0");
233
        downloadedObjects.append(' ');
234
        downloadedObjects.append(object.object ? "1":"0");
235
        downloadedObjects.append(' ');
236
        downloadedObjects.append(object.extras ? "1":"0");
237
        }
238
239 84d746d7 Leszek Koltunski
      String objects = downloadedObjects.toString();
240 e847c553 Leszek Koltunski
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "saving: "+objects);
241 84d746d7 Leszek Koltunski
      editor.putString("rol_downloaded", objects );
242 506f7ceb Leszek Koltunski
      }
243 7e9139c1 Leszek Koltunski
    else
244
      {
245
      editor.putString("rol_downloaded", "" );
246
      }
247 f12e4de9 Leszek Koltunski
    }
248 400ff34d Leszek Koltunski
249 f12e4de9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
250
251
  public static void saveMeshState(SharedPreferences.Editor editor)
252
    {
253
    for(int i=0; i<mNumObjects; i++)
254 400ff34d Leszek Koltunski
      {
255 f12e4de9 Leszek Koltunski
      RubikObject obj = getObject(i);
256
257
      if( obj!=null )
258
        {
259 84d746d7 Leszek Koltunski
        String name = obj.getUpperName();
260 f12e4de9 Leszek Koltunski
        editor.putInt("rol_"+name, obj.getMeshState() );
261
        }
262 400ff34d Leszek Koltunski
      }
263
    }
264
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266
267 81493402 Leszek Koltunski
  public static void restorePreferences(Context context, SharedPreferences preferences)
268 400ff34d Leszek Koltunski
    {
269 526a5906 Leszek Koltunski
    if( mThis==null ) mThis = new RubikObjectList();
270
271 506f7ceb Leszek Koltunski
    String downloaded = preferences.getString("rol_downloaded","");
272
273 e847c553 Leszek Koltunski
    if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", downloaded);
274 506f7ceb Leszek Koltunski
275
    if( !downloaded.equals(""))
276
      {
277
      String[] dObjects = downloaded.split(",");
278
279
      for(String dObj : dObjects)
280
        {
281
        String[] parts = dObj.split(" ");
282
283 84d746d7 Leszek Koltunski
        if( parts.length==7 )
284 506f7ceb Leszek Koltunski
          {
285
          String name = parts[0];
286 84d746d7 Leszek Koltunski
          String scra = parts[1];
287
          String objM = parts[2];
288
          String extM = parts[3];
289
          String icon = parts[4];
290
          String obje = parts[5];
291
          String extr = parts[6];
292
293
          int scrambles = Integer.parseInt(scra);
294
          int oMinor    = Integer.parseInt(objM);
295
          int eMinor    = Integer.parseInt(extM);
296 506f7ceb Leszek Koltunski
297
          boolean bIcon = icon.equals("1");
298
          boolean bObje = obje.equals("1");
299
          boolean bExtr = extr.equals("1");
300
301 81493402 Leszek Koltunski
          addDownloadedObject(context,name,scrambles,oMinor,eMinor,bIcon,bObje,bExtr);
302 506f7ceb Leszek Koltunski
          }
303
        }
304
      }
305 d9d2c5fb Leszek Koltunski
306
    RubikObject object = getObject(DEF_OBJECT);
307
    String defName = object==null ? "CUBE_3" : object.getUpperName();
308
    String objName= preferences.getString("rol_objName",defName);
309
    mObject = getOrdinal(objName);
310
    if( mObject<0 || mObject>=mNumObjects ) mObject = DEF_OBJECT;
311 400ff34d Leszek Koltunski
    }
312
313 f12e4de9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
314
315
  public static void restoreMeshState(SharedPreferences preferences)
316
    {
317
    for(int i=0; i<mNumObjects; i++)
318
      {
319
      RubikObject obj = getObject(i);
320
321
      if( obj!=null )
322
        {
323 84d746d7 Leszek Koltunski
        String name  = obj.getUpperName();
324 f12e4de9 Leszek Koltunski
        int meshState= preferences.getInt("rol_"+name,MESH_NICE);
325
        obj.setMeshState(meshState);
326
        }
327
      }
328
    }
329
330 400ff34d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
331
332 1088a219 Leszek Koltunski
  public static boolean setCurrObject(int ordinal)
333 400ff34d Leszek Koltunski
    {
334
    if( mObject!=ordinal )
335
      {
336
      mObject = ordinal;
337
      return true;
338
      }
339
340
    return false;
341
    }
342
343
///////////////////////////////////////////////////////////////////////////////////////////////////
344
345
  public static int getCurrObject()
346
    {
347
    return mObject;
348
    }
349
350 69b66386 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
351
352
  public static String getCurrentName()
353
    {
354
    RubikObject object = mObjects.get(mObject);
355
    return object==null ? "" : object.getUpperName();
356
    }
357
358 d433b50e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
359 a7d8c3cd Leszek Koltunski
360
  public static RubikObject getObject(int ordinal)
361
    {
362 506f7ceb Leszek Koltunski
    if( mThis==null ) mThis = new RubikObjectList();
363 a7d8c3cd Leszek Koltunski
    return ordinal>=0 && ordinal<mNumObjects ? mObjects.get(ordinal) : null;
364
    }
365
366 41a5dd89 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
367
368
  public static RubikObject getObject(String shortUpperName)
369
    {
370
    if( mThis==null ) mThis = new RubikObjectList();
371
372
    for(int i=0; i<mNumObjects; i++)
373
      {
374
      RubikObject object = mObjects.get(i);
375
      if( object.getUpperName().equals(shortUpperName) )
376
        {
377
        return object;
378
        }
379
      }
380
381
    return null;
382
    }
383
384 a7d8c3cd Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
385
386
  public static int getNumObjects()
387
    {
388 506f7ceb Leszek Koltunski
    if( mThis==null ) mThis = new RubikObjectList();
389 a7d8c3cd Leszek Koltunski
    return mNumObjects;
390
    }
391
392
///////////////////////////////////////////////////////////////////////////////////////////////////
393
394
  public static int getOrdinal(String name)
395
    {
396 506f7ceb Leszek Koltunski
    if( mThis==null ) mThis = new RubikObjectList();
397 a7d8c3cd Leszek Koltunski
398 5e048300 Leszek Koltunski
    String lowerName = name.toLowerCase(Locale.ENGLISH);
399 84d746d7 Leszek Koltunski
400 a7d8c3cd Leszek Koltunski
    for(int i=0; i<mNumObjects; i++)
401
      {
402
      RubikObject obj = mObjects.get(i);
403 84d746d7 Leszek Koltunski
      if( obj.getLowerName().equals(lowerName) ) return i;
404 a7d8c3cd Leszek Koltunski
      }
405
406
    return -1;
407
    }
408 804293f0 Leszek Koltunski
409 fcf7320f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
410
411
  public static int getNumExtrasObjects()
412
    {
413
    return mNumExtras;
414
    }
415
416 804293f0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
417
418
  public static int getNumTutorialObjects()
419
    {
420 fcf7320f Leszek Koltunski
    return mNumExtras;
421 804293f0 Leszek Koltunski
    }
422
423
///////////////////////////////////////////////////////////////////////////////////////////////////
424
425 fcf7320f Leszek Koltunski
  public static int getObjectOrdinal(int extrasOrdinal)
426 804293f0 Leszek Koltunski
    {
427 fcf7320f Leszek Koltunski
    for(int i=extrasOrdinal; i<mNumObjects; i++)
428 804293f0 Leszek Koltunski
      {
429
      RubikObject object = getObject(i);
430 fcf7320f Leszek Koltunski
      int extOrd = object!=null ? object.getExtrasOrdinal() : -1;
431
      if( extOrd==extrasOrdinal ) return i;
432 804293f0 Leszek Koltunski
      }
433
434
    return -1;
435
    }
436
437 fcf7320f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
438
439
  public static int getExtrasOrdinal(int objectOrdinal)
440
    {
441
    RubikObject object = getObject(objectOrdinal);
442
    return object!=null ? object.getExtrasOrdinal() : -1;
443
    }
444
445 804293f0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
446
447
  public static int getTutorialOrdinal(int objectOrdinal)
448
    {
449
    RubikObject object = getObject(objectOrdinal);
450 fcf7320f Leszek Koltunski
    return object!=null ? object.getExtrasOrdinal() : -1;
451
    }
452
453
///////////////////////////////////////////////////////////////////////////////////////////////////
454
455
  public static int getLocalObjectMinor(int objectOrdinal)
456
    {
457
    RubikObject object = getObject(objectOrdinal);
458
    return object!=null ? object.getObjectMinor() : -1;
459
    }
460
461
///////////////////////////////////////////////////////////////////////////////////////////////////
462
463
  public static int getLocalExtrasMinor(int objectOrdinal)
464
    {
465
    RubikObject object = getObject(objectOrdinal);
466
    return object!=null ? object.getExtrasMinor() : -1;
467 804293f0 Leszek Koltunski
    }
468 a7d8c3cd Leszek Koltunski
}