Revision 4debbf44
Added by Leszek Koltunski over 4 years ago
src/main/java/org/distorted/dialogs/RubikDialogPattern.java | ||
---|---|---|
86 | 86 |
final View view = inflater.inflate(R.layout.dialog_tabbed, null); |
87 | 87 |
builder.setView(view); |
88 | 88 |
|
89 |
// TabLayout tl = view.findViewById(R.id.sliding_tabs); |
|
90 |
|
|
91 |
|
|
92 | 89 |
ViewPager viewPager = view.findViewById(R.id.viewpager); |
93 | 90 |
TabLayout tabLayout = view.findViewById(R.id.sliding_tabs); |
94 | 91 |
mPagerAdapter = new RubikDialogPatternPagerAdapter(act, viewPager, this); |
src/main/java/org/distorted/dialogs/RubikDialogPatternListAdapter.java | ||
---|---|---|
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 android.view.LayoutInflater; |
|
24 |
import android.view.View; |
|
25 |
import android.view.ViewGroup; |
|
26 |
import android.widget.BaseExpandableListAdapter; |
|
27 |
import android.widget.TextView; |
|
28 |
|
|
29 |
import org.distorted.patterns.RubikPattern; |
|
30 |
import org.distorted.main.R; |
|
31 |
|
|
32 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
33 |
|
|
34 |
class RubikDialogPatternListAdapter extends BaseExpandableListAdapter |
|
35 |
{ |
|
36 |
private Context mContext; |
|
37 |
private int mTab; |
|
38 |
|
|
39 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
40 |
|
|
41 |
public RubikDialogPatternListAdapter(Context context, int tab) |
|
42 |
{ |
|
43 |
mContext = context; |
|
44 |
mTab = tab; |
|
45 |
} |
|
46 |
|
|
47 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
48 |
|
|
49 |
@Override |
|
50 |
public Object getChild(int groupPosition, int childPosition) |
|
51 |
{ |
|
52 |
RubikPattern pattern = RubikPattern.getInstance(); |
|
53 |
return pattern.getPatternName(mTab,groupPosition,childPosition); |
|
54 |
} |
|
55 |
|
|
56 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
57 |
|
|
58 |
@Override |
|
59 |
public long getChildId(int groupPosition, int childPosition) |
|
60 |
{ |
|
61 |
return childPosition; |
|
62 |
} |
|
63 |
|
|
64 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
65 |
|
|
66 |
@Override |
|
67 |
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) |
|
68 |
{ |
|
69 |
String childName = (String) getChild(groupPosition, childPosition); |
|
70 |
|
|
71 |
if (view == null) |
|
72 |
{ |
|
73 |
LayoutInflater infalInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
|
74 |
view = infalInflater.inflate(R.layout.dialog_pattern_child_item, null); |
|
75 |
} |
|
76 |
|
|
77 |
TextView sequence = view.findViewById(R.id.sequence); |
|
78 |
sequence.setText(mContext.getString(R.string.sq_placeholder,childPosition+1)); |
|
79 |
TextView childItem = view.findViewById(R.id.childItem); |
|
80 |
childItem.setText(childName); |
|
81 |
|
|
82 |
return view; |
|
83 |
} |
|
84 |
|
|
85 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
86 |
|
|
87 |
@Override |
|
88 |
public int getChildrenCount(int groupPosition) |
|
89 |
{ |
|
90 |
RubikPattern pattern = RubikPattern.getInstance(); |
|
91 |
return pattern.getNumPatterns(mTab,groupPosition); |
|
92 |
} |
|
93 |
|
|
94 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
95 |
|
|
96 |
@Override |
|
97 |
public Object getGroup(int groupPosition) |
|
98 |
{ |
|
99 |
RubikPattern pattern = RubikPattern.getInstance(); |
|
100 |
return pattern.getCategoryName(mTab,groupPosition); |
|
101 |
} |
|
102 |
|
|
103 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
104 |
|
|
105 |
@Override |
|
106 |
public int getGroupCount() |
|
107 |
{ |
|
108 |
RubikPattern pattern = RubikPattern.getInstance(); |
|
109 |
return pattern.getNumCategories(mTab); |
|
110 |
} |
|
111 |
|
|
112 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
113 |
|
|
114 |
@Override |
|
115 |
public long getGroupId(int groupPosition) |
|
116 |
{ |
|
117 |
return groupPosition; |
|
118 |
} |
|
119 |
|
|
120 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
121 |
|
|
122 |
@Override |
|
123 |
public View getGroupView(int groupPosition, boolean isLastChild, View view, ViewGroup parent) |
|
124 |
{ |
|
125 |
String groupName = (String) getGroup(groupPosition); |
|
126 |
|
|
127 |
if (view == null) |
|
128 |
{ |
|
129 |
LayoutInflater inf = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
|
130 |
view = inf.inflate(R.layout.dialog_pattern_group_item, null); |
|
131 |
} |
|
132 |
|
|
133 |
TextView heading = view.findViewById(R.id.heading); |
|
134 |
heading.setText(groupName); |
|
135 |
|
|
136 |
return view; |
|
137 |
} |
|
138 |
|
|
139 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
140 |
|
|
141 |
@Override |
|
142 |
public boolean hasStableIds() |
|
143 |
{ |
|
144 |
return true; |
|
145 |
} |
|
146 |
|
|
147 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
148 |
|
|
149 |
@Override |
|
150 |
public boolean isChildSelectable(int groupPosition, int childPosition) |
|
151 |
{ |
|
152 |
return true; |
|
153 |
} |
|
154 |
} |
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 |
} |
src/main/java/org/distorted/states/RubikStatePlay.java | ||
---|---|---|
24 | 24 |
import android.graphics.drawable.BitmapDrawable; |
25 | 25 |
import android.os.Build; |
26 | 26 |
import android.os.Bundle; |
27 |
import android.util.DisplayMetrics; |
|
28 | 27 |
import android.util.TypedValue; |
29 | 28 |
import android.view.Gravity; |
30 | 29 |
import android.view.LayoutInflater; |
src/main/res/layout/dialog_pattern_child_item.xml | ||
---|---|---|
1 |
<?xml version="1.0" encoding="utf-8"?> |
|
2 |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" |
|
3 |
android:layout_width="match_parent" |
|
4 |
android:layout_height="match_parent" |
|
5 |
android:orientation="vertical"> |
|
6 |
|
|
7 |
<TextView |
|
8 |
android:id="@+id/sequence" |
|
9 |
android:layout_width="wrap_content" |
|
10 |
android:layout_height="wrap_content" |
|
11 |
android:layout_alignParentLeft="true" |
|
12 |
android:layout_alignParentTop="true" |
|
13 |
android:paddingLeft="35sp" |
|
14 |
/> |
|
15 |
|
|
16 |
<TextView |
|
17 |
android:id="@+id/childItem" |
|
18 |
android:layout_width="wrap_content" |
|
19 |
android:layout_height="wrap_content" |
|
20 |
android:layout_alignParentTop="true" |
|
21 |
android:layout_toRightOf="@id/sequence" |
|
22 |
/> |
|
23 |
|
|
24 |
</RelativeLayout> |
src/main/res/layout/dialog_pattern_group_item.xml | ||
---|---|---|
1 |
<?xml version="1.0" encoding="utf-8"?> |
|
2 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
|
3 |
android:layout_width="fill_parent" |
|
4 |
android:layout_height="match_parent" |
|
5 |
android:orientation="vertical" > |
|
6 |
|
|
7 |
<TextView |
|
8 |
android:id="@+id/heading" |
|
9 |
android:layout_width="wrap_content" |
|
10 |
android:layout_height="wrap_content" |
|
11 |
android:paddingLeft="35sp" |
|
12 |
android:textStyle="bold" /> |
|
13 |
|
|
14 |
</LinearLayout> |
src/main/res/layout/dialog_pattern_tab.xml | ||
---|---|---|
1 | 1 |
<?xml version="1.0" encoding="utf-8"?> |
2 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
|
3 |
android:layout_width="match_parent" |
|
4 |
android:layout_height="wrap_content" |
|
5 |
android:background="@color/grey" |
|
6 |
android:orientation="vertical" > |
|
7 |
|
|
8 |
<Spinner |
|
9 |
android:id="@+id/pattern_category_spinner" |
|
10 |
android:background="@drawable/ui_small_spinner" |
|
11 |
android:layout_marginLeft="20dp" |
|
12 |
android:layout_marginRight="20dp" |
|
13 |
android:layout_marginTop="10dp" |
|
14 |
android:layout_marginBottom="10dp" |
|
15 |
android:layout_width="match_parent" |
|
16 |
android:layout_height="50dp"/> |
|
17 |
|
|
18 |
<ScrollView |
|
19 |
android:id="@+id/tabScrollView" |
|
20 |
android:paddingBottom="10dp" |
|
21 |
android:background="@color/grey" |
|
22 |
android:layout_width="match_parent" |
|
23 |
android:layout_height="match_parent"> |
|
24 |
|
|
25 |
<LinearLayout |
|
26 |
android:id="@+id/tabLayout" |
|
27 |
android:layout_width="match_parent" |
|
28 |
android:layout_height="wrap_content" |
|
29 |
android:orientation="vertical"> |
|
30 |
</LinearLayout> |
|
31 |
</ScrollView> |
|
32 |
|
|
33 |
</LinearLayout> |
|
2 |
<ExpandableListView xmlns:android="http://schemas.android.com/apk/res/android" |
|
3 |
android:id="@+id/patternListView" |
|
4 |
android:background="@color/grey" |
|
5 |
android:paddingLeft="10dp" |
|
6 |
android:paddingRight="10dp" |
|
7 |
android:paddingTop="10dp" |
|
8 |
android:paddingBottom="10dp" |
|
9 |
android:layout_width="fill_parent" |
|
10 |
android:layout_height="fill_parent" |
|
11 |
/> |
src/main/res/values/strings.xml | ||
---|---|---|
1 | 1 |
<resources> |
2 | 2 |
<string name="app_name">Magic Cube</string> |
3 |
<string name="distorted">DISTORTED</string> |
|
4 |
<string name="opengl_error">Graphics Error</string> |
|
3 |
<string name="distorted" translatable="false">DISTORTED</string>
|
|
4 |
<string name="opengl_error" translatable="false">Graphics Error</string>
|
|
5 | 5 |
<string name="scramble">Scramble</string> |
6 | 6 |
<string name="solve">Solve</string> |
7 | 7 |
<string name="exit">Exit</string> |
... | ... | |
58 | 58 |
<string name="ap_placeholder">%1$s %2$s</string> |
59 | 59 |
<string name="ti_placeholder">%1$.1f seconds</string> |
60 | 60 |
<string name="mo_placeholder">%1$d/%2$d</string> |
61 |
<string name="sq_placeholder" translatable="false">%1$2d.</string> |
|
61 | 62 |
</resources> |
Also available in: Unified diff
Reinvent the Pattern Dialog (Part 1)