Project

General

Profile

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

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

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.main_old.RubikActivity;
30
import org.distorted.objectlib.main.ObjectType;
31
import org.distorted.objectlib.patterns.RubikPatternList;
32
import org.distorted.solvers.ImplementedSolversList;
33

    
34
import static org.distorted.main_old.RubikActivity.SHOW_DOWNLOADED_DEBUG;
35

    
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37

    
38
public class RubikObject
39
{
40
  public static final int FAST_SCRAMBLES = 80;
41

    
42
  private final String mLowerName, mUpperName;
43
  private final int mIconID;
44
  private final String[][] mPatterns;
45
  private final int mPrice;
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
  private int mSolverOrdinal;
53

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

    
56
  RubikObject(ObjectType type)
57
    {
58
    int ordinal= type.ordinal();
59

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

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

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

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

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

    
82
  RubikObject(RubikObjectList.DownloadedObject object)
83
    {
84
    if( 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

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

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105

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

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

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

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

    
125
      mExtrasVersion = object.extrasVersion;
126
      mExtrasID = -1;
127
      changed = true;
128
      }
129

    
130
    return changed;
131
    }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

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

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151

    
152
  public void setExtrasOrdinal(int ordinal)
153
    {
154
    mExtrasOrdinal = ordinal;
155
    }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

    
159
  public int getExtrasOrdinal()
160
    {
161
    return mExtrasOrdinal;
162
    }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165
// PUBLIC API
166

    
167
  public String getLowerName()
168
    {
169
    return mLowerName;
170
    }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

    
174
  public String getUpperName()
175
    {
176
    return mUpperName;
177
    }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

    
181
  public int getNumScramble()
182
    {
183
    return mNumScramble;
184
    }
185

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187

    
188
  public int getPrice()
189
    {
190
    return mPrice;
191
    }
192

    
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194

    
195
  public boolean isFree()
196
    {
197
    return (!RubikActivity.USE_IAP || mIsFree);
198
    }
199

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201

    
202
  public void markFree()
203
    {
204
    mIsFree=true;
205
    }
206

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

    
209
  public int getObjectVersion()
210
    {
211
    return mObjectVersion;
212
    }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

    
216
  public int getExtrasVersion()
217
    {
218
    return mExtrasVersion;
219
    }
220

    
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222

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

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

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

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

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

    
252
    return null;
253
    }
254

    
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256

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

    
265
    return null;
266
    }
267

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

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

    
283
    return null;
284
    }
285

    
286
///////////////////////////////////////////////////////////////////////////////////////////////////
287

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

    
293
///////////////////////////////////////////////////////////////////////////////////////////////////
294

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

    
300
///////////////////////////////////////////////////////////////////////////////////////////////////
301

    
302
  public int getSolverOrdinal()
303
    {
304
    return mSolverOrdinal;
305
    }
306

    
307
///////////////////////////////////////////////////////////////////////////////////////////////////
308

    
309
  public boolean hasPatterns()
310
    {
311
    return mPatterns!=null;
312
    }
313

    
314
///////////////////////////////////////////////////////////////////////////////////////////////////
315

    
316
  public String[][] getPatterns()
317
    {
318
    return mPatterns;
319
    }
320
}
(1-1/2)