Project

General

Profile

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

magiccube / src / main / java / org / distorted / purchase / PurchaseScreenPane.java @ 4279106d

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 cc0fc0c5 Leszek Koltunski
  private static final float TEXT_RATIO_1  = 0.032f;
55
  private static final float TEXT_RATIO_2  = 0.020f;
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 6b034729 Leszek Koltunski
  private void showStatus(PurchaseActivity act)
79 7480fbab Leszek Koltunski
    {
80 4d6327a1 Leszek Koltunski
    RubikDialogStarsStatus d = new RubikDialogStarsStatus();
81 45831b78 Leszek Koltunski
    d.show(act.getSupportFragmentManager(), null);
82 7480fbab Leszek Koltunski
    }
83
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85
86 45831b78 Leszek Koltunski
  private void showSuccess(PurchaseActivity act, RubikObject object)
87
    {
88 7654a99d Leszek Koltunski
    act.blockUI();
89 f8e8a08e Leszek Koltunski
    RubikScores scores = RubikScores.getInstance();
90
    int totStars = scores.getNumStars();
91
    int price = object==null ? UNLOCK_ALL_PRICE:object.getPrice();
92
    PurchaseRenderer renderer = act.getRenderer();
93
    DistortedScreen screen = renderer.getScreen();
94
    OverlayStars stars = new OverlayStars();
95 7654a99d Leszek Koltunski
    DataStars data = new DataStars(totStars+price,-price,act.getResources());
96
    stars.startOverlay(screen,this,data);
97 45831b78 Leszek Koltunski
    }
98
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100
101
  private void oneButtonClicked(PurchaseActivity act)
102 7480fbab Leszek Koltunski
    {
103
    if( mObject!=null )
104
      {
105 45831b78 Leszek Koltunski
      int price = mObject.getPrice();
106
107
      if( chargeUser(price) )
108 7480fbab Leszek Koltunski
        {
109 e9e744f7 Leszek Koltunski
        RubikObjectList.buyObject(mObject);
110 45831b78 Leszek Koltunski
        showSuccess(act,mObject);
111
        }
112
      else
113
        {
114 6b034729 Leszek Koltunski
        showStatus(act);
115 7480fbab Leszek Koltunski
        }
116
      }
117
    }
118
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120
121 45831b78 Leszek Koltunski
  private void allButtonClicked(PurchaseActivity act)
122 7480fbab Leszek Koltunski
    {
123 4d6327a1 Leszek Koltunski
    if( chargeUser(UNLOCK_ALL_PRICE) )
124 7480fbab Leszek Koltunski
      {
125
      RubikObjectList.buyAll();
126 45831b78 Leszek Koltunski
      showSuccess(act,null);
127
      }
128
    else
129
      {
130 6b034729 Leszek Koltunski
      showStatus(act);
131 7480fbab Leszek Koltunski
      }
132
    }
133
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135
136 600b1343 Leszek Koltunski
  private void setUpButtons(PurchaseActivity act, LinearLayout one, LinearLayout all)
137 7480fbab Leszek Koltunski
    {
138 4ac0ea40 Leszek Koltunski
    ImageButton butO = one.findViewById(R.id.purchaseButtonOne);
139
    ImageButton butA = all.findViewById(R.id.purchaseButtonAll);
140 7480fbab Leszek Koltunski
141 600b1343 Leszek Koltunski
    int id,price = act.getObjectPrice();
142
143
         if( price<=10 ) id = R.drawable.price_10;
144
    else if( price<=20 ) id = R.drawable.price_20;
145
    else if( price<=30 ) id = R.drawable.price_30;
146
    else if( price<=40 ) id = R.drawable.price_40;
147
    else if( price<=50 ) id = R.drawable.price_50;
148
    else if( price<=60 ) id = R.drawable.price_60;
149
    else if( price<=70 ) id = R.drawable.price_70;
150
    else if( price<=80 ) id = R.drawable.price_80;
151
    else if( price<=90 ) id = R.drawable.price_90;
152
    else                 id = R.drawable.price_100;
153
154
    Drawable drawable = ResourcesCompat.getDrawable(act.getResources(), id, null);
155
    butO.setImageDrawable(drawable);
156
157 7480fbab Leszek Koltunski
    butO.setOnClickListener( new View.OnClickListener()
158
      {
159
      @Override
160
      public void onClick(View v)
161
        {
162 45831b78 Leszek Koltunski
        oneButtonClicked(act);
163 7480fbab Leszek Koltunski
        }
164
      });
165
166
    butA.setOnClickListener( new View.OnClickListener()
167
      {
168
      @Override
169
      public void onClick(View v)
170
        {
171 45831b78 Leszek Koltunski
        allButtonClicked(act);
172 7480fbab Leszek Koltunski
        }
173
      });
174
    }
175
176 c7238c67 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
177
178
  void updatePane(PurchaseActivity act, int objectOrdinal)
