Project

General

Profile

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

magiccube / src / main / java / org / distorted / config / ConfigScreenPane.java @ 71cda061

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.config;
11

    
12
import java.io.InputStream;
13

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

    
22
import org.distorted.main.R;
23
import org.distorted.objectlib.json.JsonReader;
24
import org.distorted.objects.RubikObject;
25
import org.distorted.objects.RubikObjectList;
26

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

    
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31

    
32
public class ConfigScreenPane
33
{
34
  private static final int[] IDS =
35
    {
36
    R.id.configDifficulty0,
37
    R.id.configDifficulty1,
38
    R.id.configDifficulty2,
39
    R.id.configDifficulty3,
40
    R.id.configDifficulty4
41
    };
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_IDS         = IDS.length;
53
  public  static final float PADDING_RATIO = 0.025f;
54
  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(ConfigActivity act, int meshState)
63
    {
64
    RubikObjectList.setMeshState(mObjectOrdinal,meshState);
65
    RubikObject object = RubikObjectList.getObject(mObjectOrdinal);
66
    act.changeMeshState(object,mObjectOrdinal);
67
    }
68

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

    
71
  void updatePane(ConfigActivity act, int objectOrdinal)
72
    {
73
    RubikObject object = RubikObjectList.getObject(objectOrdinal);
74

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

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

    
84
      try
85
        {
86
        reader.parseJsonFileMetadata(stream);
87
        name       = reader.getObjectName();
88
        author     = reader.getInventor();
89
        year       = reader.getYearOfInvention();
90
        difficulty = reader.getComplexity();
91
        }
92
      catch(Exception ex)
93
        {
94
        name = "?";
95
        author = "?";
96
        year = 0;
97
        difficulty = 0;
98
        }
99

    
100
      String both = year>0 ? author+" "+year : author;
101

    
102
      LinearLayout layout = act.findViewById(R.id.configLayout);
103
      TextView view = layout.findViewById(R.id.configDetailsName2);
104
      view.setText(name);
105
      view = layout.findViewById(R.id.configDetailsAuthor2);
106
      view.setText(both);
107

    
108
      if( difficulty<0        ) difficulty=0;
109
      if( difficulty>=NUM_IDS ) difficulty=NUM_IDS-1;
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;
125
      }
126
    }
127

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

    
130
  ConfigScreenPane(final ConfigActivity act, int objectOrdinal)
131
    {
132
    int width = act.getScreenWidthInPixels();
133
    float textSize = width*TEXT_RATIO;
134
    int padding = (int)(width*PADDING_RATIO);
135

    
136
    LinearLayout configLayout    = act.findViewById(R.id.configLayout);
137
    LinearLayout nameLayout      = configLayout.findViewById(R.id.configLayoutName);
138
    LinearLayout authorLayout    = configLayout.findViewById(R.id.configLayoutAuthor);
139
    LinearLayout difficultyLayout= configLayout.findViewById(R.id.configLayoutDifficulty);
140
    LinearLayout meshLayout      = configLayout.findViewById(R.id.configLayoutMesh);
141

    
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);
146

    
147
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1.00f);
148
    params.bottomMargin = 0;
149
    params.topMargin    = padding;
150
    params.leftMargin   = padding;
151
    params.rightMargin  = padding;
152

    
153
    nameLayout.setLayoutParams(params);
154
    authorLayout.setLayoutParams(params);
155
    difficultyLayout.setLayoutParams(params);
156
    meshLayout.setLayoutParams(params);
157

    
158
    TextView text;
159
    text = nameLayout.findViewById(R.id.configDetailsName1);
160
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
161
    text = nameLayout.findViewById(R.id.configDetailsName2);
162
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
163
    text = authorLayout.findViewById(R.id.configDetailsAuthor1);
164
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
165
    text = authorLayout.findViewById(R.id.configDetailsAuthor2);
166
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
167
    text = difficultyLayout.findViewById(R.id.configDifficultyTitle);
168
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
169
    text = meshLayout.findViewById(R.id.configMeshTitle);
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.configDifficultyLayout);
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
    }
197
}
(5-5/6)