Project

General

Profile

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

magiccube / src / main / java / org / distorted / helpers / ObjectGridCreator.java @ 48e61813

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.FrameLayout;
24
import android.widget.GridLayout;
25
import android.widget.ImageButton;
26
import android.widget.ImageView;
27
import android.widget.LinearLayout;
28
import android.widget.ScrollView;
29
import android.widget.TextView;
30

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

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

    
39
public class ObjectGridCreator
40
  {
41
  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
  private static final float TITLE_MARGIN   = 0.15f;
45

    
46
  private GridLayout mGrid;
47
  private GridLayout[] mCategoryGrids;
48
  private RubikObjectCategories mROC;
49
  private int mSortMode;
50
  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

    
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
      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
      }
83
    }
84

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

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

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

    
115
    if( sortMode==MainSettingsPopup.SORT_CLASSIC )
116
      {
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
      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
      }
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
      layout.setPadding(0,mMargin,0,mMargin);
135
      scrollView.addView(layout);
136

    
137
      int height = (int)(TITLE_SIZE*mCubeSize);
138

    
139
      mROC = RubikObjectCategories.create(sortMode);
140
      if( mROC.hasIcons() ) constructIconBasedGrid(act,layout,mROC,height);
141
      else                  constructIconlessGrid(act,layout,mROC,height);
142
      }
143
    }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

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

    
151
    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
      }
179
    }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

    
183
  private View constructTitle(Activity act, int iconID, int height)
184
    {
185
    ImageView view = new ImageView(act);
186
    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
    view.setLayoutParams(params);
191

    
192
    view.setBackgroundResource(R.color.dark_grey);
193

    
194
    int p = (int)(TITLE_PADDING*height);
195
    view.setPadding( p,p,p,p );
196
    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
    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
    view.setLayoutParams(params);
211
    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
    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
    {
242
    int rowCount = (objects.length + NUM_COLUMNS-1) / NUM_COLUMNS;
243
    int colCount = NUM_COLUMNS;
244

    
245
    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
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
264
    int obj = 0;
265

    
266
    for(int object : objects)
267
      {
268
      View v = createView(act,layoutInflater,object,mPadding );
269
      int row = obj/colCount;
270
      obj++;
271
      GridLayout.Spec rs = rowSpecs[row];
272
      GridLayout.Spec cs = colSpecs[nextInRow[row]];
273

    
274
      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rs,cs);
275
      params.bottomMargin = mMargin;
276
      params.topMargin    = mMargin;
277
      params.leftMargin   = mMargin;
278
      params.rightMargin  = mMargin;
279

    
280
      params.width = mCubeSize;
281
      params.height= mCubeSize;
282

    
283
      nextInRow[row]++;
284

    
285
      grid.addView(v,params);
286
      }
287
    }
288

    
289
///////////////////////////////////////////////////////////////////////////////////////////////////
290

    
291
  public ImageButton getButton(RubikObject object, View view)
292
    {
293
    return object!=null && object.isFree() ? (ImageButton)view : view.findViewById(R.id.non_free_button);
294
    }
295

    
296
///////////////////////////////////////////////////////////////////////////////////////////////////
297

    
298
  private View createView(Activity act, LayoutInflater inflater, int ordinal, int pad)
299
    {
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
        ImageView view= layout.findViewById(R.id.non_free_lock);
316
        view.setPadding(0,pad,0,0);
317
        return layout;
318
        }
319
      }
320

    
321
    return null;
322
    }
323
  }
(3-3/5)