Project

General

Profile

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

magiccube / src / main / java / org / distorted / purchase / PurchaseScreenPane.java @ 7480fbab

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[] IMAGES =
31
    {
32
    R.drawable.difficulty1,
33
    R.drawable.difficulty2,
34
    R.drawable.difficulty3,
35
    R.drawable.difficulty4,
36
    R.drawable.difficulty5,
37
    };
38

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

    
43
  private RubikObject mObject;
44

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46
// TODO
47

    
48
  private boolean buyOne(String shortName)
49
    {
50
    return true;
51
    }
52

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54
// TODO
55

    
56
  private boolean buyAll()
57
    {
58
    return true;
59
    }
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
  private void oneButtonClicked()
64
    {
65
    if( mObject!=null )
66
      {
67
      String shortName = mObject.getUpperName();
68

    
69
      if( !RubikObjectList.objectAlreadyBought(shortName) && buyOne(shortName) )
70
        {
71
        android.util.Log.e("D", "buying "+shortName);
72

    
73
        RubikObjectList.buyObject(shortName);
74
        }
75
      }
76
    }
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79

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

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89

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

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

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

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

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

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

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

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

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

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

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

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

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

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

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

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

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

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

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

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