Project

General

Profile

« Previous | Next » 

Revision 0b5e585c

Added by Leszek Koltunski over 2 years ago

  • ID 0b5e585cb8c00a1dd32b0f9da5a2c056eba34550
  • Parent fad10885

Sort the objects in the object popup by difficulty level.

View differences:

src/main/java/org/distorted/dialogs/RubikDialogUpdateView.java
194 194
        try
195 195
          {
196 196
          JsonReader reader = JsonReader.getInstance();
197
          mInfo.mNumScrambles = reader.readNumScrambles(act,objectName);
198
          if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Read from JSON numScrambles="+mInfo.mNumScrambles);
197
          reader.readNumScramblesAndComplexity(act,objectName);
198
          mInfo.mNumScrambles = reader.getNumScrambles();
199
          mInfo.mDifficulty   = reader.getComplexity();
200

  
201
          if( SHOW_DOWNLOADED_DEBUG )
202
            {
203
            android.util.Log.e("D", "Read from JSON numScrambles="+mInfo.mNumScrambles);
204
            android.util.Log.e("D", "Read from JSON difficulty="+mInfo.mDifficulty);
205
            }
199 206

  
200 207
          if( mInfo.mExtrasStream!=null )
201 208
            {
......
211 218

  
212 219
            if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "1");
213 220

  
214
            boolean success = RubikObjectList.addDownloadedObject(act, mInfo.mObjectShortName,mInfo.mNumScrambles, mInfo.mObjectMinorVersion,
215
                                                                  mInfo.mExtrasMinorVersion, mIconSaved, oSuccess, eSuccess);
221
            boolean success = RubikObjectList.addDownloadedObject(act, mInfo.mObjectShortName,mInfo.mNumScrambles, mInfo.mDifficulty,
222
                                                                  mInfo.mObjectMinorVersion, mInfo.mExtrasMinorVersion, mIconSaved, oSuccess, eSuccess);
216 223
            if( success )
217 224
              {
218 225
              if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "2");
src/main/java/org/distorted/external/RubikUpdates.java
36 36
    public final boolean mUpdateExtras;
37 37

  
38 38
    public int mNumScrambles;
39
    public int mDifficulty;
39 40
    public Bitmap mIcon;
40 41
    public InputStream mObjectStream;
41 42
    public InputStream mExtrasStream;
......
55 56

  
56 57
      mIcon = null;
57 58
      mNumScrambles = 0;
59
      mDifficulty   = 0;
58 60
      }
59 61
    }
60 62

  
src/main/java/org/distorted/objects/RubikObject.java
40 40
  private final String mLowerName, mUpperName;
41 41
  private final int mIconID;
42 42
  private final String[][] mPatterns;
43
  private final int mDifficulty;
43 44

  
44 45
  private int mJsonID, mMeshID, mExtrasID;
45 46
  private int mObjectMinor, mExtrasMinor;
......
57 58
    mUpperName   = type.name();
58 59
    mLowerName   = type.name().toLowerCase(Locale.ENGLISH);
59 60
    mNumScramble = type.getNumScramble();
61
    mDifficulty  = type.getDifficulty();
60 62

  
61 63
    mIconID      = type.getIconID();
62 64
    mJsonID      = ObjectJson.getObjectJsonID(ordinal);
......
85 87
    mLowerName     = object.shortName;
86 88
    mUpperName     = object.shortName.toUpperCase(Locale.ENGLISH);
87 89
    mNumScramble   = object.numScrambles;
90
    mDifficulty    = object.difficulty;
88 91
    mObjectMinor   = object.objectMinor;
89 92
    mExtrasMinor   = object.extrasMinor;
90 93

  
......
204 207
    return mUpperName;
205 208
    }
206 209

  
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

  
212
  public int getDifficulty()
213
    {
214
    return mDifficulty;
215
    }
216

  
207 217
///////////////////////////////////////////////////////////////////////////////////////////////////
208 218

  
209 219
  public int getNumScramble()
