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 |
|
|
import android.util.TypedValue;
|
13 |
7480fbab
|
Leszek Koltunski
|
import android.view.View;
|
14 |
|
|
import android.widget.Button;
|
15 |
c7238c67
|
Leszek Koltunski
|
import android.widget.ImageView;
|
16 |
|
|
import android.widget.LinearLayout;
|
17 |
|
|
import android.widget.TextView;
|
18 |
|
|
|
19 |
|
|
import org.distorted.main.R;
|
20 |
|
|
import org.distorted.objectlib.json.JsonReader;
|
21 |
|
|
import org.distorted.objects.RubikObject;
|
22 |
|
|
import org.distorted.objects.RubikObjectList;
|
23 |
|
|
|
24 |
|
|
import java.io.InputStream;
|
25 |
|
|
|
26 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
27 |
|
|
|
28 |
|
|
public class PurchaseScreenPane
|
29 |
|
|
{
|
30 |
|
|
private static final int[] IMAGES =
|
31 |
|
|
{
|
32 |
|
|
R.drawable.difficulty1,
|
33 |
|
|
R.drawable.difficulty2,
|
34 |
|
|
R.drawable.difficulty3,
|
35 |
|
|
R.drawable.difficulty4,
|
36 |
|
|
R.drawable.difficulty5,
|
37 |
|
|
};
|
38 |
|
|
|
39 |
71cda061
|
Leszek Koltunski
|
private static final int NUM_IMAGES = IMAGES.length;
|
40 |
c7238c67
|
Leszek Koltunski
|
public static final float PADDING_RATIO = 0.025f;
|
41 |
cd432dd3
|
Leszek Koltunski
|
private static final float TEXT_RATIO = 0.050f;
|
42 |
c7238c67
|
Leszek Koltunski
|
|
43 |
7480fbab
|
Leszek Koltunski
|
private RubikObject mObject;
|
44 |
|
|
|
45 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
46 |
|
|
// TODO
|
47 |
|
|
|
48 |
|
|
private boolean buyOne(String shortName)
|
49 |
|
|
{
|
50 |
|
|
return true;
|
51 |
|
|
}
|
52 |
|
|
|
53 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
54 |
|
|
// TODO
|
55 |
|
|
|
56 |
|
|
private boolean buyAll()
|
57 |
|
|
{
|
58 |
|
|
return true;
|
59 |
|
|
}
|
60 |
|
|
|
61 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
62 |
|
|
|
63 |
|
|
private void oneButtonClicked()
|
64 |
|
|
{
|
65 |
|
|
if( mObject!=null )
|
66 |
|
|
{
|
67 |
|
|
String shortName = mObject.getUpperName();
|
68 |
|
|
|
69 |
|
|
if( !RubikObjectList.objectAlreadyBought(shortName) && buyOne(shortName) )
|
70 |
|
|
{
|
71 |
|
|
android.util.Log.e("D", "buying "+shortName);
|
72 |
|
|
|
73 |
|
|
RubikObjectList.buyObject(shortName);
|
74 |
|
|
}
|
75 |
|
|
}
|
76 |
|
|
}
|
77 |
|
|
|
78 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
79 |
|
|
|
80 |
|
|
private void allButtonClicked()
|
81 |
|
|
{
|
82 |
|
|
if( !RubikObjectList.allAlreadyBought() && buyAll() )
|
83 |
|
|
{
|
84 |
|
|
RubikObjectList.buyAll();
|
85 |
|
|
}
|
86 |
|
|
}
|
87 |
|
|
|
88 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
89 |
|
|
|
90 |
|
|
private void setUpButtons(LinearLayout one, LinearLayout all)
|
91 |
|
|
{
|
92 |
|
|
Button butO = one.findViewById(R.id.purchaseButtonOne);
|
93 |
|
|
Button butA = all.findViewById(R.id.purchaseButtonAll);
|
94 |
|
|
|
95 |
|
|
butO.setOnClickListener( new View.OnClickListener()
|
96 |
|
|
{
|
97 |
|
|
@Override
|
98 |
|
|
public void onClick(View v)
|
99 |
|
|
{
|
100 |
|
|
oneButtonClicked();
|
101 |
|
|
}
|
102 |
|
|
});
|
103 |
|
|
|
104 |
|
|
butA.setOnClickListener( new View.OnClickListener()
|
105 |
|
|
{
|
106 |
|
|
@Override
|
107 |
|
|
public void onClick(View v)
|
108 |
|
|
{
|
109 |
|
|
allButtonClicked();
|
110 |
|
|
}
|
111 |
|
|
});
|
112 |
|
|
}
|
113 |
|
|
|
114 |
c7238c67
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
115 |
|
|
|
116 |
|
|
void updatePane(PurchaseActivity act, int objectOrdinal)
|
117 |
|
|
{
|
118 |
7480fbab
|
Leszek Koltunski
|
mObject = RubikObjectList.getObject(objectOrdinal);
|
119 |
c7238c67
|
Leszek Koltunski
|
|
120 |
7480fbab
|
Leszek Koltunski
|
if( mObject!=null )
|
121 |
c7238c67
|
Leszek Koltunski
|
{
|
122 |
7480fbab
|
Leszek Koltunski
|
InputStream stream = mObject.getObjectStream(act);
|
123 |
c7238c67
|
Leszek Koltunski
|
JsonReader reader = JsonReader.getInstance();
|
124 |
cd432dd3
|
Leszek Koltunski
|
String author, name;
|
125 |
|
|
int year, difficulty;
|
126 |
c7238c67
|
Leszek Koltunski
|
|
127 |
|
|
try
|
128 |
|
|
{
|
129 |
|
|
reader.parseJsonFileMetadata(stream);
|
130 |
|
|
name = reader.getObjectName();
|
131 |
cd432dd3
|
Leszek Koltunski
|
author = reader.getInventor();
|
132 |
|
|
year = reader.getYearOfInvention();
|
133 |
c7238c67
|
Leszek Koltunski
|
difficulty = reader.getComplexity();
|
134 |
|
|
}
|
135 |
|
|
catch(Exception ex)
|
136 |
|
|
{
|
137 |
|
|
name = "?";
|
138 |
cd432dd3
|
Leszek Koltunski
|
author = "?";
|
139 |
|
|
year = 0;
|
140 |
c7238c67
|
Leszek Koltunski
|
difficulty = 0;
|
141 |
|
|
}
|
142 |
|
|
|
143 |
71cda061
|
Leszek Koltunski
|
if( difficulty<0 ) difficulty=0;
|
144 |
|
|
if( difficulty>=NUM_IMAGES ) difficulty=NUM_IMAGES-1;
|
145 |
c7238c67
|
Leszek Koltunski
|
|
146 |
cd432dd3
|
Leszek Koltunski
|
String both = year>0 ? author+" "+year : author;
|
147 |
|
|
|
148 |
|
|
TextView v1 = act.findViewById(R.id.purchaseUpperName);
|
149 |
|
|
v1.setText(name);
|
150 |
|
|
TextView v2 = act.findViewById(R.id.purchaseUpperAuthor);
|
151 |
|
|
v2.setText(both);
|
152 |
71cda061
|
Leszek Koltunski
|
ImageView image = act.findViewById(R.id.purchaseDifficulty);
|
153 |
|
|
image.setImageResource(IMAGES[difficulty]);
|
154 |
c7238c67
|
Leszek Koltunski
|
}
|
155 |
|
|
}
|
156 |
|
|
|
157 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
158 |
|
|
|
159 |
71cda061
|
Leszek Koltunski
|
PurchaseScreenPane(final PurchaseActivity act)
|
160 |
c7238c67
|
Leszek Koltunski
|
{
|
161 |
|
|
int width = act.getScreenWidthInPixels();
|
162 |
|
|
float textSize = width*TEXT_RATIO;
|
163 |
|
|
int padding = (int)(width*PADDING_RATIO);
|
164 |
|
|
|
165 |
cd432dd3
|
Leszek Koltunski
|
LinearLayout upperBar = act.findViewById(R.id.upperBar);
|
166 |
|
|
LinearLayout oneLayout = act.findViewById(R.id.purchaseLayoutOne);
|
167 |
|
|
LinearLayout allLayout = act.findViewById(R.id.purchaseLayoutAll);
|
168 |
c7238c67
|
Leszek Koltunski
|
|
169 |
af0710b6
|
Leszek Koltunski
|
upperBar.setPadding( padding,padding ,padding,padding );
|
170 |
71cda061
|
Leszek Koltunski
|
oneLayout.setPadding(padding,padding/2,padding,padding/2);
|
171 |
|
|
allLayout.setPadding(padding,padding/2,padding,padding/2);
|
172 |
c7238c67
|
Leszek Koltunski
|
|
173 |
cd432dd3
|
Leszek Koltunski
|
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1.00f);
|
174 |
|
|
params1.bottomMargin = 0;
|
175 |
|
|
params1.topMargin = padding;
|
176 |
|
|
params1.leftMargin = padding;
|
177 |
|
|
params1.rightMargin = padding;
|
178 |
|
|
|
179 |
|
|
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1.00f);
|
180 |
|
|
params2.bottomMargin = padding;
|
181 |
|
|
params2.topMargin = padding;
|
182 |
|
|
params2.leftMargin = padding;
|
183 |
|
|
params2.rightMargin = padding;
|
184 |
c7238c67
|
Leszek Koltunski
|
|
185 |
af0710b6
|
Leszek Koltunski
|
LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1.00f);
|
186 |
|
|
params3.bottomMargin = 0;
|
187 |
|
|
params3.topMargin = 0;
|
188 |
|
|
params3.leftMargin = padding;
|
189 |
|
|
params3.rightMargin = padding;
|
190 |
|
|
|
191 |
cd432dd3
|
Leszek Koltunski
|
upperBar.setLayoutParams(params1);
|
192 |
af0710b6
|
Leszek Koltunski
|
oneLayout.setLayoutParams(params3);
|
193 |
cd432dd3
|
Leszek Koltunski
|
allLayout.setLayoutParams(params2);
|
194 |
c7238c67
|
Leszek Koltunski
|
|
195 |
|
|
TextView text;
|
196 |
cd432dd3
|
Leszek Koltunski
|
text = upperBar.findViewById(R.id.purchaseUpperName);
|
197 |
|
|
text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
|
198 |
|
|
text = upperBar.findViewById(R.id.purchaseUpperAuthor);
|
199 |
c7238c67
|
Leszek Koltunski
|
text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
|
200 |
71cda061
|
Leszek Koltunski
|
text = oneLayout.findViewById(R.id.purchaseTextOne);
|
201 |
c7238c67
|
Leszek Koltunski
|
text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
|
202 |
71cda061
|
Leszek Koltunski
|
text = allLayout.findViewById(R.id.purchaseTextAll);
|
203 |
c7238c67
|
Leszek Koltunski
|
text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
|
204 |
7480fbab
|
Leszek Koltunski
|
|
205 |
|
|
setUpButtons(oneLayout,allLayout);
|
206 |
c7238c67
|
Leszek Koltunski
|
}
|
207 |
|
|
}
|