Revision e108b57e
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; |
|
23 | 22 |
import android.content.SharedPreferences; |
24 |
import android.graphics.drawable.BitmapDrawable; |
|
23 |
import android.os.Bundle; |
|
24 |
import android.support.v4.app.FragmentManager; |
|
25 | 25 |
import android.util.DisplayMetrics; |
26 |
import android.view.Gravity; |
|
27 | 26 |
import android.view.LayoutInflater; |
28 | 27 |
import android.view.View; |
29 | 28 |
import android.widget.Button; |
30 |
import android.widget.ImageButton; |
|
31 | 29 |
import android.widget.LinearLayout; |
32 |
import android.widget.PopupWindow; |
|
33 | 30 |
import android.widget.TextView; |
34 | 31 |
|
32 |
import org.distorted.dialog.RubikDialogPattern; |
|
35 | 33 |
import org.distorted.magic.R; |
36 | 34 |
import org.distorted.magic.RubikActivity; |
37 | 35 |
import org.distorted.object.RubikObjectList; |
... | ... | |
41 | 39 |
|
42 | 40 |
public class RubikStatePattern extends RubikStateAbstract |
43 | 41 |
{ |
44 |
private ImageButton mObjButton; |
|
45 | 42 |
private Button mBackButton; |
46 |
private PopupWindow mPopup; |
|
47 |
private int mLayoutWidth, mLayoutHeight; |
|
48 |
private LinearLayout mLayout; |
|
49 | 43 |
private int mSize; |
50 | 44 |
|
51 | 45 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
88 | 82 |
act.changeObject(RubikObjectList.CUBE,mSize); |
89 | 83 |
} |
90 | 84 |
|
85 |
FragmentManager mana = act.getSupportFragmentManager(); |
|
86 |
RubikDialogPattern diag = (RubikDialogPattern) mana.findFragmentByTag(RubikDialogPattern.getDialogTag()); |
|
87 |
|
|
88 |
if( diag==null ) showDialog(mana); |
|
89 |
|
|
91 | 90 |
LayoutInflater inflater = act.getLayoutInflater(); |
92 | 91 |
|
93 | 92 |
// TOP //////////////////////////// |
... | ... | |
101 | 100 |
DisplayMetrics metrics = act.getResources().getDisplayMetrics(); |
102 | 101 |
final float scale = metrics.density; |
103 | 102 |
|
104 |
if( mObjButton==null ) setupObjectButton(act,scale); |
|
105 |
|
|
106 |
LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft); |
|
107 |
layoutLeft.removeAllViews(); |
|
108 |
layoutLeft.addView(mObjButton); |
|
109 |
|
|
110 | 103 |
if( mBackButton==null ) setupBackButton(act,scale); |
111 | 104 |
|
112 | 105 |
LinearLayout layoutRight = act.findViewById(R.id.mainBarRight); |
113 | 106 |
layoutRight.removeAllViews(); |
114 | 107 |
layoutRight.addView(mBackButton); |
115 |
|
|
116 |
if( mPopup==null ) setupPopupWindow(act, scale); |
|
117 | 108 |
} |
118 | 109 |
|
119 | 110 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
120 | 111 |
|
121 |
private void setupObjectButton(final RubikActivity act, final float scale)
|
|
112 |
private void showDialog(FragmentManager manager)
|
|
122 | 113 |
{ |
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 |
}); |
|
114 |
Bundle bundle = new Bundle(); |
|
115 |
int object = RubikObjectList.CUBE.ordinal(); |
|
116 |
int sizeIndex = RubikObjectList.getSizeIndex(object,mSize); |
|
117 |
bundle.putInt("tab", RubikObjectList.pack(object,sizeIndex) ); |
|
118 |
|
|
119 |
RubikDialogPattern diag = new RubikDialogPattern(); |
|
120 |
diag.setArguments(bundle); |
|
121 |
diag.show( manager, RubikDialogPattern.getDialogTag() ); |
|
147 | 122 |
} |
148 | 123 |
|
149 | 124 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
163 | 138 |
@Override |
164 | 139 |
public void onClick(View v) |
165 | 140 |
{ |
166 |
RubikState.goBack(act); |
|
167 |
} |
|
168 |
}); |
|
169 |
} |
|
141 |
FragmentManager mana = act.getSupportFragmentManager(); |
|
142 |
RubikDialogPattern diag = (RubikDialogPattern) mana.findFragmentByTag(RubikDialogPattern.getDialogTag()); |
|
170 | 143 |
|
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); |
|
178 |
|
|
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) |
|
144 |
if( diag==null ) |
|
210 | 145 |
{ |
211 |
mSize = sizes[size]; |
|
212 |
act.changeObject(RubikObjectList.CUBE,sizes[size]); |
|
213 |
mPopup.dismiss(); |
|
146 |
showDialog(mana); |
|
214 | 147 |
} |
215 |
}); |
|
216 |
|
|
217 |
mLayout.addView(button); |
|
218 |
} |
|
148 |
else |
|
149 |
{ |
|
150 |
diag.dismiss(); |
|
151 |
RubikState.goBack(act); |
|
152 |
} |
|
153 |
} |
|
154 |
}); |
|
219 | 155 |
} |
220 | 156 |
|
221 | 157 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
222 | 158 |
|
223 | 159 |
public void savePreferences(SharedPreferences.Editor editor) |
224 | 160 |
{ |
225 |
mObjButton = null; |
|
226 | 161 |
mBackButton= null; |
227 |
|
|
228 |
if( mPopup!=null ) |
|
229 |
{ |
|
230 |
mPopup.dismiss(); |
|
231 |
mPopup = null; |
|
232 |
} |
|
233 | 162 |
} |
234 | 163 |
|
235 | 164 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
Also available in: Unified diff
Re-add the Pattern dialog, but attach it to the Pattern uistate this time.
The idea to do away with the dialog proves impossible - we'd have to resize the top bar, which causes very unpleasant flashes.