1 |
e108b57e
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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 |
3a9d19ed
|
Leszek Koltunski
|
import android.widget.AdapterView;
|
28 |
|
|
import android.widget.ArrayAdapter;
|
29 |
e108b57e
|
Leszek Koltunski
|
import android.widget.Button;
|
30 |
|
|
import android.widget.FrameLayout;
|
31 |
|
|
import android.widget.LinearLayout;
|
32 |
3a9d19ed
|
Leszek Koltunski
|
import android.widget.Spinner;
|
33 |
e108b57e
|
Leszek Koltunski
|
|
34 |
|
|
import org.distorted.magic.R;
|
35 |
|
|
import org.distorted.magic.RubikActivity;
|
36 |
3a9d19ed
|
Leszek Koltunski
|
import org.distorted.patterns.RubikPattern;
|
37 |
e108b57e
|
Leszek Koltunski
|
import org.distorted.uistate.RubikState;
|
38 |
3a9d19ed
|
Leszek Koltunski
|
import org.distorted.uistate.RubikStatePattern;
|
39 |
e108b57e
|
Leszek Koltunski
|
|
40 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
41 |
|
|
|
42 |
3a9d19ed
|
Leszek Koltunski
|
public class RubikDialogPatternView extends FrameLayout implements AdapterView.OnItemSelectedListener
|
43 |
e108b57e
|
Leszek Koltunski
|
{
|
44 |
|
|
private LinearLayout mLayout;
|
45 |
|
|
private RubikDialogPattern mDialog;
|
46 |
3a9d19ed
|
Leszek Koltunski
|
private int mPosition;
|
47 |
e108b57e
|
Leszek Koltunski
|
|
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 |
3a9d19ed
|
Leszek Koltunski
|
public RubikDialogPatternView(FragmentActivity act, RubikDialogPattern dialog, int position)
|
65 |
e108b57e
|
Leszek Koltunski
|
{
|
66 |
|
|
super(act);
|
67 |
|
|
|
68 |
|
|
mDialog = dialog;
|
69 |
3a9d19ed
|
Leszek Koltunski
|
mPosition = position;
|
70 |
e108b57e
|
Leszek Koltunski
|
View tab = inflate( act, R.layout.dialog_pattern_tab, null);
|
71 |
|
|
mLayout = tab.findViewById(R.id.tabLayout);
|
72 |
3a9d19ed
|
Leszek Koltunski
|
|
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 |
e108b57e
|
Leszek Koltunski
|
|
84 |
|
|
addView(tab);
|
85 |
|
|
}
|
86 |
|
|
|
87 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
88 |
|
|
|
89 |
3a9d19ed
|
Leszek Koltunski
|
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 |
e108b57e
|
Leszek Koltunski
|
{
|
108 |
3a9d19ed
|
Leszek Koltunski
|
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(int category)
|
124 |
|
|
{
|
125 |
|
|
final RubikActivity act = (RubikActivity)getContext();
|
126 |
|
|
|
127 |
e108b57e
|
Leszek Koltunski
|
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 |
3a9d19ed
|
Leszek Koltunski
|
final String[] patterns = createPatterns(category);
|
134 |
|
|
int len = patterns.length;
|
135 |
e108b57e
|
Leszek Koltunski
|
|
136 |
3a9d19ed
|
Leszek Koltunski
|
mLayout.removeAllViews();
|
137 |
|
|
|
138 |
|
|
for(int i=0; i<len; i++)
|
139 |
e108b57e
|
Leszek Koltunski
|
{
|
140 |
3a9d19ed
|
Leszek Koltunski
|
final int ii = i;
|
141 |
e108b57e
|
Leszek Koltunski
|
Button button = new Button(act);
|
142 |
|
|
button.setLayoutParams(bParams);
|
143 |
3a9d19ed
|
Leszek Koltunski
|
button.setText(patterns[i]);
|
144 |
e108b57e
|
Leszek Koltunski
|
|
145 |
|
|
button.setOnClickListener( new View.OnClickListener()
|
146 |
|
|
{
|
147 |
|
|
@Override
|
148 |
|
|
public void onClick(View view)
|
149 |
|
|
{
|
150 |
3a9d19ed
|
Leszek Koltunski
|
RubikStatePattern state = (RubikStatePattern) RubikState.PATT.getStateClass();
|
151 |
|
|
state.setPatternName(patterns[ii]);
|
152 |
e108b57e
|
Leszek Koltunski
|
mDialog.dismiss();
|
153 |
|
|
}
|
154 |
|
|
});
|
155 |
|
|
|
156 |
|
|
mLayout.addView(button);
|
157 |
|
|
}
|
158 |
|
|
}
|
159 |
3a9d19ed
|
Leszek Koltunski
|
|
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 |
e108b57e
|
Leszek Koltunski
|
}
|