src/main/java/org/distorted/objects/RubikObjectList.java
17 17

  
18 18
import org.distorted.external.RubikFiles;
19 19
import org.distorted.main.RubikActivity;
20
import org.distorted.objectlib.json.JsonReader;
20 21
import org.distorted.objectlib.main.ObjectSignatures;
21 22
import org.distorted.objectlib.main.ObjectType;
22 23

  
......
28 29

  
29 30
public class RubikObjectList
30 31
{
32
  public static final int NUM_DIFFICULTIES = 5;
31 33
  public static final int DEF_OBJECT= ObjectSignatures.CUBE_3;
32 34
  private static RubikObjectList mThis;
33 35
  private static int mNumObjects;
......
39 41
    {
40 42
    String shortName;
41 43
    boolean icon,object,extras;
42
    int numScrambles, objectMinor, extrasMinor;
44
    int numScrambles, difficulty, objectMinor, extrasMinor;
43 45

  
44
    DownloadedObject(String sName, int scrambles, int oMinor, int eMinor, boolean i, boolean o, boolean e)
46
    DownloadedObject(String sName, int scrambles, int diff, int oMinor, int eMinor, boolean i, boolean o, boolean e)
45 47
      {
46 48
      shortName = sName;
47 49

  
48 50
      numScrambles= scrambles;
51
      difficulty  = diff;
49 52
      objectMinor = oMinor;
50 53
      extrasMinor = eMinor;
51 54

  
......
56 59
    }
57 60

  
58 61
  private static ArrayList<DownloadedObject> mDownloadedObjects;
62
  private static int[][] mDifficultyOrdinals;
59 63

  
60 64
///////////////////////////////////////////////////////////////////////////////////////////////////
61 65

  
......
130 134
    return true;
131 135
    }
132 136

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

  
139
  private static void createDifficultyList(int difficulty)
140
    {
141
    int num=0;
142

  
143
    for(int i=0; i<mNumObjects; i++)
144
      {
145
      RubikObject object = mObjects.get(i);
146
      if( object.getDifficulty()==difficulty ) num++;
147
      }
148

  
149
    if( num>0 )
150
      {
151
      mDifficultyOrdinals[difficulty] = new int[num];
152
      num=0;
153

  
154
      for(int i=0; i<mNumObjects; i++)
155
        {
156
        RubikObject object = mObjects.get(i);
157
        if( object.getDifficulty()==difficulty )
158
          {
159
          mDifficultyOrdinals[difficulty][num] = i;
160
          num++;
161
          }
162
        }
163
      }
164
    }
165

  
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

  
168
  private static int figureOutDifficulty(Context context, String objectName)
169
    {
170
    JsonReader reader = JsonReader.getInstance();
171

  
172
    try
173
      {
174
      reader.readNumScramblesAndComplexity(context,objectName);
175
      return reader.getComplexity();
176
      }
177
    catch(Exception ex)
178
      {
179
      android.util.Log.e("D", "exception "+ex.getMessage()+" trying to read complexity of a puzzle "+objectName);
180
      return 0;
181
      }
182
    }
183

  
133 184
///////////////////////////////////////////////////////////////////////////////////////////////////
134 185
// PUBLIC API
135 186

  
136
  public static boolean addDownloadedObject(Context context, String shortName, int numScrambles, int objectMinor,
187
  public static boolean addDownloadedObject(Context context, String shortName, int numScrambles, int difficulty, int objectMinor,
137 188
                                         int extrasMinor, boolean icon, boolean object, boolean extras)
138 189
    {
139 190
    if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "New downloaded object "+shortName+" icon="+icon+" object="+object+" extras="+extras);
......
171 222
    if( !object ) objectMinor=-1;
172 223
    if( !extras ) extrasMinor=-1;
173 224

  
174
    DownloadedObject obj = new DownloadedObject(shortName,numScrambles,objectMinor,extrasMinor,icon,object,extras);
225
    DownloadedObject obj = new DownloadedObject(shortName,numScrambles,difficulty,objectMinor,extrasMinor,icon,object,extras);
175 226
    if ( internalAddDownloadedObject(obj) )
176 227
      {
177 228
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Adding new downloaded object "+shortName+" icon="+obj.icon+" object="+obj.object+" extras="+obj.extras);
......
234 285
        downloadedObjects.append(object.object ? "1":"0");
235 286
        downloadedObjects.append(' ');
236 287
        downloadedObjects.append(object.extras ? "1":"0");
288
        downloadedObjects.append(' ');
289
        downloadedObjects.append(object.difficulty);
237 290
        }
238 291

  
239 292
      String objects = downloadedObjects.toString();
......
280 333
        {
281 334
        String[] parts = dObj.split(" ");
282 335

  
283
        if( parts.length==7 )
336
        if( parts.length==7 || parts.length==8 )
284 337
          {
285 338
          String name = parts[0];
286 339
          String scra = parts[1];
......
290 343
          String obje = parts[5];
291 344
          String extr = parts[6];
292 345

  
346
          int difficulty= parts.length==7 ? figureOutDifficulty(context,name) : Integer.parseInt(parts[7]);
293 347
          int scrambles = Integer.parseInt(scra);
294 348
          int oMinor    = Integer.parseInt(objM);
295 349
          int eMinor    = Integer.parseInt(extM);
......
298 352
          boolean bObje = obje.equals("1");
299 353
          boolean bExtr = extr.equals("1");
300 354

  
301
          addDownloadedObject(context,name,scrambles,oMinor,eMinor,bIcon,bObje,bExtr);
355
          addDownloadedObject(context,name,scrambles,difficulty,oMinor,eMinor,bIcon,bObje,bExtr);
302 356
          }
303 357
        }
304 358
      }
......
465 519
    RubikObject object = getObject(objectOrdinal);
466 520
    return object!=null ? object.getExtrasMinor() : -1;
467 521
    }
