Project

General

Profile

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

magiccube / src / main / java / org / distorted / helpers / ObjectGridCreator.java @ 30667941

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