Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
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
import android.app.Activity;
17
import android.content.Context;
18
import android.util.TypedValue;
19
import android.view.Gravity;
20
import android.view.LayoutInflater;
21
import android.view.View;
22
import android.view.ViewGroup;
23
import android.widget.GridLayout;
24
import android.widget.ImageButton;
25
import android.widget.ImageView;
26
import android.widget.LinearLayout;
27
import android.widget.ScrollView;
28
import android.widget.TextView;
29

    
30
import org.distorted.main.MainSettingsPopup;
31
import org.distorted.main.R;
32
import org.distorted.objects.RubikObject;
33
import org.distorted.objects.RubikObjectCategories;
34
import org.distorted.objects.RubikObjectList;
35

    
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37

    
38
public class ObjectGridCreator
39
  {
40
  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
  private GridLayout mGrid;
45
  private GridLayout[] mCategoryGrids;
46
  private RubikObjectCategories mROC;
47
  private int mSortMode;
48
  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

    
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
      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
      }
81
    }
82

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84

    
85
  public int getObjectIndex(int index)
86
    {
87
    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

    
109
  public void createObjectGrid(Activity act, ScrollView scrollView, int sortMode)
110
    {
111
    mSortMode = sortMode;
112

    
113
    if( sortMode==MainSettingsPopup.SORT_CLASSIC )
114
      {
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
      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
      }
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
      layout.setPadding(0,mMargin,0,mMargin);
133
      scrollView.addView(layout);
134

    
135
      int height = (int)(TITLE_SIZE*mCubeSize);
136

    
137
      mROC = new RubikObjectCategories(sortMode);
138
      if( mROC.hasIcons() ) constructIconBasedGrid(act,layout,mROC,height);
139
      else                  constructIconlessGrid(act,layout,mROC,height);
140
      }
141
    }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

    
145
  private void constructIconBasedGrid(Activity act, LinearLayout layout, RubikObjectCategories roc, int height)
146
    {
147
    int numCategories = roc.getNumCategories();
148

    
149
    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
      }
177
    }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

    
181
  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
    view.setBackgroundResource(R.color.dark_grey);
187

    
188
    int p = (int)(TITLE_PADDING*height);
189
    view.setPadding( p,p,p,p );
190
    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
    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
    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
    {
233
    int rowCount = (objects.length + NUM_COLUMNS-1) / NUM_COLUMNS;
234
    int colCount = NUM_COLUMNS;
235

    
236
    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
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
255
    int obj = 0;
256

    
257
    for(int object : objects)
258
      {
259
      View v = createView(act,layoutInflater,object,mPadding );
260
      int row = obj/colCount;
261
      obj++;
262
      GridLayout.Spec rs = rowSpecs[row];
263
      GridLayout.Spec cs = colSpecs[nextInRow[row]];
264

    
265
      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rs,cs);
266
      params.bottomMargin = mMargin;
267
      params.topMargin    = mMargin;
268
      params.leftMargin   = mMargin;
269
      params.rightMargin  = mMargin;
270

    
271
      params.width = mCubeSize;
272
      params.height= mCubeSize;
273

    
274
      nextInRow[row]++;
275

    
276
      grid.addView(v,params);
277
      }
278
    }
279

    
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281

    
282
  public ImageButton getButton(RubikObject object, View view)
283
    {
284
    return object!=null && object.isFree() ? (ImageButton)view : view.findViewById(R.id.non_free_button);
285
    }
286

    
287
///////////////////////////////////////////////////////////////////////////////////////////////////
288

    
289
  private View createView(Activity act, LayoutInflater inflater, int ordinal, int pad)
290
    {
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
        ImageView view= layout.findViewById(R.id.non_free_lock);
307
        view.setPadding(0,pad,0,0);
308
        return layout;
309
        }
310
      }
311

    
312
    return null;
313
    }
314
  }
(3-3/5)