Project

General

Profile

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

magiccube / src / main / java / org / distorted / helpers / ObjectGridCreator.java @ 0c9b1ef5

1 432a5f2c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 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.helpers;
11
12 3eaf5f2a leszek
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
16 432a5f2c Leszek Koltunski
import android.app.Activity;
17 ab66b70e Leszek Koltunski
import android.content.Context;
18 0c9b1ef5 leszek
import android.util.TypedValue;
19
import android.view.Gravity;
20 ab66b70e Leszek Koltunski
import android.view.LayoutInflater;
21
import android.view.View;
22 def32b2c leszek
import android.view.ViewGroup;
23 432a5f2c Leszek Koltunski
import android.widget.GridLayout;
24
import android.widget.ImageButton;
25 a88b947f Leszek Koltunski
import android.widget.ImageView;
26 def32b2c leszek
import android.widget.LinearLayout;
27
import android.widget.ScrollView;
28 3eaf5f2a leszek
import android.widget.TextView;
29 432a5f2c Leszek Koltunski
30 def32b2c leszek
import org.distorted.main.MainSettingsPopup;
31 ab66b70e Leszek Koltunski
import org.distorted.main.R;
32 432a5f2c Leszek Koltunski
import org.distorted.objects.RubikObject;
33 3eaf5f2a leszek
import org.distorted.objects.RubikObjectCategories;
34 432a5f2c Leszek Koltunski
import org.distorted.objects.RubikObjectList;
35
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37
38 def32b2c leszek
public class ObjectGridCreator
39 432a5f2c Leszek Koltunski
  {
40 0c9b1ef5 leszek
  private static final float TITLE_SIZE     = 0.8f;
41
  private static final float TEXT_SIZE      = 0.5f;
42
  private static final float TITLE_PADDING  = 0.15f;
43
44 def32b2c leszek
  private GridLayout mGrid;
45 3eaf5f2a leszek
  private GridLayout[] mCategoryGrids;
46
  private RubikObjectCategories mROC;
47 def32b2c leszek
  private int mSortMode;
48 3eaf5f2a leszek
  private final int mMargin, mPadding, mCubeSize;
49
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51
52
  public ObjectGridCreator(float screenW)
53
    {
54
    mMargin   = (int)(screenW*POPUP_MARGIN);
55
    mPadding  = (int)(screenW*POPUP_PADDING);
56
    mCubeSize = (int)(screenW/NUM_COLUMNS - 2*mMargin);
57
    }
58 def32b2c leszek
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60
61
  public View getChildAt(int index)
62
    {
63
    if( mSortMode==MainSettingsPopup.SORT_CLASSIC )
64
      {
65
      return mGrid.getChildAt(index);
66
      }
67
    else
68
      {
69 3eaf5f2a leszek
      if( mROC!=null )
70
        {
71
        int category = mROC.getCategory(index);
72
        int posInCat = mROC.getPosInCat(category,index);
73
        return mCategoryGrids[category].getChildAt(posInCat);
74
        }
75
      else
76
        {
77
        android.util.Log.e("D", "ERROR 1 in ObjectGridCreator!");
78
        return null;
79
        }
80 def32b2c leszek
      }
81
    }
82
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84
85 3eaf5f2a leszek
  public int getObjectIndex(int index)
86 def32b2c leszek
    {
87 3eaf5f2a leszek
    if( mSortMode==MainSettingsPopup.SORT_CLASSIC )
88
      {
89
      return index;
90
      }
91
    else
92
      {
93
      if( mROC!=null )
94
        {
95
        int category = mROC.getCategory(index);
96
        int posInCat = mROC.getPosInCat(category,index);
97
        return mROC.getObjectIndex(category,posInCat);
98
        }
99
      else
100
        {
101
        android.util.Log.e("D", "ERROR 2 in ObjectGridCreator!");
102
        return -1;
103
        }
104
      }
105
    }
106
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108 def32b2c leszek
109 3eaf5f2a leszek
  public void createObjectGrid(Activity act, ScrollView scrollView, int sortMode)
110
    {
111
    mSortMode = sortMode;
112
113
    if( sortMode==MainSettingsPopup.SORT_CLASSIC )
114 def32b2c leszek
      {
115
      mGrid = new GridLayout(act);
116
      ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
117
      mGrid.setLayoutParams(params);
118
      scrollView.addView(mGrid);
119
120 3eaf5f2a leszek
      int numObjects= RubikObjectList.getNumObjects();
121
      int[] objects = new int[numObjects];
122
      for(int i=0; i<numObjects; i++) objects[i] = i;
123
124
      createObjectGridClassic(mGrid,act,objects);
125 def32b2c leszek
      }
126
    else
127
      {
128
      LinearLayout layout = new LinearLayout(act);
129
      ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
130
      layout.setLayoutParams(params);
131
      layout.setOrientation(LinearLayout.VERTICAL);
132 3eaf5f2a leszek
      layout.setPadding(0,mMargin,0,mMargin);
133 def32b2c leszek
      scrollView.addView(layout);
134
135 0c9b1ef5 leszek
      int height = (int)(TITLE_SIZE*mCubeSize);
136
137 3eaf5f2a leszek
      mROC = new RubikObjectCategories(sortMode);
138 0c9b1ef5 leszek
      if( mROC.hasIcons() ) constructIconBasedGrid(act,layout,mROC,height);
139
      else                  constructIconlessGrid(act,layout,mROC,height);
140 3eaf5f2a leszek
      }
141
    }
142
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144 def32b2c leszek
145 3eaf5f2a leszek
  private void constructIconBasedGrid(Activity act, LinearLayout layout, RubikObjectCategories roc, int height)
146
    {
147
    int numCategories = roc.getNumCategories();
148 def32b2c leszek
149 3eaf5f2a leszek
    mCategoryGrids = new GridLayout[numCategories];
150
151
    for(int c=0; c<numCategories; c++)
152
      {
153
      int iconID = roc.getIconId(c);
154
      View title = constructTitle(act,iconID,height);
155
      layout.addView(title);
156
      mCategoryGrids[c] = constructGrid(act,c);
157
      layout.addView(mCategoryGrids[c]);
158
      }
159
    }
160
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162
163
  private void constructIconlessGrid(Activity act, LinearLayout layout, RubikObjectCategories roc, int height)
164
    {
165
    int numCategories = roc.getNumCategories();
166
167
    mCategoryGrids = new GridLayout[numCategories];
168
169
    for(int c=0; c<numCategories; c++)
170
      {
171
      String titleStr = roc.getTitle(c);
172
      View title = constructTitle(act,titleStr,height);
173
      layout.addView(title);
174
      mCategoryGrids[c] = constructGrid(act,c);
175
      layout.addView(mCategoryGrids[c]);
176 def32b2c leszek
      }
177
    }
178
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180
181 3eaf5f2a leszek
  private View constructTitle(Activity act, int iconID, int height)
182
    {
183
    ImageView view = new ImageView(act);
184
    ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,height);
185
    view.setLayoutParams(params);
186 0c9b1ef5 leszek
    view.setBackgroundResource(R.color.dark_grey);
187
188
    int p = (int)(TITLE_PADDING*height);
189
    view.setPadding( p,p,p,p );
190 3eaf5f2a leszek
    view.setImageResource(iconID);
191
192
    return view;
193
    }