522

  
523
///////////////////////////////////////////////////////////////////////////////////////////////////
524

  
525
  public static int[] produceListOfOrdinals(int difficulty)
526
    {
527
    if( mDifficultyOrdinals==null )
528
      {
529
      mDifficultyOrdinals = new int[NUM_DIFFICULTIES][];
530
      }
531

  
532
    if( difficulty>=0 && difficulty<NUM_DIFFICULTIES )
533
      {
534
      if( mDifficultyOrdinals[difficulty]==null ) createDifficultyList(difficulty);
535
      }
536

  
537
    return mDifficultyOrdinals[difficulty];
538
    }
539

  
468 540
}
src/main/java/org/distorted/screens/RubikScreenPlay.java
25 25
import android.widget.Button;
26 26
import android.widget.GridLayout;
27 27
import android.widget.ImageButton;
28
import android.widget.ImageView;
28 29
import android.widget.LinearLayout;
29 30
import android.widget.PopupWindow;
30 31
import android.widget.RelativeLayout;
......
52 53

  
53 54
public class RubikScreenPlay extends RubikScreenBase implements RubikNetwork.Updatee
54 55
  {
55
  public static final int NUM_COLUMNS  = 5;
56
  public static final int NUM_COLUMNS  = 6;
56 57
  public static final int LEVELS_SHOWN = 8;
57 58
  private static final int NUM_BUTTONS = 6;
58 59
  private static final int[] mLocation = new int[2];
......
65 66
  private int mObjectSize, mMenuLayoutWidth, mMenuLayoutHeight, mMenuButtonHeight, mMenuTextSize;
66 67
  private int mLevelValue;
67 68
  private int mColCount, mRowCount, mMaxRowCount;
68
  private int mUpperBarHeight;
69
  private int mBarHeight;
69 70
  private boolean mShouldReactToEndOfScrambling;
70
  private float mScreenWidth;
71
  private float mScreenWidth, mScreenHeight;
72
  private int mMargin;
71 73

  
72 74
///////////////////////////////////////////////////////////////////////////////////////////////////
73 75

  
......
80 82

  
81 83
  void enterScreen(final RubikActivity act)
82 84
    {
83
    mWeakAct = new WeakReference<>(act);
85
    mWeakAct     = new WeakReference<>(act);
84 86
    mScreenWidth = act.getScreenWidthInPixels();
85
    mUpperBarHeight = act.getHeightUpperBar();
87
    mScreenHeight= act.getScreenHeightInPixels();
88
    mBarHeight   = act.getHeightUpperBar();
86 89

  
87 90
    mMenuButtonHeight = (int)(mScreenWidth*RubikActivity.MENU_BUTTON_HEIGHT);
88 91
    mMenuTextSize     = (int)(mScreenWidth*RubikActivity.MENU_MAIN_TEXT_SIZE);
92
    mMargin           = (int)(mScreenWidth*RubikActivity.LARGE_MARGIN);
89 93

  
90 94
    mObjectPopup = null;
91 95

  
......
101 105
    layoutTop.addView(mScrambleButton);
102 106

  
103 107
    // BOTTOM /////////////////////////
104
    setupObjectButton(act,mScreenWidth);
108
    setupObjectButton(act,mScreenWidth,mScreenHeight);
105 109
    createBottomPane(act,mObjButton,null);
106 110
    }
107 111

  
108 112
//////////////////////////////////////////////////////////////////////////////////////////////////
109 113

  
110
  private void setupObjectButton(final RubikActivity act, final float width)
114
  private void setupObjectButton(final RubikActivity act, final float width, final float height)
111 115
    {
112 116
    final int margin  = (int)(width*RubikActivity.SMALL_MARGIN);
113 117
    final int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube_menu,R.drawable.ui_medium_cube_menu, R.drawable.ui_big_cube_menu, R.drawable.ui_huge_cube_menu);
......
128 132

  
129 133
        if( act.getControl().isUINotBlocked())
130 134
          {
131
          int rowCount = Math.min(mMaxRowCount,mRowCount);
135
          //int rowCount = Math.min(mMaxRowCount,mRowCount);
132 136
          View popupView = mObjectPopup.getContentView();
133 137
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
134
          displayPopup(act,view,mObjectPopup,mObjectSize*mColCount,mObjectSize*rowCount+5*margin,margin,margin);
138
          int popupWidth = (int)width;
139
          int popupHeight= (int)(height-2*mBarHeight);
140
          displayPopup(act,view,mObjectPopup,popupWidth,popupHeight,margin,margin);
135 141
          }
136 142
        }
