| 1 |
f6fcf06a
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 2 |
|
|
// Copyright 2019 Leszek Koltunski //
|
| 3 |
|
|
// //
|
| 4 |
|
|
// This file is part of Distorted. //
|
| 5 |
|
|
// //
|
| 6 |
|
|
// Distorted 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 |
|
|
// Distorted 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 Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
| 18 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 19 |
|
|
|
| 20 |
|
|
package org.distorted.magic;
|
| 21 |
|
|
|
| 22 |
|
|
import android.app.Dialog;
|
| 23 |
|
|
import android.content.Context;
|
| 24 |
|
|
import android.content.DialogInterface;
|
| 25 |
|
|
import android.os.Bundle;
|
| 26 |
|
|
import android.support.annotation.NonNull;
|
| 27 |
|
|
import android.support.annotation.Nullable;
|
| 28 |
|
|
import android.support.v4.app.FragmentActivity;
|
| 29 |
|
|
import android.support.v7.app.AlertDialog;
|
| 30 |
|
|
import android.support.v7.app.AppCompatDialogFragment;
|
| 31 |
22fdfc36
|
Leszek Koltunski
|
import android.view.Gravity;
|
| 32 |
f6fcf06a
|
Leszek Koltunski
|
import android.view.LayoutInflater;
|
| 33 |
|
|
import android.view.View;
|
| 34 |
|
|
import android.widget.AdapterView;
|
| 35 |
|
|
import android.widget.ArrayAdapter;
|
| 36 |
22fdfc36
|
Leszek Koltunski
|
import android.widget.LinearLayout;
|
| 37 |
f6fcf06a
|
Leszek Koltunski
|
import android.widget.SeekBar;
|
| 38 |
|
|
import android.widget.Spinner;
|
| 39 |
1cbcc6bf
|
Leszek Koltunski
|
import android.widget.TextView;
|
| 40 |
f6fcf06a
|
Leszek Koltunski
|
|
| 41 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 42 |
|
|
|
| 43 |
|
|
public class RubikSettings extends AppCompatDialogFragment implements SeekBar.OnSeekBarChangeListener, AdapterView.OnItemSelectedListener
|
| 44 |
|
|
{
|
| 45 |
|
|
public interface OnCompleteListener
|
| 46 |
|
|
{
|
| 47 |
22fdfc36
|
Leszek Koltunski
|
void onComplete(int sizeP, int solvP, int scraP, int sizeT, int solvT, int scraT);
|
| 48 |
f6fcf06a
|
Leszek Koltunski
|
}
|
| 49 |
|
|
|
| 50 |
|
|
private OnCompleteListener mListener;
|
| 51 |
22fdfc36
|
Leszek Koltunski
|
|
| 52 |
|
|
private TextView[] mDurationText;
|
| 53 |
|
|
private int[] mSeekBarID;
|
| 54 |
|
|
private int[] mSpinnerID;
|
| 55 |
|
|
private int[] mPos;
|
| 56 |
|
|
private int[] mType;
|
| 57 |
|
|
|
| 58 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 59 |
|
|
|
| 60 |
|
|
public RubikSettings()
|
| 61 |
|
|
{
|
| 62 |
|
|
mDurationText = new TextView[RubikSettingsEnum.LENGTH];
|
| 63 |
|
|
mSeekBarID = new int[RubikSettingsEnum.LENGTH];
|
| 64 |
|
|
mSpinnerID = new int[RubikSettingsEnum.LENGTH];
|
| 65 |
|
|
mPos = new int[RubikSettingsEnum.LENGTH];
|
| 66 |
|
|
mType = new int[RubikSettingsEnum.LENGTH];
|
| 67 |
|
|
}
|
| 68 |
1cbcc6bf
|
Leszek Koltunski
|
|
| 69 |
f6fcf06a
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 70 |
|
|
|
| 71 |
|
|
@Override
|
| 72 |
|
|
public void onAttach(Context context)
|
| 73 |
|
|
{
|
| 74 |
|
|
super.onAttach(context);
|
| 75 |
|
|
|
| 76 |
|
|
try
|
| 77 |
|
|
{
|
| 78 |
|
|
mListener = (OnCompleteListener)context;
|
| 79 |
|
|
}
|
| 80 |
|
|
catch (final ClassCastException e)
|
| 81 |
|
|
{
|
| 82 |
|
|
throw new ClassCastException(context.toString() + " must implement OnCompleteListener");
|
| 83 |
|
|
}
|
| 84 |
|
|
}
|
| 85 |
|
|
|
| 86 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 87 |
|
|
|
| 88 |
|
|
@Override
|
| 89 |
|
|
public void onCreate(@Nullable Bundle savedInstanceState)
|
| 90 |
|
|
{
|
| 91 |
|
|
super.onCreate(savedInstanceState);
|
| 92 |
|
|
|
| 93 |
|
|
Bundle args = getArguments();
|
| 94 |
|
|
|
| 95 |
22fdfc36
|
Leszek Koltunski
|
String name;
|
| 96 |
|
|
|
| 97 |
|
|
for (int i=0; i<RubikSettingsEnum.LENGTH; i++)
|
| 98 |
fe381d8e
|
Leszek Koltunski
|
{
|
| 99 |
22fdfc36
|
Leszek Koltunski
|
RubikSettingsEnum rse = RubikSettingsEnum.getEnum(i);
|
| 100 |
|
|
name = rse.name();
|
| 101 |
|
|
|
| 102 |
|
|
try
|
| 103 |
|
|
{
|
| 104 |
|
|
mPos[i] = args.getInt(name+"_Pos" );
|
| 105 |
|
|
mType[i] = args.getInt(name+"_Type");
|
| 106 |
|
|
}
|
| 107 |
|
|
catch(NullPointerException ex)
|
| 108 |
|
|
{
|
| 109 |
|
|
mPos[i] = rse.getDefaultPos();
|
| 110 |
|
|
mType[i] = rse.getDefaultType();
|
| 111 |
|
|
}
|
| 112 |
fe381d8e
|
Leszek Koltunski
|
}
|
| 113 |
f6fcf06a
|
Leszek Koltunski
|
}
|
| 114 |
|
|
|
| 115 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 116 |
|
|
|
| 117 |
|
|
@NonNull
|
| 118 |
|
|
@Override
|
| 119 |
|
|
public Dialog onCreateDialog(Bundle savedInstanceState)
|
| 120 |
|
|
{
|
| 121 |
|
|
FragmentActivity act = getActivity();
|
| 122 |
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(act);
|
| 123 |
|
|
|
| 124 |
|
|
builder.setCancelable(false);
|
| 125 |
|
|
builder.setPositiveButton( R.string.save, new DialogInterface.OnClickListener()
|
| 126 |
|
|
{
|
| 127 |
|
|
@Override
|
| 128 |
|
|
public void onClick(DialogInterface dialog, int which)
|
| 129 |
|
|
{
|
| 130 |
|
|
saveOptions();
|
| 131 |
|
|
}
|
| 132 |
|
|
});
|
| 133 |
|
|
|
| 134 |
|
|
LayoutInflater inflater = act.getLayoutInflater();
|
| 135 |
|
|
final View view = inflater.inflate(R.layout.settings, null);
|
| 136 |
|
|
builder.setView(view);
|
| 137 |
|
|
|
| 138 |
22fdfc36
|
Leszek Koltunski
|
LinearLayout linearLayout = view.findViewById(R.id.main_settings_layout);
|
| 139 |
f6fcf06a
|
Leszek Koltunski
|
|
| 140 |
22fdfc36
|
Leszek Koltunski
|
if( linearLayout!=null )
|
| 141 |
f6fcf06a
|
Leszek Koltunski
|
{
|
| 142 |
22fdfc36
|
Leszek Koltunski
|
for (int i=0; i<RubikSettingsEnum.LENGTH; i++)
|
| 143 |
f6fcf06a
|
Leszek Koltunski
|
{
|
| 144 |
22fdfc36
|
Leszek Koltunski
|
createSettingsSection(act,linearLayout,i);
|
| 145 |
f6fcf06a
|
Leszek Koltunski
|
}
|
| 146 |
|
|
}
|
| 147 |
|
|
else
|
| 148 |
|
|
{
|
| 149 |
22fdfc36
|
Leszek Koltunski
|
android.util.Log.e("settings", "linearLayout NULL!");
|
| 150 |
f6fcf06a
|
Leszek Koltunski
|
}
|
| 151 |
|
|
|
| 152 |
22fdfc36
|
Leszek Koltunski
|
return builder.create();
|
| 153 |
|
|
}
|
| 154 |
f6fcf06a
|
Leszek Koltunski
|
|
| 155 |
22fdfc36
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 156 |
a7012218
|
Leszek Koltunski
|
|
| 157 |
22fdfc36
|
Leszek Koltunski
|
private void createSettingsSection(FragmentActivity act, LinearLayout layout, int index)
|
| 158 |
a7012218
|
Leszek Koltunski
|
{
|
| 159 |
22fdfc36
|
Leszek Koltunski
|
RubikSettingsEnum rsEnum = RubikSettingsEnum.getEnum(index);
|
| 160 |
|
|
float scale = act.getResources().getDisplayMetrics().density;
|
| 161 |
|
|
|
| 162 |
|
|
///// TEXT ///////////////////////////////////////////////////////////////////////////
|
| 163 |
|
|
|
| 164 |
|
|
int layoutHeight = (int)(scale*48 + 0.5f);
|
| 165 |
|
|
int padding = (int)(scale*15 + 0.5f);
|
| 166 |
|
|
|
| 167 |
|
|
LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,layoutHeight);
|
| 168 |
|
|
|
| 169 |
|
|
TextView textView = new TextView(act);
|
| 170 |
|
|
textView.setText(rsEnum.getText());
|
| 171 |
|
|
textView.setLayoutParams(textParams);
|
| 172 |
|
|
textView.setGravity(Gravity.START|Gravity.CENTER);
|
| 173 |
|
|
textView.setPadding(padding,0,padding,0);
|
| 174 |
|
|
textView.setTextAppearance(android.R.style.TextAppearance_Medium);
|
| 175 |
|
|
layout.addView(textView);
|
| 176 |
|
|
|
| 177 |
|
|
///// OUTER LAYOUT ///////////////////////////////////////////////////////////////////
|
| 178 |
|
|
|
| 179 |
|
|
int margin = (int)(scale*10 + 0.5f);
|
| 180 |
|
|
int color = act.getResources().getColor(R.color.grey);
|
| 181 |
|
|
LinearLayout outerLayout = new LinearLayout(act);
|
| 182 |
|
|
LinearLayout.LayoutParams outerLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT, 0.5f);
|
| 183 |
|
|
outerLayoutParams.bottomMargin = margin;
|
| 184 |
|
|
outerLayoutParams.leftMargin = margin;
|
| 185 |
|
|
outerLayoutParams.rightMargin = margin;
|
| 186 |
|
|
|
| 187 |
|
|
outerLayout.setLayoutParams(outerLayoutParams);
|
| 188 |
|
|
outerLayout.setGravity(Gravity.CENTER|Gravity.FILL_HORIZONTAL);
|
| 189 |
|
|
outerLayout.setBackgroundColor(color);
|
| 190 |
|
|
outerLayout.setOrientation(LinearLayout.VERTICAL);
|
| 191 |
|
|
layout.addView(outerLayout);
|
| 192 |
|
|
|
| 193 |
|
|
///// INNER LAYOUT1 //////////////////////////////////////////////////////////////////
|
| 194 |
|
|
|
| 195 |
|
|
int innerLayout1Height = (int)(scale*36 + 0.5f);
|
| 196 |
|
|
LinearLayout innerLayout1 = new LinearLayout(act);
|
| 197 |
|
|
LinearLayout.LayoutParams innerLayout1Params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,innerLayout1Height);
|
| 198 |
|
|
|
| 199 |
|
|
innerLayout1.setLayoutParams(innerLayout1Params);
|
| 200 |
|
|
innerLayout1.setGravity(Gravity.CENTER|Gravity.FILL_HORIZONTAL);
|
| 201 |
|
|
innerLayout1.setOrientation(LinearLayout.HORIZONTAL);
|
| 202 |
|
|
outerLayout.addView(innerLayout1);
|
| 203 |
|
|
|
| 204 |
|
|
///// STUFF INSIDE INNER LAYOUT1 /////////////////////////////////////////////////////
|
| 205 |
|
|
|
| 206 |
|
|
int text1Padding = (int)(scale*5 + 0.5f);
|
| 207 |
|
|
LinearLayout.LayoutParams text1LayoutParams = new LinearLayout.LayoutParams(0,layoutHeight,0.2f);
|
| 208 |
|
|
|
| 209 |
|
|
TextView text1View = new TextView(act);
|
| 210 |
|
|
text1View.setText(R.string.duration);
|
| 211 |
|
|
text1View.setLayoutParams(text1LayoutParams);
|
| 212 |
|
|
text1View.setGravity(Gravity.START|Gravity.CENTER);
|
| 213 |
|
|
text1View.setPadding(text1Padding,0,text1Padding,0);
|
| 214 |
|
|
text1View.setTextAppearance(android.R.style.TextAppearance_Small);
|
| 215 |
|
|
innerLayout1.addView(text1View);
|
| 216 |
|
|
//////////////////////////////////////////////////////////////////
|
| 217 |
|
|
int text2Padding = (int)(scale*5 + 0.5f);
|
| 218 |
|
|
LinearLayout.LayoutParams text2LayoutParams = new LinearLayout.LayoutParams(0,layoutHeight,0.2f);
|
| 219 |
|
|
|
| 220 |
|
|
mDurationText[index] = new TextView(act);
|
| 221 |
|
|
mDurationText[index].setLayoutParams(text2LayoutParams);
|
| 222 |
|
|
mDurationText[index].setGravity(Gravity.END|Gravity.CENTER);
|
| 223 |
|
|
mDurationText[index].setPadding(text2Padding,0,text2Padding,0);
|
| 224 |
|
|
mDurationText[index].setTextAppearance(android.R.style.TextAppearance_Small);
|
| 225 |
|
|
innerLayout1.addView(mDurationText[index]);
|
| 226 |
|
|
//////////////////////////////////////////////////////////////////
|
| 227 |
|
|
int seekPadding = (int)(scale*10 + 0.5f);
|
| 228 |
|
|
LinearLayout.LayoutParams seekLayoutParams = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,0.6f);
|
| 229 |
|
|
|
| 230 |
|
|
SeekBar seekBar = new SeekBar(act);
|
| 231 |
|
|
seekBar.setLayoutParams(seekLayoutParams);
|
| 232 |
|
|
seekBar.setPadding(seekPadding,0,seekPadding,0);
|
| 233 |
|
|
seekBar.setId(index);
|
| 234 |
|
|
innerLayout1.addView(seekBar);
|
| 235 |
|
|
|
| 236 |
|
|
mSeekBarID[index] = seekBar.getId();
|
| 237 |
|
|
|
| 238 |
|
|
seekBar.setOnSeekBarChangeListener(this);
|
| 239 |
|
|
seekBar.setProgress(mPos[index]);
|
| 240 |
|
|
|
| 241 |
|
|
///// INNER LAYOUT2 //////////////////////////////////////////////////////////////////
|
| 242 |
|
|
|
| 243 |
|
|
int innerLayout2Height = (int)(scale*36 + 0.5f);
|
| 244 |
|
|
LinearLayout innerLayout2 = new LinearLayout(act);
|
| 245 |
|
|
LinearLayout.LayoutParams innerLayout2Params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,innerLayout2Height);
|
| 246 |
|
|
|
| 247 |
|
|
innerLayout2.setLayoutParams(innerLayout2Params);
|
| 248 |
|
|
innerLayout2.setGravity(Gravity.CENTER|Gravity.FILL_HORIZONTAL);
|
| 249 |
|
|
innerLayout2.setOrientation(LinearLayout.HORIZONTAL);
|
| 250 |
|
|
outerLayout.addView(innerLayout2);
|
| 251 |
|
|
|
| 252 |
|
|
///// STUFF INSIDE INNER LAYOUT2 /////////////////////////////////////////////////////
|
| 253 |
|
|
|
| 254 |
|
|
int text3Padding = (int)(scale*5 + 0.5f);
|
| 255 |
|
|
LinearLayout.LayoutParams text3LayoutParams = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,0.4f);
|
| 256 |
|
|
|
| 257 |
|
|
TextView text3View = new TextView(act);
|
| 258 |
|
|
text3View.setText(R.string.type);
|
| 259 |
|
|
text3View.setLayoutParams(text3LayoutParams);
|
| 260 |
|
|
text3View.setGravity(Gravity.START|Gravity.CENTER);
|
| 261 |
|
|
text3View.setPadding(text3Padding,0,text3Padding,0);
|
| 262 |
|
|
text3View.setTextAppearance(android.R.style.TextAppearance_Small);
|
| 263 |
|
|
innerLayout2.addView(text3View);
|
| 264 |
|
|
//////////////////////////////////////////////////////////////////
|
| 265 |
|
|
int spinnerPadding = (int)(scale*10 + 0.5f);
|
| 266 |
|
|
LinearLayout.LayoutParams spinnerLayoutParams = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,0.6f);
|
| 267 |
|
|
|
| 268 |
|
|
Spinner spinner = new Spinner(act);
|
| 269 |
|
|
spinner.setLayoutParams(spinnerLayoutParams);
|
| 270 |
|
|
spinner.setPadding(spinnerPadding,0,spinnerPadding,0);
|
| 271 |
|
|
spinner.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
|
| 272 |
|
|
spinner.setId(index);
|
| 273 |
|
|
innerLayout2.addView(spinner);
|
| 274 |
|
|
|
| 275 |
|
|
mSpinnerID[index] = spinner.getId();
|
| 276 |
|
|
|
| 277 |
|
|
spinner.setOnItemSelectedListener(this);
|
| 278 |
|
|
String[] appear = RubikSettingsEnum.getNames(index);
|
| 279 |
|
|
ArrayAdapter<String> adapterType = new ArrayAdapter<>(act,android.R.layout.simple_spinner_item, appear);
|
| 280 |
a7012218
|
Leszek Koltunski
|
adapterType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
| 281 |
22fdfc36
|
Leszek Koltunski
|
spinner.setAdapter(adapterType);
|
| 282 |
a7012218
|
Leszek Koltunski
|
|
| 283 |
22fdfc36
|
Leszek Koltunski
|
int type = mType[index];
|
| 284 |
a7012218
|
Leszek Koltunski
|
|
| 285 |
22fdfc36
|
Leszek Koltunski
|
if(type>=0 && type<appear.length)
|
| 286 |
3b12e641
|
Leszek Koltunski
|
{
|
| 287 |
22fdfc36
|
Leszek Koltunski
|
spinner.setSelection(type);
|
| 288 |
3b12e641
|
Leszek Koltunski
|
}
|
| 289 |
|
|
}
|
| 290 |
|
|
|
| 291 |
f6fcf06a
|
Leszek Koltunski
|
|
| 292 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 293 |
|
|
|
| 294 |
|
|
private void saveOptions()
|
| 295 |
|
|
{
|
| 296 |
22fdfc36
|
Leszek Koltunski
|
mListener.onComplete(mPos[0], mPos[1], mPos[2], mType[0], mType[1], mType[2]);
|
| 297 |
f6fcf06a
|
Leszek Koltunski
|
}
|
| 298 |
|
|
|
| 299 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 300 |
|
|
|
| 301 |
|
|
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
|
| 302 |
|
|
{
|
| 303 |
22fdfc36
|
Leszek Koltunski
|
int parentID = parent.getId();
|
| 304 |
|
|
|
| 305 |
|
|
for (int i=0; i<RubikSettingsEnum.LENGTH; i++)
|
| 306 |
f6fcf06a
|
Leszek Koltunski
|
{
|
| 307 |
22fdfc36
|
Leszek Koltunski
|
if( mSpinnerID[i] == parentID )
|
| 308 |
|
|
{
|
| 309 |
|
|
mType[i] = pos;
|
| 310 |
|
|
break;
|
| 311 |
|
|
}
|
| 312 |
f6fcf06a
|
Leszek Koltunski
|
}
|
| 313 |
|
|
}
|
| 314 |
|
|
|
| 315 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 316 |
|
|
|
| 317 |
|
|
public void onProgressChanged(SeekBar bar, int progress, boolean fromUser)
|
| 318 |
|
|
{
|
| 319 |
22fdfc36
|
Leszek Koltunski
|
int barID = bar.getId();
|
| 320 |
|
|
|
| 321 |
|
|
for (int i=0; i<RubikSettingsEnum.LENGTH; i++)
|
| 322 |
f6fcf06a
|
Leszek Koltunski
|
{
|
| 323 |
22fdfc36
|
Leszek Koltunski
|
if( mSeekBarID[i] == barID )
|
| 324 |
|
|
{
|
| 325 |
|
|
mPos[i] = progress;
|
| 326 |
|
|
int ms = RubikActivity.translateDuration(mPos[i]);
|
| 327 |
|
|
mDurationText[i].setText(getString(R.string.ms_placeholder,ms));
|
| 328 |
|
|
break;
|
| 329 |
|
|
}
|
| 330 |
f6fcf06a
|
Leszek Koltunski
|
}
|
| 331 |
|
|
}
|
| 332 |
|
|
|
| 333 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 334 |
|
|
|
| 335 |
|
|
public void onNothingSelected(AdapterView<?> parent) { }
|
| 336 |
|
|
public void onStartTrackingTouch(SeekBar bar) { }
|
| 337 |
|
|
public void onStopTrackingTouch(SeekBar bar) { }
|
| 338 |
|
|
}
|