179
    {
180 7480fbab Leszek Koltunski
    mObject = RubikObjectList.getObject(objectOrdinal);
181 c7238c67 Leszek Koltunski
182 7480fbab Leszek Koltunski
    if( mObject!=null )
183 c7238c67 Leszek Koltunski
      {
184 7480fbab Leszek Koltunski
      InputStream stream = mObject.getObjectStream(act);
185 5305fdc8 Leszek Koltunski
      JsonReader reader = new JsonReader();
186 cd432dd3 Leszek Koltunski
      String author, name;
187
      int year, difficulty;
188 c7238c67 Leszek Koltunski
189
      try
190
        {
191
        reader.parseJsonFileMetadata(stream);
192
        name       = reader.getObjectName();
193 cd432dd3 Leszek Koltunski
        author     = reader.getInventor();
194
        year       = reader.getYearOfInvention();
195 c7238c67 Leszek Koltunski
        difficulty = reader.getComplexity();
196
        }
197
      catch(Exception ex)
198
        {
199
        name = "?";
200 cd432dd3 Leszek Koltunski
        author = "?";
201
        year = 0;
202 c7238c67 Leszek Koltunski
        difficulty = 0;
203
        }
204
205 71cda061 Leszek Koltunski
      if( difficulty<0           ) difficulty=0;
206
      if( difficulty>=NUM_IMAGES ) difficulty=NUM_IMAGES-1;
207 c7238c67 Leszek Koltunski
208 cd432dd3 Leszek Koltunski
      String both = year>0 ? author+" "+year : author;
209
210
      TextView v1 = act.findViewById(R.id.purchaseUpperName);
211
      v1.setText(name);
212
      TextView v2 = act.findViewById(R.id.purchaseUpperAuthor);
213
      v2.setText(both);
214 71cda061 Leszek Koltunski
      ImageView image = act.findViewById(R.id.purchaseDifficulty);
215
      image.setImageResource(IMAGES[difficulty]);
216 c7238c67 Leszek Koltunski
      }
217
    }
218
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220
221 71cda061 Leszek Koltunski
  PurchaseScreenPane(final PurchaseActivity act)
222 c7238c67 Leszek Koltunski
    {
223 7654a99d Leszek Koltunski
    mAct = new WeakReference<>(act);
224 9dfb553f Leszek Koltunski
    int height = act.getScreenHeightInPixels();
225 cc0fc0c5 Leszek Koltunski
    float textSize1 = height*TEXT_RATIO_1;
226
    float textSize2 = height*TEXT_RATIO_2;
227 9dfb553f Leszek Koltunski
    int margin = (int)(height*PADDING_RATIO);
228 600b1343 Leszek Koltunski
    int padding = margin/3;
229 c7238c67 Leszek Koltunski
230 cd432dd3 Leszek Koltunski
    LinearLayout upperBar  = act.findViewById(R.id.upperBar);
231
    LinearLayout oneLayout = act.findViewById(R.id.purchaseLayoutOne);
232
    LinearLayout allLayout = act.findViewById(R.id.purchaseLayoutAll);
233 c7238c67 Leszek Koltunski
234 600b1343 Leszek Koltunski
    upperBar.setPadding(   margin,  margin,  margin,  margin);
235
    oneLayout.setPadding( padding, padding, padding, padding);
236
    allLayout.setPadding( padding, padding, padding, padding);
237 c7238c67 Leszek Koltunski
238 cd432dd3 Leszek Koltunski
    LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1.00f);
239
    params1.bottomMargin = 0;
240 600b1343 Leszek Koltunski
    params1.topMargin    = margin;
241
    params1.leftMargin   = margin;
242
    params1.rightMargin  = margin;
243 cd432dd3 Leszek Koltunski
244
    LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1.00f);
245 600b1343 Leszek Koltunski
    params2.bottomMargin = margin;
246
    params2.topMargin    = margin;
247
    params2.leftMargin   = margin;
248
    params2.rightMargin  = margin;
249 c7238c67 Leszek Koltunski
250 af0710b6 Leszek Koltunski
    LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1.00f);
251
    params3.bottomMargin = 0;
252
    params3.topMargin    = 0;
253 600b1343 Leszek Koltunski
    params3.leftMargin   = margin;
254
    params3.rightMargin  = margin;
255 af0710b6 Leszek Koltunski
256 cd432dd3 Leszek Koltunski
    upperBar.setLayoutParams(params1);
257 af0710b6 Leszek Koltunski
    oneLayout.setLayoutParams(params3);
258 cd432dd3 Leszek Koltunski
    allLayout.setLayoutParams(params2);
259 c7238c67 Leszek Koltunski
260
    TextView text;
261 cd432dd3 Leszek Koltunski
    text = upperBar.findViewById(R.id.purchaseUpperName);
262 cc0fc0c5 Leszek Koltunski
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize1);
263 cd432dd3 Leszek Koltunski
    text = upperBar.findViewById(R.id.purchaseUpperAuthor);
264 cc0fc0c5 Leszek Koltunski
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize2);
265 71cda061 Leszek Koltunski
    text = oneLayout.findViewById(R.id.purchaseTextOne);
266 cc0fc0c5 Leszek Koltunski
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize1);
267 71cda061 Leszek Koltunski
    text = allLayout.findViewById(R.id.purchaseTextAll);
268 cc0fc0c5 Leszek Koltunski
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize1);
269 7480fbab Leszek Koltunski
270 600b1343 Leszek Koltunski
    setUpButtons(act,oneLayout,allLayout);
271 c7238c67 Leszek Koltunski
    }
272 7654a99d Leszek Koltunski
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274
275
  public void overlayFinished(long id)
276
    {
277
    PurchaseActivity act = mAct.get();
278
279
    if( act!=null )
280
      {
281
      String upperName = mObject.getUpperName();
282
      int ordinal = RubikObjectList.getOrdinal(upperName);
283
      RubikObjectList.setCurrObject(ordinal);
284
      ObjectControl control = act.getControl();
285
      if( control!=null ) control.unblockEverything();
286
      act.finish();
287
      }
288
    }
289 c7238c67 Leszek Koltunski
}