Project

General

Profile

Download (9.25 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / purchase / PurchaseScreenPane.java @ e35ed7e1

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
import org.distorted.objects.RubikObject;
28
import org.distorted.objects.RubikObjectList;
29 f8e8a08e Leszek Koltunski
import org.distorted.overlays.DataStars;
30 7654a99d Leszek Koltunski
import org.distorted.overlays.ListenerOverlay;
31 f8e8a08e Leszek Koltunski
import org.distorted.overlays.OverlayStars;
32 c7238c67 Leszek Koltunski
33
import java.io.InputStream;
34 7654a99d Leszek Koltunski
import java.lang.ref.WeakReference;
35 c7238c67 Leszek Koltunski
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37
38 7654a99d Leszek Koltunski
public class PurchaseScreenPane implements ListenerOverlay
39 c7238c67 Leszek Koltunski
{
40 9dfb553f Leszek Koltunski
  public static final int UNLOCK_ALL_PRICE = 600;
41 e9e744f7 Leszek Koltunski
42 c7238c67 Leszek Koltunski
  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 71cda061 Leszek Koltunski
  private static final int NUM_IMAGES      = IMAGES.length;
52 9dfb553f Leszek Koltunski
  public  static final float PADDING_RATIO = 0.017f;
53 cc0fc0c5 Leszek Koltunski
  private static final float TEXT_RATIO_1  = 0.032f;
54
  private static final float TEXT_RATIO_2  = 0.020f;
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 e35ed7e1 leszek
        difficulty = (int)reader.getComplexity();
195 c7238c67 Leszek Koltunski
        }
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 cc0fc0c5 Leszek Koltunski
    float textSize1 = height*TEXT_RATIO_1;
225
    float textSize2 = height*TEXT_RATIO_2;
226 9dfb553f Leszek Koltunski
    int margin = (int)(height*PADDING_RATIO);
227 600b1343 Leszek Koltunski
    int padding = margin/3;
228 c7238c67 Leszek Koltunski
229 cd432dd3 Leszek Koltunski
    LinearLayout upperBar  = act.findViewById(R.id.upperBar);
230
    LinearLayout oneLayout = act.findViewById(R.id.purchaseLayoutOne);
231
    LinearLayout allLayout = act.findViewById(R.id.purchaseLayoutAll);
232 c7238c67 Leszek Koltunski
233 600b1343 Leszek Koltunski
    upperBar.setPadding(   margin,  margin,  margin,  margin);
234
    oneLayout.setPadding( padding, padding, padding, padding);
235
    allLayout.setPadding( padding, padding, padding, padding);
236 c7238c67 Leszek Koltunski
237 cd432dd3 Leszek Koltunski
    LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1.00f);
238
    params1.bottomMargin = 0;
239 600b1343 Leszek Koltunski
    params1.topMargin    = margin;
240
    params1.leftMargin   = margin;
241
    params1.rightMargin  = margin;
242 cd432dd3 Leszek Koltunski
243
    LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1.00f);
244 600b1343 Leszek Koltunski
    params2.bottomMargin = margin;
245
    params2.topMargin    = margin;
246
    params2.leftMargin   = margin;
247
    params2.rightMargin  = margin;
248 c7238c67 Leszek Koltunski
249 af0710b6 Leszek Koltunski
    LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1.00f);
250
    params3.bottomMargin = 0;
251
    params3.topMargin    = 0;
252 600b1343 Leszek Koltunski
    params3.leftMargin   = margin;
253
    params3.rightMargin  = margin;
254 af0710b6 Leszek Koltunski
255 cd432dd3 Leszek Koltunski
    upperBar.setLayoutParams(params1);
256 af0710b6 Leszek Koltunski
    oneLayout.setLayoutParams(params3);
257 cd432dd3 Leszek Koltunski
    allLayout.setLayoutParams(params2);
258 c7238c67 Leszek Koltunski
259
    TextView text;
260 cd432dd3 Leszek Koltunski
    text = upperBar.findViewById(R.id.purchaseUpperName);
261 cc0fc0c5 Leszek Koltunski
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize1);
262 cd432dd3 Leszek Koltunski
    text = upperBar.findViewById(R.id.purchaseUpperAuthor);
263 cc0fc0c5 Leszek Koltunski
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize2);
264 71cda061 Leszek Koltunski
    text = oneLayout.findViewById(R.id.purchaseTextOne);
265 cc0fc0c5 Leszek Koltunski
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize1);
266 71cda061 Leszek Koltunski
    text = allLayout.findViewById(R.id.purchaseTextAll);
267 cc0fc0c5 Leszek Koltunski
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize1);
268 7480fbab Leszek Koltunski
269 600b1343 Leszek Koltunski
    setUpButtons(act,oneLayout,allLayout);
270 c7238c67 Leszek Koltunski
    }
271 7654a99d Leszek Koltunski
272
///////////////////////////////////////////////////////////////////////////////////////////////////
273
274
  public void overlayFinished(long id)
275
    {
276
    PurchaseActivity act = mAct.get();
277 c02235d5 leszek
    if( act!=null ) act.finish();
278 7654a99d Leszek Koltunski
    }
279 c7238c67 Leszek Koltunski
}