Project

General

Profile

« Previous | Next » 

Revision 71cda061

Added by Leszek Koltunski over 1 year ago

IAP part 5: new 'Purchase' activity (cont'd)

View differences:

src/main/java/org/distorted/purchase/PurchaseScreenPane.java
9 9

  
10 10
package org.distorted.purchase;
11 11

  
12
import android.graphics.PorterDuff;
13 12
import android.util.TypedValue;
14 13
import android.widget.ImageView;
15 14
import android.widget.LinearLayout;
16
import android.widget.RadioButton;
17
import android.widget.RadioGroup;
15
import android.widget.RelativeLayout;
18 16
import android.widget.TextView;
19 17

  
20 18
import org.distorted.main.R;
......
24 22

  
25 23
import java.io.InputStream;
26 24

  
27
import static org.distorted.objectlib.main.TwistyObject.MESH_FAST;
28
import static org.distorted.objectlib.main.TwistyObject.MESH_NICE;
29

  
30 25
///////////////////////////////////////////////////////////////////////////////////////////////////
31 26

  
32 27
public class PurchaseScreenPane
33 28
{
34
  private static final int[] IDS =
35
    {
36
    R.id.purchaseDifficulty0,
37
    R.id.purchaseDifficulty1,
38
    R.id.purchaseDifficulty2,
39
    R.id.purchaseDifficulty3,
40
    R.id.purchaseDifficulty4
41
    };
42

  
43 29
  private static final int[] IMAGES =
44 30
    {
45 31
    R.drawable.difficulty1,
......
49 35
    R.drawable.difficulty5,
50 36
    };
51 37

  
52
  private static final int NUM_IDS         = IDS.length;
38
  private static final int NUM_IMAGES      = IMAGES.length;
53 39
  public  static final float PADDING_RATIO = 0.025f;
54 40
  private static final float TEXT_RATIO    = 0.042f;
55
  private static final float RADIO_RATIO   = 0.900f;
56

  
57
  private int mObjectOrdinal;
58
  private boolean mProgramatic;
59

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

  
62
  private void switchMeshState(PurchaseActivity act, int meshState)
63
    {
64
    RubikObjectList.setMeshState(mObjectOrdinal,meshState);
65
    RubikObject object = RubikObjectList.getObject(mObjectOrdinal);
66
    act.changeMeshState(object,mObjectOrdinal);
67
    }
68 41

  
69 42
///////////////////////////////////////////////////////////////////////////////////////////////////
70 43

  
......
75 48
    if( object!=null )
76 49
      {
77 50
      InputStream stream = object.getObjectStream(act);
78

  
79
      mObjectOrdinal = objectOrdinal;
80 51
      JsonReader reader = JsonReader.getInstance();
81
      String name,author;
82
      int year, difficulty;
52
      String name;
53
      int difficulty;
83 54

  
84 55
      try
85 56
        {
86 57
        reader.parseJsonFileMetadata(stream);
87 58
        name       = reader.getObjectName();
88
        author     = reader.getInventor();
89
        year       = reader.getYearOfInvention();
90 59
        difficulty = reader.getComplexity();
91 60
        }
92 61
      catch(Exception ex)
93 62
        {
94 63
        name = "?";
95
        author = "?";
96
        year = 0;
97 64
        difficulty = 0;
98 65
        }
99 66

  
100
      String both = year>0 ? author+" "+year : author;
67
      if( difficulty<0           ) difficulty=0;
68
      if( difficulty>=NUM_IMAGES ) difficulty=NUM_IMAGES-1;
101 69

  
102
      LinearLayout layout = act.findViewById(R.id.purchaseLayout);
103
      TextView view = layout.findViewById(R.id.purchaseDetailsName2);
70
      TextView view = act.findViewById(R.id.purchaseDetailsName);
104 71
      view.setText(name);
105
      view = layout.findViewById(R.id.purchaseDetailsAuthor2);
106
      view.setText(both);
107

  
108
      if( difficulty<0       ) difficulty=0;
109
      if( difficulty>NUM_IDS ) difficulty=NUM_IDS;
110

  
111
      for(int i=0; i<NUM_IDS; i++)
112
        {
113
        ImageView image = layout.findViewById(IDS[i]);
114
        image.setImageResource(IMAGES[i]);
115
        image.setColorFilter( difficulty==i ? 0xffff0000 : 0xffffffff, PorterDuff.Mode.MULTIPLY );
116
        }
117

  
118
      int meshState = object.getMeshState();
119
      int id = meshState==MESH_NICE ? R.id.meshNice : R.id.meshSimple;
120
      RadioButton button = act.findViewById(id);
121

  
122
      mProgramatic = true;
123
      button.setChecked(true);
124
      mProgramatic = false;
72
      ImageView image = act.findViewById(R.id.purchaseDifficulty);
73
      image.setImageResource(IMAGES[difficulty]);
125 74
      }
126 75
    }
127 76

  
128 77
///////////////////////////////////////////////////////////////////////////////////////////////////
129 78

  
130
  PurchaseScreenPane(final PurchaseActivity act, int objectOrdinal)
79
  PurchaseScreenPane(final PurchaseActivity act)
131 80
    {
132 81
    int width = act.getScreenWidthInPixels();
133 82
    float textSize = width*TEXT_RATIO;
134 83
    int padding = (int)(width*PADDING_RATIO);
135 84

  
136
    LinearLayout configLayout    = act.findViewById(R.id.purchaseLayout);
137
    LinearLayout nameLayout      = configLayout.findViewById(R.id.purchaseLayoutName);
138
    LinearLayout authorLayout    = configLayout.findViewById(R.id.purchaseLayoutAuthor);
139
    LinearLayout difficultyLayout= configLayout.findViewById(R.id.purchaseLayoutDifficulty);
140
    LinearLayout meshLayout      = configLayout.findViewById(R.id.purchaseLayoutMesh);
85
    RelativeLayout upperBar  = act.findViewById(R.id.upperBar);
86
    LinearLayout oneLayout   = act.findViewById(R.id.purchaseLayoutOne);
87
    LinearLayout allLayout   = act.findViewById(R.id.purchaseLayoutAll);
141 88

  
142
    nameLayout.setPadding(padding,padding,padding,padding/2);
143
    authorLayout.setPadding(padding,padding/2,padding,padding/2);
144
    difficultyLayout.setPadding(padding,padding/2,padding,padding/2);
145
    meshLayout.setPadding(padding,padding/2,padding,padding);
89
    upperBar.setPadding(padding,padding,padding,padding/2);
90
    oneLayout.setPadding(padding,padding/2,padding,padding/2);
91
    allLayout.setPadding(padding,padding/2,padding,padding/2);
146 92

  
147 93
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1.00f);
148 94
    params.bottomMargin = 0;
......
150 96
    params.leftMargin   = padding;
151 97
    params.rightMargin  = padding;
152 98

  
153
    nameLayout.setLayoutParams(params);
154
    authorLayout.setLayoutParams(params);
155
    difficultyLayout.setLayoutParams(params);
156
    meshLayout.setLayoutParams(params);
99
    upperBar.setLayoutParams(params);
100
    oneLayout.setLayoutParams(params);
101
    allLayout.setLayoutParams(params);
157 102

  
158 103
    TextView text;
159
    text = nameLayout.findViewById(R.id.purchaseDetailsName1);
160
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
161
    text = nameLayout.findViewById(R.id.purchaseDetailsName2);
104
    text = upperBar.findViewById(R.id.purchaseDetailsName);
162 105
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
163
    text = authorLayout.findViewById(R.id.purchaseDetailsAuthor1);
106
    text = oneLayout.findViewById(R.id.purchaseTextOne);
164 107
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
165
    text = authorLayout.findViewById(R.id.purchaseDetailsAuthor2);
108
    text = allLayout.findViewById(R.id.purchaseTextAll);
166 109
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
167
    text = difficultyLayout.findViewById(R.id.purchaseDifficultyTitle);
168
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
169
    text = meshLayout.findViewById(R.id.purchaseMeshTitle);
170
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
171

  
172
    RadioButton butt1 = meshLayout.findViewById(R.id.meshNice);
173
    butt1.setTextSize(TypedValue.COMPLEX_UNIT_PX, RADIO_RATIO*textSize);
174
    RadioButton butt2 = meshLayout.findViewById(R.id.meshSimple);
175
    butt2.setTextSize(TypedValue.COMPLEX_UNIT_PX, RADIO_RATIO*textSize);
176

  
177
    LinearLayout layoutDiff = difficultyLayout.findViewById(R.id.purchaseDifficultyLayout);
178
    layoutDiff.setPadding(padding,padding,padding,padding);
179

  
180
    RadioGroup radioGroup = meshLayout.findViewById(R.id.meshRadioGroup);
181

  
182
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
183
      {
184
      @Override
185
      public void onCheckedChanged(RadioGroup group, int checkedId)
186
        {
187
        if( !mProgramatic )
188
          {
189
          int meshState = checkedId == R.id.meshNice ? MESH_NICE : MESH_FAST;
190
          switchMeshState( act, meshState );
191
          }
192
        }
193
      });
194

  
195
    updatePane(act,objectOrdinal);
196 110
    }
197 111
}

Also available in: Unified diff