Project

General

Profile

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

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

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
import android.util.TypedValue;
13 7480fbab Leszek Koltunski
import android.view.View;
14
import android.widget.Button;
15 c7238c67 Leszek Koltunski
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 e9e744f7 Leszek Koltunski
  private static final int UNLOCK_ALL_PRICE = 1000;
31
32 c7238c67 Leszek Koltunski
  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 71cda061 Leszek Koltunski
  private static final int NUM_IMAGES      = IMAGES.length;
42 c7238c67 Leszek Koltunski
  public  static final float PADDING_RATIO = 0.025f;
43 cd432dd3 Leszek Koltunski
  private static final float TEXT_RATIO    = 0.050f;
44 c7238c67 Leszek Koltunski
45 7480fbab Leszek Koltunski
  private RubikObject mObject;
46
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48 e9e744f7 Leszek Koltunski
// TODO: charge the user an amount of stars
49 7480fbab Leszek Koltunski
50 e9e744f7 Leszek Koltunski
  private boolean chargeUser(RubikObject object)
51 7480fbab Leszek Koltunski
    {
52
    return true;
53
    }
54
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56 e9e744f7 Leszek Koltunski
// TODO: charge the user an amount of stars
57 7480fbab Leszek Koltunski
58 e9e744f7 Leszek Koltunski
  private boolean chargeUser()
59 7480fbab Leszek Koltunski
    {
60
    return true;
61
    }
62
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64
65
  private void oneButtonClicked()
66
    {
67
    if( mObject!=null )
68
      {
69 e9e744f7 Leszek Koltunski
      if( !RubikObjectList.objectAlreadyBought(mObject) && chargeUser(mObject) )
70 7480fbab Leszek Koltunski
        {
71 e9e744f7 Leszek Koltunski
        android.util.Log.e("D", "buying "+mObject.getUpperName());
72
        RubikObjectList.buyObject(mObject);
73 7480fbab Leszek Koltunski
        }
74
      }
75
    }
76
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78
79
  private void allButtonClicked()
80
    {
81 e9e744f7 Leszek Koltunski
    if( !RubikObjectList.allAlreadyBought() && chargeUser() )
82 7480fbab Leszek Koltunski
      {
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 c7238c67 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
114
115
  void updatePane(PurchaseActivity act, int objectOrdinal)
116
    {
117 7480fbab Leszek Koltunski
    mObject = RubikObjectList.getObject(objectOrdinal);
118 c7238c67 Leszek Koltunski
119 7480fbab Leszek Koltunski
    if( mObject!=null )
120 c7238c67 Leszek Koltunski
      {
121 7480fbab Leszek Koltunski
      InputStream stream = mObject.getObjectStream(act);
122 c7238c67 Leszek Koltunski
      JsonReader reader = JsonReader.getInstance();
123 cd432dd3 Leszek Koltunski
      String author, name;
124
      int year, difficulty;
125 c7238c67 Leszek Koltunski
126
      try
127
        {
128
        reader.parseJsonFileMetadata(stream);
129
        name       = reader.getObjectName();
130 cd432dd3 Leszek Koltunski
        author     = reader.getInventor();
131
        year       = reader.getYearOfInvention();
132 c7238c67 Leszek Koltunski
        difficulty = reader.getComplexity();
133
        }
134
      catch(Exception ex)
135
        {
136
        name = "?";
137 cd432dd3 Leszek Koltunski
        author = "?";
138
        year = 0;
139 c7238c67 Leszek Koltunski
        difficulty = 0;
140
        }
141
142 71cda061 Leszek Koltunski
      if( difficulty<0           ) difficulty=0;
143
      if( difficulty>=NUM_IMAGES ) difficulty=NUM_IMAGES-1;
144 c7238c67 Leszek Koltunski
145 cd432dd3 Leszek Koltunski
      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 71cda061 Leszek Koltunski
      ImageView image = act.findViewById(R.id.purchaseDifficulty);
152
      image.setImageResource(IMAGES[difficulty]);
153 c7238c67 Leszek Koltunski
      }
154
    }
155
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157
158 71cda061 Leszek Koltunski
  PurchaseScreenPane(final PurchaseActivity act)
159 c7238c67 Leszek Koltunski
    {
160
    int width = act.getScreenWidthInPixels();
161
    float textSize = width*TEXT_RATIO;
162
    int padding = (int)(width*PADDING_RATIO);
163
164 cd432dd3 Leszek Koltunski
    LinearLayout upperBar  = act.findViewById(R.id.upperBar);
165
    LinearLayout oneLayout = act.findViewById(R.id.purchaseLayoutOne);
166
    LinearLayout allLayout = act.findViewById(R.id.purchaseLayoutAll);
167 c7238c67 Leszek Koltunski
168 af0710b6 Leszek Koltunski
    upperBar.setPadding( padding,padding  ,padding,padding  );
169 71cda061 Leszek Koltunski
    oneLayout.setPadding(padding,padding/2,padding,padding/2);
170
    allLayout.setPadding(padding,padding/2,padding,padding/2);
171 c7238c67 Leszek Koltunski
172 cd432dd3 Leszek Koltunski
    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 c7238c67 Leszek Koltunski
184 af0710b6 Leszek Koltunski
    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 cd432dd3 Leszek Koltunski
    upperBar.setLayoutParams(params1);
191 af0710b6 Leszek Koltunski
    oneLayout.setLayoutParams(params3);
192 cd432dd3 Leszek Koltunski
    allLayout.setLayoutParams(params2);
193 c7238c67 Leszek Koltunski
194
    TextView text;
195 cd432dd3 Leszek Koltunski
    text = upperBar.findViewById(R.id.purchaseUpperName);
196
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
197
    text = upperBar.findViewById(R.id.purchaseUpperAuthor);
198 c7238c67 Leszek Koltunski
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
199 71cda061 Leszek Koltunski
    text = oneLayout.findViewById(R.id.purchaseTextOne);
200 c7238c67 Leszek Koltunski
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
201 71cda061 Leszek Koltunski
    text = allLayout.findViewById(R.id.purchaseTextAll);
202 c7238c67 Leszek Koltunski
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
203 7480fbab Leszek Koltunski
204
    setUpButtons(oneLayout,allLayout);
205 c7238c67 Leszek Koltunski
    }
206
}