Project

General

Profile

« Previous | Next » 

Revision 3eaf5f2a

Added by Leszek Koltunski 4 months ago

Progress with sorting the objects by various criteria.

View differences:

src/main/java/org/distorted/config/ConfigScreen.java
39 39
  private static final float MARGIN    = 0.0024f;
40 40
  private static final int NUM_COLUMNS = 5;
41 41
  private static final int[] mLocation = new int[2];
42
  private static final float POPUP_PADDING = 0.028f;
43 42
  private static final float POPUP_MARGIN  = 0.016f;
44 43

  
45 44
  private TransparentImageButton mBackButton, mObjectButton, mPrevButton, mNextButton;
......
62 61

  
63 62
    int cubeSize  = (int)( (Math.min(width,(int)(height*0.7f))) / 9 );
64 63
    int margin    = (int)(height*POPUP_MARGIN);
65
    int padding   = (int)(height*POPUP_PADDING);
66 64
    mObjectSize   = (int)(cubeSize + 2*margin + 0.5f);
67 65
    mMaxRowCount  = (int)((height-mBarHeight)/mObjectSize);
68 66

  
69 67
    ScrollView view = (ScrollView)inflate( act, R.layout.popup_object_simple, null);
70 68
    GridLayout objectGrid = view.findViewById(R.id.objectGrid);
71 69

  
72
    ObjectGridCreator.createObjectGridClassic(objectGrid,act,mRowCount,mColCount,numObjects,margin,cubeSize,padding);
70
    int[] objects = new int[numObjects];
71
    for(int i=0; i<numObjects; i++) objects[i] = i;
72

  
73
    ObjectGridCreator creator = new ObjectGridCreator(width);
74
    creator.createObjectGridClassic(objectGrid,act,objects);
73 75

  
74 76
    for(int child=0; child<numObjects; child++)
