Project

General

Profile

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

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

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

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

    
25
///////////////////////////////////////////////////////////////////////////////////////////////////
26

    
27
public class ConfigScreenPane
28
{
29
  private static final int[] IDS =
30
    {
31
    R.id.configDifficulty0,
32
    R.id.configDifficulty1,
33
    R.id.configDifficulty2,
34
    R.id.configDifficulty3,
35
    R.id.configDifficulty4
36
    };
37

    
38
  private static final int[] IMAGES =
39
    {
40
    R.drawable.difficulty1,
41
    R.drawable.difficulty2,
42
    R.drawable.difficulty3,
43
    R.drawable.difficulty4,
44
    R.drawable.difficulty5,
45
    };
46

    
47
  private static final int NUM_IDS         = IDS.length;
48
  private static final float PADDING_RATIO = 0.016f;
49
  private static final float TEXT_RATIO    = 0.025f;
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
  void updatePane(ConfigActivity act, int objectOrdinal)
54
    {
55
    RubikObject object = RubikObjectList.getObject(objectOrdinal);
56

    
57
    if( object!=null )
58
      {
59
      InputStream stream = object.getObjectStream(act);
60

    
61
      JsonReader reader = new JsonReader();
62
      String name,author;
63
      int year, difficulty;
64

    
65
      try
66
        {
67
        reader.parseJsonFileMetadata(stream);
68
        name       = reader.getObjectName();
69
        author     = reader.getInventor();
70
        year       = reader.getYearOfInvention();
71
        difficulty = (int)reader.getComplexity();
72
        }
73
      catch(Exception ex)
74
        {
75
        name = "?";
76
        author = "?";
77
        year = 0;
78
        difficulty = 0;
79
        }
80

    
81
      LinearLayout layout = act.findViewById(R.id.configLayout);
82
      TextView view = layout.findViewById(R.id.configDetailsName2);
83
      view.setText(name);
84
      view = layout.findViewById(R.id.configDetailsAuthor2);
85
      view.setText(author);
86
      view = layout.findViewById(R.id.configDetailsYear2);
87
      view.setText( year>0 ? String.valueOf(year) : "?" );
88

    
89
      if( difficulty<0        ) difficulty=0;
90
      if( difficulty>=NUM_IDS ) difficulty=NUM_IDS-1;
91

    
92
      for(int i=0; i<NUM_IDS; i++)
93
        {
94
        ImageView image = layout.findViewById(IDS[i]);
95
        image.setImageResource(IMAGES[i]);
96
        image.setColorFilter( difficulty==i ? 0xffff0000 : 0xffffffff, PorterDuff.Mode.MULTIPLY );
97
        }
98
      }
99
    }
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102

    
103
  ConfigScreenPane(final ConfigActivity act, int objectOrdinal)
104
    {
105
    int height = act.getScreenHeightInPixels();
106
    float textSize = height*TEXT_RATIO;
107
    int padding = (int)(height*PADDING_RATIO);
108

    
109
    LinearLayout configLayout    = act.findViewById(R.id.configLayout);
110
    LinearLayout nameLayout      = configLayout.findViewById(R.id.configLayoutName);
111
    LinearLayout difficultyLayout= configLayout.findViewById(R.id.configLayoutDifficulty);
112
    LinearLayout authorLayout    = configLayout.findViewById(R.id.configLayoutAuthor);
113
    LinearLayout yearLayout      = configLayout.findViewById(R.id.configLayoutYear);
114

    
115
    nameLayout.setPadding(padding,padding,padding,padding/2);
116
    difficultyLayout.setPadding(padding,padding/2,padding,padding/2);
117
    authorLayout.setPadding(padding,padding/2,padding,padding/2);
118
    yearLayout.setPadding(padding,padding/2,padding,padding/2);
119

    
120
    LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1.00f);
121
    params1.bottomMargin = 0;
122
    params1.topMargin    = padding;
123
    params1.leftMargin   = padding;
124
    params1.rightMargin  = padding;
125

    
126
    LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1.00f);
127
    params2.bottomMargin = padding;
128
    params2.topMargin    = padding;
129
    params2.leftMargin   = padding;
130
    params2.rightMargin  = padding;
131

    
132
    nameLayout.setLayoutParams(params1);
133
    difficultyLayout.setLayoutParams(params1);
134
    authorLayout.setLayoutParams(params1);
135
    yearLayout.setLayoutParams(params2);
136

    
137
    TextView text;
138
    text = nameLayout.findViewById(R.id.configDetailsName1);
139
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
140
    text = nameLayout.findViewById(R.id.configDetailsName2);
141
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
142
    text = authorLayout.findViewById(R.id.configDetailsAuthor1);
143
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
144
    text = authorLayout.findViewById(R.id.configDetailsAuthor2);
145
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
146
    text = difficultyLayout.findViewById(R.id.configDifficultyTitle);
147
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
148
    text = yearLayout.findViewById(R.id.configDetailsYear1);
149
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
150
    text = yearLayout.findViewById(R.id.configDetailsYear2);
151
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
152

    
153
    LinearLayout layoutDiff = difficultyLayout.findViewById(R.id.configDifficultyLayout);
154
    layoutDiff.setPadding(padding,padding,padding,padding);
155

    
156
    updatePane(act,objectOrdinal);
157
    }
158
}
(5-5/6)