194
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196
197
  private View constructTitle(Activity act, String title, int height)
198
    {
199
    TextView view = new TextView(act);
200
    ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,height);
201
    view.setLayoutParams(params);
202 0c9b1ef5 leszek
    view.setBackgroundResource(R.color.dark_grey);
203
    view.setGravity(Gravity.CENTER);
204
205
    int size = (int)(TEXT_SIZE*height);
206
    view.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
207 3eaf5f2a leszek
    view.setText(title);
208
209
    return view;
210
    }
211
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213
214
  private GridLayout constructGrid(Activity act, int category)
215
    {
216
    GridLayout grid = new GridLayout(act);
217
    ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
218
    grid.setLayoutParams(params);
219
220
    int numInCat = mROC.getNumObjects(category);
221
    int[] objects = new int[numInCat];
222
    for(int o=0; o<numInCat; o++) objects[o] = mROC.getObjectIndex(category,o);
223
224
    createObjectGridClassic(grid,act,objects);
225
226
    return grid;
227
    }
228
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230
231
  public void createObjectGridClassic(GridLayout grid, Activity act, int[] objects)
232 432a5f2c Leszek Koltunski
    {
233 3eaf5f2a leszek
    int rowCount = (objects.length + NUM_COLUMNS-1) / NUM_COLUMNS;
234
    int colCount = NUM_COLUMNS;
235
236 432a5f2c Leszek Koltunski
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[rowCount];
237
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[colCount];
238
239
    grid.setColumnCount(colCount);
240
    grid.setRowCount(rowCount);
241
242
    int[] nextInRow = new int[rowCount];
243
244
    for(int row=0; row<rowCount; row++)
245
      {
246
      rowSpecs[row] = GridLayout.spec(row);
247
      nextInRow[row]= 0;
248
      }
249
    for(int col=0; col<colCount; col++)
250
      {
251
      colSpecs[col] = GridLayout.spec(col);
252
      }
253
254 ab66b70e Leszek Koltunski
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
255 ff4a2a13 leszek
    int obj = 0;
256 ab66b70e Leszek Koltunski
257 3eaf5f2a leszek
    for(int object : objects)
258 432a5f2c Leszek Koltunski
      {
259 3eaf5f2a leszek
      View v = createView(act,layoutInflater,object,mPadding );
260 ff4a2a13 leszek
      int row = obj/colCount;
261
      obj++;
262
      GridLayout.Spec rs = rowSpecs[row];
263
      GridLayout.Spec cs = colSpecs[nextInRow[row]];
264 432a5f2c Leszek Koltunski
265 ff4a2a13 leszek
      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rs,cs);
266 3eaf5f2a leszek
      params.bottomMargin = mMargin;
267
      params.topMargin    = mMargin;
268
      params.leftMargin   = mMargin;
269
      params.rightMargin  = mMargin;
270 432a5f2c Leszek Koltunski
271 3eaf5f2a leszek
      params.width = mCubeSize;
272
      params.height= mCubeSize;
273 432a5f2c Leszek Koltunski
274
      nextInRow[row]++;
275
276 ab66b70e Leszek Koltunski
      grid.addView(v,params);
277
      }