75 77
      {
76 78
      final RubikObject obj = RubikObjectList.getObject(child);
77 79
      View v = objectGrid.getChildAt(child);
78
      ImageButton button = ObjectGridCreator.getButton(obj,v);
80
      ImageButton button = creator.getButton(obj,v);
79 81
      final int ordinal = child;
80 82

  
81 83
      button.setOnClickListener( new View.OnClickListener()
src/main/java/org/distorted/helpers/ObjectGridCreator.java
9 9

  
10 10
package org.distorted.helpers;
11 11

  
12
import static org.distorted.main.MainScrollGrid.NUM_COLUMNS;
13
import static org.distorted.main.MainScrollGrid.POPUP_MARGIN;
14
import static org.distorted.main.MainScrollGrid.POPUP_PADDING;
15

  
12 16
import android.app.Activity;
13 17
import android.content.Context;
14 18
import android.view.LayoutInflater;
......
19 23
import android.widget.ImageView;
20 24
import android.widget.LinearLayout;
21 25
import android.widget.ScrollView;
26
import android.widget.TextView;
22 27

  
23 28
import org.distorted.main.MainSettingsPopup;
24 29
import org.distorted.main.R;
25 30
import org.distorted.objects.RubikObject;
31
import org.distorted.objects.RubikObjectCategories;
26 32
import org.distorted.objects.RubikObjectList;
27 33

  
28 34
///////////////////////////////////////////////////////////////////////////////////////////////////
......
30 36
public class ObjectGridCreator
31 37
  {
32 38
  private GridLayout mGrid;
39
  private GridLayout[] mCategoryGrids;
40
  private RubikObjectCategories mROC;
33 41
  private int mSortMode;
42
  private final int mMargin, mPadding, mCubeSize;
43

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

  
46
  public ObjectGridCreator(float screenW)
47
    {
48
    mMargin   = (int)(screenW*POPUP_MARGIN);
49
    mPadding  = (int)(screenW*POPUP_PADDING);
50
    mCubeSize = (int)(screenW/NUM_COLUMNS - 2*mMargin);
51
    }
34 52

  
35 53
///////////////////////////////////////////////////////////////////////////////////////////////////
36 54

  
......
42 60
      }
43 61
    else
44 62
      {
45
      return null;
63
      if( mROC!=null )
64
        {
65
        int category = mROC.getCategory(index);
66
        int posInCat = mROC.getPosInCat(category,index);
67
        return mCategoryGrids[category].getChildAt(posInCat);
68
        }
69
      else
70
        {
71
        android.util.Log.e("D", "ERROR 1 in ObjectGridCreator!");
72
        return null;
73
        }
46 74
      }
47 75
    }
48 76

  
49 77
///////////////////////////////////////////////////////////////////////////////////////////////////
50 78

  
51
  public void createObjectGrid(ScrollView scrollView, Activity act, int rowCount, int colCount, int numObjects, int margin, int size, int pad, int mode)
79
  public int getObjectIndex(int index)
52 80
    {
53
    mSortMode = mode;
81
    if( mSortMode==MainSettingsPopup.SORT_CLASSIC )
82
      {
83
      return index;
84
      }
85
    else
86
      {
87
      if( mROC!=null )
88
        {
89
        int category = mROC.getCategory(index);
90
        int posInCat = mROC.getPosInCat(category,index);
91
        return mROC.getObjectIndex(category,posInCat);
92
        }
93
      else
94
        {
95
        android.util.Log.e("D", "ERROR 2 in ObjectGridCreator!");
96
        return -1;
97
        }
98
      }
99
    }
100

  
101
///////////////////////////////////////////////////////////////////////////////////////////////////
54 102

  
55
    if( mode==MainSettingsPopup.SORT_CLASSIC )
103
  public void createObjectGrid(Activity act, ScrollView scrollView, int sortMode)
104
    {
105
    mSortMode = sortMode;
106

  
107
    if( sortMode==MainSettingsPopup.SORT_CLASSIC )
56 108
      {
57 109
      mGrid = new GridLayout(act);
58 110
      ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
59 111
      mGrid.setLayoutParams(params);
60 112
      scrollView.addView(mGrid);
61 113

  
62
      createObjectGridClassic(mGrid,act,rowCount,colCount,numObjects,margin,size,pad);
114
      int numObjects= RubikObjectList.getNumObjects();
115
      int[] objects = new int[numObjects];
116
      for(int i=0; i<numObjects; i++) objects[i] = i;
117

  
118
      createObjectGridClassic(mGrid,act,objects);
63 119
      }
64 120
    else
65 121
      {
......
67 123
      ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
68 124
      layout.setLayoutParams(params);
69 125
      layout.setOrientation(LinearLayout.VERTICAL);
126
      layout.setPadding(0,mMargin,0,mMargin);
70 127
      scrollView.addView(layout);
71 128

  
129
      mROC = new RubikObjectCategories(sortMode);
130
      if( mROC.hasIcons() ) constructIconBasedGrid(act,layout,mROC,mCubeSize);
131
      else                  constructIconlessGrid(act,layout,mROC,mCubeSize);
132
      }
133
    }
134

  
135
///////////////////////////////////////////////////////////////////////////////////////////////////
72 136

  
137
  private void constructIconBasedGrid(Activity act, LinearLayout layout, RubikObjectCategories roc, int height)
138
    {
139
    int numCategories = roc.getNumCategories();
73 140

  
141
    mCategoryGrids = new GridLayout[numCategories];
142

  
143
    for(int c=0; c<numCategories; c++)
144
      {
145
      int iconID = roc.getIconId(c);
146
      View title = constructTitle(act,iconID,height);
147
      layout.addView(title);
148
      mCategoryGrids[c] = constructGrid(act,c);
149
      layout.addView(mCategoryGrids[c]);
150
      }
151
    }
152

  
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

  
155
  private void constructIconlessGrid(Activity act, LinearLayout layout, RubikObjectCategories roc, int height)
156
    {
157
    int numCategories = roc.getNumCategories();
158

  
159
    mCategoryGrids = new GridLayout[numCategories];
160

  
161
    for(int c=0; c<numCategories; c++)
162
      {
163
      String titleStr = roc.getTitle(c);
164
      View title = constructTitle(act,titleStr,height);
165
      layout.addView(title);
166
      mCategoryGrids[c] = constructGrid(act,c);
167
      layout.addView(mCategoryGrids[c]);
74 168
      }
75 169
    }
76 170

  
77 171
///////////////////////////////////////////////////////////////////////////////////////////////////
78 172

  
79
  public static void createObjectGridClassic(GridLayout grid, Activity act, int rowCount, int colCount, int numObjects, int margin, int size, int pad)
173
  private View constructTitle(Activity act, int iconID, int height)
174
    {
175
    ImageView view = new ImageView(act);
176
    ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,height);
177
    view.setLayoutParams(params);
178
    view.setImageResource(iconID);
179

  
180
    return view;
181
    }
182

  
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184

  
185
  private View constructTitle(Activity act, String title, int height)
186
    {
187
    TextView view = new TextView(act);
188
    ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,height);
