Project

General

Profile

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

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

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

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

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

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

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

    
77
    int patternOrdinal  = RubikPatternList.getOrdinal(mObjectOrdinal);
78
    mPatterns = RubikPatternList.getPatterns(patternOrdinal);
79

    
80
    mSolverOrdinal = ImplementedSolversList.getSolverOrdinal(mObjectOrdinal);
81
    mExtrasOrdinal = -1;
82

    
83
    mObjectVersion = meta.objectVersion();
84
    mExtrasVersion = meta.extrasVersion();
85
    }
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88

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

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

    
105
    mPatterns      = null;
106
    mExtrasOrdinal = -1;
107
    mSolverOrdinal = -1;
108
    mObjectOrdinal = -1;
109
    mIsLocal       = true;
110

    
111
    mMeshID        =  0;
112
    mJsonID        = object.object ? -1 : 0;
113
    mExtrasID      = object.extras ? -1 : 0;
114
    mIconID        = object.icon   ? -1 : 0;
115
    }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

    
119
  public boolean updateObject(RubikObjectList.DownloadedObject object)
120
    {
121
    boolean changed = false;
122

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

    
127
      mObjectVersion= object.objectVersion;
128
      mNumScramble  = object.numScrambles;
129
      mMeshID =  0;
130
      mJsonID = -1;
131
      changed = true;
132
      }
133

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

    
138
      mExtrasVersion = object.extrasVersion;
139
      mExtrasID = -1;
140
      changed = true;
141
      }
142

    
143
    return changed;
144
    }
145

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

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

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164

    
165
  public void setExtrasOrdinal(int ordinal)
166
    {
167
    mExtrasOrdinal = ordinal;
168
    }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

    
172
  public int getExtrasOrdinal()
173
    {
174
    return mExtrasOrdinal;
175
    }
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178
// PUBLIC API
179

    
180
  public String getLowerName()
181
    {
182
    return mLowerName;
183
    }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186

    
187
  public String getUpperName()
188
    {
189
    return mUpperName;
190
    }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193

    
194
  public int getNumScramble()
195
    {
196
    return mNumScramble;
197
    }
198

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200

    
201
  public int getPrice()
202
    {
203
    return mPrice;
204
    }
205

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

    
208
  public boolean isFree()
209
    {
210
    return (!RubikObjectList.USE_IAP || mIsFree);
211
    }
212

    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214

    
215
  public void markFree()
216
    {
217
    mIsFree=true;
218
    }
219

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221

    
222
  public int getObjectVersion()
223
    {
224
    return mObjectVersion;
225
    }
226

    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228

    
229
  public int getExtrasVersion()
230
    {
231
    return mExtrasVersion;
232
    }
233

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235

    
236
  public void setIconTo(Activity act,ImageButton button)
237
    {
238
    Drawable icon = createIconDrawable(act);
239
    button.setBackground(icon);
240
    }
241

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

    
244
  public void setIconTo(Activity act,ImageView view)
245
    {
246
    Drawable icon = createIconDrawable(act);
247
    view.setImageDrawable(icon);
248
    }
249

    
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251

    
252
  public InputStream getObjectStream(Activity act)
253
    {
254
    if( mJsonID>0 )
255
      {
256
      Resources res = act.getResources();
257
      return res.openRawResource(mJsonID);
258
      }
259
    if( mJsonID<0 )
260
      {
261
      RubikFiles files = RubikFiles.getInstance();
262
      return files.openFile(act,mLowerName+"_object.json");
263
      }
264

    
265
    return null;
266
    }
267

    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269

    
270
  public InputStream getMeshStream(Activity act)
271
    {
272
    if( mMeshID>0 )
273
      {
274
      Resources res = act.getResources();
275
      return res.openRawResource(mMeshID);
276
      }
277

    
278
    return null;
279
    }
280

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

    
283
  public InputStream getExtrasStream(Activity act)
284
    {
285
    if( mExtrasID>0 )
286
      {
287
      Resources res = act.getResources();
288
      return res.openRawResource(mExtrasID);
289
      }
290
    if( mExtrasID<0 )
291
      {
292
      RubikFiles files = RubikFiles.getInstance();
293
      return files.openFile(act,mLowerName+"_extras.json");
294
      }
295

    
296
    return null;
297
    }
298

    
299
///////////////////////////////////////////////////////////////////////////////////////////////////
300

    
301
  public int getCategory()
302
    {
303
    return mCategory;
304
    }
305

    
306
///////////////////////////////////////////////////////////////////////////////////////////////////
307

    
308
  public float getDifficulty()
309
    {
310
    return mDifficulty;
311
    }
312

    
313
///////////////////////////////////////////////////////////////////////////////////////////////////
314

    
315
  public int getYearOfInvention()
316
    {
317
    return mYear;
318
    }
319

    
320
///////////////////////////////////////////////////////////////////////////////////////////////////
321

    
322
  public String getAuthor()
323
    {
324
    return mAuthor;
325
    }
326

    
327
///////////////////////////////////////////////////////////////////////////////////////////////////
328

    
329
  public boolean hasExtras()
330
    {
331
    return mExtrasID!=0;
332
    }
333

    
334
///////////////////////////////////////////////////////////////////////////////////////////////////
335

    
336
  public boolean hasSolver()
337
    {
338
    return mSolverOrdinal>=0;
339
    }
340

    
341
///////////////////////////////////////////////////////////////////////////////////////////////////
342

    
343
  public int getSolverOrdinal()
344
    {
345
    return mSolverOrdinal;
346
    }
347

    
348
///////////////////////////////////////////////////////////////////////////////////////////////////
349

    
350
  public int getObjectOrdinal()
351
    {
352
    return mObjectOrdinal;
353
    }
354

    
355
///////////////////////////////////////////////////////////////////////////////////////////////////
356

    
357
  public boolean isLocal()
358
    {
359
    return mIsLocal;
360
    }
361

    
362
///////////////////////////////////////////////////////////////////////////////////////////////////
363

    
364
  public boolean hasPatterns()
365
    {
366
    return mPatterns!=null;
367
    }
368

    
369
///////////////////////////////////////////////////////////////////////////////////////////////////
370

    
371
  public String[][] getPatterns()
372
    {
373
    return mPatterns;
374
    }
375
}
(1-1/3)