Project

General

Profile

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

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

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
import java.util.Locale;
24

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

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

    
42
import static org.distorted.objectlib.main.TwistyObject.MESH_NICE;
43
import static org.distorted.main.RubikActivity.SHOW_DOWNLOADED_DEBUG;
44

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

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

    
53
  private int mJsonID, mMeshID, mExtrasID;
54
  private int mObjectMinor, mExtrasMinor;
55
  private int mNumScramble;
56
  private int mMeshState;
57
  private int mExtrasOrdinal;
58
  private Drawable mStaticIconD, mRescaledIconD;
59
  private final int mSignature;
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
  RubikObject(ObjectType type)
64
    {
65
    mSignature   = type.ordinal();
66

    
67
    mUpperName   = type.name();
68
    mLowerName   = type.name().toLowerCase(Locale.ENGLISH);
69
    mNumScramble = type.getNumScramble();
70

    
71
    mIconID      = type.getIconID();
72
    mJsonID      = ObjectJson.getObjectJsonID(mSignature);
73
    mMeshID      = ObjectMesh.getMeshID(mSignature);
74
    mExtrasID    = ObjectJson.getExtrasJsonID(mSignature);
75

    
76
    int patternOrdinal  = RubikPatternList.getOrdinal(mSignature);
77
    mPatterns = RubikPatternList.getPatterns(patternOrdinal);
78

    
79
    mMeshState = MESH_NICE;
80
    mExtrasOrdinal = -1;
81

    
82
    mObjectMinor = JsonWriter.VERSION_OBJECT_MINOR;
83
    mExtrasMinor = JsonWriter.VERSION_EXTRAS_MINOR;
84

    
85
    mStaticIconD  = null;
86
    mRescaledIconD= null;
87
    }
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

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

    
95
    mLowerName     = object.shortName;
96
    mUpperName     = object.shortName.toUpperCase(Locale.ENGLISH);
97
    mNumScramble   = object.numScrambles;
98
    mObjectMinor   = object.objectMinor;
99
    mExtrasMinor   = object.extrasMinor;
100

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

    
105
    mMeshID        =  0;
106
    mJsonID        = -1;
107
    mExtrasID      = -1;
108
    mIconID        = -1;
109
    mSignature     = -1;
110
    }
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

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

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

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

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

    
133
      mExtrasMinor = object.extrasMinor;
134
      mExtrasID = -1;
135
      changed = true;
136
      }
137

    
138
    return changed;
139
    }
140

    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142

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

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159

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

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

    
177
  public void setExtrasOrdinal(int ordinal)
178
    {
179
    mExtrasOrdinal = ordinal;
180
    }
181

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

    
184
  public int getExtrasOrdinal()
185
    {
186
    return mExtrasOrdinal;
187
    }
188

    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190

    
191
  public void setMeshState(int state)
192
    {
193
    mMeshState = state;
194
    }
195

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197

    
198
  public int getMeshState()
199
    {
200
    return mMeshState;
201
    }
202

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204
// PUBLIC API
205

    
206
  public String getLowerName()
207
    {
208
    return mLowerName;
209
    }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

    
213
  public String getUpperName()
214
    {
215
    return mUpperName;
216
    }
217

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

    
220
  public int getSignature()
221
    {
222
    return mSignature;
223
    }
224

    
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226

    
227
  public int getNumScramble()
228
    {
229
    return mNumScramble;
230
    }
231

    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233

    
234
  public int getObjectMinor()
235
    {
236
    return mObjectMinor;
237
    }
238

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

    
241
  public int getExtrasMinor()
242
    {
243
    return mExtrasMinor;
244
    }
245

    
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247

    
248
  public void setIconTo(Activity act,ImageButton button)
249
    {
250
    if( mStaticIconD==null ) createStaticIconDrawable(act);
251
    button.setBackground(mStaticIconD);
252
    }
253

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

    
256
  public void setIconTo(Activity act,ImageView view)
257
    {
258
    if( mRescaledIconD==null ) createRescaledIconDrawable(act);
259
    view.setImageDrawable(mRescaledIconD);
260
    }
261

    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263

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

    
277
    return null;
278
    }
279

    
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281

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

    
290
    return null;
291
    }
292

    
293
///////////////////////////////////////////////////////////////////////////////////////////////////
294

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

    
308
    return null;
309
    }
310

    
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312

    
313
  public boolean hasExtras()
314
    {
315
    return mExtrasID!=0;
316
    }
317

    
318
///////////////////////////////////////////////////////////////////////////////////////////////////
319
// a downloaded object (signature<0) for sure does not have a solver.
320

    
321
  public boolean hasSolver()
322
    {
323
    return ImplementedSolversList.isImplemented(mSignature);
324
    }
325

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

    
328
  public String[][] getPatterns()
329
    {
330
    return mPatterns;
331
    }
332
}
(3-3/4)