189
    view.setLayoutParams(params);
190
    view.setText(title);
191

  
192
    return view;
193
    }
194

  
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196

  
197
  private GridLayout constructGrid(Activity act, int category)
198
    {
199
    GridLayout grid = new GridLayout(act);
200
    ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
201
    grid.setLayoutParams(params);
202

  
203
    int numInCat = mROC.getNumObjects(category);
204
    int[] objects = new int[numInCat];
205
    for(int o=0; o<numInCat; o++) objects[o] = mROC.getObjectIndex(category,o);
206

  
207
    createObjectGridClassic(grid,act,objects);
208

  
209
    return grid;
210
    }
211

  
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

  
214
  public void createObjectGridClassic(GridLayout grid, Activity act, int[] objects)
80 215
    {
216
    int rowCount = (objects.length + NUM_COLUMNS-1) / NUM_COLUMNS;
217
    int colCount = NUM_COLUMNS;
218

  
81 219
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[rowCount];
82 220
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[colCount];
83 221

  
......
98 236

  
99 237
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
100 238

  
101
    for(int object=0; object<numObjects; object++)
239
    for(int object : objects)
102 240
      {
103
      View v = createView(act,layoutInflater,object,pad);
241
      View v = createView(act,layoutInflater,object,mPadding );
104 242
      int row = object/colCount;
105 243

  
106 244
      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
107
      params.bottomMargin = margin;
108
      params.topMargin    = margin;
109
      params.leftMargin   = margin;
110
      params.rightMargin  = margin;
245
      params.bottomMargin = mMargin;
246
      params.topMargin    = mMargin;
247
      params.leftMargin   = mMargin;
248
      params.rightMargin  = mMargin;
111 249

  
112
      params.width = size;
113
      params.height= size;
250
      params.width = mCubeSize;
251
      params.height= mCubeSize;
114 252

  
115 253
      nextInRow[row]++;
116 254

  
......
120 258

  
121 259
///////////////////////////////////////////////////////////////////////////////////////////////////
122 260

  
123
  public static ImageButton getButton(RubikObject object, View view)
261
  public ImageButton getButton(RubikObject object, View view)
124 262
    {
125 263
    return object!=null && object.isFree() ? (ImageButton)view : view.findViewById(R.id.non_free_button);
126 264
    }
127 265

  
128 266
///////////////////////////////////////////////////////////////////////////////////////////////////
129 267

  
130
  private static View createView(Activity act, LayoutInflater inflater, int ordinal, int pad)
268
  private View createView(Activity act, LayoutInflater inflater, int ordinal, int pad)
131 269
    {
132 270
    final RubikObject obj = RubikObjectList.getObject(ordinal);
133 271

  
src/main/java/org/distorted/main/MainScrollGrid.java
33 33

  
34 34
  void createGrid(final MainActivity act, int windowWidth, int sortMode)
35 35
    {
36
    if( mCreator==null ) mCreator = new ObjectGridCreator();
36
    if( mCreator==null ) mCreator = new ObjectGridCreator(windowWidth);
37 37

  
38 38
    mSortMode = sortMode;
39 39

  
40 40
    int numObjects = RubikObjectList.getNumObjects();
41
    int rowCount   = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
42
    int colCount   = NUM_COLUMNS;
43
    int objectSize = windowWidth / NUM_COLUMNS;
44
    int margin     = (int)(windowWidth*POPUP_MARGIN);
45
    int padding    = (int)(windowWidth*POPUP_PADDING);
46
    int cubeSize   = objectSize - 2*margin;
47

  
48 41
    ScrollView scrollView = act.findViewById(R.id.objectScroll);
49 42
    scrollView.removeAllViews();
50 43

  
51
    mCreator.createObjectGrid(scrollView,act,rowCount,colCount,numObjects,margin,cubeSize,padding,sortMode);
44
    mCreator.createObjectGrid(act,scrollView,sortMode);
52 45

  
53 46
    DisplayMetrics displaymetrics = new DisplayMetrics();
54 47
    act.getWindowManager().getDefaultDisplay().getRealMetrics(displaymetrics);
......
57 50
      {
58 51
      final RubikObject obj = RubikObjectList.getObject(child);
59 52
      View v = mCreator.getChildAt(child);
60
      ImageButton button = ObjectGridCreator.getButton(obj,v);
61
      final int ordinal = child;
53
      ImageButton button = mCreator.getButton(obj,v);
54
      final int ordinal = mCreator.getObjectIndex(child);
62 55

  
63 56
      button.setOnClickListener( new View.OnClickListener()
64 57
        {
src/main/java/org/distorted/objects/RubikObjectCategories.java
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2024 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8
///////////////////////////////////////////////////////////////////////////////////////////////////
9

  
10
package org.distorted.objects;
11

  
12
import static org.distorted.main.MainSettingsPopup.*;
13

  
14
///////////////////////////////////////////////////////////////////////////////////////////////////
15

  
16
public class RubikObjectCategories
17
{
18
  private final int mNumCategories;
19
  private int[][] mObjectIndices;
20
  private int[] mIconIDs;
21
  private String[] mTitles;
22

  
23
///////////////////////////////////////////////////////////////////////////////////////////////////
24

  
25
  public RubikObjectCategories(int sortMode)
26
    {
27
    switch(sortMode)
28
      {
29
      case SORT_CATEGORY  : buildSortCategory(); break;
30
      case SORT_DIFFICULTY: buildSortDifficulty(); break;
31
      case SORT_AUTHOR    : buildSortAuthor(); break;
32
      case SORT_YEAR      : buildSortYear(); break;
33
      }
34

  
35
    mNumCategories = mObjectIndices.length;
36

  
37
    switch(sortMode)
38
      {
39
      case SORT_CATEGORY  : buildIconsCategory(); break;
40
      case SORT_DIFFICULTY: buildIconsDifficulty(); break;
41
      case SORT_AUTHOR    : buildTitleAuthor(); break;
42
      case SORT_YEAR      : buildTitleYear(); break;
43
      }
44
    }
45

  
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

  
48
  private void buildSortCategory()
49
    {
50

  
51
    }
52

  
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

  
55
  private void buildSortDifficulty()
56
    {
57

  
58
    }
59

  
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

  
62
  private void buildSortAuthor()
63
    {
64

  
65
    }
66

  
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

  
69
  private void buildSortYear()
70
    {
71

  
72
    }
73

  
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75

  
76
  private void buildIconsCategory()
77
    {
78

  
79
    }
80

  
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82

  
83
  private void buildIconsDifficulty()
84
    {
85

  
86
    }
87

  
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89

  
90
  private void buildTitleAuthor()
91
    {
92
    mTitles = new String[mNumCategories];
93

  
94
    for(int t=0; t<mNumCategories; t++)
95
      {
96
      int obj = mObjectIndices[t][0];
97
      RubikObject object = RubikObjectList.getObject(obj);
98

  
99
      }
100
    }
101

  
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

  
104
  private void buildTitleYear()
105
    {
106

  
107
    }
108

  
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
// PUBLIC API
111

  
112
  public int getNumCategories()
113
    {
114
    return mNumCategories;
115
    }
116

  
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

  
119
  public boolean hasIcons()
120
    {
121
    return mIconIDs!=null;
122
    }
123

  
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

  
126
  public int getIconId(int category)
127
    {
128
    return ( mIconIDs!=null && category>=0 && category<mNumCategories ) ? mIconIDs[category] : -1;
129
    }
130

  
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132

  
133
  public String getTitle(int category)
134
    {
135
    return (mTitles!=null && category>=0 && category<mNumCategories) ? mTitles[category] : null;
136
    }
137

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

  
140
  public int getNumObjects(int category)
141
    {
142
    return (category>=0 && category<mNumCategories) ? mObjectIndices[category].length : 0;
143
    }
144

  
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

  
147
  public int getObjectIndex(int category, int index)
148
    {
149
    if( category>=0 && category<mNumCategories )
150
      {
151
      int[] objects = mObjectIndices[category];
152
      if( index>=0 && index<objects.length ) return objects[index];
153
      }
154

  
155
    return -1;
156
    }
157

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

  
160
  public int getCategory(int objectIndex)
161
    {
162
    int numC = mObjectIndices.length;
163

  
164
    for(int c=0; c<numC; c++)
165
      {
166
      int[] objects = mObjectIndices[c];
167
      int len = objects.length;
168

  
169
      for(int o=0; o<len; o++)
170
        if( o==objectIndex ) return c;
171
      }
172

  
173
    return -1;
174
    }
175

  
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177

  
178
  public int getPosInCat(int category, int objectIndex)
179
    {
180
    int[] objects = mObjectIndices[category];
181
    int len = objects.length;
182

  
183
    for(int o=0; o<len; o++)
184
      if( o==objectIndex ) return o;
185

  
186
    return -1;
187
    }
188
}

Also available in: Unified diff