Project

General

Profile

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

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

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 9b6f8f18 leszek
  public void createObjectGrid(Activity act, ScrollView scrollView, int sortMode, int color)
112 3eaf5f2a leszek
    {
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 9b6f8f18 leszek
      if( mROC.hasIcons() ) constructIconBasedGrid(act,layout,mROC,height,color);
141
      else                  constructIconlessGrid(act,layout,mROC,height,color);
142 3eaf5f2a leszek
      }
143
    }
144
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146 def32b2c leszek
147 9b6f8f18 leszek
  private void constructIconBasedGrid(Activity act, LinearLayout layout, RubikObjectCategories roc, int height, int color)
148 3eaf5f2a leszek
    {
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 9b6f8f18 leszek
      View title = constructTitle(act,iconID,height,color);
157 3eaf5f2a leszek
      layout.addView(title);
158
      mCategoryGrids[c] = constructGrid(act,c);
159
      layout.addView(mCategoryGrids[c]);
160
      }
161
    }
162
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164
165 9b6f8f18 leszek
  private void constructIconlessGrid(Activity act, LinearLayout layout, RubikObjectCategories roc, int height, int color)
166 3eaf5f2a leszek
    {
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 9b6f8f18 leszek
      View title = constructTitle(act,titleStr,height,color);
175 3eaf5f2a leszek
      layout.addView(title);
176
      mCategoryGrids[c] = constructGrid(act,c);
177
      layout.addView(mCategoryGrids[c]);
178 def32b2c leszek
      }
179
    }
180
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182
183 9b6f8f18 leszek
  private View constructTitle(Activity act, int iconID, int height, int color)
184 3eaf5f2a leszek
    {
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 9b6f8f18 leszek
    view.setBackgroundResource(color);
192 0c9b1ef5 leszek
    int p = (int)(TITLE_PADDING*height);
193
    view.setPadding( p,p,p,p );
194 3eaf5f2a leszek
    view.setImageResource(iconID);
195
196
    return view;
197
    }
198
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200
201 9b6f8f18 leszek
  private View constructTitle(Activity act, String title, int height, int color)
202 3eaf5f2a leszek
    {
203
    TextView view = new TextView(act);
204 48e61813 leszek
    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,height);
205
    int m = (int)(TITLE_MARGIN*height);
206
    params.leftMargin = m;
207
    params.rightMargin = m;
208 3eaf5f2a leszek
    view.setLayoutParams(params);
209 9b6f8f18 leszek
    view.setBackgroundResource(color);
210 0c9b1ef5 leszek
    view.setGravity(Gravity.CENTER);
211
212
    int size = (int)(TEXT_SIZE*height);
213
    view.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
214 3eaf5f2a leszek
    view.setText(title);
215
216
    return view;
217
    }
218
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220
221
  private GridLayout constructGrid(Activity act, int category)
222
    {
223
    GridLayout grid = new GridLayout(act);
224
    ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
225
    grid.setLayoutParams(params);
226
227
    int numInCat = mROC.getNumObjects(category);
228
    int[] objects = new int[numInCat];
229
    for(int o=0; o<numInCat; o++) objects[o] = mROC.getObjectIndex(category,o);
230
231
    createObjectGridClassic(grid,act,objects);
232
233
    return grid;
234
    }
235
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237
238
  public void createObjectGridClassic(GridLayout grid, Activity act, int[] objects)
239 432a5f2c Leszek Koltunski
    {
240 3eaf5f2a leszek
    int rowCount = (objects.length + NUM_COLUMNS-1) / NUM_COLUMNS;
241
    int colCount = NUM_COLUMNS;
242
243 432a5f2c Leszek Koltunski
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[rowCount];
244
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[colCount];
245
246
    grid.setColumnCount(colCount);
247
    grid.setRowCount(rowCount);
248
249
    int[] nextInRow = new int[rowCount];
250
251
    for(int row=0; row<rowCount; row++)
252
      {
253
      rowSpecs[row] = GridLayout.spec(row);
254
      nextInRow[row]= 0;
255
      }
256
    for(int col=0; col<colCount; col++)
257
      {
258
      colSpecs[col] = GridLayout.spec(col);
259
      }
260
261 ab66b70e Leszek Koltunski
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
262 ff4a2a13 leszek
    int obj = 0;
263 ab66b70e Leszek Koltunski
264 3eaf5f2a leszek
    for(int object : objects)
265 432a5f2c Leszek Koltunski
      {
266 3eaf5f2a leszek
      View v = createView(act,layoutInflater,object,mPadding );
267 ff4a2a13 leszek
      int row = obj/colCount;
268
      obj++;
269
      GridLayout.Spec rs = rowSpecs[row];
270
      GridLayout.Spec cs = colSpecs[nextInRow[row]];
271 432a5f2c Leszek Koltunski
272 ff4a2a13 leszek
      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rs,cs);
273 3eaf5f2a leszek
      params.bottomMargin = mMargin;
274
      params.topMargin    = mMargin;
275
      params.leftMargin   = mMargin;
276
      params.rightMargin  = mMargin;
277 432a5f2c Leszek Koltunski
278 3eaf5f2a leszek
      params.width = mCubeSize;
279
      params.height= mCubeSize;
280 432a5f2c Leszek Koltunski
281
      nextInRow[row]++;
282
283 ab66b70e Leszek Koltunski
      grid.addView(v,params);
284
      }
285
    }
286
287 302c76a0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
288
289 3eaf5f2a leszek
  public ImageButton getButton(RubikObject object, View view)
290 302c76a0 Leszek Koltunski
    {
291
    return object!=null && object.isFree() ? (ImageButton)view : view.findViewById(R.id.non_free_button);
292
    }
293
294 ab66b70e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
295
296 3eaf5f2a leszek
  private View createView(Activity act, LayoutInflater inflater, int ordinal, int pad)
297 ab66b70e Leszek Koltunski
    {
298
    final RubikObject obj = RubikObjectList.getObject(ordinal);
299
300
    if( obj!=null )
301
      {
302
      if( obj.isFree() )
303
        {
304
        ImageButton button = new ImageButton(act);
305
        obj.setIconTo(act,button);
306
        return button;
307
        }
308
      else
309
        {
310
        final View layout = inflater.inflate(R.layout.non_free_object, null);
311
        ImageButton button= layout.findViewById(R.id.non_free_button);
312
        obj.setIconTo(act,button);
313 a88b947f Leszek Koltunski
        ImageView view= layout.findViewById(R.id.non_free_lock);
314
        view.setPadding(0,pad,0,0);
315 ab66b70e Leszek Koltunski
        return layout;
316
        }
317 432a5f2c Leszek Koltunski
      }
318 ab66b70e Leszek Koltunski
319
    return null;
320 432a5f2c Leszek Koltunski
    }
321
  }