Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObject.java @ 8f200211

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

    
33
import static org.distorted.objectlib.main.TwistyObject.MESH_NICE;
34
import static org.distorted.main.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 mMeshState;
52
  private int mExtrasOrdinal;
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
    mMeshState = MESH_NICE;
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
    mMeshState     = MESH_NICE;
96
    mExtrasOrdinal = -1;
97

    
98
    mMeshID        =  0;
99
    mJsonID        = -1;
100
    mExtrasID      = -1;
101
    mIconID        = -1;
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

    
166
  public void setMeshState(int state)
167
    {
168
    mMeshState = state;
169
    }
170

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172

    
173
  public int getMeshState()
174
    {
175
    return mMeshState;
176
    }
177

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179
// PUBLIC API
180

    
181
  public String getLowerName()
182
    {
183
    return mLowerName;
184
    }
185

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

    
188
  public String getUpperName()
189
    {
190
    return mUpperName;
191
    }
192

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

    
195
  public int getNumScramble()
196
    {
197
    return mNumScramble;
198
    }
199

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

    
202
  public int getPrice()
203
    {
204
    return mPrice;
205
    }
206

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

    
209
  public boolean isFree()
210
    {
211
    return (!RubikActivity.USE_IAP || mIsFree);
212
    }
213

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

    
216
  public void markFree()
217
    {
218
    mIsFree=true;
219
    }
220

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

    
223
  public int getObjectVersion()
224
    {
225
    return mObjectVersion;
226
    }
227

    
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229

    
230
  public int getExtrasVersion()
231
    {
232
    return mExtrasVersion;
233
    }
234

    
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236

    
237
  public void setIconTo(Activity act,ImageButton button)
238
    {
239
    Drawable icon = createIconDrawable(act);
240
    button.setBackground(icon);
241
    }
242

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

    
245
  public void setIconTo(Activity act,ImageView view)
246
    {
247
    Drawable icon = createIconDrawable(act);
248
    view.setImageDrawable(icon);
249
    }
250

    
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252

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

    
266
    return null;
267
    }
268

    
269
///////////////////////////////////////////////////////////////////////////////////////////////////
270

    
271
  public InputStream getMeshStream(Activity act)
272
    {
273
    if( mMeshID>0 )
274
      {
275
      Resources res = act.getResources();
276
      return res.openRawResource(mMeshID);
277
      }
278

    
279
    return null;
280
    }
281

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283

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

    
297
    return null;
298
    }
299

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

    
302
  public boolean hasExtras()
303
    {
304
    return mExtrasID!=0;
305
    }
306

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

    
309
  public String[][] getPatterns()
310
    {
311
    return mPatterns;
312
    }
313
}
(1-1/2)