Project

General

Profile

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

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

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

    
34
import static org.distorted.objectlib.main.TwistyObject.MESH_NICE;
35
import static org.distorted.main.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 mObjectMinor, mExtrasMinor;
51
  private int mNumScramble;
52
  private int mMeshState;
53
  private int mExtrasOrdinal;
54
  private Drawable mStaticIconD, mRescaledIconD;
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

    
68
    mIconID      = type.getIconID();
69
    mJsonID      = ObjectJson.getObjectJsonID(ordinal);
70
    mMeshID      = ObjectMesh.getMeshID(ordinal);
71
    mExtrasID    = ObjectJson.getExtrasJsonID(ordinal);
72

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

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

    
79
    mObjectMinor = JsonWriter.VERSION_OBJECT_MINOR;
80
    mExtrasMinor = JsonWriter.VERSION_EXTRAS_MINOR;
81

    
82
    mStaticIconD  = null;
83
    mRescaledIconD= null;
84
    }
85

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87

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

    
92
    mLowerName     = object.shortName;
93
    mUpperName     = object.shortName.toUpperCase(Locale.ENGLISH);
94
    mNumScramble   = object.numScrambles;
95
    mPrice         = object.price;
96
    mIsFree        = mPrice==0;
97
    mObjectMinor   = object.objectMinor;
98
    mExtrasMinor   = object.extrasMinor;
99

    
100
    mPatterns      = null;
101
    mMeshState     = MESH_NICE;
102
    mExtrasOrdinal = -1;
103

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

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

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

    
116
    if( object.objectMinor>JsonWriter.VERSION_OBJECT_MINOR )
117
      {
118
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Updating RubikObject's "+object.shortName+" main JSON");
119

    
120
      mObjectMinor = object.objectMinor;
121
      mNumScramble = object.numScrambles;
122
      mMeshID =  0;
123
      mJsonID = -1;
124
      changed = true;
125
      }
126

    
127
    if( object.extrasMinor>JsonWriter.VERSION_EXTRAS_MINOR )
128
      {
129
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Updating RubikObject's "+object.shortName+" extras JSON");
130

    
131
      mExtrasMinor = object.extrasMinor;
132
      mExtrasID = -1;
133
      changed = true;
134
      }
135

    
136
    return changed;
137
    }
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

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

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

    
158
  private void createRescaledIconDrawable(Activity act)
159
    {
160
    if( mIconID>0 )
161
      {
162
      mRescaledIconD = AppCompatResources.getDrawable(act,mIconID);
163
      }
164
    else
165
      {
166
      RubikFiles files = RubikFiles.getInstance();
167
      Bitmap bmp = files.getIcon(act,mLowerName+".png");
168
      if( bmp==null ) mRescaledIconD = AppCompatResources.getDrawable(act,R.drawable.unknown_icon);
169
      else            mRescaledIconD = new BitmapDrawable(act.getResources(), bmp);
170
      }
171
    }
172

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

    
175
  public void setExtrasOrdinal(int ordinal)
176
    {
177
    mExtrasOrdinal = ordinal;
178
    }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

    
182
  public int getExtrasOrdinal()
183
    {
184
    return mExtrasOrdinal;
185
    }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

    
189
  public void setMeshState(int state)
190
    {
191
    mMeshState = state;
192
    }
193

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

    
196
  public int getMeshState()
197
    {
198
    return mMeshState;
199
    }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202
// PUBLIC API
203

    
204
  public String getLowerName()
205
    {
206
    return mLowerName;
207
    }
208

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

    
211
  public String getUpperName()
212
    {
213
    return mUpperName;
214
    }
215

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

    
218
  public int getNumScramble()
219
    {
220
    return mNumScramble;
221
    }
222

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

    
225
  public int getPrice()
226
    {
227
    return mPrice;
228
    }
229

    
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231

    
232
  public boolean isFree()
233
    {
234
    return (!RubikActivity.USE_IAP || mIsFree);
235
    }
236

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

    
239
  public void markFree()
240
    {
241
    mIsFree=true;
242
    }
243

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

    
246
  public int getObjectMinor()
247
    {
248
    return mObjectMinor;
249
    }
250

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

    
253
  public int getExtrasMinor()
254
    {
255
    return mExtrasMinor;
256
    }
257

    
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259

    
260
  public void setIconTo(Activity act,ImageButton button)
261
    {
262
    if( mStaticIconD==null ) createStaticIconDrawable(act);
263
    button.setBackground(mStaticIconD);
264
    }
265

    
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267

    
268
  public void setIconTo(Activity act,ImageView view)
269
    {
270
    if( mRescaledIconD==null ) createRescaledIconDrawable(act);
271
    view.setImageDrawable(mRescaledIconD);
272
    }
273

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

    
276
  public InputStream getObjectStream(Activity act)
277
    {
278
    if( mJsonID>0 )
279
      {
280
      Resources res = act.getResources();
281
      return res.openRawResource(mJsonID);
282
      }
283
    if( mJsonID<0 )
284
      {
285
      RubikFiles files = RubikFiles.getInstance();
286
      return files.openFile(act,mLowerName+"_object.json");
287
      }
288

    
289
    return null;
290
    }
291

    
292
///////////////////////////////////////////////////////////////////////////////////////////////////
293

    
294
  public InputStream getMeshStream(Activity act)
295
    {
296
    if( mMeshID>0 )
297
      {
298
      Resources res = act.getResources();
299
      return res.openRawResource(mMeshID);
300
      }
301

    
302
    return null;
303
    }
304

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

    
307
  public InputStream getExtrasStream(Activity act)
308
    {
309
    if( mExtrasID>0 )
310
      {
311
      Resources res = act.getResources();
312
      return res.openRawResource(mExtrasID);
313
      }
314
    if( mExtrasID<0 )
315
      {
316
      RubikFiles files = RubikFiles.getInstance();
317
      return files.openFile(act,mLowerName+"_extras.json");
318
      }
319

    
320
    return null;
321
    }
322

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

    
325
  public boolean hasExtras()
326
    {
327
    return mExtrasID!=0;
328
    }
329

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

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