Project

General

Profile

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

magiccube / src / main / java / org / distorted / config / ConfigScreenPane.java @ 9211042f

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2024 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 android.app.Activity;
13
import android.util.TypedValue;
14
import android.view.View;
15
import android.view.ViewGroup;
16
import android.widget.GridLayout;
17
import android.widget.ImageButton;
18
import android.widget.ImageView;
19
import android.widget.LinearLayout;
20
import android.widget.SeekBar;
21
import android.widget.TextView;
22

    
23
import androidx.appcompat.widget.AppCompatImageButton;
24

    
25
import org.distorted.main.MainActivity;
26
import org.distorted.main.R;
27
import org.distorted.os.OSInterface;
28

    
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30

    
31
public class ConfigScreenPane
32
{
33
  public static final int DEFAULT_BORDERS = 2;
34
  public static final int DEFAULT_CORNERS = 2;
35

    
36
  public static final float[] BORDER_STEPS = { 0.50f, 0.75f, 1.00f, 1.50f, 2.00f };
37
  public static final float[] CORNER_STEPS = { 0.00f, 0.50f, 1.00f, 1.50f, 2.00f };
38

    
39
  private static final float PADDING_RATIO = 0.010f;
40
  private static final float MARGIN_RATIO  = 0.017f;
41
  private static final float TEXT_RATIO    = 0.025f;
42

    
43
  static final int ICO_01= 0xffe191b4;
44
  static final int ICO_02= 0xff604c94;
45
  static final int ICO_03= 0xff472258;
46
  static final int ICO_04= 0xff591218;
47
  static final int ICO_05= 0xffba365b;
48
  static final int ICO_06= 0xffe4e000;
49
  static final int ICO_07= 0xff4a90d5;
50
  static final int ICO_08= 0xff0e28a3;
51
  static final int ICO_09= 0xff00ff00;
52
  static final int ICO_10= 0xffd7660a;
53
  static final int ICO_11= 0xff092d2b;
54
  static final int ICO_12= 0xff034d3e;
55
  static final int ICO_13= 0xff1a7516;
56
  static final int ICO_14= 0xff58b6be;
57
  static final int ICO_15= 0xffd3c49b;
58
  static final int ICO_16= 0xff0a1662;
59
  static final int ICO_17= 0xffa21a1c;
60
  static final int ICO_18= 0xffe1b410;
61
  static final int ICO_19= 0xffff0000;
62
  static final int ICO_20= 0xff0157ba;
63
  static final int ICO_21= 0xff000000;
64
  static final int ICO_22= 0xff222222;
65
  static final int ICO_23= 0xff555555;
66
  static final int ICO_24= 0xffffffff;
67

    
68
  private static final int[][] COLORS =
69
    {
70
      { ICO_01, ICO_02, ICO_03, ICO_04, ICO_05, ICO_21 },
71
      { ICO_06, ICO_07, ICO_08, ICO_09, ICO_10, ICO_22 },
72
      { ICO_11, ICO_12, ICO_13, ICO_14, ICO_15, ICO_23 },
73
      { ICO_16, ICO_17, ICO_18, ICO_19, ICO_20, ICO_24 },
74
    };
75

    
76
  private final SeekBar mSeekBarBorders, mSeekBarCorners;
77
  private int mCurrentBorders, mCurrentCorners;
78
  private GridLayout mGrid;
79
  private int mRow, mCol;
80

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82

    
83
  void resetUI(ConfigActivity act)
84
    {
85
    mSeekBarBorders.setProgress(DEFAULT_BORDERS);
86
    mSeekBarCorners.setProgress(DEFAULT_CORNERS);
87

    
88
    act.runOnUiThread(new Runnable()
89
      {
90
      @Override
91
      public void run()
92
        {
93
        markButton(0,0);
94
        }
95
      });
96
    }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

    
100
  private void createColorGrid(final ConfigActivity act, int width, int height)
101
    {
102
    mGrid = new GridLayout(act);
103
    ViewGroup.LayoutParams paramsG = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
104
    mGrid.setLayoutParams(paramsG);
105

    
106
    int rowCount = COLORS.length;
107
    int colCount = COLORS[0].length;
108

    
109
    int margin  = (int)(width*MARGIN_RATIO);
110
    int hsize   = (int)(0.83f*width/colCount - margin);
111
    int vsize   = (int)(0.78f*(height*(1-MainActivity.RATIO_BAR)*(2.0f)/(2.0f+0.75f+3.0f))/rowCount - margin );
112

    
113
    mGrid.setColumnCount(colCount);
114
    mGrid.setRowCount(rowCount);
115

    
116
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[rowCount];
117
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[colCount];
118

    
119
    for(int row=0; row<rowCount; row++) rowSpecs[row] = GridLayout.spec(row);
120
    for(int col=0; col<colCount; col++) colSpecs[col] = GridLayout.spec(col);
121

    
122
    for(int row=0; row<rowCount; row++)
123
      for(int col=0; col<colCount; col++)
124
        {
125
        View v = createView(act,row,col);
126
        GridLayout.Spec rs = rowSpecs[row];
127
        GridLayout.Spec cs = colSpecs[col];
128

    
129
        GridLayout.LayoutParams params = new GridLayout.LayoutParams(rs,cs);
130
        params.bottomMargin = margin;
131
        params.topMargin    = margin;
132
        params.leftMargin   = margin;
133
        params.rightMargin  = margin;
134
        params.width = hsize;
135
        params.height= vsize;
136

    
137
        mGrid.addView(v,params);
138
        }
139

    
140
    markButton(mRow,mCol);
141
    }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

    
145
  private View createView(final Activity act, final int row, final int col)
146
    {
147
    AppCompatImageButton button = new AppCompatImageButton(act);
148

    
149
    int p = 10;
150
    button.setPadding(p,p,p,p);
151
    button.setScaleType(ImageView.ScaleType.FIT_CENTER);
152
    button.setBackgroundColor(COLORS[row][col]);
153

    
154
    button.setOnClickListener( new View.OnClickListener()
155
      {
156
      @Override
157
      public void onClick(View v)
158
        {
159
        markButton(row,col);
160
        }
161
      });
162

    
163
    return button;
164
    }
165

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

    
168
  private void markButton(int row, int col)
169
    {
170
    int index = mRow*COLORS[0].length + mCol;
171
    ImageButton button = (ImageButton)mGrid.getChildAt(index);
172
    button.setImageResource(0);
173

    
174
    mRow = row;
175
    mCol = col;
176

    
177
    index = mRow*COLORS[0].length + mCol;
178
    button = (ImageButton)mGrid.getChildAt(index);
179
    button.setImageResource(R.drawable.check);
180
    }
181

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

    
184
  int getCurrentColor()
185
    {
186
    return COLORS[mRow][mCol];
187
    }
188

    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190

    
191
  private int extractIndex(float val, float[] table)
192
    {
193
    int len = table.length;
194

    
195
    for(int i=0; i<len; i++)
196
      if( table[i]==val ) return i;
197

    
198
    return len/2;
199
    }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

    
203
  ConfigScreenPane(final ConfigActivity act, String key, OSInterface os)
204
    {
205
    mRow = 0;
206
    mCol = 0;
207
    int width = act.getScreenWidthInPixels();
208
    int height = act.getScreenHeightInPixels();
209
    float textSize = height*TEXT_RATIO;
210
    int padding = (int)(height*PADDING_RATIO);
211

    
212
    float borders = os.getFloat(key+"_border",1.0f);
213
    float corners = os.getFloat(key+"_corner",1.0f);
214

    
215
    ///// UPPER LAYOUT /////////////////////////////////
216
    LinearLayout configLayoutU = act.findViewById(R.id.configLayoutUpper);
217
    configLayoutU.setPadding(padding,padding,padding,padding);
218

    
219
    LinearLayout.LayoutParams paramsLayoutU = (LinearLayout.LayoutParams)configLayoutU.getLayoutParams();
220
    paramsLayoutU.bottomMargin = padding;
221
    paramsLayoutU.topMargin    = 0;
222
    paramsLayoutU.leftMargin   = padding;
223
    paramsLayoutU.rightMargin  = padding;
224

    
225
    configLayoutU.setLayoutParams(paramsLayoutU);
226

    
227
    TextView textBorder   = configLayoutU.findViewById(R.id.configTextBorder);
228
    textBorder.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
229
    TextView textCorner   = configLayoutU.findViewById(R.id.configTextCorner);
230
    textCorner.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
231

    
232
    mSeekBarBorders = configLayoutU.findViewById(R.id.configSeekBarBorder);
233
    mCurrentBorders = extractIndex(borders,BORDER_STEPS);
234

    
235
    if( mCurrentBorders!=mSeekBarBorders.getProgress() )
236
      {
237
      mSeekBarBorders.setProgress(mCurrentBorders);
238
      }
239

    
240
    mSeekBarBorders.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
241
      {
242
      @Override
243
      public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
244
        {
245
        mCurrentBorders = progress;
246
        }
247

    
248
      @Override
249
      public void onStartTrackingTouch(SeekBar seekBar) {}
250
      @Override
251
      public void onStopTrackingTouch(SeekBar seekBar)
252
        {
253
        act.recreateStickers(mCurrentBorders,mCurrentCorners);
254
        }
255
      });
256

    
257
    mSeekBarCorners = configLayoutU.findViewById(R.id.configSeekBarCorner);
258
    mCurrentCorners = extractIndex(corners,CORNER_STEPS);
259

    
260
    if( mCurrentCorners!=mSeekBarCorners.getProgress() )
261
      {
262
      mSeekBarCorners.setProgress(mCurrentCorners);
263
      }
264

    
265
    mSeekBarCorners.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
266
      {
267
      @Override
268
      public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
269
        {
270
        mCurrentCorners = progress;
271
        }
272

    
273
      @Override
274
      public void onStartTrackingTouch(SeekBar seekBar) {}
275
      @Override
276
      public void onStopTrackingTouch(SeekBar seekBar)
277
        {
278
        act.recreateStickers(mCurrentBorders,mCurrentCorners);
279
        }
280
      });
281

    
282
    ///// LOWER LAYOUT /////////////////////////////////
283
    LinearLayout configLayoutL = act.findViewById(R.id.configLayoutLower);
284
    configLayoutL.setPadding(padding,padding,padding,padding);
285

    
286
    LinearLayout.LayoutParams paramsLayoutL = (LinearLayout.LayoutParams)configLayoutL.getLayoutParams();
287
    paramsLayoutL.bottomMargin = padding;
288
    paramsLayoutL.topMargin    = 0;
289
    paramsLayoutL.leftMargin   = padding;
290
    paramsLayoutL.rightMargin  = padding;
291

    
292
    configLayoutL.setLayoutParams(paramsLayoutL);
293

    
294
    createColorGrid(act, width, height);
295
    configLayoutL.removeAllViews();
296
    configLayoutL.addView(mGrid);
297
    }
298
}
(5-5/6)