Project

General

Profile

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

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

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
      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

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

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

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

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

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

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

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

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

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

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

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