1 |
c7238c67
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
// Copyright 2020 Leszek Koltunski //
|
3 |
|
|
// //
|
4 |
|
|
// This file is part of Magic Cube. //
|
5 |
|
|
// //
|
6 |
|
|
// Magic Cube is proprietary software licensed under an EULA which you should have received //
|
7 |
|
|
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html //
|
8 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
9 |
|
|
|
10 |
|
|
package org.distorted.purchase;
|
11 |
|
|
|
12 |
600b1343
|
Leszek Koltunski
|
import android.graphics.drawable.Drawable;
|
13 |
c7238c67
|
Leszek Koltunski
|
import android.util.TypedValue;
|
14 |
7480fbab
|
Leszek Koltunski
|
import android.view.View;
|
15 |
4ac0ea40
|
Leszek Koltunski
|
import android.widget.ImageButton;
|
16 |
c7238c67
|
Leszek Koltunski
|
import android.widget.ImageView;
|
17 |
|
|
import android.widget.LinearLayout;
|
18 |
|
|
import android.widget.TextView;
|
19 |
|
|
|
20 |
600b1343
|
Leszek Koltunski
|
import androidx.core.content.res.ResourcesCompat;
|
21 |
|
|
|
22 |
4d6327a1
|
Leszek Koltunski
|
import org.distorted.dialogs.RubikDialogStarsStatus;
|
23 |
45831b78
|
Leszek Koltunski
|
import org.distorted.external.RubikScores;
|
24 |
f8e8a08e
|
Leszek Koltunski
|
import org.distorted.library.main.DistortedScreen;
|
25 |
c7238c67
|
Leszek Koltunski
|
import org.distorted.main.R;
|
26 |
|
|
import org.distorted.objectlib.json.JsonReader;
|
27 |
7654a99d
|
Leszek Koltunski
|
import org.distorted.objectlib.main.ObjectControl;
|
28 |
c7238c67
|
Leszek Koltunski
|
import org.distorted.objects.RubikObject;
|
29 |
|
|
import org.distorted.objects.RubikObjectList;
|
30 |
f8e8a08e
|
Leszek Koltunski
|
import org.distorted.overlays.DataStars;
|
31 |
7654a99d
|
Leszek Koltunski
|
import org.distorted.overlays.ListenerOverlay;
|
32 |
f8e8a08e
|
Leszek Koltunski
|
import org.distorted.overlays.OverlayStars;
|
33 |
c7238c67
|
Leszek Koltunski
|
|
34 |
|
|
import java.io.InputStream;
|
35 |
7654a99d
|
Leszek Koltunski
|
import java.lang.ref.WeakReference;
|
36 |
c7238c67
|
Leszek Koltunski
|
|
37 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
38 |
|
|
|
39 |
7654a99d
|
Leszek Koltunski
|
public class PurchaseScreenPane implements ListenerOverlay
|
40 |
c7238c67
|
Leszek Koltunski
|
{
|
41 |
9dfb553f
|
Leszek Koltunski
|
public static final int UNLOCK_ALL_PRICE = 600;
|
42 |
e9e744f7
|
Leszek Koltunski
|
|
43 |
c7238c67
|
Leszek Koltunski
|
private static final int[] IMAGES =
|
44 |
|
|
{
|
45 |
|
|
R.drawable.difficulty1,
|
46 |
|
|
R.drawable.difficulty2,
|
47 |
|
|
R.drawable.difficulty3,
|
48 |
|
|
R.drawable.difficulty4,
|
49 |
|
|
R.drawable.difficulty5,
|
50 |
|
|
};
|
51 |
|
|
|
52 |
71cda061
|
Leszek Koltunski
|
private static final int NUM_IMAGES = IMAGES.length;
|
53 |
9dfb553f
|
Leszek Koltunski
|
public static final float PADDING_RATIO = 0.017f;
|
54 |
|
|
private static final float TEXT_RATIO = 0.032f;
|
55 |
c7238c67
|
Leszek Koltunski
|
|
56 |
7654a99d
|
Leszek Koltunski
|
private final WeakReference<PurchaseActivity> mAct;
|
57 |
7480fbab
|
Leszek Koltunski
|
private RubikObject mObject;
|
58 |
|
|
|
59 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
60 |
|
|
|
61 |
45831b78
|
Leszek Koltunski
|
private boolean chargeUser(int amount)
|
62 |
7480fbab
|
Leszek Koltunski
|
{
|
63 |
45831b78
|
Leszek Koltunski
|
RubikScores scores = RubikScores.getInstance();
|
64 |
|
|
int numStars = scores.getNumStars();
|
65 |
|
|
|
66 |
|
|
if( numStars>=amount )
|
67 |
|
|
{
|
68 |
|
|
scores.changeNumStars(-amount);
|
69 |
|
|
return true;
|
70 |
|
|
}
|
71 |
|
|
|
72 |
|
|
return false;
|
73 |
7480fbab
|
Leszek Koltunski
|
}
|
74 |
|
|
|
75 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
76 |
|
|
|
77 |
6b034729
|
Leszek Koltunski
|
private void showStatus(PurchaseActivity act)
|
78 |
7480fbab
|
Leszek Koltunski
|
{
|
79 |
4d6327a1
|
Leszek Koltunski
|
RubikDialogStarsStatus d = new RubikDialogStarsStatus();
|
80 |
45831b78
|
Leszek Koltunski
|
d.show(act.getSupportFragmentManager(), null);
|
81 |
7480fbab
|
Leszek Koltunski
|
}
|
82 |
|
|
|
83 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
84 |
|
|
|
85 |
45831b78
|
Leszek Koltunski
|
private void showSuccess(PurchaseActivity act, RubikObject object)
|
86 |
|
|
{
|
87 |
7654a99d
|
Leszek Koltunski
|
act.blockUI();
|
88 |
f8e8a08e
|
Leszek Koltunski
|
RubikScores scores = RubikScores.getInstance();
|
89 |
|
|
int totStars = scores.getNumStars();
|
90 |
|
|
int price = object==null ? UNLOCK_ALL_PRICE:object.getPrice();
|
91 |
|
|
PurchaseRenderer renderer = act.getRenderer();
|
92 |
|
|
DistortedScreen screen = renderer.getScreen();
|
93 |
|
|
OverlayStars stars = new OverlayStars();
|
94 |
7654a99d
|
Leszek Koltunski
|
DataStars data = new DataStars(totStars+price,-price,act.getResources());
|
95 |
|
|
stars.startOverlay(screen,this,data);
|
96 |
45831b78
|
Leszek Koltunski
|
}
|
97 |
|
|
|
98 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
99 |
|
|
|
100 |
|
|
private void oneButtonClicked(PurchaseActivity act)
|
101 |
7480fbab
|
Leszek Koltunski
|
{
|
102 |
|
|
if( mObject!=null )
|
103 |
|
|
{
|
104 |
45831b78
|
Leszek Koltunski
|
int price = mObject.getPrice();
|
105 |
|
|
|
106 |
|
|
if( chargeUser(price) )
|
107 |
7480fbab
|
Leszek Koltunski
|
{
|
108 |
e9e744f7
|
Leszek Koltunski
|
RubikObjectList.buyObject(mObject);
|
109 |
45831b78
|
Leszek Koltunski
|
showSuccess(act,mObject);
|
110 |
|
|
}
|
111 |
|
|
else
|
112 |
|
|
{
|
113 |
6b034729
|
Leszek Koltunski
|
showStatus(act);
|
114 |
7480fbab
|
Leszek Koltunski
|
}
|
115 |
|
|
}
|
116 |
|
|
}
|
117 |
|
|
|
118 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
119 |
|
|
|
120 |
45831b78
|
Leszek Koltunski
|
private void allButtonClicked(PurchaseActivity act)
|
121 |
7480fbab
|
Leszek Koltunski
|
{
|
122 |
4d6327a1
|
Leszek Koltunski
|
if( chargeUser(UNLOCK_ALL_PRICE) )
|
123 |
7480fbab
|
Leszek Koltunski
|
{
|
124 |
|
|
RubikObjectList.buyAll();
|
125 |
45831b78
|
Leszek Koltunski
|
showSuccess(act,null);
|
126 |
|
|
}
|
127 |
|
|
else
|
128 |
|
|
{
|
129 |
6b034729
|
Leszek Koltunski
|
showStatus(act);
|
130 |
7480fbab
|
Leszek Koltunski
|
}
|
131 |
|
|
}
|
132 |
|
|
|
133 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
134 |
|
|
|
135 |
600b1343
|
Leszek Koltunski
|
private void setUpButtons(PurchaseActivity act, LinearLayout one, LinearLayout all)
|
136 |
7480fbab
|
Leszek Koltunski
|
{
|
137 |
4ac0ea40
|
Leszek Koltunski
|
ImageButton butO = one.findViewById(R.id.purchaseButtonOne);
|
138 |
|
|
ImageButton butA = all.findViewById(R.id.purchaseButtonAll);
|
139 |
7480fbab
|
Leszek Koltunski
|
|
140 |
600b1343
|
Leszek Koltunski
|
int id,price = act.getObjectPrice();
|
141 |
|
|
|
142 |
|
|
if( price<=10 ) id = R.drawable.price_10;
|
143 |
|
|
else if( price<=20 ) id = R.drawable.price_20;
|
144 |
|
|
else if( price<=30 ) id = R.drawable.price_30;
|
145 |
|
|
else if( price<=40 ) id = R.drawable.price_40;
|
146 |
|
|
else if( price<=50 ) id = R.drawable.price_50;
|
147 |
|
|
else if( price<=60 ) id = R.drawable.price_60;
|
148 |
|
|
else if( price<=70 ) id = R.drawable.price_70;
|
149 |
|
|
else if( price<=80 ) id = R.drawable.price_80;
|
150 |
|
|
else if( price<=90 ) id = R.drawable.price_90;
|
151 |
|
|
else id = R.drawable.price_100;
|
152 |
|
|
|
153 |
|
|
Drawable drawable = ResourcesCompat.getDrawable(act.getResources(), id, null);
|
154 |
|
|
butO.setImageDrawable(drawable);
|
155 |
|
|
|
156 |
7480fbab
|
Leszek Koltunski
|
butO.setOnClickListener( new View.OnClickListener()
|
157 |
|
|
{
|
158 |
|
|
@Override
|
159 |
|
|
public void onClick(View v)
|
160 |
|
|
{
|
161 |
45831b78
|
Leszek Koltunski
|
oneButtonClicked(act);
|
162 |
7480fbab
|
Leszek Koltunski
|
}
|
163 |
|
|
});
|
164 |
|
|
|
165 |
|
|
butA.setOnClickListener( new View.OnClickListener()
|
166 |
|
|
{
|
167 |
|
|
@Override
|
168 |
|
|
public void onClick(View v)
|
169 |
|
|
{
|
170 |
45831b78
|
Leszek Koltunski
|
allButtonClicked(act);
|
171 |
7480fbab
|
Leszek Koltunski
|
}
|
172 |
|
|
});
|
173 |
|
|
}
|
174 |
|
|
|
175 |
c7238c67
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
176 |
|
|
|
177 |
|
|
void updatePane(PurchaseActivity act, int objectOrdinal)
|
178 |
|
|
{
|
179 |
7480fbab
|
Leszek Koltunski
|
mObject = RubikObjectList.getObject(objectOrdinal);
|
180 |
c7238c67
|
Leszek Koltunski
|
|
181 |
7480fbab
|
Leszek Koltunski
|
if( mObject!=null )
|
182 |
c7238c67
|
Leszek Koltunski
|
{
|
183 |
7480fbab
|
Leszek Koltunski
|
InputStream stream = mObject.getObjectStream(act);
|
184 |
5305fdc8
|
Leszek Koltunski
|
JsonReader reader = new JsonReader();
|
185 |
cd432dd3
|
Leszek Koltunski
|
String author, name;
|
186 |
|
|
int year, difficulty;
|
187 |
c7238c67
|
Leszek Koltunski
|
|
188 |
|
|
try
|
189 |
|
|
{
|
190 |
|
|
reader.parseJsonFileMetadata(stream);
|
191 |
|
|
name = reader.getObjectName();
|
192 |
cd432dd3
|
Leszek Koltunski
|
author = reader.getInventor();
|
193 |
|
|
year = reader.getYearOfInvention();
|
194 |
c7238c67
|
Leszek Koltunski
|
difficulty = reader.getComplexity();
|
195 |
|
|
}
|
196 |
|
|
catch(Exception ex)
|
197 |
|
|
{
|
198 |
|
|
name = "?";
|
199 |
cd432dd3
|
Leszek Koltunski
|
author = "?";
|
200 |
|
|
year = 0;
|
201 |
c7238c67
|
Leszek Koltunski
|
difficulty = 0;
|
202 |
|
|
}
|
203 |
|
|
|
204 |
71cda061
|
Leszek Koltunski
|
if( difficulty<0 ) difficulty=0;
|
205 |
|
|
if( difficulty>=NUM_IMAGES ) difficulty=NUM_IMAGES-1;
|
206 |
c7238c67
|
Leszek Koltunski
|
|
207 |
cd432dd3
|
Leszek Koltunski
|
String both = year>0 ? author+" "+year : author;
|
208 |
|
|
|
209 |
|
|
TextView v1 = act.findViewById(R.id.purchaseUpperName);
|
210 |
|
|
v1.setText(name);
|
211 |
|
|
TextView v2 = act.findViewById(R.id.purchaseUpperAuthor);
|
212 |
|
|
v2.setText(both);
|
213 |
71cda061
|
Leszek Koltunski
|
ImageView image = act.findViewById(R.id.purchaseDifficulty);
|
214 |
|
|
image.setImageResource(IMAGES[difficulty]);
|
215 |
c7238c67
|
Leszek Koltunski
|
}
|
216 |
|
|
}
|
217 |
|
|
|
218 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
219 |
|
|
|
220 |
71cda061
|
Leszek Koltunski
|
PurchaseScreenPane(final PurchaseActivity act)
|
221 |
c7238c67
|
Leszek Koltunski
|
{
|
222 |
7654a99d
|
Leszek Koltunski
|
mAct = new WeakReference<>(act);
|
223 |
9dfb553f
|
Leszek Koltunski
|
int height = act.getScreenHeightInPixels();
|
224 |
|
|
float textSize = height*TEXT_RATIO;
|
225 |
|
|
int margin = (int)(height*PADDING_RATIO);
|
226 |
600b1343
|
Leszek Koltunski
|
int padding = margin/3;
|
227 |
c7238c67
|
Leszek Koltunski
|
|
228 |
cd432dd3
|
Leszek Koltunski
|
LinearLayout upperBar = act.findViewById(R.id.upperBar);
|
229 |
|
|
LinearLayout oneLayout = act.findViewById(R.id.purchaseLayoutOne);
|
230 |
|
|
LinearLayout allLayout = act.findViewById(R.id.purchaseLayoutAll);
|
231 |
c7238c67
|
Leszek Koltunski
|
|
232 |
600b1343
|
Leszek Koltunski
|
upperBar.setPadding( margin, margin, margin, margin);
|
233 |
|
|
oneLayout.setPadding( padding, padding, padding, padding);
|
234 |
|
|
allLayout.setPadding( padding, padding, padding, padding);
|
235 |
c7238c67
|
Leszek Koltunski
|
|
236 |
cd432dd3
|
Leszek Koltunski
|
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1.00f);
|
237 |
|
|
params1.bottomMargin = 0;
|
238 |
600b1343
|
Leszek Koltunski
|
params1.topMargin = margin;
|
239 |
|
|
params1.leftMargin = margin;
|
240 |
|
|
params1.rightMargin = margin;
|
241 |
cd432dd3
|
Leszek Koltunski
|
|
242 |
|
|
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1.00f);
|
243 |
600b1343
|
Leszek Koltunski
|
params2.bottomMargin = margin;
|
244 |
|
|
params2.topMargin = margin;
|
245 |
|
|
params2.leftMargin = margin;
|
246 |
|
|
params2.rightMargin = margin;
|
247 |
c7238c67
|
Leszek Koltunski
|
|
248 |
af0710b6
|
Leszek Koltunski
|
LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1.00f);
|
249 |
|
|
params3.bottomMargin = 0;
|
250 |
|
|
params3.topMargin = 0;
|
251 |
600b1343
|
Leszek Koltunski
|
params3.leftMargin = margin;
|
252 |
|
|
params3.rightMargin = margin;
|
253 |
af0710b6
|
Leszek Koltunski
|
|
254 |
cd432dd3
|
Leszek Koltunski
|
upperBar.setLayoutParams(params1);
|
255 |
af0710b6
|
Leszek Koltunski
|
oneLayout.setLayoutParams(params3);
|
256 |
cd432dd3
|
Leszek Koltunski
|
allLayout.setLayoutParams(params2);
|
257 |
c7238c67
|
Leszek Koltunski
|
|
258 |
|
|
TextView text;
|
259 |
cd432dd3
|
Leszek Koltunski
|
text = upperBar.findViewById(R.id.purchaseUpperName);
|
260 |
|
|
text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
|
261 |
|
|
text = upperBar.findViewById(R.id.purchaseUpperAuthor);
|
262 |
c7238c67
|
Leszek Koltunski
|
text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
|
263 |
71cda061
|
Leszek Koltunski
|
text = oneLayout.findViewById(R.id.purchaseTextOne);
|
264 |
c7238c67
|
Leszek Koltunski
|
text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
|
265 |
71cda061
|
Leszek Koltunski
|
text = allLayout.findViewById(R.id.purchaseTextAll);
|
266 |
c7238c67
|
Leszek Koltunski
|
text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
|
267 |
7480fbab
|
Leszek Koltunski
|
|
268 |
600b1343
|
Leszek Koltunski
|
setUpButtons(act,oneLayout,allLayout);
|
269 |
c7238c67
|
Leszek Koltunski
|
}
|
270 |
7654a99d
|
Leszek Koltunski
|
|
271 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
272 |
|
|
|
273 |
|
|
public void overlayFinished(long id)
|
274 |
|
|
{
|
275 |
|
|
PurchaseActivity act = mAct.get();
|
276 |
|
|
|
277 |
|
|
if( act!=null )
|
278 |
|
|
{
|
279 |
|
|
String upperName = mObject.getUpperName();
|
280 |
|
|
int ordinal = RubikObjectList.getOrdinal(upperName);
|
281 |
|
|
RubikObjectList.setCurrObject(ordinal);
|
282 |
|
|
ObjectControl control = act.getControl();
|
283 |
|
|
if( control!=null ) control.unblockEverything();
|
284 |
|
|
act.finish();
|
285 |
|
|
}
|
286 |
|
|
}
|
287 |
c7238c67
|
Leszek Koltunski
|
}
|