Project

General

Profile

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

magiccube / src / main / java / org / distorted / config / ConfigScreenPane.java @ c020555e

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.main.R;
32
import org.distorted.objectlib.json.JsonReader;
33
import org.distorted.objects.RubikObject;
34
import org.distorted.objects.RubikObjectList;
35

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

    
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40

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

    
52
  private static final int NUM_IMAGES = IMAGES.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_IMAGES ) difficulty=NUM_IMAGES;
110

    
111
      for(int i=0; i<NUM_IMAGES; i++)
112
        {
113
        ImageView image = layout.findViewById(IMAGES[i]);
114
        image.setImageResource( i==difficulty ? R.drawable.ui_difficulty_checked : R.drawable.ui_difficulty_unchecked );
115
        }
116

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

    
121
      mProgramatic = true;
122
      button.setChecked(true);
123
      mProgramatic = false;
124
      }
125
    }
126

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

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

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

    
141
    nameLayout.setPadding(padding,padding,padding,padding/2);
142
    authorLayout.setPadding(padding,padding/2,padding,padding/2);
143
    difficultyLayout.setPadding(padding,padding/2,padding,padding/2);
144
    meshLayout.setPadding(padding,padding/2,padding,padding);
145

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

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

    
157
    TextView text;
158
    text = nameLayout.findViewById(R.id.configDetailsName1);
159
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
160
    text = nameLayout.findViewById(R.id.configDetailsName2);
161
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
162
    text = authorLayout.findViewById(R.id.configDetailsAuthor1);
163
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
164
    text = authorLayout.findViewById(R.id.configDetailsAuthor2);
165
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
166
    text = difficultyLayout.findViewById(R.id.configDifficultyTitle);
167
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
168
    text = meshLayout.findViewById(R.id.configMeshTitle);
169
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
170

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

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

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

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

    
194
    updatePane(act,objectOrdinal);
195
    }
196
}
(5-5/6)