Project

General

Profile

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

magiccube / src / main / java / org / distorted / config / ConfigScreenPane.java @ 99c2e327

1 58fd2ec0 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
2 14da3188 leszek
// Copyright 2024 Leszek Koltunski                                                               //
3 58fd2ec0 leszek
//                                                                                               //
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 2659f9dd leszek
import android.app.Activity;
13 58fd2ec0 leszek
import android.util.TypedValue;
14 2659f9dd leszek
import android.view.View;
15
import android.view.ViewGroup;
16
import android.widget.GridLayout;
17
import android.widget.ImageButton;
18 3979a11d leszek
import android.widget.ImageView;
19 58fd2ec0 leszek
import android.widget.LinearLayout;
20 14da3188 leszek
import android.widget.SeekBar;
21 58fd2ec0 leszek
import android.widget.TextView;
22
23 3979a11d leszek
import androidx.appcompat.widget.AppCompatImageButton;
24
25 2659f9dd leszek
import org.distorted.main.MainActivity;
26 58fd2ec0 leszek
import org.distorted.main.R;
27 9211042f leszek
import org.distorted.os.OSInterface;
28 58fd2ec0 leszek
29 40249d5b leszek
import static org.distorted.objectlib.shape.ShapeColors.*;
30
31 58fd2ec0 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
32
33
public class ConfigScreenPane
34
{
35 14da3188 leszek
  public static final int DEFAULT_BORDERS = 2;
36
  public static final int DEFAULT_CORNERS = 2;
37
38 406c2fa5 leszek
  public static final float[] BORDER_STEPS = { 0.50f, 0.75f, 1.00f, 1.50f, 2.00f };
39
  public static final float[] CORNER_STEPS = { 0.00f, 0.50f, 1.00f, 1.50f, 2.00f };
40 14da3188 leszek
41 94ff5962 leszek
  private static final float PADDING_RATIO = 0.010f;
42 2659f9dd leszek
  private static final float MARGIN_RATIO  = 0.017f;
43 58fd2ec0 leszek
  private static final float TEXT_RATIO    = 0.025f;
44
45 2659f9dd leszek
  private static final int[][] COLORS =
46
    {
47 99c2e327 leszek
      {COLOR_WHITE, COLOR_YELLOW, COLOR_BLUE, COLOR_LGREEN, COLOR_RED, COLOR_ORANGE},
48
      {COLOR_LPINK, COLOR_VIOLET, COLOR_PURPLE, COLOR_BURGUN, COLOR_DPINK, COLOR_DYELLO},
49
      {COLOR_DGREEN, COLOR_SELEDY, COLOR_GREEN, COLOR_BLUEISH, COLOR_SANDY, COLOR_DBLUE},
50
      {COLOR_BROWN, COLOR_LBLUE, COLOR_BLACK, COLOR_DGREY, COLOR_LGREY, COLOR_GREY},
51 2659f9dd leszek
    };
52
53 14da3188 leszek
  private final SeekBar mSeekBarBorders, mSeekBarCorners;
54
  private int mCurrentBorders, mCurrentCorners;
55 2659f9dd leszek
  private GridLayout mGrid;
56 3979a11d leszek
  private int mRow, mCol;
57 14da3188 leszek
58 58fd2ec0 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
59
60 4bd09fe2 leszek
  void resetUI(ConfigActivity act)
61 58fd2ec0 leszek
    {
62 2659f9dd leszek
    mSeekBarBorders.setProgress(DEFAULT_BORDERS);
63
    mSeekBarCorners.setProgress(DEFAULT_CORNERS);
64 58fd2ec0 leszek
65 4bd09fe2 leszek
    act.runOnUiThread(new Runnable()
66
      {
67
      @Override
68
      public void run()
69
        {
70
        markButton(0,0);
71
        }
72
      });
73 58fd2ec0 leszek
    }
74
75 14da3188 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
76
77 2659f9dd leszek
  private void createColorGrid(final ConfigActivity act, int width, int height)
78 14da3188 leszek
    {
79 2659f9dd leszek
    mGrid = new GridLayout(act);
80
    ViewGroup.LayoutParams paramsG = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
81
    mGrid.setLayoutParams(paramsG);
82
83
    int rowCount = COLORS.length;
84
    int colCount = COLORS[0].length;
85
86
    int margin  = (int)(width*MARGIN_RATIO);
87
    int hsize   = (int)(0.83f*width/colCount - margin);
88
    int vsize   = (int)(0.78f*(height*(1-MainActivity.RATIO_BAR)*(2.0f)/(2.0f+0.75f+3.0f))/rowCount - margin );
89
90
    mGrid.setColumnCount(colCount);
91
    mGrid.setRowCount(rowCount);
92
93
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[rowCount];
94
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[colCount];
95
96
    for(int row=0; row<rowCount; row++) rowSpecs[row] = GridLayout.spec(row);
97
    for(int col=0; col<colCount; col++) colSpecs[col] = GridLayout.spec(col);
98
99
    for(int row=0; row<rowCount; row++)
100
      for(int col=0; col<colCount; col++)
101
        {
102
        View v = createView(act,row,col);
103
        GridLayout.Spec rs = rowSpecs[row];
104
        GridLayout.Spec cs = colSpecs[col];
105
106
        GridLayout.LayoutParams params = new GridLayout.LayoutParams(rs,cs);
107
        params.bottomMargin = margin;
108
        params.topMargin    = margin;
109
        params.leftMargin   = margin;
110
        params.rightMargin  = margin;
111
        params.width = hsize;
112
        params.height= vsize;
113
114
        mGrid.addView(v,params);
115
        }
116 3979a11d leszek
117
    markButton(mRow,mCol);
118 2659f9dd leszek
    }
119
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121
122 3979a11d leszek
  private View createView(final Activity act, final int row, final int col)
123 2659f9dd leszek
    {
124 3979a11d leszek
    AppCompatImageButton button = new AppCompatImageButton(act);
125
126
    int p = 10;
127
    button.setPadding(p,p,p,p);
128
    button.setScaleType(ImageView.ScaleType.FIT_CENTER);
129
    button.setBackgroundColor(COLORS[row][col]);
130
131
    button.setOnClickListener( new View.OnClickListener()
132
      {
133
      @Override
134
      public void onClick(View v)
135
        {
136
        markButton(row,col);
137
        }
138
      });
139
140 2659f9dd leszek
    return button;
141 14da3188 leszek
    }
142
143 3979a11d leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
144
145
  private void markButton(int row, int col)
146
    {
147
    int index = mRow*COLORS[0].length + mCol;
148
    ImageButton button = (ImageButton)mGrid.getChildAt(index);
149
    button.setImageResource(0);
150
151
    mRow = row;
152
    mCol = col;
153
154
    index = mRow*COLORS[0].length + mCol;
155
    button = (ImageButton)mGrid.getChildAt(index);
156
    button.setImageResource(R.drawable.check);
157
    }
158
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160
161
  int getCurrentColor()
162
    {
163
    return COLORS[mRow][mCol];
164
    }
165
166 4bd09fe2 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
167
168 9211042f leszek
  private int extractIndex(float val, float[] table)
169 4bd09fe2 leszek
    {
170 9211042f leszek
    int len = table.length;
171 4bd09fe2 leszek
172 9211042f leszek
    for(int i=0; i<len; i++)
173
      if( table[i]==val ) return i;
174 4bd09fe2 leszek
175 9211042f leszek
    return len/2;
176 4bd09fe2 leszek
    }
177
178 58fd2ec0 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
179
180 9211042f leszek
  ConfigScreenPane(final ConfigActivity act, String key, OSInterface os)
181 58fd2ec0 leszek
    {
182 3979a11d leszek
    mRow = 0;
183
    mCol = 0;
184 2659f9dd leszek
    int width = act.getScreenWidthInPixels();
185 58fd2ec0 leszek
    int height = act.getScreenHeightInPixels();
186
    float textSize = height*TEXT_RATIO;
187
    int padding = (int)(height*PADDING_RATIO);
188
189 9211042f leszek
    float borders = os.getFloat(key+"_border",1.0f);
190
    float corners = os.getFloat(key+"_corner",1.0f);
191
192 94ff5962 leszek
    ///// UPPER LAYOUT /////////////////////////////////
193
    LinearLayout configLayoutU = act.findViewById(R.id.configLayoutUpper);
194
    configLayoutU.setPadding(padding,padding,padding,padding);
195 58fd2ec0 leszek
196 94ff5962 leszek
    LinearLayout.LayoutParams paramsLayoutU = (LinearLayout.LayoutParams)configLayoutU.getLayoutParams();
197
    paramsLayoutU.bottomMargin = padding;
198
    paramsLayoutU.topMargin    = 0;
199
    paramsLayoutU.leftMargin   = padding;
200
    paramsLayoutU.rightMargin  = padding;
201 58fd2ec0 leszek
202 94ff5962 leszek
    configLayoutU.setLayoutParams(paramsLayoutU);
203 58fd2ec0 leszek
204 94ff5962 leszek
    TextView textBorder   = configLayoutU.findViewById(R.id.configTextBorder);
205 14da3188 leszek
    textBorder.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
206 94ff5962 leszek
    TextView textCorner   = configLayoutU.findViewById(R.id.configTextCorner);
207 14da3188 leszek
    textCorner.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
208
209 94ff5962 leszek
    mSeekBarBorders = configLayoutU.findViewById(R.id.configSeekBarBorder);
210 9211042f leszek
    mCurrentBorders = extractIndex(borders,BORDER_STEPS);
211
212
    if( mCurrentBorders!=mSeekBarBorders.getProgress() )
213
      {
214
      mSeekBarBorders.setProgress(mCurrentBorders);
215
      }
216 14da3188 leszek
217
    mSeekBarBorders.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
218
      {
219
      @Override
220
      public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
221
        {
222
        mCurrentBorders = progress;
223
        }
224
225
      @Override
226
      public void onStartTrackingTouch(SeekBar seekBar) {}
227
      @Override
228
      public void onStopTrackingTouch(SeekBar seekBar)
229
        {
230
        act.recreateStickers(mCurrentBorders,mCurrentCorners);
231
        }
232
      });
233
234 94ff5962 leszek
    mSeekBarCorners = configLayoutU.findViewById(R.id.configSeekBarCorner);
235 9211042f leszek
    mCurrentCorners = extractIndex(corners,CORNER_STEPS);
236
237
    if( mCurrentCorners!=mSeekBarCorners.getProgress() )
238
      {
239
      mSeekBarCorners.setProgress(mCurrentCorners);
240
      }
241 14da3188 leszek
242
    mSeekBarCorners.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
243
      {
244
      @Override
245
      public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
246
        {
247
        mCurrentCorners = progress;
248
        }
249
250
      @Override
251
      public void onStartTrackingTouch(SeekBar seekBar) {}
252
      @Override
253
      public void onStopTrackingTouch(SeekBar seekBar)
254
        {
255
        act.recreateStickers(mCurrentBorders,mCurrentCorners);
256
        }
257
      });
258 94ff5962 leszek
259
    ///// LOWER LAYOUT /////////////////////////////////
260
    LinearLayout configLayoutL = act.findViewById(R.id.configLayoutLower);
261
    configLayoutL.setPadding(padding,padding,padding,padding);
262
263
    LinearLayout.LayoutParams paramsLayoutL = (LinearLayout.LayoutParams)configLayoutL.getLayoutParams();
264
    paramsLayoutL.bottomMargin = padding;
265
    paramsLayoutL.topMargin    = 0;
266
    paramsLayoutL.leftMargin   = padding;
267
    paramsLayoutL.rightMargin  = padding;
268
269
    configLayoutL.setLayoutParams(paramsLayoutL);
270 2659f9dd leszek
271
    createColorGrid(act, width, height);
272 427ba5bf leszek
    configLayoutL.removeAllViews();
273 2659f9dd leszek
    configLayoutL.addView(mGrid);
274 58fd2ec0 leszek
    }
275
}