Project

General

Profile

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

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

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.helpers.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.objectlib.solvers.verifiers.ImplementedVerifierList;
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 mNumSolvers;
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
    int[] solverOrdinals= ImplementedVerifierList.getSolverOrdinals(mUpperName);
85
    mNumSolvers = solverOrdinals==null ? 0 : solverOrdinals.length;
86
    mExtrasOrdinal = -1;
87

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

    
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93

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

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

    
112
    mPatterns      = null;
113
    mExtrasOrdinal = -1;
114
    mNumSolvers    = 0;
115
    mObjectOrdinal = -1;
116
    mIsLocal       = true;
117

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

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

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

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

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

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

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

    
150
    return changed;
151
    }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

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

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

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173
// PUBLIC API
174

    
175
  public void setIconTo(Activity act,ImageButton button)
176
    {
177
    Drawable icon = createIconDrawable(act);
178
    button.setBackground(icon);
179
    }
180

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

    
183
  public void setIconTo(Activity act,ImageView view)
184
    {
185
    Drawable icon = createIconDrawable(act);
186
    view.setImageDrawable(icon);
187
    }
188

    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190

    
191
  public InputStream getObjectStream(Activity act)
192
    {
193
    if( mJsonID>0 )
194
      {
195
      Resources res = act.getResources();
196
      return res.openRawResource(mJsonID);
197
      }
198
    if( mJsonID<0 )
199
      {
200
      RubikFiles files = RubikFiles.getInstance();
201
      return files.openFile(act,mLowerName+"_object.json");
202
      }
203

    
204
    return null;
205
    }
206

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

    
209
  public InputStream getMeshStream(Activity act)
210
    {
211
    if( mMeshID>0 )
212
      {
213
      Resources res = act.getResources();
214
      return res.openRawResource(mMeshID);
215
      }
216

    
217
    return null;
218
    }
219

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

    
222
  public InputStream getExtrasStream(Activity act)
223
    {
224
    if( mExtrasID>0 )
225
      {
226
      Resources res = act.getResources();
227
      return res.openRawResource(mExtrasID);
228
      }
229
    if( mExtrasID<0 )
230
      {
231
      RubikFiles files = RubikFiles.getInstance();
232
      return files.openFile(act,mLowerName+"_extras.json");
233
      }
234

    
235
    return null;
236
    }
237

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239

    
240
  public int getCategory()                  { return mCategory; }
241
  public int getIndex()                     { return mObjectIndex; }
242
  public float getDifficulty()              { return mDifficulty; }
243
  public int getYearOfInvention()           { return mYear; }
244
  public String getAuthor()                 { return mAuthor; }
245
  public boolean hasExtras()                { return mExtrasID!=0; }
246
  public int numSolvers()                   { return mNumSolvers; }
247
  public int getObjectOrdinal()             { return mObjectOrdinal; }
248
  public boolean isLocal()                  { return mIsLocal; }
249
  public boolean hasPatterns()              { return mPatterns!=null; }
250
  public String[][] getPatterns()           { return mPatterns; }
251
  public String getLowerName()              { return mLowerName; }
252
  public String getUpperName()              { return mUpperName; }
253
  public int getNumScramble()               { return mNumScramble; }
254
  public int getPrice()                     { return mPrice; }
255
  public boolean isFree()                   { return (!RubikObjectList.USE_IAP || mIsFree); }
256
  public void markFree()                    { mIsFree=true; }
257
  public int getObjectVersion()             { return mObjectVersion; }
258
  public int getExtrasVersion()             { return mExtrasVersion; }
259
  public void setExtrasOrdinal(int ordinal) { mExtrasOrdinal = ordinal; }
260
  public int getExtrasOrdinal()             { return mExtrasOrdinal; }
261
  public boolean supportsAdjustableColors() { return mAdjColors; }
262
}
(1-1/7)