Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObject.java @ 920cbdf9

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.dmesh.ObjectMesh;
26
import org.distorted.external.RubikFiles;
27
import org.distorted.jsons.ObjectJson;
28
import org.distorted.main.R;
29
import org.distorted.objectlib.main.ObjectType;
30
import org.distorted.objectlib.metadata.Metadata;
31
import org.distorted.objectlib.patterns.RubikPatternList;
32
import org.distorted.solvers.ImplementedSolversList;
33

    
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35

    
36
public class RubikObject
37
{
38
  public static final int FAST_SCRAMBLES = 80;
39

    
40
  private final String mLowerName, mUpperName;
41
  private final int mIconID;
42
  private final String[][] mPatterns;
43
  private final int mPrice;
44
  private final int mSolverOrdinal;
45
  private final int mObjectOrdinal;
46
  private final boolean mIsLocal;
47

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

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

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

    
61
    mUpperName   = type.name();
62
    mLowerName   = type.name().toLowerCase(Locale.ENGLISH);
63
    mNumScramble = meta.numScrambles();
64
    mPrice       = meta.price();
65
    mIsFree      = mPrice==0;
66
    mIconID      = meta.icon();
67
    mJsonID      = ObjectJson.getObjectJsonID(mObjectOrdinal);
68
    mMeshID      = ObjectMesh.getMeshID(mObjectOrdinal);
69
    mExtrasID    = ObjectJson.getExtrasJsonID(mObjectOrdinal);
70
    mIsLocal     = false;
71

    
72
    int patternOrdinal  = RubikPatternList.getOrdinal(mObjectOrdinal);
73
    mPatterns = RubikPatternList.getPatterns(patternOrdinal);
74

    
75
    mSolverOrdinal = ImplementedSolversList.getSolverOrdinal(mObjectOrdinal);
76
    mExtrasOrdinal = -1;
77

    
78
    mObjectVersion = meta.objectVersion();
79
    mExtrasVersion = meta.extrasVersion();
80
    }
81

    
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83

    
84
  RubikObject(RubikObjectList.DownloadedObject object)
85
    {
86
    if( RubikObjectList.SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "new downloaded RubikObject "+object.shortName+" added");
87

    
88
    mLowerName     = object.shortName;
89
    mUpperName     = object.shortName.toUpperCase(Locale.ENGLISH);
90
    mNumScramble   = object.numScrambles;
91
    mPrice         = object.price;
92
    mIsFree        = mPrice==0;
93
    mObjectVersion = object.objectVersion;
94
    mExtrasVersion = object.extrasVersion;
95

    
96
    mPatterns      = null;
97
    mExtrasOrdinal = -1;
98
    mSolverOrdinal = -1;
99
    mObjectOrdinal = -1;
100
    mIsLocal       = true;
101

    
102
    mMeshID        =  0;
103
    mJsonID        = object.object ? -1 : 0;
104
    mExtrasID      = object.extras ? -1 : 0;
105
    mIconID        = object.icon   ? -1 : 0;
106
    }
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109

    
110
  public boolean updateObject(RubikObjectList.DownloadedObject object)
