Project

General

Profile

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

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

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

    
19
import org.distorted.main.R;
20
import org.distorted.objectlib.json.JsonReader;
21
import org.distorted.objects.RubikObject;
22
import org.distorted.objects.RubikObjectList;
23

    
24
import java.io.InputStream;
25

    
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27

    
28
public class PurchaseScreenPane
29
{
30
  private static final int UNLOCK_ALL_PRICE = 1000;
31

    
32
  private static final int[] IMAGES =
33
    {
34
    R.drawable.difficulty1,
35
    R.drawable.difficulty2,
36
    R.drawable.difficulty3,
37
    R.drawable.difficulty4,
38
    R.drawable.difficulty5,
39
    };
40

    
41
  private static final int NUM_IMAGES      = IMAGES.length;
42
  public  static final float PADDING_RATIO = 0.025f;
43
  private static final float TEXT_RATIO    = 0.050f;
44

    
45
  private RubikObject mObject;
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48
// TODO: charge the user an amount of stars
49

    
50
  private boolean chargeUser(RubikObject object)
51
    {
52
    return true;
53
    }
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56
// TODO: charge the user an amount of stars
57

    
58
  private boolean chargeUser()
59
    {
60
    return true;
61
    }
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

    
65
  private void oneButtonClicked()
66
    {
67
    if( mObject!=null )
68
      {
69
      if( !RubikObjectList.objectAlreadyBought(mObject) && chargeUser(mObject) )
70
        {
71
        android.util.Log.e("D", "buying "+mObject.getUpperName());
72
        RubikObjectList.buyObject(mObject);
73
        }
74
      }
75
    }
76

    
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78

    
79
  private void allButtonClicked()
80
    {
81
    if( !RubikObjectList.allAlreadyBought() && chargeUser() )
82
      {
83
      RubikObjectList.buyAll();
84
      }
85
    }
86

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

    
89
  private void setUpButtons(LinearLayout one, LinearLayout all)
90
    {
91
    Button butO = one.findViewById(R.id.purchaseButtonOne);
92
    Button butA = all.findViewById(R.id.purchaseButtonAll);
93

    
94
    butO.setOnClickListener( new View.OnClickListener()
95
      {
96
      @Override
97
      public void onClick(View v)
98
        {
99
        oneButtonClicked();
100
        }
101
      });
102

    
103
    butA.setOnClickListener( new View.OnClickListener()
104
      {
105
      @Override
106
      public void onClick(View v)
107
        {
108
        allButtonClicked();
109
        }
110
      });
111
    }
112

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114

    
115
  void updatePane(PurchaseActivity act, int objectOrdinal)
116
    {
117
    mObject = RubikObjectList.getObject(objectOrdinal);
118

    
119
    if( mObject!=null )
120
      {
121
      InputStream stream = mObject.getObjectStream(act);
122
      JsonReader reader = JsonReader.getInstance();
123
      String author, name;
124
      int year, difficulty;
125

    
126
      try
127
        {
128
        reader.parseJsonFileMetadata(stream);
129
        name       = reader.getObjectName();
130
        author     = reader.getInventor();
131
        year       = reader.getYearOfInvention();
132
        difficulty = reader.getComplexity();
133
        }
134
      catch(Exception ex)
135
        {
136
        name = "?";
137
        author = "?";
138
        year = 0;
139
        difficulty = 0;
140
        }
141

    
142
      if( difficulty<0           ) difficulty=0;
143
      if( difficulty>=NUM_IMAGES ) difficulty=NUM_IMAGES-1;
144

    
145
      String both = year>0 ? author+" "+year : author;
146

    
147
      TextView v1 = act.findViewById(R.id.purchaseUpperName);
148
      v1.setText(name);
149
      TextView v2 = act.findViewById(R.id.purchaseUpperAuthor);
150
      v2.setText(both);
151
      ImageView image = act.findViewById(R.id.purchaseDifficulty);
152
      image.setImageResource(IMAGES[difficulty]);
153
      }
154
    }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

    
158
  PurchaseScreenPane(final PurchaseActivity act)
159
    {
160
    int width = act.getScreenWidthInPixels();
161
    float textSize = width*TEXT_RATIO;
162
    int padding = (int)(width*PADDING_RATIO);
163

    
164
    LinearLayout upperBar  = act.findViewById(R.id.upperBar);
165
    LinearLayout oneLayout = act.findViewById(R.id.purchaseLayoutOne);
166
    LinearLayout allLayout = act.findViewById(R.id.purchaseLayoutAll);
167

    
168
    upperBar.setPadding( padding,padding  ,padding,padding  );
169
    oneLayout.setPadding(padding,padding/2,padding,padding/2);
170
    allLayout.setPadding(padding,padding/2,padding,padding/2);
171

    
172
    LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1.00f);
173
    params1.bottomMargin = 0;
174
    params1.topMargin    = padding;
175
    params1.leftMargin   = padding;
176
    params1.rightMargin  = padding;
177

    
178
    LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1.00f);
179
    params2.bottomMargin = padding;
180
    params2.topMargin    = padding;
181
    params2.leftMargin   = padding;
182
    params2.rightMargin  = padding;
183

    
184
    LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1.00f);
185
    params3.bottomMargin = 0;
186
    params3.topMargin    = 0;
187
    params3.leftMargin   = padding;
188
    params3.rightMargin  = padding;
189

    
190
    upperBar.setLayoutParams(params1);
191
    oneLayout.setLayoutParams(params3);
192
    allLayout.setLayoutParams(params2);
193

    
194
    TextView text;
195
    text = upperBar.findViewById(R.id.purchaseUpperName);
196
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
197
    text = upperBar.findViewById(R.id.purchaseUpperAuthor);
198
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
199
    text = oneLayout.findViewById(R.id.purchaseTextOne);
200
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
201
    text = allLayout.findViewById(R.id.purchaseTextAll);
202
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
203

    
204
    setUpButtons(oneLayout,allLayout);
205
    }
206
}
(5-5/6)