Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObject.java @ 90288e23

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2021 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.objects;
21

    
22
import java.io.InputStream;
23

    
24
import android.app.Activity;
25
import android.content.res.Resources;
26
import android.graphics.Bitmap;
27
import android.graphics.drawable.BitmapDrawable;
28
import android.graphics.drawable.Drawable;
29
import android.widget.ImageButton;
30
import android.widget.ImageView;
31

    
32
import org.distorted.dmesh.ObjectMesh;
33
import org.distorted.external.RubikFiles;
34
import org.distorted.jsons.ObjectJson;
35
import org.distorted.main.R;
36
import org.distorted.objectlib.json.JsonWriter;
37
import org.distorted.objectlib.main.ObjectType;
38
import org.distorted.patterns.RubikPatternList;
39

    
40
import static org.distorted.objectlib.main.TwistyObject.MESH_NICE;
41
import static org.distorted.main.RubikActivity.SHOW_DOWNLOADED_DEBUG;
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

    
45
public class RubikObject
46
{
47
  private final String mLowerName, mUpperName;
48
  private final int mIconID;
49
  private final String[][] mPatterns;
50

    
51
  private int mJsonID, mMeshID, mExtrasID;
52
  private int mObjectMinor, mExtrasMinor;
53
  private int mNumScramble;
54
  private int mMeshState;
55
  private int mExtrasOrdinal;
56
  private Drawable mStaticIconD, mRescaledIconD;
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
  RubikObject(ObjectType type)
61
    {
62
    int ordinal= type.ordinal();
63

    
64
    mUpperName   = type.name();
65
    mLowerName   = type.name().toLowerCase();
66
    mNumScramble = type.getNumScramble();
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();
94
    mNumScramble   = object.numScrambles;
95
    mObjectMinor   = object.objectMinor;
96
    mExtrasMinor   = object.extrasMinor;
97

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

    
102
    mMeshID        =  0;
103
    mJsonID        = -1;
104
    mExtrasID      = -1;
105

    
106
    if( object.icon )  // icon successfully downloaded
107
      {
108
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", object.shortName+" : icon downloaded");
109
      mIconID = -1;  // i.e. look for the icon in local per-app storage
110
      }
111
    else
112
      {
113
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", object.shortName+" : icon failed to download");
114
      mIconID = R.drawable.unknown_icon;
115
      }
116
    }
117

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

    
120
  public boolean updateObject(RubikObjectList.DownloadedObject object)
121
    {
122
    boolean changed = false;
123

    
124
    if( object.objectMinor>JsonWriter.VERSION_OBJECT_MINOR )
125
      {
126
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Updating RubikObject's "+object.shortName+" main JSON");
127

    
128
      mObjectMinor = object.objectMinor;
129
      mNumScramble = object.numScrambles;
130
      mMeshID =  0;
131
      mJsonID = -1;
132
      changed = true;
133
      }
134

    
135
    if( object.extrasMinor>JsonWriter.VERSION_EXTRAS_MINOR )
136
      {
137
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Updating RubikObject's "+object.shortName+" extras JSON");
138

    
139
      mExtrasMinor = object.extrasMinor;
140
      mExtrasID = -1;
141
      changed = true;
142
      }
143

    
144
    return changed;
145
    }
146

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

    
149
  private void createStaticIconDrawable(Activity act)
150
    {
151
    if( mIconID>0 )
152
      {
153
      mStaticIconD = act.getDrawable(mIconID);
154
      }
155
    else
156
      {
157
      RubikFiles files = RubikFiles.getInstance();
158
      Bitmap bmp = files.getIcon(act,mLowerName+".png");
159
      mStaticIconD = new BitmapDrawable(act.getResources(), bmp);
160
      }
161
    }
162

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164

    
165
  private void createRescaledIconDrawable(Activity act)
166
    {
167
    if( mIconID>0 )
168
      {
169
      mRescaledIconD = act.getDrawable(mIconID);
170
      }
171
    else
172
      {
173
      RubikFiles files = RubikFiles.getInstance();
174
      Bitmap bmp = files.getIcon(act,mLowerName+".png");
175
      mRescaledIconD = new BitmapDrawable(act.getResources(), bmp);
176
      }
177
    }
178

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

    
181
  public void setExtrasOrdinal(int ordinal)
182
    {
183
    mExtrasOrdinal = ordinal;
184
    }
185

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

    
188
  public int getExtrasOrdinal()
189
    {
190
    return mExtrasOrdinal;
191
    }
192

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

    
195
  public void setMeshState(int state)
196
    {
197
    mMeshState = state;
198
    }
199

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

    
202
  public int getMeshState()
203
    {
204
    return mMeshState;
205
    }
206

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208
// PUBLIC API
209

    
210
  public String getLowerName()
211
    {
212
    return mLowerName;
213
    }
214

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

    
217
  public String getUpperName()
218
    {
219
    return mUpperName;
220
    }
221

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

    
224
  public int getNumScramble()
225
    {
226
    return mNumScramble;
227
    }
228

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

    
231
  public int getObjectMinor()
232
    {
233
    return mObjectMinor;
234
    }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
  public int getExtrasMinor()
239
    {
240
    return mExtrasMinor;
241
    }
242

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

    
245
  public void setIconTo(Activity act,ImageButton button)
246
    {
247
    if( mStaticIconD==null ) createStaticIconDrawable(act);
248
    button.setBackground(mStaticIconD);
249
    }
250

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

    
253
  public void setIconTo(Activity act,ImageView view)
254
    {
255
    if( mRescaledIconD==null ) createRescaledIconDrawable(act);
256
    view.setImageDrawable(mRescaledIconD);
257
    }
258

    
259
///////////////////////////////////////////////////////////////////////////////////////////////////
260

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

    
274
    return null;
275
    }
276

    
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278

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

    
287
    return null;
288
    }
289

    
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291

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

    
305
    return null;
306
    }
307

    
308
///////////////////////////////////////////////////////////////////////////////////////////////////
309

    
310
  public boolean hasExtras()
311
    {
312
    return mExtrasID!=0;
313
    }
314

    
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316

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