1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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
|
import android.graphics.drawable.Drawable;
|
13
|
import android.util.TypedValue;
|
14
|
import android.view.View;
|
15
|
import android.widget.ImageButton;
|
16
|
import android.widget.ImageView;
|
17
|
import android.widget.LinearLayout;
|
18
|
import android.widget.TextView;
|
19
|
|
20
|
import androidx.core.content.res.ResourcesCompat;
|
21
|
|
22
|
import org.distorted.dialogs.RubikDialogStarsStatus;
|
23
|
import org.distorted.external.RubikScores;
|
24
|
import org.distorted.library.main.DistortedScreen;
|
25
|
import org.distorted.main.R;
|
26
|
import org.distorted.objectlib.json.JsonReader;
|
27
|
import org.distorted.objects.RubikObject;
|
28
|
import org.distorted.objects.RubikObjectList;
|
29
|
import org.distorted.overlays.DataStars;
|
30
|
import org.distorted.overlays.ListenerOverlay;
|
31
|
import org.distorted.overlays.OverlayStars;
|
32
|
|
33
|
import java.io.InputStream;
|
34
|
import java.lang.ref.WeakReference;
|
35
|
|
36
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
37
|
|
38
|
public class PurchaseScreenPane implements ListenerOverlay
|
39
|
{
|
40
|
public static final int UNLOCK_ALL_PRICE = 600;
|
41
|
|
42
|
private static final int[] IMAGES =
|
43
|
{
|
44
|
R.drawable.difficulty1,
|
45
|
R.drawable.difficulty2,
|
46
|
R.drawable.difficulty3,
|
47
|
R.drawable.difficulty4,
|
48
|
R.drawable.difficulty5,
|
49
|
};
|
50
|
|
51
|
private static final int NUM_IMAGES = IMAGES.length;
|
52
|
public static final float PADDING_RATIO = 0.017f;
|
53
|
private static final float TEXT_RATIO_1 = 0.032f;
|
54
|
private static final float TEXT_RATIO_2 = 0.020f;
|
55
|
|
56
|
private final WeakReference<PurchaseActivity> mAct;
|
57
|
private RubikObject mObject;
|
58
|
|
59
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
60
|
|
61
|
private boolean chargeUser(int amount)
|
62
|
{
|
63
|
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
|
}
|
74
|
|
75
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
76
|
|
77
|
private void showStatus(PurchaseActivity act)
|
78
|
{
|
79
|
RubikDialogStarsStatus d = new RubikDialogStarsStatus();
|
80
|
d.show(act.getSupportFragmentManager(), null);
|
81
|
}
|
82
|
|
83
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
84
|
|
85
|
private void showSuccess(PurchaseActivity act, RubikObject object)
|
86
|
{
|
87
|
act.blockUI();
|
88
|
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
|
DataStars data = new DataStars(totStars+price,-price,act.getResources());
|
95
|
stars.startOverlay(screen,this,data);
|
96
|
}
|
97
|
|
98
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
99
|
|
100
|
private void oneButtonClicked(PurchaseActivity act)
|
101
|
{
|
102
|
if( mObject!=null )
|
103
|
{
|
104
|
int price = mObject.getPrice();
|
105
|
|
106
|
if( chargeUser(price) )
|
107
|
{
|
108
|
RubikObjectList.buyObject(mObject);
|
109
|
showSuccess(act,mObject);
|
110
|
}
|
111
|
else
|
112
|
{
|
113
|
showStatus(act);
|
114
|
}
|
115
|
}
|
116
|
}
|
117
|
|
118
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
119
|
|
120
|
private void allButtonClicked(PurchaseActivity act)
|
121
|
{
|
122
|
if( chargeUser(UNLOCK_ALL_PRICE) )
|
123
|
{
|
124
|
RubikObjectList.buyAll();
|
125
|
showSuccess(act,null);
|
126
|
}
|
127
|
else
|
128
|
{
|
129
|
showStatus(act);
|
130
|
}
|
131
|
}
|
132
|
|
133
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
134
|
|
135
|
private void setUpButtons(PurchaseActivity act, LinearLayout one, LinearLayout all)
|
136
|
{
|
137
|
ImageButton butO = one.findViewById(R.id.purchaseButtonOne);
|
138
|
ImageButton butA = all.findViewById(R.id.purchaseButtonAll);
|
139
|
|
140
|
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
|
butO.setOnClickListener( new View.OnClickListener()
|
157
|
{
|
158
|
@Override
|
159
|
public void onClick(View v)
|
160
|
{
|
161
|
oneButtonClicked(act);
|
162
|
}
|
163
|
});
|
164
|
|
165
|
butA.setOnClickListener( new View.OnClickListener()
|
166
|
{
|
167
|
@Override
|
168
|
public void onClick(View v)
|
169
|
{
|
170
|
allButtonClicked(act);
|
171
|
}
|
172
|
});
|
173
|
}
|
174
|
|
175
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
176
|
|
177
|
void updatePane(PurchaseActivity act, int objectOrdinal)
|
178
|
{
|
179
|
mObject = RubikObjectList.getObject(objectOrdinal);
|
180
|
|
181
|
if( mObject!=null )
|
182
|
{
|
183
|
InputStream stream = mObject.getObjectStream(act);
|
184
|
JsonReader reader = new JsonReader();
|
185
|
String author, name;
|
186
|
int year, difficulty;
|
187
|
|
188
|
try
|
189
|
{
|
190
|
reader.parseJsonFileMetadata(stream);
|
191
|
name = reader.getObjectName();
|
192
|
author = reader.getInventor();
|
193
|
year = reader.getYearOfInvention();
|
194
|
difficulty = reader.getComplexity();
|
195
|
}
|
196
|
catch(Exception ex)
|
197
|
{
|
198
|
name = "?";
|
199
|
author = "?";
|
200
|
year = 0;
|
201
|
difficulty = 0;
|
202
|
}
|
203
|
|
204
|
if( difficulty<0 ) difficulty=0;
|
205
|
if( difficulty>=NUM_IMAGES ) difficulty=NUM_IMAGES-1;
|
206
|
|
207
|
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
|
ImageView image = act.findViewById(R.id.purchaseDifficulty);
|
214
|
image.setImageResource(IMAGES[difficulty]);
|
215
|
}
|
216
|
}
|
217
|
|
218
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
219
|
|
220
|
PurchaseScreenPane(final PurchaseActivity act)
|
221
|
{
|
222
|
mAct = new WeakReference<>(act);
|
223
|
int height = act.getScreenHeightInPixels();
|
224
|
float textSize1 = height*TEXT_RATIO_1;
|
225
|
float textSize2 = height*TEXT_RATIO_2;
|
226
|
int margin = (int)(height*PADDING_RATIO);
|
227
|
int padding = margin/3;
|
228
|
|
229
|
LinearLayout upperBar = act.findViewById(R.id.upperBar);
|
230
|
LinearLayout oneLayout = act.findViewById(R.id.purchaseLayoutOne);
|
231
|
LinearLayout allLayout = act.findViewById(R.id.purchaseLayoutAll);
|
232
|
|
233
|
upperBar.setPadding( margin, margin, margin, margin);
|
234
|
oneLayout.setPadding( padding, padding, padding, padding);
|
235
|
allLayout.setPadding( padding, padding, padding, padding);
|
236
|
|
237
|
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1.00f);
|
238
|
params1.bottomMargin = 0;
|
239
|
params1.topMargin = margin;
|
240
|
params1.leftMargin = margin;
|
241
|
params1.rightMargin = margin;
|
242
|
|
243
|
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1.00f);
|
244
|
params2.bottomMargin = margin;
|
245
|
params2.topMargin = margin;
|
246
|
params2.leftMargin = margin;
|
247
|
params2.rightMargin = margin;
|
248
|
|
249
|
LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1.00f);
|
250
|
params3.bottomMargin = 0;
|
251
|
params3.topMargin = 0;
|
252
|
params3.leftMargin = margin;
|
253
|
params3.rightMargin = margin;
|
254
|
|
255
|
upperBar.setLayoutParams(params1);
|
256
|
oneLayout.setLayoutParams(params3);
|
257
|
allLayout.setLayoutParams(params2);
|
258
|
|
259
|
TextView text;
|
260
|
text = upperBar.findViewById(R.id.purchaseUpperName);
|
261
|
text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize1);
|
262
|
text = upperBar.findViewById(R.id.purchaseUpperAuthor);
|
263
|
text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize2);
|
264
|
text = oneLayout.findViewById(R.id.purchaseTextOne);
|
265
|
text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize1);
|
266
|
text = allLayout.findViewById(R.id.purchaseTextAll);
|
267
|
text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize1);
|
268
|
|
269
|
setUpButtons(act,oneLayout,allLayout);
|
270
|
}
|
271
|
|
272
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
273
|
|
274
|
public void overlayFinished(long id)
|
275
|
{
|
276
|
PurchaseActivity act = mAct.get();
|
277
|
|
278
|
if( act!=null )
|
279
|
{
|
280
|
String upperName = mObject.getUpperName();
|
281
|
int ordinal = RubikObjectList.getOrdinal(upperName);
|
282
|
RubikObjectList.setCurrObject(ordinal);
|
283
|
act.finish();
|
284
|
}
|
285
|
}
|
286
|
}
|