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.dialogs;
|
21
|
|
22
|
import android.content.Context;
|
23
|
import androidx.fragment.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.ScrollView;
|
33
|
import android.widget.Spinner;
|
34
|
|
35
|
import org.distorted.main.R;
|
36
|
import org.distorted.main.RubikActivity;
|
37
|
import org.distorted.objects.RubikObjectList;
|
38
|
import org.distorted.patterns.RubikPattern;
|
39
|
import org.distorted.patterns.RubikPatternList;
|
40
|
import org.distorted.states.RubikState;
|
41
|
import org.distorted.states.RubikStatePattern;
|
42
|
|
43
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
44
|
|
45
|
public class RubikDialogPatternView extends FrameLayout implements AdapterView.OnItemSelectedListener
|
46
|
{
|
47
|
private LinearLayout mLayout;
|
48
|
private ScrollView mScroll;
|
49
|
private RubikDialogPattern mDialog;
|
50
|
private int mTab, mPos;
|
51
|
|
52
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
53
|
|
54
|
public RubikDialogPatternView(Context context, AttributeSet attrs, int defStyle)
|
55
|
{
|
56
|
super(context, attrs, defStyle);
|
57
|
}
|
58
|
|
59
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
60
|
|
61
|
public RubikDialogPatternView(Context context, AttributeSet attrs)
|
62
|
{
|
63
|
super(context, attrs);
|
64
|
}
|
65
|
|
66
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
67
|
|
68
|
public RubikDialogPatternView(FragmentActivity act, RubikDialogPattern dialog, int position)
|
69
|
{
|
70
|
super(act);
|
71
|
|
72
|
mDialog = dialog;
|
73
|
mTab = position;
|
74
|
View tab = inflate( act, R.layout.dialog_pattern_tab, null);
|
75
|
mLayout = tab.findViewById(R.id.tabLayout);
|
76
|
mScroll = tab.findViewById(R.id.tabScrollView);
|
77
|
|
78
|
String[] categories = createCategories();
|
79
|
|
80
|
Spinner spinner = tab.findViewById(R.id.pattern_category_spinner);
|
81
|
spinner.setOnItemSelectedListener(this);
|
82
|
|
83
|
ArrayAdapter<String> adapter = new ArrayAdapter<>(act, android.R.layout.simple_spinner_item, categories);
|
84
|
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
85
|
spinner.setAdapter(adapter);
|
86
|
|
87
|
RubikPattern pattern = RubikPattern.getInstance();
|
88
|
spinner.setSelection(pattern.recallCategory(mTab));
|
89
|
|
90
|
addView(tab);
|
91
|
}
|
92
|
|
93
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
94
|
|
95
|
private String[] createCategories()
|
96
|
{
|
97
|
RubikPattern pattern = RubikPattern.getInstance();
|
98
|
int numCat = pattern.getNumCategories(mTab);
|
99
|
|
100
|
String[] ret = new String[numCat];
|
101
|
|
102
|
for(int i=0; i<numCat; i++)
|
103
|
{
|
104
|
ret[i] = pattern.getCategoryName(mTab,i);
|
105
|
}
|
106
|
|
107
|
return ret;
|
108
|
}
|
109
|
|
110
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
111
|
|
112
|
private String[] createPatterns(int category)
|
113
|
{
|
114
|
RubikPattern pattern = RubikPattern.getInstance();
|
115
|
int numPat = pattern.getNumPatterns(mTab, category);
|
116
|
|
117
|
String[] ret = new String[numPat];
|
118
|
|
119
|
for(int i=0; i<numPat; i++)
|
120
|
{
|
121
|
ret[i] = pattern.getPatternName(mTab, category, i);
|
122
|
}
|
123
|
|
124
|
return ret;
|
125
|
}
|
126
|
|
127
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
128
|
|
129
|
private void fillPatterns(final int category)
|
130
|
{
|
131
|
final RubikActivity act = (RubikActivity)getContext();
|
132
|
|
133
|
DisplayMetrics metrics = act.getResources().getDisplayMetrics();
|
134
|
final float scale = metrics.density;
|
135
|
int margin = (int)(3*scale + 0.5f);
|
136
|
LinearLayout.LayoutParams bParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
|
137
|
bParams.setMargins(margin, margin, margin, margin);
|
138
|
|
139
|
final String[] patterns = createPatterns(category);
|
140
|
int len = patterns.length;
|
141
|
final RubikPattern pattern = RubikPattern.getInstance();
|
142
|
|
143
|
mLayout.removeAllViews();
|
144
|
|
145
|
for(int i=0; i<len; i++)
|
146
|
{
|
147
|
final int ii = i;
|
148
|
Button button = new Button(act);
|
149
|
button.setLayoutParams(bParams);
|
150
|
button.setText(patterns[i]);
|
151
|
|
152
|
button.setOnClickListener( new View.OnClickListener()
|
153
|
{
|
154
|
@Override
|
155
|
public void onClick(View view)
|
156
|
{
|
157
|
int[][] moves = pattern.reInitialize(mTab, category, ii);
|
158
|
|
159
|
RubikObjectList list = RubikPatternList.getObject(mTab);
|
160
|
int size = RubikPatternList.getSize(mTab);
|
161
|
|
162
|
act.changeObject( list, size, moves);
|
163
|
|
164
|
RubikStatePattern state = (RubikStatePattern) RubikState.PATT.getStateClass();
|
165
|
state.setPattern(act, mTab, category, ii);
|
166
|
mDialog.rememberState();
|
167
|
mDialog.dismiss();
|
168
|
}
|
169
|
});
|
170
|
|
171
|
mLayout.addView(button);
|
172
|
}
|
173
|
}
|
174
|
|
175
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
176
|
|
177
|
int getCurrentCategory()
|
178
|
{
|
179
|
return mPos;
|
180
|
}
|
181
|
|
182
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
183
|
|
184
|
int getCurrentScrollPos()
|
185
|
{
|
186
|
return mScroll.getScrollY();
|
187
|
}
|
188
|
|
189
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
190
|
|
191
|
@Override
|
192
|
protected void onLayout(boolean changed, int left, int top, int right, int bottom)
|
193
|
{
|
194
|
super.onLayout(changed,left,top,right,bottom);
|
195
|
|
196
|
if( !changed )
|
197
|
{
|
198
|
final RubikPattern pattern = RubikPattern.getInstance();
|
199
|
mScroll.setScrollY( pattern.recallScrollPos(mTab) );
|
200
|
}
|
201
|
}
|
202
|
|
203
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
204
|
|
205
|
@Override
|
206
|
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
|
207
|
{
|
208
|
if( parent.getId() == R.id.pattern_category_spinner )
|
209
|
{
|
210
|
mPos = pos;
|
211
|
fillPatterns(pos);
|
212
|
}
|
213
|
}
|
214
|
|
215
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
216
|
|
217
|
@Override
|
218
|
public void onNothingSelected(AdapterView<?> parent)
|
219
|
{
|
220
|
|
221
|
}
|
222
|
}
|