Project

General

Profile

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

magiccube / src / main / java / org / distorted / config / ConfigScreenPane.java @ 804293f0

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.config;
21

    
22
import java.io.InputStream;
23

    
24
import android.util.TypedValue;
25
import android.widget.ImageView;
26
import android.widget.LinearLayout;
27
import android.widget.RadioButton;
28
import android.widget.RadioGroup;
29
import android.widget.TextView;
30

    
31
import org.distorted.jsons.ObjectJson;
32
import org.distorted.main.R;
33
import org.distorted.objectlib.json.JsonReader;
34
import org.distorted.objects.RubikObject;
35
import org.distorted.objects.RubikObjectList;
36

    
37
import static org.distorted.objectlib.main.TwistyObject.MESH_NICE;
38
import static org.distorted.objectlib.main.TwistyObject.MESH_FAST;
39

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

    
42
public class ConfigScreenPane
43
{
44
  private static final int[] IMAGES =
45
    {
46
    R.id.configDifficulty0,
47
    R.id.configDifficulty1,
48
    R.id.configDifficulty2,
49
    R.id.configDifficulty3,
50
    R.id.configDifficulty4
51
    };
52

    
53
  private static final int NUM_IMAGES = IMAGES.length;
54
  public  static final float PADDING_RATIO = 0.025f;
55
  private static final float TEXT_RATIO    = 0.042f;
56
  private static final float RADIO_RATIO   = 0.900f;
57

    
58
  private int mObjectOrdinal;
59
  private boolean mProgramatic;
60

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

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

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

    
72
  void updatePane(ConfigActivity act, int objectOrdinal)
73
    {
74
    JsonReader reader = JsonReader.getInstance();
75

    
76
    mObjectOrdinal = objectOrdinal;
77

    
78
    RubikObject object = RubikObjectList.getObject(objectOrdinal);
79
    int jsonID = object.getJsonID();
80
    InputStream stream = ObjectJson.getObjectStream(jsonID,act);
81
    reader.parseJsonFileMetadata(stream);
82

    
83
    String name = reader.getObjectName();
84
    String author = reader.getInventor();
85
    int year = reader.getYearOfInvention();
86
    String both = year>0 ? author+" "+year : author;
87

    
88
    LinearLayout layout = act.findViewById(R.id.configLayout);
89
    TextView view = layout.findViewById(R.id.configDetailsName2);
90
    view.setText(name);
91
    view = layout.findViewById(R.id.configDetailsAuthor2);
92
    view.setText(both);
93

    
94
    int difficulty = reader.getComplexity();
95

    
96
    if( difficulty<0          ) difficulty=0;
97
    if( difficulty>NUM_IMAGES ) difficulty=NUM_IMAGES;
98

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

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

    
109
    mProgramatic = true;
110
    button.setChecked(true);
111
    mProgramatic = false;
112
    }
113

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

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

    
122
    LinearLayout configLayout    = act.findViewById(R.id.configLayout);
123
    LinearLayout nameLayout      = configLayout.findViewById(R.id.configLayoutName);
124
    LinearLayout authorLayout    = configLayout.findViewById(R.id.configLayoutAuthor);
125
    LinearLayout difficultyLayout= configLayout.findViewById(R.id.configLayoutDifficulty);
126
    LinearLayout meshLayout      = configLayout.findViewById(R.id.configLayoutMesh);
127

    
128
    nameLayout.setPadding(padding,padding,padding,padding/2);
129
    authorLayout.setPadding(padding,padding/2,padding,padding/2);
130
    difficultyLayout.setPadding(padding,padding/2,padding,padding/2);
131
    meshLayout.setPadding(padding,padding/2,padding,padding);
132

    
133
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1.00f);
134
    params.bottomMargin = 0;
135
    params.topMargin    = padding;
136
    params.leftMargin   = padding;
137
    params.rightMargin  = padding;
138

    
139
    nameLayout.setLayoutParams(params);
140
    authorLayout.setLayoutParams(params);
141
    difficultyLayout.setLayoutParams(params);
142
    meshLayout.setLayoutParams(params);
143

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

    
158
    RadioButton butt1 = meshLayout.findViewById(R.id.meshNice);
159
    butt1.setTextSize(TypedValue.COMPLEX_UNIT_PX, RADIO_RATIO*textSize);
160
    RadioButton butt2 = meshLayout.findViewById(R.id.meshSimple);
161
    butt2.setTextSize(TypedValue.COMPLEX_UNIT_PX, RADIO_RATIO*textSize);
162

    
163
    LinearLayout layoutDiff = difficultyLayout.findViewById(R.id.configDifficultyLayout);
164
    layoutDiff.setPadding(padding,padding,padding,padding);
165

    
166
    RadioGroup radioGroup = meshLayout.findViewById(R.id.meshRadioGroup);
167

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

    
181
    updatePane(act,objectOrdinal);
182
    }
183
}
(5-5/6)