Project

General

Profile

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

magiccube / src / main / java / org / distorted / config / ConfigScreenPane.java @ 306aa049

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.util.TypedValue;
15
import android.widget.ImageView;
16
import android.widget.LinearLayout;
17
import android.widget.RadioButton;
18
import android.widget.RadioGroup;
19
import android.widget.TextView;
20

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

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

    
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30

    
31
public class ConfigScreenPane
32
{
33
  private static final int[] IMAGES =
34
    {
35
    R.id.configDifficulty0,
36
    R.id.configDifficulty1,
37
    R.id.configDifficulty2,
38
    R.id.configDifficulty3,
39
    R.id.configDifficulty4
40
    };
41

    
42
  private static final int NUM_IMAGES = IMAGES.length;
43
  public  static final float PADDING_RATIO = 0.025f;
44
  private static final float TEXT_RATIO    = 0.042f;
45
  private static final float RADIO_RATIO   = 0.900f;
46

    
47
  private int mObjectOrdinal;
48
  private boolean mProgramatic;
49

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
  private void switchMeshState(ConfigActivity act, int meshState)
53
    {
54
    RubikObjectList.setMeshState(mObjectOrdinal,meshState);
55
    RubikObject object = RubikObjectList.getObject(mObjectOrdinal);
56
    act.changeMeshState(object,mObjectOrdinal);
57
    }
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

    
61
  void updatePane(ConfigActivity act, int objectOrdinal)
62
    {
63
    RubikObject object = RubikObjectList.getObject(objectOrdinal);
64

    
65
    if( object!=null )
66
      {
67
      InputStream stream = object.getObjectStream(act);
68

    
69
      mObjectOrdinal = objectOrdinal;
70
      JsonReader reader = JsonReader.getInstance();
71
      String name,author;
72
      int year, difficulty;
73

    
74
      try
75
        {
76
        reader.parseJsonFileMetadata(stream);
77
        name       = reader.getObjectName();
78
        author     = reader.getInventor();
79
        year       = reader.getYearOfInvention();
80
        difficulty = reader.getComplexity();
81
        }
82
      catch(Exception ex)
83
        {
84
        name = "?";
85
        author = "?";
86
        year = 0;
87
        difficulty = 0;
88
        }
89

    
90
      String both = year>0 ? author+" "+year : author;
91

    
92
      LinearLayout layout = act.findViewById(R.id.configLayout);
93
      TextView view = layout.findViewById(R.id.configDetailsName2);
94
      view.setText(name);
95
      view = layout.findViewById(R.id.configDetailsAuthor2);
96
      view.setText(both);
97

    
98
      if( difficulty<0          ) difficulty=0;
99
      if( difficulty>NUM_IMAGES ) difficulty=NUM_IMAGES;
100

    
101
      for(int i=0; i<NUM_IMAGES; i++)
102
        {
103
        ImageView image = layout.findViewById(IMAGES[i]);
104
        image.setImageResource( i==difficulty ? R.drawable.ui_difficulty_checked : R.drawable.ui_difficulty_unchecked );
105
        }
106

    
107
      int meshState = object.getMeshState();
108
      int id = meshState==MESH_NICE ? R.id.meshNice : R.id.meshSimple;
109
      RadioButton button = act.findViewById(id);
110

    
111
      mProgramatic = true;
112
      button.setChecked(true);
113
      mProgramatic = false;
114
      }
115
    }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

    
119
  ConfigScreenPane(final ConfigActivity act, int objectOrdinal)
120
    {
121
    int width = act.getScreenWidthInPixels();
122
    float textSize = width*TEXT_RATIO;
123
    int padding = (int)(width*PADDING_RATIO);
124

    
125
    LinearLayout configLayout    = act.findViewById(R.id.configLayout);
126
    LinearLayout nameLayout      = configLayout.findViewById(R.id.configLayoutName);
127
    LinearLayout authorLayout    = configLayout.findViewById(R.id.configLayoutAuthor);
128
    LinearLayout difficultyLayout= configLayout.findViewById(R.id.configLayoutDifficulty);
129
    LinearLayout meshLayout      = configLayout.findViewById(R.id.configLayoutMesh);
130

    
131
    nameLayout.setPadding(padding,padding,padding,padding/2);
132
    authorLayout.setPadding(padding,padding/2,padding,padding/2);
133
    difficultyLayout.setPadding(padding,padding/2,padding,padding/2);
134
    meshLayout.setPadding(padding,padding/2,padding,padding);
135

    
136
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1.00f);
137
    params.bottomMargin = 0;
138
    params.topMargin    = padding;
139
    params.leftMargin   = padding;
140
    params.rightMargin  = padding;
141

    
142
    nameLayout.setLayoutParams(params);
143
    authorLayout.setLayoutParams(params);
144
    difficultyLayout.setLayoutParams(params);
145
    meshLayout.setLayoutParams(params);
146

    
147
    TextView text;
148
    text = nameLayout.findViewById(R.id.configDetailsName1);
149
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
150
    text = nameLayout.findViewById(R.id.configDetailsName2);
151
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
152
    text = authorLayout.findViewById(R.id.configDetailsAuthor1);
153
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
154
    text = authorLayout.findViewById(R.id.configDetailsAuthor2);
155
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
156
    text = difficultyLayout.findViewById(R.id.configDifficultyTitle);
157
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
158
    text = meshLayout.findViewById(R.id.configMeshTitle);
159
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
160

    
161
    RadioButton butt1 = meshLayout.findViewById(R.id.meshNice);
162
    butt1.setTextSize(TypedValue.COMPLEX_UNIT_PX, RADIO_RATIO*textSize);
163
    RadioButton butt2 = meshLayout.findViewById(R.id.meshSimple);
164
    butt2.setTextSize(TypedValue.COMPLEX_UNIT_PX, RADIO_RATIO*textSize);
165

    
166
    LinearLayout layoutDiff = difficultyLayout.findViewById(R.id.configDifficultyLayout);
167
    layoutDiff.setPadding(padding,padding,padding,padding);
168

    
169
    RadioGroup radioGroup = meshLayout.findViewById(R.id.meshRadioGroup);
170

    
171
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
172
      {
173
      @Override
174
      public void onCheckedChanged(RadioGroup group, int checkedId)
175
        {
176
        if( !mProgramatic )
177
          {
178
          int meshState = checkedId == R.id.meshNice ? MESH_NICE : MESH_FAST;
179
          switchMeshState( act, meshState );
180
          }
181
        }
182
      });
183

    
184
    updatePane(act,objectOrdinal);
185
    }
186
}
(5-5/6)