Project

General

Profile

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

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

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.objectlib.main.TwistyObject.MESH_NICE;
35
import static org.distorted.main_old.RubikActivity.SHOW_DOWNLOADED_DEBUG;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

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

    
43
  private final String mLowerName, mUpperName;
44
  private final int mIconID;
45
  private final String[][] mPatterns;
46
  private final int mPrice;
47

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

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

    
58
  RubikObject(ObjectType type)
59
    {
60
    int ordinal= type.ordinal();
61

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

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

    
75
    mSolverOrdinal = ImplementedSolversList.getSolverOrdinal(ordinal);
76

    
77
    mMeshState = MESH_NICE;
78
    mExtrasOrdinal = -1;
79

    
80
    mObjectVersion = ObjectType.getObjectVersion(ordinal);
81
    mExtrasVersion = ObjectType.getExtrasVersion(ordinal);
82
    }
83

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

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

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

    
98
    mPatterns      = null;
99
    mMeshState     = MESH_NICE;
100
    mExtrasOrdinal = -1;
101
    mSolverOrdinal = -1;
102

    
103
    mMeshID        =  0;
104
    mJsonID        = -1;
105
    mExtrasID      = -1;
106
    mIconID        = -1;
107
    }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

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

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

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

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

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

    
135
    return changed;
136
    }
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

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

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156

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

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163

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

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170

    
171
  public void setMeshState(int state)
172
    {
173
    mMeshState = state;
174
    }
175

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

    
178
  public int getMeshState()
179
    {
180
    return mMeshState;
181
    }
182

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184
// PUBLIC API
185

    
186
  public String getLowerName()
187
    {
188
    return mLowerName;
189
    }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

    
193
  public String getUpperName()
194
    {
195
    return mUpperName;
196
    }
197

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

    
200
  public int getNumScramble()
201
    {
202
    return mNumScramble;
203
    }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

    
207
  public int getPrice()
208
    {
209
    return mPrice;
210
    }
211

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

    
214
  public boolean isFree()
215
    {
216
    return (!RubikActivity.USE_IAP || mIsFree);
217
    }
218

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

    
221
  public void markFree()
222
    {
223
    mIsFree=true;
224
    }
225

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227

    
228
  public int getObjectVersion()
229
    {
230
    return mObjectVersion;
231
    }
232

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

    
235
  public int getExtrasVersion()
236
    {
237
    return mExtrasVersion;
238
    }
239

    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241

    
242
  public void setIconTo(Activity act,ImageButton button)
243
    {
244
    Drawable icon = createIconDrawable(act);
245
    button.setBackground(icon);
246
    }
247

    
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249

    
250
  public void setIconTo(Activity act,ImageView view)
251
    {
252
    Drawable icon = createIconDrawable(act);
253
    view.setImageDrawable(icon);
254
    }
255

    
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257

    
258
  public InputStream getObjectStream(Activity act)
259
    {
260
    if( mJsonID>0 )
261
      {
262
      Resources res = act.getResources();
263
      return res.openRawResource(mJsonID);
264
      }
265
    if( mJsonID<0 )
266
      {
267
      RubikFiles files = RubikFiles.getInstance();
268
      return files.openFile(act,mLowerName+"_object.json");
269
      }
270

    
271
    return null;
272
    }
273

    
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275

    
276
  public InputStream getMeshStream(Activity act)
277
    {
278
    if( mMeshID>0 )
279
      {
280
      Resources res = act.getResources();
281
      return res.openRawResource(mMeshID);
282
      }
283

    
284
    return null;
285
    }
286

    
287
///////////////////////////////////////////////////////////////////////////////////////////////////
288

    
289
  public InputStream getExtrasStream(Activity act)
290
    {
291
    if( mExtrasID>0 )
292
      {
293
      Resources res = act.getResources();
294
      return res.openRawResource(mExtrasID);
295
      }
296
    if( mExtrasID<0 )
297
      {
298
      RubikFiles files = RubikFiles.getInstance();
299
      return files.openFile(act,mLowerName+"_extras.json");
300
      }
301

    
302
    return null;
303
    }
304

    
305
///////////////////////////////////////////////////////////////////////////////////////////////////
306

    
307
  public boolean hasExtras()
308
    {
309
    return mExtrasID!=0;
310
    }
311

    
312
///////////////////////////////////////////////////////////////////////////////////////////////////
313

    
314
  public boolean hasSolver()
315
    {
316
    return mSolverOrdinal>=0;
317
    }
318

    
319
///////////////////////////////////////////////////////////////////////////////////////////////////
320

    
321
  public int getSolverOrdinal()
322
    {
323
    return mSolverOrdinal;
324
    }
325

    
326
///////////////////////////////////////////////////////////////////////////////////////////////////
327

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

    
333
///////////////////////////////////////////////////////////////////////////////////////////////////
334

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