Project

General

Profile

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

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

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

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

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

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

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

    
39
public class PurchaseScreenPane implements ListenerOverlay
40
{
41
  public static final int UNLOCK_ALL_PRICE = 600;
42

    
43
  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
  private static final int NUM_IMAGES      = IMAGES.length;
53
  public  static final float PADDING_RATIO = 0.017f;
54
  private static final float TEXT_RATIO_1  = 0.032f;
55
  private static final float TEXT_RATIO_2  = 0.020f;
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 showStatus(PurchaseActivity act)
79
    {
80
    RubikDialogStarsStatus d = new RubikDialogStarsStatus();
81
    d.show(act.getSupportFragmentManager(), null);
82
    }
83

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

    
86
  private void showSuccess(PurchaseActivity act, RubikObject object)
87
    {
88
    act.blockUI();
89
    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
    DataStars data = new DataStars(totStars+price,-price,act.getResources());
96
    stars.startOverlay(screen,this,data);
97
    }
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

    
101
  private void oneButtonClicked(PurchaseActivity act)
102
    {
103
    if( mObject!=null )
104
      {
105
      int price = mObject.getPrice();
106

    
107
      if( chargeUser(price) )
108
        {
109
        RubikObjectList.buyObject(mObject);
110
        showSuccess(act,mObject);
111
        }
112
      else
113
        {
114
        showStatus(act);
115
        }
116
      }
117
    }
118

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

    
121
  private void allButtonClicked(PurchaseActivity act)
122
    {
123
    if( chargeUser(UNLOCK_ALL_PRICE) )
124
      {
125
      RubikObjectList.buyAll();
126
      showSuccess(act,null);
127
      }
128
    else
129
      {
130
      showStatus(act);
131
      }
132
    }
133

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

    
136
  private void setUpButtons(PurchaseActivity act, LinearLayout one, LinearLayout all)
137
    {
138
    ImageButton butO = one.findViewById(R.id.purchaseButtonOne);
139
    ImageButton butA = all.findViewById(R.id.purchaseButtonAll);
140

    
141
    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
    butO.setOnClickListener( new View.OnClickListener()
158
      {
159
      @Override
160
      public void onClick(View v)
161
        {
162
        oneButtonClicked(act);
163
        }
164
      });
165

    
166
    butA.setOnClickListener( new View.OnClickListener()
167
      {
168
      @Override
169
      public void onClick(View v)
170
        {
171
        allButtonClicked(act);
172
        }
173
      });
174
    }
175

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177

    
178
  void updatePane(PurchaseActivity act, int objectOrdinal)
179
    {
180
    mObject = RubikObjectList.getObject(objectOrdinal);
181

    
182
    if( mObject!=null )
183
      {
184
      InputStream stream = mObject.getObjectStream(act);
185
      JsonReader reader = new JsonReader();
186
      String author, name;
187
      int year, difficulty;
188

    
189
      try
190
        {
191
        reader.parseJsonFileMetadata(stream);
192
        name       = reader.getObjectName();
193
        author     = reader.getInventor();
194
        year       = reader.getYearOfInvention();
195
        difficulty = reader.getComplexity();
196
        }
197
      catch(Exception ex)
198
        {
199
        name = "?";
200
        author = "?";
201
        year = 0;
202
        difficulty = 0;
203
        }
204

    
205
      if( difficulty<0           ) difficulty=0;
206
      if( difficulty>=NUM_IMAGES ) difficulty=NUM_IMAGES-1;
207

    
208
      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
      ImageView image = act.findViewById(R.id.purchaseDifficulty);
215
      image.setImageResource(IMAGES[difficulty]);
216
      }
217
    }
218

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

    
221
  PurchaseScreenPane(final PurchaseActivity act)
222
    {
223
    mAct = new WeakReference<>(act);
224
    int height = act.getScreenHeightInPixels();
225
    float textSize1 = height*TEXT_RATIO_1;
226
    float textSize2 = height*TEXT_RATIO_2;
227
    int margin = (int)(height*PADDING_RATIO);
228
    int padding = margin/3;
229

    
230
    LinearLayout upperBar  = act.findViewById(R.id.upperBar);
231
    LinearLayout oneLayout = act.findViewById(R.id.purchaseLayoutOne);
232
    LinearLayout allLayout = act.findViewById(R.id.purchaseLayoutAll);
233

    
234
    upperBar.setPadding(   margin,  margin,  margin,  margin);
235
    oneLayout.setPadding( padding, padding, padding, padding);
236
    allLayout.setPadding( padding, padding, padding, padding);
237

    
238
    LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1.00f);
239
    params1.bottomMargin = 0;
240
    params1.topMargin    = margin;
241
    params1.leftMargin   = margin;
242
    params1.rightMargin  = margin;
243

    
244
    LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1.00f);
245
    params2.bottomMargin = margin;
246
    params2.topMargin    = margin;
247
    params2.leftMargin   = margin;
248
    params2.rightMargin  = margin;
249

    
250
    LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1.00f);
251
    params3.bottomMargin = 0;
252
    params3.topMargin    = 0;
253
    params3.leftMargin   = margin;
254
    params3.rightMargin  = margin;
255

    
256
    upperBar.setLayoutParams(params1);
257
    oneLayout.setLayoutParams(params3);
258
    allLayout.setLayoutParams(params2);
259

    
260
    TextView text;
261
    text = upperBar.findViewById(R.id.purchaseUpperName);
262
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize1);
263
    text = upperBar.findViewById(R.id.purchaseUpperAuthor);
264
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize2);
265
    text = oneLayout.findViewById(R.id.purchaseTextOne);
266
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize1);
267
    text = allLayout.findViewById(R.id.purchaseTextAll);
268
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize1);
269

    
270
    setUpButtons(act,oneLayout,allLayout);
271
    }
272

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