Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObject.java @ 9eeb9c88

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
    mIconID        = -1;
106
    }
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109

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

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

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

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

    
129
      mExtrasMinor = object.extrasMinor;
130
      mExtrasID = -1;
131
      changed = true;
132
      }
133

    
134
    return changed;
135
    }
136

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138

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

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155

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

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

    
173
  public void setExtrasOrdinal(int ordinal)
174
    {
175
    mExtrasOrdinal = ordinal;
176
    }
177

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179

    
180
  public int getExtrasOrdinal()
181
    {
182
    return mExtrasOrdinal;
183
    }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186

    
187
  public void setMeshState(int state)
188
    {
189
    mMeshState = state;
190
    }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193

    
194
  public int getMeshState()
195
    {
196
    return mMeshState;
197
    }
198

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200
// PUBLIC API
201

    
202
  public String getLowerName()
203
    {
204
    return mLowerName;
205
    }
206

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

    
209
  public String getUpperName()
210
    {
211
    return mUpperName;
212
    }
213

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

    
216
  public int getNumScramble()
217
    {
218
    return mNumScramble;
219
    }
220

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

    
223
  public int getObjectMinor()
224
    {
225
    return mObjectMinor;
226
    }
227

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

    
230
  public int getExtrasMinor()
231
    {
232
    return mExtrasMinor;
233
    }
234

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

    
237
  public void setIconTo(Activity act,ImageButton button)
238
    {
239
    if( mStaticIconD==null ) createStaticIconDrawable(act);
240
    button.setBackground(mStaticIconD);
241
    }
242

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

    
245
  public void setIconTo(Activity act,ImageView view)
246
    {
247
    if( mRescaledIconD==null ) createRescaledIconDrawable(act);
248
    view.setImageDrawable(mRescaledIconD);
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)