111
    {
112
    boolean changed = false;
113

    
114
    if( object.objectVersion>mObjectVersion )
115
      {
116
      if( RubikObjectList.SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Updating RubikObject's "+object.shortName+" main JSON");
117

    
118
      mObjectVersion= object.objectVersion;
119
      mNumScramble  = object.numScrambles;
120
      mMeshID =  0;
121
      mJsonID = -1;
122
      changed = true;
123
      }
124

    
125
    if( object.extrasVersion>mExtrasVersion )
126
      {
127
      if( RubikObjectList.SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Updating RubikObject's "+object.shortName+" extras JSON");
128

    
129
      mExtrasVersion = object.extrasVersion;
130
      mExtrasID = -1;
131
      changed = true;
132
      }
133

    
134
    return changed;
135
    }
136

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138

    
139
  private Drawable createIconDrawable(Activity act)
140
    {
141
    if( mIconID>0 )
142
      {
143
      return AppCompatResources.getDrawable(act,mIconID);
144
      }
145
    else
146
      {
147
      RubikFiles files = RubikFiles.getInstance();
148
      Bitmap bmp = files.getIcon(act,mLowerName+".png");
149
      if( bmp==null ) return AppCompatResources.getDrawable(act,R.drawable.unknown_icon);
150
      else            return new BitmapDrawable(act.getResources(), bmp);
151
      }
152
    }
153

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155

    
156
  public void setExtrasOrdinal(int ordinal)
157
    {
158
    mExtrasOrdinal = ordinal;
159
    }
160

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162

    
163
  public int getExtrasOrdinal()
164
    {
165
    return mExtrasOrdinal;
166
    }
167

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169
// PUBLIC API
170

    
171
  public String getLowerName()
172
    {
173
    return mLowerName;
174
    }
175

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

    
178
  public String getUpperName()
179
    {
180
    return mUpperName;
181
    }
182

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184

    
185
  public int getNumScramble()
186
    {
187
    return mNumScramble;
188
    }
189

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191

    
192
  public int getPrice()
193
    {
194
    return mPrice;
195
    }
196

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198

    
199
  public boolean isFree()
200
    {
201
    return (!RubikObjectList.USE_IAP || mIsFree);
202
    }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

    
206
  public void markFree()
207
    {
208
    mIsFree=true;
209
    }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

    
213
  public int getObjectVersion()
214
    {
215
    return mObjectVersion;
216
    }
217

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

    
220
  public int getExtrasVersion()
221
    {
222
    return mExtrasVersion;
223
    }
224

    
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226

    
227
  public void setIconTo(Activity act,ImageButton button)
228
    {
229
    Drawable icon = createIconDrawable(act);
230
    button.setBackground(icon);
231
    }
232

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

    
235
  public void setIconTo(Activity act,ImageView view)
236
    {
237
    Drawable icon = createIconDrawable(act);
238
    view.setImageDrawable(icon);
239
    }
240

    
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242

    
243
  public InputStream getObjectStream(Activity act)
244
    {
245
    if( mJsonID>0 )
246
      {
247
      Resources res = act.getResources();
248
      return res.openRawResource(mJsonID);
249
      }
250
    if( mJsonID<0 )
251
      {
252
      RubikFiles files = RubikFiles.getInstance();
253
      return files.openFile(act,mLowerName+"_object.json");
254
      }
255

    
256
    return null;
257
    }
258

    
259
///////////////////////////////////////////////////////////////////////////////////////////////////
260

    
261
  public InputStream getMeshStream(Activity act)
262
    {
263
    if( mMeshID>0 )
264
      {
265
      Resources res = act.getResources();
266
      return res.openRawResource(mMeshID);
267
      }
268

    
269
    return null;
270
    }
271

    
272
///////////////////////////////////////////////////////////////////////////////////////////////////
273

    
274
  public InputStream getExtrasStream(Activity act)
275
    {
276
    if( mExtrasID>0 )
277
      {
278
      Resources res = act.getResources();
279
      return res.openRawResource(mExtrasID);
280
      }
281
    if( mExtrasID<0 )
282
      {
283
      RubikFiles files = RubikFiles.getInstance();
284
      return files.openFile(act,mLowerName+"_extras.json");
285
      }
286

    
287
    return null;
288
    }
289

    
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291

    
292
  public boolean hasExtras()
293
    {
294
    return mExtrasID!=0;
295
    }
296

    
297
///////////////////////////////////////////////////////////////////////////////////////////////////
298

    
299
  public boolean hasSolver()
300
    {
301
    return mSolverOrdinal>=0;
302
    }
303

    
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305

    
306
  public int getSolverOrdinal()
307
    {
308
    return mSolverOrdinal;
309
    }
310

    
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312

    
313
  public int getObjectOrdinal()
314
    {
315
    return mObjectOrdinal;
316
    }
317

    
318
///////////////////////////////////////////////////////////////////////////////////////////////////
319

    
320
  public boolean isLocal()
321
    {
322
    return mIsLocal;
323
    }
324

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326

    
327
  public boolean hasPatterns()
328
    {
329
    return mPatterns!=null;
330
    }
331

    
332
///////////////////////////////////////////////////////////////////////////////////////////////////
333

    
334
  public String[][] getPatterns()
335
    {
336
    return mPatterns;
337
    }
338
}
(1-1/2)