Project

General

Profile

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

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

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.objectlib.json.JsonWriter;
36
import org.distorted.objectlib.main.ObjectType;
37
import org.distorted.patterns.RubikPatternList;
38

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

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

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

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

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

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

    
63
    mUpperName   = type.name();
64
    mLowerName   = type.name().toLowerCase();
65
    mNumScramble = type.getNumScramble();
66

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

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

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

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

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

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

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

    
91
    mLowerName     = object.shortName;
92
    mUpperName     = object.shortName.toUpperCase();
93
    mNumScramble   = object.numScrambles;
94
    mObjectMinor   = object.objectMinor;
95
    mExtrasMinor   = object.extrasMinor;
96

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

    
101
    mMeshID        =  0;
102
    mIconID        = -1;
103
    mJsonID        = -1;
104
    mExtrasID      = -1;
105
    }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108

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

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

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

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

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

    
133
    return changed;
134
    }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137

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

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153

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

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

    
170
  public void setExtrasOrdinal(int ordinal)
171
    {
172
    mExtrasOrdinal = ordinal;
173
    }
174

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

    
177
  public int getExtrasOrdinal()
178
    {
179
    return mExtrasOrdinal;
180
    }
181

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

    
184
  public void setMeshState(int state)
185
    {
186
    mMeshState = state;
187
    }
188

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

    
191
  public int getMeshState()
192
    {
193
    return mMeshState;
194
    }
195

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197
// PUBLIC API
198

    
199
  public String getLowerName()
200
    {
201
    return mLowerName;
202
    }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

    
206
  public String getUpperName()
207
    {
208
    return mUpperName;
209
    }
210

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

    
213
  public int getNumScramble()
214
    {
215
    return mNumScramble;
216
    }
217

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

    
220
  public int getObjectMinor()
221
    {
222
    return mObjectMinor;
223
    }
224

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

    
227
  public int getExtrasMinor()
228
    {
229
    return mExtrasMinor;
230
    }
231

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

    
234
  public void setIconTo(Activity act,ImageButton button)
235
    {
236
    if( mStaticIconD==null ) createStaticIconDrawable(act);
237
    button.setBackground(mStaticIconD);
238
    }
239

    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241

    
242
  public void setIconTo(Activity act,ImageView view)
243
    {
244
    if( mRescaledIconD==null ) createRescaledIconDrawable(act);
245
    view.setImageDrawable(mRescaledIconD);
246
    }
247

    
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249

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

    
263
    return null;
264
    }
265

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

    
268
  public InputStream getMeshStream(Activity act)
269
    {
270
    if( mMeshID>0 )
271
      {
272
      Resources res = act.getResources();
273
      return res.openRawResource(mMeshID);
274
      }
275

    
276
    return null;
277
    }
278

    
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280

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

    
294
    return null;
295
    }
296

    
297
///////////////////////////////////////////////////////////////////////////////////////////////////
298

    
299
  public boolean hasExtras()
300
    {
301
    return mExtrasID!=0;
302
    }
303

    
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305

    
306
  public String[][] getPatterns()
307
    {
308
    return mPatterns;
309
    }
310
}
(1-1/2)