Revision 53f23b64
Added by Leszek Koltunski about 4 years ago
src/main/java/org/distorted/uistate/RubikStatePattern.java | ||
---|---|---|
19 | 19 |
|
20 | 20 |
package org.distorted.uistate; |
21 | 21 |
|
22 |
import android.content.Context; |
|
22 | 23 |
import android.content.SharedPreferences; |
23 |
import android.support.v4.app.FragmentManager;
|
|
24 |
import android.graphics.drawable.BitmapDrawable;
|
|
24 | 25 |
import android.util.DisplayMetrics; |
26 |
import android.view.Gravity; |
|
25 | 27 |
import android.view.LayoutInflater; |
26 | 28 |
import android.view.View; |
27 | 29 |
import android.widget.Button; |
30 |
import android.widget.ImageButton; |
|
28 | 31 |
import android.widget.LinearLayout; |
32 |
import android.widget.PopupWindow; |
|
29 | 33 |
import android.widget.TextView; |
30 | 34 |
|
31 |
import org.distorted.dialog.RubikDialogPattern; |
|
32 | 35 |
import org.distorted.magic.R; |
33 | 36 |
import org.distorted.magic.RubikActivity; |
37 |
import org.distorted.object.RubikObjectList; |
|
38 |
import org.distorted.patterns.RubikPattern; |
|
34 | 39 |
|
35 | 40 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
36 | 41 |
|
37 | 42 |
public class RubikStatePattern extends RubikStateAbstract |
38 | 43 |
{ |
39 |
private TextView mText; |
|
44 |
private ImageButton mObjButton; |
|
45 |
private Button mBackButton; |
|
46 |
private PopupWindow mPopup; |
|
47 |
private int mLayoutWidth, mLayoutHeight; |
|
48 |
private LinearLayout mLayout; |
|
49 |
private int mSize; |
|
40 | 50 |
|
41 | 51 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
42 | 52 |
|
... | ... | |
49 | 59 |
|
50 | 60 |
void leaveState(RubikActivity act) |
51 | 61 |
{ |
52 |
act.setCanRotate(true); |
|
62 |
RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass(); |
|
63 |
|
|
64 |
if( !play.setObjectAndSize(RubikObjectList.CUBE, mSize) ) |
|
65 |
{ |
|
66 |
int object= play.getObject(); |
|
67 |
int size = play.getSize(); |
|
68 |
|
|
69 |
act.changeObject(RubikObjectList.getObject(object),size); |
|
70 |
} |
|
53 | 71 |
} |
54 | 72 |
|
55 | 73 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
56 | 74 |
|
57 | 75 |
void enterState(final RubikActivity act) |
58 | 76 |
{ |
59 |
act.setCanRotate(false); |
|
77 |
RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass(); |
|
78 |
int obj = play.getObject(); |
|
79 |
int size = play.getSize(); |
|
80 |
|
|
81 |
if( size>=RubikPattern.MIN_CUBE && size<=RubikPattern.MAX_CUBE && obj==RubikObjectList.CUBE.ordinal() ) |
|
82 |
{ |
|
83 |
mSize = size; |
|
84 |
} |
|
85 |
else |
|
86 |
{ |
|
87 |
mSize = RubikStatePlay.DEF_SIZE; |
|
88 |
act.changeObject(RubikObjectList.CUBE,mSize); |
|
89 |
} |
|
60 | 90 |
|
61 | 91 |
LayoutInflater inflater = act.getLayoutInflater(); |
62 | 92 |
|
63 | 93 |
// TOP //////////////////////////// |
64 | 94 |
LinearLayout layoutTop = act.findViewById(R.id.mainTitle); |
65 | 95 |
layoutTop.removeAllViews(); |
66 |
mText = (TextView)inflater.inflate(R.layout.upper_text, null);
|
|
67 |
mText.setText(R.string.patterns);
|
|
68 |
layoutTop.addView(mText);
|
|
96 |
TextView text = (TextView)inflater.inflate(R.layout.upper_text, null);
|
|
97 |
text.setText(R.string.patterns);
|
|
98 |
layoutTop.addView(text);
|
|
69 | 99 |
|
70 | 100 |
// BOT //////////////////////////// |
101 |
DisplayMetrics metrics = act.getResources().getDisplayMetrics(); |
|
102 |
final float scale = metrics.density; |
|
103 |
|
|
104 |
if( mObjButton==null ) setupObjectButton(act,scale); |
|
105 |
|
|
71 | 106 |
LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft); |
72 | 107 |
layoutLeft.removeAllViews(); |
108 |
layoutLeft.addView(mObjButton); |
|
109 |
|
|
110 |
if( mBackButton==null ) setupBackButton(act,scale); |
|
111 |
|
|
73 | 112 |
LinearLayout layoutRight = act.findViewById(R.id.mainBarRight); |
74 | 113 |
layoutRight.removeAllViews(); |
114 |
layoutRight.addView(mBackButton); |
|
75 | 115 |
|
76 |
DisplayMetrics metrics = act.getResources().getDisplayMetrics(); |
|
77 |
float scale = metrics.density; |
|
78 |
int padding = (int)(5*scale + 0.5f); |
|
79 |
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT); |
|
116 |
if( mPopup==null ) setupPopupWindow(act, scale); |
|
117 |
} |
|
80 | 118 |
|
81 |
Button button = new Button(act); |
|
82 |
button.setLayoutParams(params); |
|
83 |
button.setId(BUTTON_ID_BACK); |
|
84 |
button.setPadding(padding,0,padding,0); |
|
85 |
button.setText(R.string.back); |
|
119 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
86 | 120 |
|
87 |
button.setOnClickListener( new View.OnClickListener() |
|
121 |
private void setupObjectButton(final RubikActivity act, final float scale) |
|
122 |
{ |
|
123 |
int padding = (int)(3*scale + 0.5f); |
|
124 |
LinearLayout.LayoutParams objectParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT); |
|
125 |
mObjButton = new ImageButton(act); |
|
126 |
mObjButton.setLayoutParams(objectParams); |
|
127 |
mObjButton.setPadding(padding,0,padding,0); |
|
128 |
mObjButton.setImageResource(R.drawable.cube_menu); |
|
129 |
|
|
130 |
mObjButton.setOnClickListener( new View.OnClickListener() |
|
131 |
{ |
|
132 |
@Override |
|
133 |
public void onClick(View view) |
|
134 |
{ |
|
135 |
int total = RubikPattern.NUM_CUBES; |
|
136 |
boolean vertical = act.isVertical(); |
|
137 |
mLayout.setOrientation(vertical ? LinearLayout.VERTICAL:LinearLayout.HORIZONTAL); |
|
138 |
|
|
139 |
int height = view.getHeight(); |
|
140 |
int width = view.getWidth(); |
|
141 |
int laywid = mLayoutWidth * (vertical? 1:total); |
|
142 |
int layhei = mLayoutHeight* (vertical? total:1); |
|
143 |
|
|
144 |
mPopup.showAsDropDown(view, (width-laywid)/2, -height-layhei, Gravity.LEFT); |
|
145 |
} |
|
146 |
}); |
|
147 |
} |
|
148 |
|
|
149 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
150 |
|
|
151 |
private void setupBackButton(final RubikActivity act, final float scale) |
|
152 |
{ |
|
153 |
int padding = (int)(3*scale + 0.5f); |
|
154 |
LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT); |
|
155 |
mBackButton = new Button(act); |
|
156 |
mBackButton.setLayoutParams(backParams); |
|
157 |
mBackButton.setId(BUTTON_ID_BACK); |
|
158 |
mBackButton.setPadding(padding,0,padding,0); |
|
159 |
mBackButton.setText(R.string.back); |
|
160 |
|
|
161 |
mBackButton.setOnClickListener( new View.OnClickListener() |
|
88 | 162 |
{ |
89 | 163 |
@Override |
90 | 164 |
public void onClick(View v) |
91 | 165 |
{ |
92 | 166 |
RubikState.goBack(act); |
93 |
|
|
94 |
FragmentManager mana = act.getSupportFragmentManager(); |
|
95 |
RubikDialogPattern diag = new RubikDialogPattern(); |
|
96 |
diag.show( mana, RubikDialogPattern.getDialogTag() ); |
|
97 | 167 |
} |
98 | 168 |
}); |
169 |
} |
|
170 |
|
|
171 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
172 |
|
|
173 |
private void setupPopupWindow(final RubikActivity act, final float scale) |
|
174 |
{ |
|
175 |
LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
|
176 |
final View layout = layoutInflater.inflate(R.layout.popup_objects, null); |
|
177 |
mLayout = layout.findViewById(R.id.popup); |
|
99 | 178 |
|
100 |
layoutRight.addView(button); |
|
179 |
mPopup = new PopupWindow(act); |
|
180 |
mPopup.setContentView(layout); |
|
181 |
mPopup.setFocusable(true); |
|
182 |
int margin = (int)(5*scale + 0.5f); |
|
183 |
|
|
184 |
BitmapDrawable bd = (BitmapDrawable) act.getResources().getDrawable(R.drawable.cube2); |
|
185 |
int cubeWidth = bd.getIntrinsicWidth(); |
|
186 |
int cubeHeight = bd.getIntrinsicHeight(); |
|
187 |
|
|
188 |
mLayoutWidth = (int)(cubeWidth + 2*margin + 0.5f); |
|
189 |
mLayoutHeight= (int)(cubeHeight+ 2*margin + 0.5f); |
|
190 |
|
|
191 |
final int[] icons = {R.drawable.cube2, R.drawable.cube3, R.drawable.cube4, R.drawable.cube5 }; |
|
192 |
final int[] sizes = {2,3,4,5}; |
|
193 |
int len = icons.length; |
|
194 |
|
|
195 |
for(int i=0; i<len; i++) |
|
196 |
{ |
|
197 |
final int size = i; |
|
198 |
|
|
199 |
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT); |
|
200 |
p.setMargins(margin, margin, margin, margin); |
|
201 |
|
|
202 |
ImageButton button = new ImageButton(act); |
|
203 |
button.setLayoutParams(p); |
|
204 |
|
|
205 |
button.setBackgroundResource(icons[i]); |
|
206 |
button.setOnClickListener( new View.OnClickListener() |
|
207 |
{ |
|
208 |
@Override |
|
209 |
public void onClick(View v) |
|
210 |
{ |
|
211 |
mSize = sizes[size]; |
|
212 |
act.changeObject(RubikObjectList.CUBE,sizes[size]); |
|
213 |
mPopup.dismiss(); |
|
214 |
} |
|
215 |
}); |
|
216 |
|
|
217 |
mLayout.addView(button); |
|
218 |
} |
|
101 | 219 |
} |
102 | 220 |
|
103 | 221 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
104 | 222 |
|
105 | 223 |
public void savePreferences(SharedPreferences.Editor editor) |
106 | 224 |
{ |
225 |
mObjButton = null; |
|
226 |
mBackButton= null; |
|
107 | 227 |
|
228 |
if( mPopup!=null ) |
|
229 |
{ |
|
230 |
mPopup.dismiss(); |
|
231 |
mPopup = null; |
|
232 |
} |
|
108 | 233 |
} |
109 | 234 |
|
110 | 235 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
Also available in: Unified diff
Progress with Pretty Patterns - do away with the tabbed dialog!