Project

General

Profile

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

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

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

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

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

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

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

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

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

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

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

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

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

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

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

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88

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

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

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

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

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

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

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

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

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

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

    
135
    return changed;
136
    }
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

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

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156

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

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

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

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

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

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

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

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

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

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201
// PUBLIC API
202

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

    
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209

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

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

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

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

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

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

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

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

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

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

    
246
  public void setIconTo(Activity act,ImageView view)
247
    {
248
    if( mRescaledIconD==null ) createRescaledIconDrawable(act);
249
    view.setImageDrawable(mRescaledIconD);
250
    }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253

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

    
267
    return null;
268
    }
269

    
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271

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

    
280
    return null;
281
    }
282

    
283
///////////////////////////////////////////////////////////////////////////////////////////////////
284

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

    
298
    return null;
299
    }
300

    
301
///////////////////////////////////////////////////////////////////////////////////////////////////
302

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

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

    
310
  public String[][] getPatterns()
311
    {
312
    return mPatterns;
313
    }
314
}
(3-3/4)