137 143
      });
......
171 177

  
172 178
  private void setupObjectWindow(final RubikActivity act, final float width, final float height)
173 179
    {
180
    LinearLayout view = (LinearLayout)inflate( act, R.layout.popup_object, null);
181
    LinearLayout layout = view.findViewById(R.id.objectLayout);
182

  
174 183
    int numObjects = RubikObjectList.getNumObjects();
175 184
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
176 185
    mColCount = NUM_COLUMNS;
177 186

  
178 187
    int cubeSize = (int)(width/9);
179
    int margin   = (int)(width*RubikActivity.LARGE_MARGIN);
180
    mObjectSize  = (int)(cubeSize + 2*margin + 0.5f);
181
    mMaxRowCount = (int)((height-1.8f*mUpperBarHeight)/mObjectSize);
188
    mObjectSize  = (int)(cubeSize + 2*mMargin + 0.5f);
189
    mMaxRowCount = (int)((height-1.8f*mBarHeight)/mObjectSize);
182 190

  
183
    LinearLayout view = (LinearLayout)inflate( act, R.layout.popup_object, null);
184
    GridLayout objectGrid = view.findViewById(R.id.objectGrid);
191
    GridLayout section0 = produceSection(act, R.drawable.difficulty1, 0, cubeSize);
192
    GridLayout section1 = produceSection(act, R.drawable.difficulty2, 1, cubeSize);
193
    GridLayout section2 = produceSection(act, R.drawable.difficulty3, 2, cubeSize);
194
    GridLayout section3 = produceSection(act, R.drawable.difficulty4, 3, cubeSize);
195
    GridLayout section4 = produceSection(act, R.drawable.difficulty5, 4, cubeSize);
185 196

  
186
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
187
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
188

  
189
    objectGrid.setColumnCount(mColCount);
190
    objectGrid.setRowCount(mRowCount);
197
    layout.addView(section0);
198
    layout.addView(section1);
199
    layout.addView(section2);
200
    layout.addView(section3);
201
    layout.addView(section4);
191 202

  
192 203
    RelativeLayout bottomLayout = view.findViewById(R.id.bottomLayout);
193 204
    setupBottomLayout(act,bottomLayout);
......
195 206
    mObjectPopup = new PopupWindow(act);
196 207
    mObjectPopup.setFocusable(true);
197 208
    mObjectPopup.setContentView(view);
209
    }
210

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

  
213
  private GridLayout produceSection(final RubikActivity act, int iconID, int difficulty, int cubeSize)
