Project

General

Profile

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

magiccube / src / main / java / org / distorted / config / ConfigScreenPane.java @ 4893ad8a

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.040f;
56
  private static final float RADIO_RATIO   = 0.900f;
57

    
58
  private JsonReader mReader;
59
  private int mObjectOrdinal;
60
  private boolean mProgramatic;
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

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

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72

    
73
  void updatePane(ConfigActivity act, int objectOrdinal)
74
    {
75
    if( mReader==null ) mReader = new JsonReader();
76

    
77
    mObjectOrdinal = objectOrdinal;
78

    
79
    RubikObject object = RubikObjectList.getObject(objectOrdinal);
80
    int jsonID = object.getJsonID();
81
    InputStream stream = ObjectJson.getStream(jsonID,act);
82
    mReader.parseJsonFileMetadata(stream);
83

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

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

    
95
    int difficulty = mReader.getComplexity();
96

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

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

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

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

    
114
    int numFaces = mReader.getNumFaces();
115
    act.rotateObject(numFaces);
116
    }
117

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

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

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

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

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

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

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

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

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

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

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

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