1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2020 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Magic Cube. //
|
5
|
// //
|
6
|
// Magic Cube is free software: you can redistribute it and/or modify //
|
7
|
// it under the terms of the GNU General Public License as published by //
|
8
|
// the Free Software Foundation, either version 2 of the License, or //
|
9
|
// (at your option) any later version. //
|
10
|
// //
|
11
|
// Magic Cube is distributed in the hope that it will be useful, //
|
12
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
14
|
// GNU General Public License for more details. //
|
15
|
// //
|
16
|
// You should have received a copy of the GNU General Public License //
|
17
|
// along with Magic Cube. If not, see <http://www.gnu.org/licenses/>. //
|
18
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19
|
|
20
|
package org.distorted.dialog;
|
21
|
|
22
|
import android.content.Context;
|
23
|
import android.support.v4.app.FragmentActivity;
|
24
|
import android.util.AttributeSet;
|
25
|
import android.util.DisplayMetrics;
|
26
|
import android.view.View;
|
27
|
import android.widget.AdapterView;
|
28
|
import android.widget.ArrayAdapter;
|
29
|
import android.widget.Button;
|
30
|
import android.widget.FrameLayout;
|
31
|
import android.widget.LinearLayout;
|
32
|
import android.widget.Spinner;
|
33
|
|
34
|
import org.distorted.magic.R;
|
35
|
import org.distorted.magic.RubikActivity;
|
36
|
import org.distorted.patterns.RubikPattern;
|
37
|
import org.distorted.uistate.RubikState;
|
38
|
import org.distorted.uistate.RubikStatePattern;
|
39
|
|
40
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
41
|
|
42
|
public class RubikDialogPatternView extends FrameLayout implements AdapterView.OnItemSelectedListener
|
43
|
{
|
44
|
private LinearLayout mLayout;
|
45
|
private RubikDialogPattern mDialog;
|
46
|
private int mPosition;
|
47
|
|
48
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
49
|
|
50
|
public RubikDialogPatternView(Context context, AttributeSet attrs, int defStyle)
|
51
|
{
|
52
|
super(context, attrs, defStyle);
|
53
|
}
|
54
|
|
55
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
56
|
|
57
|
public RubikDialogPatternView(Context context, AttributeSet attrs)
|
58
|
{
|
59
|
super(context, attrs);
|
60
|
}
|
61
|
|
62
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
63
|
|
64
|
public RubikDialogPatternView(FragmentActivity act, RubikDialogPattern dialog, int position)
|
65
|
{
|
66
|
super(act);
|
67
|
|
68
|
mDialog = dialog;
|
69
|
mPosition = position;
|
70
|
View tab = inflate( act, R.layout.dialog_pattern_tab, null);
|
71
|
mLayout = tab.findViewById(R.id.tabLayout);
|
72
|
|
73
|
String[] categories = createCategories();
|
74
|
|
75
|
Spinner spinner = tab.findViewById(R.id.pattern_category_spinner);
|
76
|
spinner.setOnItemSelectedListener(this);
|
77
|
|
78
|
ArrayAdapter<String> adapter = new ArrayAdapter<>(act, android.R.layout.simple_spinner_item, categories);
|
79
|
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
80
|
spinner.setAdapter(adapter);
|
81
|
|
82
|
spinner.setSelection(0);
|
83
|
|
84
|
addView(tab);
|
85
|
}
|
86
|
|
87
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
88
|
|
89
|
private String[] createCategories()
|
90
|
{
|
91
|
RubikPattern pattern = RubikPattern.getInstance();
|
92
|
int numCat = pattern.getNumCategories(mPosition);
|
93
|
|
94
|
String[] ret = new String[numCat];
|
95
|
|
96
|
for(int i=0; i<numCat; i++)
|
97
|
{
|
98
|
ret[i] = pattern.getCategoryName(mPosition,i);
|
99
|
}
|
100
|
|
101
|
return ret;
|
102
|
}
|
103
|
|
104
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
105
|
|
106
|
private String[] createPatterns(int category)
|
107
|
{
|
108
|
RubikPattern pattern = RubikPattern.getInstance();
|
109
|
int numPat = pattern.getNumPatterns(mPosition, category);
|
110
|
|
111
|
String[] ret = new String[numPat];
|
112
|
|
113
|
for(int i=0; i<numPat; i++)
|
114
|
{
|
115
|
ret[i] = pattern.getPatternName(mPosition, category, i);
|
116
|
}
|
117
|
|
118
|
return ret;
|
119
|
}
|
120
|
|
121
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
122
|
|
123
|
private void fillPatterns(final int category)
|
124
|
{
|
125
|
final RubikActivity act = (RubikActivity)getContext();
|
126
|
|
127
|
DisplayMetrics metrics = act.getResources().getDisplayMetrics();
|
128
|
final float scale = metrics.density;
|
129
|
int margin = (int)(3*scale + 0.5f);
|
130
|
LinearLayout.LayoutParams bParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
|
131
|
bParams.setMargins(margin, margin, margin, margin);
|
132
|
|
133
|
final String[] patterns = createPatterns(category);
|
134
|
int len = patterns.length;
|
135
|
|
136
|
mLayout.removeAllViews();
|
137
|
|
138
|
for(int i=0; i<len; i++)
|
139
|
{
|
140
|
final int ii = i;
|
141
|
Button button = new Button(act);
|
142
|
button.setLayoutParams(bParams);
|
143
|
button.setText(patterns[i]);
|
144
|
|
145
|
button.setOnClickListener( new View.OnClickListener()
|
146
|
{
|
147
|
@Override
|
148
|
public void onClick(View view)
|
149
|
{
|
150
|
RubikStatePattern state = (RubikStatePattern) RubikState.PATT.getStateClass();
|
151
|
state.setPattern(mPosition, category, ii);
|
152
|
mDialog.dismiss();
|
153
|
}
|
154
|
});
|
155
|
|
156
|
mLayout.addView(button);
|
157
|
}
|
158
|
}
|
159
|
|
160
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
161
|
|
162
|
@Override
|
163
|
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
|
164
|
{
|
165
|
if( parent.getId() == R.id.pattern_category_spinner )
|
166
|
{
|
167
|
fillPatterns(pos);
|
168
|
}
|
169
|
}
|
170
|
|
171
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
172
|
|
173
|
@Override
|
174
|
public void onNothingSelected(AdapterView<?> parent)
|
175
|
{
|
176
|
|
177
|
}
|
178
|
}
|