Project

General

Profile

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

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

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