214
    {
215
    GridLayout objectGrid = (GridLayout)inflate( act, R.layout.popup_object_section, null);
198 216

  
199
    int[] nextInRow = new int[mRowCount];
217
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
218
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
219
    params.setMargins(0, (difficulty==0? 0:mMargin) ,0,0);
220
    objectGrid.setLayoutParams(params);
200 221

  
201
    for(int row=0; row<mRowCount; row++)
222
    int[] ordinals = RubikObjectList.produceListOfOrdinals(difficulty);
223
    int numObjects = ordinals.length;
224
    int colCount = NUM_COLUMNS-1;
225
    int rowCount = (numObjects + colCount-1) / colCount;
226

  
227
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[rowCount];
228
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[colCount+1];
229

  
230
    objectGrid.setColumnCount(colCount+1);
231
    objectGrid.setRowCount(rowCount);
232

  
233
    int[] nextInRow = new int[rowCount];
234

  
235
    for(int row=0; row<rowCount; row++)
202 236
      {
203 237
      rowSpecs[row] = GridLayout.spec(row);
204
      nextInRow[row]= 0;
238
      nextInRow[row]= 1;
205 239
      }
206
    for(int col=0; col<mColCount; col++)
240
    for(int col=0; col<colCount+1; col++)
207 241
      {
208 242
      colSpecs[col] = GridLayout.spec(col);
209 243
      }
210 244

  
245
    GridLayout.Spec rs = rowSpecs[0];
246
    GridLayout.Spec cs = colSpecs[0];
247
    GridLayout.LayoutParams ps = new GridLayout.LayoutParams(rs,cs);
248
    ps.bottomMargin = mMargin;
249
    ps.topMargin    = mMargin;
250
    ps.leftMargin   = mMargin;
251
    ps.rightMargin  = mMargin;
252
    ps.width        = cubeSize;
253
    ps.height       = cubeSize;
254

  
255
    ImageView image = new ImageView(act);
256
    image.setImageResource(iconID);
257
    objectGrid.addView(image,ps);
258

  
211 259
    for(int object=0; object<numObjects; object++)
212 260
      {
213
      final int ordinal = object;
214
      final RubikObject rObject = RubikObjectList.getObject(object);
215
      int row = object/NUM_COLUMNS;
261
      final int ordinal = ordinals[object];
262
      final RubikObject rObject = RubikObjectList.getObject(ordinal);
263
      int row = object/colCount;
216 264
      ImageButton button = new ImageButton(act);
217 265
      if( rObject!=null ) rObject.setIconTo(act,button);
218 266

  
......
233 281
          }
234 282
        });
235 283

  
236
      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
237
      params.bottomMargin = margin;
238
      params.topMargin    = margin;
239
      params.leftMargin   = margin;
240
      params.rightMargin  = margin;
241

  
242
      params.width = cubeSize;
243
      params.height= cubeSize;
284
      GridLayout.Spec r = rowSpecs[row];
285
      GridLayout.Spec c = colSpecs[nextInRow[row]];
286
      GridLayout.LayoutParams p = new GridLayout.LayoutParams(r,c);
287
      p.bottomMargin = mMargin;
288
      p.topMargin    = mMargin;
289
      p.leftMargin   = mMargin;
290
      p.rightMargin  = mMargin;
291
      p.width        = cubeSize;
292
      p.height       = cubeSize;
244 293

  
245 294
      nextInRow[row]++;
246 295

  
247
      objectGrid.addView(button, params);
296
      objectGrid.addView(button,p);
248 297
      }
298

  
299
    return objectGrid;
249 300
    }
250 301

  
251 302
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/res/layout/popup_object.xml
10 10
       android:layout_height="0dp"
11 11
       android:layout_weight="1">
12 12

  
13
       <GridLayout
14
           android:id="@+id/objectGrid"
13
       <LinearLayout
14
           android:id="@+id/objectLayout"
15 15
           android:layout_width="match_parent"
16
           android:layout_height="wrap_content">
17
       </GridLayout>
18

  
16
           android:layout_height="wrap_content"
17
           android:orientation="vertical"/>
19 18
   </ScrollView>
20 19

  
21 20
   <RelativeLayout
src/main/res/layout/popup_object_section.xml
1
<?xml version="1.0" encoding="utf-8"?>
2
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
    android:id="@+id/objectSectionGrid"
4
    android:background="@color/transparent_grey"
5
    android:layout_width="wrap_content"
6
    android:layout_height="match_parent">
7
</GridLayout>
src/main/res/values/colors.xml
9 9
    <color name="grey">#ff333333</color>
10 10
    <color name="light_grey">#ff555555</color>
11 11
    <color name="medium_grey">#ff444444</color>
12
    <color name="transparent_grey">#77333333</color>
12 13
    <color name="black">#ff010101</color>
13 14
    <color name="white">#ffffffff</color>
14 15
</resources>

Also available in: Unified diff