Revision 4debbf44
Added by Leszek Koltunski almost 4 years ago
src/main/java/org/distorted/dialogs/RubikDialogPatternView.java | ||
---|---|---|
22 | 22 |
import android.content.Context; |
23 | 23 |
import androidx.fragment.app.FragmentActivity; |
24 | 24 |
import android.util.AttributeSet; |
25 |
import android.util.DisplayMetrics; |
|
26 | 25 |
import android.view.View; |
27 |
import android.widget.AdapterView; |
|
28 |
import android.widget.ArrayAdapter; |
|
29 |
import android.widget.Button; |
|
26 |
import android.widget.ExpandableListView; |
|
30 | 27 |
import android.widget.FrameLayout; |
31 |
import android.widget.LinearLayout; |
|
32 |
import android.widget.ScrollView; |
|
33 |
import android.widget.Spinner; |
|
34 | 28 |
|
35 | 29 |
import org.distorted.main.R; |
36 | 30 |
import org.distorted.main.RubikActivity; |
... | ... | |
42 | 36 |
|
43 | 37 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
44 | 38 |
|
45 |
public class RubikDialogPatternView extends FrameLayout implements AdapterView.OnItemSelectedListener
|
|
39 |
public class RubikDialogPatternView extends FrameLayout |
|
46 | 40 |
{ |
47 |
private LinearLayout mLayout;
|
|
48 |
private ScrollView mScroll;
|
|
41 |
private ExpandableListView mListView;
|
|
42 |
private RubikDialogPatternListAdapter mListAdapter;
|
|
49 | 43 |
private RubikDialogPattern mDialog; |
50 | 44 |
private int mTab, mPos; |
45 |
private int mExpandedGroup; |
|
51 | 46 |
|
52 | 47 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
53 | 48 |
|
... | ... | |
69 | 64 |
{ |
70 | 65 |
super(act); |
71 | 66 |
|
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)); |
|
67 |
final RubikActivity ract = (RubikActivity)getContext(); |
|
89 | 68 |
|
90 |
addView(tab); |
|
91 |
} |
|
92 |
|
|
93 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
69 |
mTab = position; |
|
70 |
mDialog = dialog; |
|
71 |
mExpandedGroup = -1; |
|
94 | 72 |
|
95 |
private String[] createCategories() |
|
96 |
{ |
|
97 |
RubikPattern pattern = RubikPattern.getInstance(); |
|
98 |
int numCat = pattern.getNumCategories(mTab); |
|
73 |
View tab = inflate( act, R.layout.dialog_pattern_tab, null); |
|
99 | 74 |
|
100 |
String[] ret = new String[numCat]; |
|
75 |
mListView = tab.findViewById(R.id.patternListView); |
|
76 |
mListAdapter = new RubikDialogPatternListAdapter(act,mTab); |
|
77 |
mListView.setAdapter(mListAdapter); |
|
101 | 78 |
|
102 |
for(int i=0; i<numCat; i++)
|
|
79 |
mListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener()
|
|
103 | 80 |
{ |
104 |
ret[i] = pattern.getCategoryName(mTab,i); |
|
105 |
} |
|
81 |
@Override |
|
82 |
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) |
|
83 |
{ |
|
84 |
RubikPattern pattern = RubikPattern.getInstance(); |
|
85 |
int[][] moves = pattern.reInitialize(mTab, groupPosition, childPosition); |
|
106 | 86 |
|
107 |
return ret;
|
|
108 |
}
|
|
87 |
RubikObjectList list = RubikPatternList.getObject(mTab);
|
|
88 |
int size = RubikPatternList.getSize(mTab);
|
|
109 | 89 |
|
110 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
90 |
ract.setupObject(list, size, moves);
|
|
111 | 91 |
|
112 |
private String[] createPatterns(int category) |
|
113 |
{ |
|
114 |
RubikPattern pattern = RubikPattern.getInstance(); |
|
115 |
int numPat = pattern.getNumPatterns(mTab, category); |
|
92 |
RubikStatePattern state = (RubikStatePattern) RubikState.PATT.getStateClass(); |
|
116 | 93 |
|
117 |
String[] ret = new String[numPat];
|
|
94 |
state.setPattern(ract, mTab, groupPosition, childPosition);
|
|
118 | 95 |
|
119 |
for(int i=0; i<numPat; i++) |
|
120 |
{ |
|
121 |
ret[i] = pattern.getPatternName(mTab, category, i); |
|
122 |
} |
|
123 |
|
|
124 |
return ret; |
|
125 |
} |
|
96 |
mDialog.rememberState(); |
|
97 |
mDialog.dismiss(); |
|
126 | 98 |
|
127 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
128 |
|
|
129 |
private void fillPatterns(final int category) |
|
130 |
{ |
|
131 |
final RubikActivity act = (RubikActivity)getContext(); |
|
99 |
return false; |
|
100 |
} |
|
101 |
}); |
|
132 | 102 |
|
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++) |
|
103 |
mListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() |
|
146 | 104 |
{ |
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() |
|
105 |
@Override |
|
106 |
public void onGroupExpand(int groupPosition) |
|
153 | 107 |
{ |
154 |
@Override |
|
155 |
public void onClick(View view) |
|
108 |
if(mExpandedGroup!=-1 && groupPosition!=mExpandedGroup) |
|
156 | 109 |
{ |
157 |
int[][] moves = pattern.reInitialize(mTab, category, ii); |
|
158 |
|
|
159 |
RubikObjectList list = RubikPatternList.getObject(mTab); |
|
160 |
int size = RubikPatternList.getSize(mTab); |
|
161 |
|
|
162 |
act.setupObject( list, size, moves); |
|
163 |
|
|
164 |
RubikStatePattern state = (RubikStatePattern) RubikState.PATT.getStateClass(); |
|
165 |
state.setPattern(act, mTab, category, ii); |
|
166 |
mDialog.rememberState(); |
|
167 |
mDialog.dismiss(); |
|
110 |
mListView.collapseGroup(mExpandedGroup); |
|
168 | 111 |
} |
169 |
}); |
|
170 | 112 |
|
171 |
mLayout.addView(button); |
|
172 |
} |
|
113 |
mExpandedGroup = groupPosition; |
|
114 |
} |
|
115 |
}); |
|
116 |
|
|
117 |
addView(tab); |
|
173 | 118 |
} |
174 | 119 |
|
175 | 120 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
183 | 128 |
|
184 | 129 |
int getCurrentScrollPos() |
185 | 130 |
{ |
186 |
return mScroll.getScrollY(); |
|
131 |
return 0;//mScroll.getScrollY();
|
|
187 | 132 |
} |
188 | 133 |
|
189 | 134 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
192 | 137 |
protected void onLayout(boolean changed, int left, int top, int right, int bottom) |
193 | 138 |
{ |
194 | 139 |
super.onLayout(changed,left,top,right,bottom); |
195 |
|
|
140 |
/* |
|
196 | 141 |
if( !changed ) |
197 | 142 |
{ |
198 | 143 |
final RubikPattern pattern = RubikPattern.getInstance(); |
199 | 144 |
mScroll.setScrollY( pattern.recallScrollPos(mTab) ); |
200 | 145 |
} |
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 |
|
|
146 |
*/ |
|
221 | 147 |
} |
222 | 148 |
} |
Also available in: Unified diff
Reinvent the Pattern Dialog (Part 1)