Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObject.java @ 7bb30586

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.patterns.RubikPatternList;
31
import org.distorted.solvers.ImplementedSolversList;
32

    
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34

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

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

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

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

    
55
  RubikObject(ObjectType type)
56
    {
57
    mObjectOrdinal= type.ordinal();
58

    
59
    mUpperName   = type.name();
60
    mLowerName   = type.name().toLowerCase(Locale.ENGLISH);
61
    mNumScramble = type.getNumScramble();
62
    mPrice       = type.getPrice();
63
    mIsFree      = mPrice==0;
64
    mIconID      = type.getIconID();
65
    mJsonID      = ObjectJson.getObjectJsonID(mObjectOrdinal);
66
    mMeshID      = ObjectMesh.getMeshID(mObjectOrdinal);
67
    mExtrasID    = ObjectJson.getExtrasJsonID(mObjectOrdinal);
68
    mIsLocal     = false;
69

    
70
    int patternOrdinal  = RubikPatternList.getOrdinal(mObjectOrdinal);
71
    mPatterns = RubikPatternList.getPatterns(patternOrdinal);
72

    
73
    mSolverOrdinal = ImplementedSolversList.getSolverOrdinal(mObjectOrdinal);
74
    mExtrasOrdinal = -1;
75

    
76
    mObjectVersion = ObjectType.getObjectVersion(mObjectOrdinal);
77
    mExtrasVersion = ObjectType.getExtrasVersion(mObjectOrdinal);
78
    }
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81

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

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

    
94
    mPatterns      = null;
95
    mExtrasOrdinal = -1;
96
    mSolverOrdinal = -1;
97
    mObjectOrdinal = -1;
98
    mIsLocal       = true;
99

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

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

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

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

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

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

    
127
      mExtrasVersion = object.extrasVersion;
128
      mExtrasID = -1;
129
      changed = true;
130
      }
131

    
132
    return changed;
133
    }
134

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136

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

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

    
154
  public void setExtrasOrdinal(int ordinal)
155
    {
156
    mExtrasOrdinal = ordinal;
157
    }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

    
161
  public int getExtrasOrdinal()
162
    {
163
    return mExtrasOrdinal;
164
    }
165

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167
// PUBLIC API
168

    
169
  public String getLowerName()
170
    {
171
    return mLowerName;
172
    }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175

    
176
  public String getUpperName()
177
    {
178
    return mUpperName;
179
    }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

    
183
  public int getNumScramble()
184
    {
185
    return mNumScramble;
186
    }
187

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

    
190
  public int getPrice()
191
    {
192
    return mPrice;
193
    }
194

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

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

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

    
204
  public void markFree()
205
    {
206
    mIsFree=true;
207
    }
208

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

    
211
  public int getObjectVersion()
212
    {
213
    return mObjectVersion;
214
    }
215

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

    
218
  public int getExtrasVersion()
219
    {
220
    return mExtrasVersion;
221
    }
222

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

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

    
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232

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

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

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

    
254
    return null;
255
    }
256

    
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258

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

    
267
    return null;
268
    }
269

    
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271

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

    
285
    return null;
286
    }
287

    
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289

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

    
295
///////////////////////////////////////////////////////////////////////////////////////////////////
296

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

    
302
///////////////////////////////////////////////////////////////////////////////////////////////////
303

    
304
  public int getSolverOrdinal()
305
    {
306
    return mSolverOrdinal;
307
    }
308

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

    
311
  public int getObjectOrdinal()
312
    {
313
    return mObjectOrdinal;
314
    }
315

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

    
318
  public boolean isLocal()
319
    {
320
    return mIsLocal;
321
    }
322

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

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

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

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