Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObject.java @ 740fade2

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2021 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.objects;
11

    
12
import java.io.InputStream;
13
import java.util.Locale;
14

    
15
import android.app.Activity;
16
import android.content.res.Resources;
17
import android.graphics.Bitmap;
18
import android.graphics.drawable.BitmapDrawable;
19
import android.graphics.drawable.Drawable;
20
import android.widget.ImageButton;
21
import android.widget.ImageView;
22

    
23
import androidx.appcompat.content.res.AppCompatResources;
24

    
25
import org.distorted.external.RubikFiles;
26
import org.distorted.main.R;
27
import org.distorted.objectlib.metadata.ListObjects;
28
import org.distorted.objectlib.metadata.Metadata;
29
import org.distorted.objectlib.patterns.RubikPatternList;
30
import org.distorted.solvers.ImplementedSolversList;
31

    
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33

    
34
public class RubikObject
35
{
36
  public static final int FAST_SCRAMBLES = 80;
37

    
38
  private final String mLowerName, mUpperName;
39
  private final int mIconID;
40
  private final String[][] mPatterns;
41
  private final int mPrice;
42
  private final int mSolverOrdinal;
43
  private final int mObjectOrdinal;
44
  private final boolean mIsLocal;
45
  private final int mCategory, mYear;
46
  private final float mDifficulty;
47
  private final String mAuthor;
48
  private final boolean mAdjColors;
49
  private final int mObjectIndex;
50

    
51
  private boolean mIsFree;
52
  private int mJsonID, mMeshID, mExtrasID;
53
  private int mObjectVersion, mExtrasVersion;
54
  private int mNumScramble;
55
  private int mExtrasOrdinal;
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

    
59
  RubikObject(ListObjects type)
60
    {
61
    Metadata meta = type.getMetadata();
62
    mObjectOrdinal= type.ordinal();
63

    
64
    mUpperName   = type.name();
65
    mLowerName   = type.name().toLowerCase(Locale.ENGLISH);
66
    mNumScramble = meta.numScrambles();
67
    mPrice       = meta.price();
68
    mIsFree      = mPrice==0;
69
    mIconID      = meta.icon();
70
    mJsonID      = meta.objectJson();
71
    mMeshID      = meta.mesh();
72
    mExtrasID    = meta.extrasJson();
73
    mDifficulty  = meta.getDifficulty();
74
    mCategory    = meta.getCategory();
75
    mYear        = meta.getYearOfInvention();
76
    mAuthor      = meta.getAuthor();
77
    mIsLocal     = false;
78
    mAdjColors   = meta.supportsAdjustableStickerColors();
79
    mObjectIndex = ListObjects.getObjectIndex(mUpperName);
80

    
81
    int patternOrdinal  = RubikPatternList.getOrdinal(mObjectIndex);
82
    mPatterns = RubikPatternList.getPatterns(patternOrdinal);
83

    
84
    mSolverOrdinal = ImplementedSolversList.getSolverOrdinal(mObjectIndex);
85
    mExtrasOrdinal = -1;
86

    
87
    mObjectVersion = meta.objectVersion();
88
    mExtrasVersion = meta.extrasVersion();
89
    }
90

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

    
93
  RubikObject(RubikObjectList.DownloadedObject object)
94
    {
95
    if( RubikObjectList.SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "new downloaded RubikObject "+object.shortName+" added");
96

    
97
    mLowerName     = object.shortName;
98
    mUpperName     = object.shortName.toUpperCase(Locale.ENGLISH);
99
    mNumScramble   = object.numScrambles;
100
    mPrice         = object.price;
101
    mIsFree        = mPrice==0;
102
    mObjectVersion = object.objectVersion;
103
    mExtrasVersion = object.extrasVersion;
104
    mDifficulty    = object.difficulty;
105
    mCategory      = object.category;
106
    mYear          = object.year;
107
    mAuthor        = object.author;
108
    mAdjColors     = object.adjColors;
109
    mObjectIndex   = -1;
110

    
111
    mPatterns      = null;
112
    mExtrasOrdinal = -1;
113
    mSolverOrdinal = -1;
114
    mObjectOrdinal = -1;
115
    mIsLocal       = true;
116

    
117
    mMeshID        =  0;
118
    mJsonID        = object.object ? -1 : 0;
119
    mExtrasID      = object.extras ? -1 : 0;
120
    mIconID        = object.icon   ? -1 : 0;
121
    }
122

    
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124

    
125
  public boolean updateObject(RubikObjectList.DownloadedObject object)
126
    {
127
    boolean changed = false;
128

    
129
    if( object.objectVersion>mObjectVersion )
130
      {
131
      if( RubikObjectList.SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Updating RubikObject's "+object.shortName+" main JSON");
132

    
133
      mObjectVersion= object.objectVersion;
134
      mNumScramble  = object.numScrambles;
135
      mMeshID =  0;
136
      mJsonID = -1;
137
      changed = true;
138
      }
139

    
140
    if( object.extrasVersion>mExtrasVersion )
141
      {
142
      if( RubikObjectList.SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Updating RubikObject's "+object.shortName+" extras JSON");
143

    
144
      mExtrasVersion = object.extrasVersion;
145
      mExtrasID = -1;
146
      changed = true;
147
      }
148

    
149
    return changed;
150
    }
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153

    
154
  private Drawable createIconDrawable(Activity act)
155
    {
156
    if( mIconID>0 )
157
      {
158
      return AppCompatResources.getDrawable(act,mIconID);
159
      }
160
    else
161
      {
162
      RubikFiles files = RubikFiles.getInstance();
163
      Bitmap bmp = files.getIcon(act,mLowerName+".png");
164
      if( bmp==null ) return AppCompatResources.getDrawable(act,R.drawable.unknown_icon);
165
      else            return new BitmapDrawable(act.getResources(), bmp);
166
      }
167
    }
168

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170

    
171
  public void setExtrasOrdinal(int ordinal)
172
    {
173
    mExtrasOrdinal = ordinal;
174
    }
175

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177

    
178
  public int getExtrasOrdinal()
179
    {
180
    return mExtrasOrdinal;
181
    }
182

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184
// PUBLIC API
185

    
186
  public String getLowerName()
187
    {
188
    return mLowerName;
189
    }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

    
193
  public String getUpperName()
194
    {
195
    return mUpperName;
196
    }
197

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

    
200
  public int getNumScramble()
201
    {
202
    return mNumScramble;
203
    }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

    
207
  public int getPrice()
208
    {
209
    return mPrice;
210
    }
211

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

    
214
  public boolean isFree()
215
    {
216
    return (!RubikObjectList.USE_IAP || mIsFree);
217
    }
218

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

    
221
  public void markFree()
222
    {
223
    mIsFree=true;
224
    }
225

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227

    
228
  public int getObjectVersion()
229
    {
230
    return mObjectVersion;
231
    }
232

    
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234

    
235
  public int getExtrasVersion()
236
    {
237
    return mExtrasVersion;
238
    }
239

    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241

    
242
  public boolean supportsAdjustableColors()
243
    {
244
    return mAdjColors;
245
    }
246

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248

    
249
  public void setIconTo(Activity act,ImageButton button)
250
    {
251
    Drawable icon = createIconDrawable(act);
252
    button.setBackground(icon);
253
    }
254

    
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256

    
257
  public void setIconTo(Activity act,ImageView view)
258
    {
259
    Drawable icon = createIconDrawable(act);
260
    view.setImageDrawable(icon);
261
    }
262

    
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264

    
265
  public InputStream getObjectStream(Activity act)
266
    {
267
    if( mJsonID>0 )
268
      {
269
      Resources res = act.getResources();
270
      return res.openRawResource(mJsonID);
271
      }
272
    if( mJsonID<0 )
273
      {
274
      RubikFiles files = RubikFiles.getInstance();
275
      return files.openFile(act,mLowerName+"_object.json");
276
      }
277

    
278
    return null;
279
    }
280

    
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282

    
283
  public InputStream getMeshStream(Activity act)
284
    {
285
    if( mMeshID>0 )
286
      {
287
      Resources res = act.getResources();
288
      return res.openRawResource(mMeshID);
289
      }
290

    
291
    return null;
292
    }
293

    
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295

    
296
  public InputStream getExtrasStream(Activity act)
297
    {
298
    if( mExtrasID>0 )
299
      {
300
      Resources res = act.getResources();
301
      return res.openRawResource(mExtrasID);
302
      }
303
    if( mExtrasID<0 )
304
      {
305
      RubikFiles files = RubikFiles.getInstance();
306
      return files.openFile(act,mLowerName+"_extras.json");
307
      }
308

    
309
    return null;
310
    }
311

    
312
///////////////////////////////////////////////////////////////////////////////////////////////////
313

    
314
  public int getCategory()
315
    {
316
    return mCategory;
317
    }
318

    
319
///////////////////////////////////////////////////////////////////////////////////////////////////
320

    
321
  public int getIndex()
322
    {
323
    return mObjectIndex;
324
    }
325

    
326
///////////////////////////////////////////////////////////////////////////////////////////////////
327

    
328
  public float getDifficulty()
329
    {
330
    return mDifficulty;
331
    }
332

    
333
///////////////////////////////////////////////////////////////////////////////////////////////////
334

    
335
  public int getYearOfInvention()
336
    {
337
    return mYear;
338
    }
339

    
340
///////////////////////////////////////////////////////////////////////////////////////////////////
341

    
342
  public String getAuthor()
343
    {
344
    return mAuthor;
345
    }
346

    
347
///////////////////////////////////////////////////////////////////////////////////////////////////
348

    
349
  public boolean hasExtras()
350
    {
351
    return mExtrasID!=0;
352
    }
353

    
354
///////////////////////////////////////////////////////////////////////////////////////////////////
355

    
356
  public boolean hasSolver()
357
    {
358
    return mSolverOrdinal>=0;
359
    }
360

    
361
///////////////////////////////////////////////////////////////////////////////////////////////////
362

    
363
  public int getSolverOrdinal()
364
    {
365
    return mSolverOrdinal;
366
    }
367

    
368
///////////////////////////////////////////////////////////////////////////////////////////////////
369

    
370
  public int getObjectOrdinal()
371
    {
372
    return mObjectOrdinal;
373
    }
374

    
375
///////////////////////////////////////////////////////////////////////////////////////////////////
376

    
377
  public boolean isLocal()
378
    {
379
    return mIsLocal;
380
    }
381

    
382
///////////////////////////////////////////////////////////////////////////////////////////////////
383

    
384
  public boolean hasPatterns()
385
    {
386
    return mPatterns!=null;
387
    }
388

    
389
///////////////////////////////////////////////////////////////////////////////////////////////////
390

    
391
  public String[][] getPatterns()
392
    {
393
    return mPatterns;
394
    }
395
}
(1-1/7)