Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObject.java @ master

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

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

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

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

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

    
79
    int patternOrdinal  = RubikPatternList.getOrdinal(mObjectOrdinal);
80
    mPatterns = RubikPatternList.getPatterns(patternOrdinal);
81

    
82
    mSolverOrdinal = ImplementedSolversList.getSolverOrdinal(mObjectOrdinal);
83
    mExtrasOrdinal = -1;
84

    
85
    mObjectVersion = meta.objectVersion();
86
    mExtrasVersion = meta.extrasVersion();
87
    }
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

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

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

    
108
    mPatterns      = null;
109
    mExtrasOrdinal = -1;
110
    mSolverOrdinal = -1;
111
    mObjectOrdinal = -1;
112
    mIsLocal       = true;
113

    
114
    mMeshID        =  0;
115
    mJsonID        = object.object ? -1 : 0;
116
    mExtrasID      = object.extras ? -1 : 0;
117
    mIconID        = object.icon   ? -1 : 0;
118
    }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

    
122
  public boolean updateObject(RubikObjectList.DownloadedObject object)
123
    {
124
    boolean changed = false;
125

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

    
130
      mObjectVersion= object.objectVersion;
131
      mNumScramble  = object.numScrambles;
132
      mMeshID =  0;
133
      mJsonID = -1;
134
      changed = true;
135
      }
136

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

    
141
      mExtrasVersion = object.extrasVersion;
142
      mExtrasID = -1;
143
      changed = true;
144
      }
145

    
146
    return changed;
147
    }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

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

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

    
168
  public void setExtrasOrdinal(int ordinal)
169
    {
170
    mExtrasOrdinal = ordinal;
171
    }
172

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

    
175
  public int getExtrasOrdinal()
176
    {
177
    return mExtrasOrdinal;
178
    }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181
// PUBLIC API
182

    
183
  public String getLowerName()
184
    {
185
    return mLowerName;
186
    }
187

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189

    
190
  public String getUpperName()
191
    {
192
    return mUpperName;
193
    }
194

    
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196

    
197
  public int getNumScramble()
198
    {
199
    return mNumScramble;
200
    }
201

    
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203

    
204
  public int getPrice()
205
    {
206
    return mPrice;
207
    }
208

    
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210

    
211
  public boolean isFree()
212
    {
213
    return (!RubikObjectList.USE_IAP || mIsFree);
214
    }
215

    
216
///////////////////////////////////////////////////////////////////////////////////////////////////
217

    
218
  public void markFree()
219
    {
220
    mIsFree=true;
221
    }
222

    
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224

    
225
  public int getObjectVersion()
226
    {
227
    return mObjectVersion;
228
    }
229

    
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231

    
232
  public int getExtrasVersion()
233
    {
234
    return mExtrasVersion;
235
    }
236

    
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238

    
239
  public boolean supportsAdjustableColors()
240
    {
241
    return mAdjColors;
242
    }
243

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

    
246
  public void setIconTo(Activity act,ImageButton button)
247
    {
248
    Drawable icon = createIconDrawable(act);
249
    button.setBackground(icon);
250
    }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253

    
254
  public void setIconTo(Activity act,ImageView view)
255
    {
256
    Drawable icon = createIconDrawable(act);
257
    view.setImageDrawable(icon);
258
    }
259

    
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261

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

    
275
    return null;
276
    }
277

    
278
///////////////////////////////////////////////////////////////////////////////////////////////////
279

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

    
288
    return null;
289
    }
290

    
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292

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

    
306
    return null;
307
    }
308

    
309
///////////////////////////////////////////////////////////////////////////////////////////////////
310

    
311
  public int getCategory()
312
    {
313
    return mCategory;
314
    }
315

    
316
///////////////////////////////////////////////////////////////////////////////////////////////////
317

    
318
  public float getDifficulty()
319
    {
320
    return mDifficulty;
321
    }
322

    
323
///////////////////////////////////////////////////////////////////////////////////////////////////
324

    
325
  public int getYearOfInvention()
326
    {
327
    return mYear;
328
    }
329

    
330
///////////////////////////////////////////////////////////////////////////////////////////////////
331

    
332
  public String getAuthor()
333
    {
334
    return mAuthor;
335
    }
336

    
337
///////////////////////////////////////////////////////////////////////////////////////////////////
338

    
339
  public boolean hasExtras()
340
    {
341
    return mExtrasID!=0;
342
    }
343

    
344
///////////////////////////////////////////////////////////////////////////////////////////////////
345

    
346
  public boolean hasSolver()
347
    {
348
    return mSolverOrdinal>=0;
349
    }
350

    
351
///////////////////////////////////////////////////////////////////////////////////////////////////
352

    
353
  public int getSolverOrdinal()
354
    {
355
    return mSolverOrdinal;
356
    }
357

    
358
///////////////////////////////////////////////////////////////////////////////////////////////////
359

    
360
  public int getObjectOrdinal()
361
    {
362
    return mObjectOrdinal;
363
    }
364

    
365
///////////////////////////////////////////////////////////////////////////////////////////////////
366

    
367
  public boolean isLocal()
368
    {
369
    return mIsLocal;
370
    }
371

    
372
///////////////////////////////////////////////////////////////////////////////////////////////////
373

    
374
  public boolean hasPatterns()
375
    {
376
    return mPatterns!=null;
377
    }
378

    
379
///////////////////////////////////////////////////////////////////////////////////////////////////
380

    
381
  public String[][] getPatterns()
382
    {
383
    return mPatterns;
384
    }
385
}
(1-1/7)