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
|
import android.widget.ImageView;
|
19
|
|
20
|
import org.distorted.main.R;
|
21
|
import org.distorted.objects.RubikObject;
|
22
|
import org.distorted.objects.RubikObjectList;
|
23
|
|
24
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
25
|
|
26
|
public class PopupCreator
|
27
|
{
|
28
|
public static void createObjectGrid(GridLayout grid, Activity act, int rowCount, int colCount, int numObjects, int margin, int size, int pad)
|
29
|
{
|
30
|
GridLayout.Spec[] rowSpecs = new GridLayout.Spec[rowCount];
|
31
|
GridLayout.Spec[] colSpecs = new GridLayout.Spec[colCount];
|
32
|
|
33
|
grid.setColumnCount(colCount);
|
34
|
grid.setRowCount(rowCount);
|
35
|
|
36
|
int[] nextInRow = new int[rowCount];
|
37
|
|
38
|
for(int row=0; row<rowCount; row++)
|
39
|
{
|
40
|
rowSpecs[row] = GridLayout.spec(row);
|
41
|
nextInRow[row]= 0;
|
42
|
}
|
43
|
for(int col=0; col<colCount; col++)
|
44
|
{
|
45
|
colSpecs[col] = GridLayout.spec(col);
|
46
|
}
|
47
|
|
48
|
LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
49
|
|
50
|
for(int object=0; object<numObjects; object++)
|
51
|
{
|
52
|
View v = createView(act,layoutInflater,object,pad);
|
53
|
int row = object/colCount;
|
54
|
|
55
|
GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
|
56
|
params.bottomMargin = margin;
|
57
|
params.topMargin = margin;
|
58
|
params.leftMargin = margin;
|
59
|
params.rightMargin = margin;
|
60
|
|
61
|
params.width = size;
|
62
|
params.height= size;
|
63
|
|
64
|
nextInRow[row]++;
|
65
|
|
66
|
grid.addView(v,params);
|
67
|
}
|
68
|
}
|
69
|
|
70
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
71
|
|
72
|
public static ImageButton getButton(RubikObject object, View view)
|
73
|
{
|
74
|
return object!=null && object.isFree() ? (ImageButton)view : view.findViewById(R.id.non_free_button);
|
75
|
}
|
76
|
|
77
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
78
|
|
79
|
private static View createView(Activity act, LayoutInflater inflater, int ordinal, int pad)
|
80
|
{
|
81
|
final RubikObject obj = RubikObjectList.getObject(ordinal);
|
82
|
|
83
|
if( obj!=null )
|
84
|
{
|
85
|
if( obj.isFree() )
|
86
|
{
|
87
|
ImageButton button = new ImageButton(act);
|
88
|
obj.setIconTo(act,button);
|
89
|
return button;
|
90
|
}
|
91
|
else
|
92
|
{
|
93
|
final View layout = inflater.inflate(R.layout.non_free_object, null);
|
94
|
ImageButton button= layout.findViewById(R.id.non_free_button);
|
95
|
obj.setIconTo(act,button);
|
96
|
ImageView view= layout.findViewById(R.id.non_free_lock);
|
97
|
view.setPadding(0,pad,0,0);
|
98
|
return layout;
|
99
|
}
|
100
|
}
|
101
|
|
102
|
return null;
|
103
|
}
|
104
|
}
|