Project

General

Profile

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

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

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.os.Bundle;
14
import android.util.TypedValue;
15
import android.view.View;
16
import android.widget.ImageButton;
17
import android.widget.ImageView;
18
import android.widget.LinearLayout;
19
import android.widget.TextView;
20

    
21
import androidx.core.content.res.ResourcesCompat;
22

    
23
import org.distorted.dialogs.RubikDialogStarsError;
24
import org.distorted.external.RubikScores;
25
import org.distorted.library.main.DistortedScreen;
26
import org.distorted.main.R;
27
import org.distorted.objectlib.json.JsonReader;
28
import org.distorted.objectlib.main.ObjectControl;
29
import org.distorted.objects.RubikObject;
30
import org.distorted.objects.RubikObjectList;
31
import org.distorted.overlays.DataStars;
32
import org.distorted.overlays.ListenerOverlay;
33
import org.distorted.overlays.OverlayStars;
34

    
35
import java.io.InputStream;
36
import java.lang.ref.WeakReference;
37

    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

    
40
public class PurchaseScreenPane implements ListenerOverlay
41
{
42
  public static final int UNLOCK_ALL_PRICE = 500;
43

    
44
  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
  private static final int NUM_IMAGES      = IMAGES.length;
54
  public  static final float PADDING_RATIO = 0.025f;
55
  private static final float TEXT_RATIO    = 0.050f;
56

    
57
  private final WeakReference<PurchaseActivity> mAct;
58
  private RubikObject mObject;
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
  private boolean chargeUser(int amount)
63
    {
64
    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
    }
75

    
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77

    
78
  private void showError(PurchaseActivity act, int price)
79
    {
80
    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
    }
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88

    
89
  private void showSuccess(PurchaseActivity act, RubikObject object)
90
    {
91
    act.blockUI();
92
    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
    DataStars data = new DataStars(totStars+price,-price,act.getResources());
99
    stars.startOverlay(screen,this,data);
100
    }
101

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

    
104
  private void oneButtonClicked(PurchaseActivity act)
105
    {
106
    if( mObject!=null )
107
      {
108
      int price = mObject.getPrice();
109

    
110
      if( chargeUser(price) )
111
        {
112
        RubikObjectList.buyObject(mObject);
113
        showSuccess(act,mObject);
114
        }
115
      else
116
        {
117
        showError(act,price);
118
        }
119
      }
120
    }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

    
124
  private void allButtonClicked(PurchaseActivity act)
125
    {
126
    int price = UNLOCK_ALL_PRICE;
127

    
128
    if( chargeUser(price) )
129
      {
130
      RubikObjectList.buyAll();
131
      showSuccess(act,null);
132
      }
133
    else
134
      {
135
      showError(act,price);
136
      }
137
    }
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

    
141
  private void setUpButtons(PurchaseActivity act, LinearLayout one, LinearLayout all)
142
    {
143
    ImageButton butO = one.findViewById(R.id.purchaseButtonOne);
144
    ImageButton butA = all.findViewById(R.id.purchaseButtonAll);
145

    
146
    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
    butO.setOnClickListener( new View.OnClickListener()
163
      {
164
      @Override
165
      public void onClick(View v)
166
        {
167
        oneButtonClicked(act);
168
        }
169
      });
170

    
171
    butA.setOnClickListener( new View.OnClickListener()
172
      {
173
      @Override
174
      public void onClick(View v)
175
        {
176
        allButtonClicked(act);
177
        }
178
      });
179
    }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

    
183
  void updatePane(PurchaseActivity act, int objectOrdinal)
184
    {
185
    mObject = RubikObjectList.getObject(objectOrdinal);
186

    
187
    if( mObject!=null )
188
      {
189
      InputStream stream = mObject.getObjectStream(act);
190
      JsonReader reader = JsonReader.getInstance();
191
      String author, name;
192
      int year, difficulty;
193

    
194
      try
195
        {
196
        reader.parseJsonFileMetadata(stream);
197
        name       = reader.getObjectName();
198
        author     = reader.getInventor();
199
        year       = reader.getYearOfInvention();
200
        difficulty = reader.getComplexity();
201
        }
202
      catch(Exception ex)
203
        {
204
        name = "?";
205
        author = "?";
206
        year = 0;
207
        difficulty = 0;
208
        }
209

    
210
      if( difficulty<0           ) difficulty=0;
211
      if( difficulty>=NUM_IMAGES ) difficulty=NUM_IMAGES-1;
212

    
213
      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
      ImageView image = act.findViewById(R.id.purchaseDifficulty);
220
      image.setImageResource(IMAGES[difficulty]);
221
      }
222
    }
223

    
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225

    
226
  PurchaseScreenPane(final PurchaseActivity act)
227
    {
228
    mAct = new WeakReference<>(act);
229
    int width = act.getScreenWidthInPixels();
230
    float textSize = width*TEXT_RATIO;
231
    int margin = (int)(width*PADDING_RATIO);
232
    int padding = margin/3;
233

    
234
    LinearLayout upperBar  = act.findViewById(R.id.upperBar);
235
    LinearLayout oneLayout = act.findViewById(R.id.purchaseLayoutOne);
236
    LinearLayout allLayout = act.findViewById(R.id.purchaseLayoutAll);
237

    
238
    upperBar.setPadding(   margin,  margin,  margin,  margin);
239
    oneLayout.setPadding( padding, padding, padding, padding);
240
    allLayout.setPadding( padding, padding, padding, padding);
241

    
242
    LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1.00f);
243
    params1.bottomMargin = 0;
244
    params1.topMargin    = margin;
245
    params1.leftMargin   = margin;
246
    params1.rightMargin  = margin;
247

    
248
    LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1.00f);
249
    params2.bottomMargin = margin;
250
    params2.topMargin    = margin;
251
    params2.leftMargin   = margin;
252
    params2.rightMargin  = margin;
253

    
254
    LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1.00f);
255
    params3.bottomMargin = 0;
256
    params3.topMargin    = 0;
257
    params3.leftMargin   = margin;
258
    params3.rightMargin  = margin;
259

    
260
    upperBar.setLayoutParams(params1);
261
    oneLayout.setLayoutParams(params3);
262
    allLayout.setLayoutParams(params2);
263

    
264
    TextView text;
265
    text = upperBar.findViewById(R.id.purchaseUpperName);
266
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
267
    text = upperBar.findViewById(R.id.purchaseUpperAuthor);
268
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
269
    text = oneLayout.findViewById(R.id.purchaseTextOne);
270
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
271
    text = allLayout.findViewById(R.id.purchaseTextAll);
272
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
273

    
274
    setUpButtons(act,oneLayout,allLayout);
275
    }
276

    
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
}
(5-5/6)