Project

General

Profile

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

magiccube / src / main / java / org / distorted / helpers / PopupCreator.java @ ab66b70e

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 android.app.Activity;
13
import android.content.Context;
14
import android.view.LayoutInflater;
15
import android.view.View;
16
import android.widget.GridLayout;
17
import android.widget.ImageButton;
18

    
19
import org.distorted.main.R;
20
import org.distorted.objects.RubikObject;
21
import org.distorted.objects.RubikObjectList;
22

    
23
///////////////////////////////////////////////////////////////////////////////////////////////////
24

    
25
public class PopupCreator
26
  {
27
  public static void createObjectGrid(GridLayout grid, Activity act, int rowCount, int colCount, int numObjects, int margin, int size)
28
    {
29
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[rowCount];
30
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[colCount];
31

    
32
    grid.setColumnCount(colCount);
33
    grid.setRowCount(rowCount);
34

    
35
    int[] nextInRow = new int[rowCount];
36

    
37
    for(int row=0; row<rowCount; row++)
38
      {
39
      rowSpecs[row] = GridLayout.spec(row);
40
      nextInRow[row]= 0;
41
      }
42
    for(int col=0; col<colCount; col++)
43
      {
44
      colSpecs[col] = GridLayout.spec(col);
45
      }
46

    
47
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
48

    
49
    for(int object=0; object<numObjects; object++)
50
      {
51
      View v = createView(act,layoutInflater,object);
52
      int row = object/colCount;
53

    
54
      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
55
      params.bottomMargin = margin;
56
      params.topMargin    = margin;
57
      params.leftMargin   = margin;
58
      params.rightMargin  = margin;
59

    
60
      params.width = size;
61
      params.height= size;
62

    
63
      nextInRow[row]++;
64

    
65
      grid.addView(v,params);
66
      }
67
    }
68

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

    
71
  private static View createView(Activity act, LayoutInflater inflater, int ordinal)
72
    {
73
    final RubikObject obj = RubikObjectList.getObject(ordinal);
74

    
75
    if( obj!=null )
76
      {
77
      if( obj.isFree() )
78
        {
79
        ImageButton button = new ImageButton(act);
80
        obj.setIconTo(act,button);
81
        return button;
82
        }
83
      else
84
        {
85
        final View layout = inflater.inflate(R.layout.non_free_object, null);
86
        ImageButton button= layout.findViewById(R.id.non_free_button);
87
        obj.setIconTo(act,button);
88
        return layout;
89
        }
90
      }
91

    
92
    return null;
93
    }
94
  }
(3-3/5)