278
    }
279
280 302c76a0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
281
282 3eaf5f2a leszek
  public ImageButton getButton(RubikObject object, View view)
283 302c76a0 Leszek Koltunski
    {
284
    return object!=null && object.isFree() ? (ImageButton)view : view.findViewById(R.id.non_free_button);
285
    }
286
287 ab66b70e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
288
289 3eaf5f2a leszek
  private View createView(Activity act, LayoutInflater inflater, int ordinal, int pad)
290 ab66b70e Leszek Koltunski
    {
291
    final RubikObject obj = RubikObjectList.getObject(ordinal);
292
293
    if( obj!=null )
294
      {
295
      if( obj.isFree() )
296
        {
297
        ImageButton button = new ImageButton(act);
298
        obj.setIconTo(act,button);
299
        return button;
300
        }
301
      else
302
        {
303
        final View layout = inflater.inflate(R.layout.non_free_object, null);
304
        ImageButton button= layout.findViewById(R.id.non_free_button);
305
        obj.setIconTo(act,button);
306 a88b947f Leszek Koltunski
        ImageView view= layout.findViewById(R.id.non_free_lock);
307
        view.setPadding(0,pad,0,0);
308 ab66b70e Leszek Koltunski
        return layout;
309
        }
310 432a5f2c Leszek Koltunski
      }
311 ab66b70e Leszek Koltunski
312
    return null;
313 432a5f2c Leszek Koltunski
    }
314
  }