Revision 3eaf5f2a
Added by Leszek Koltunski 4 months ago
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 |
|
Also available in: Unified diff
Progress with sorting the objects by various criteria.