1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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
|
import android.view.Gravity;
|
32
|
import android.view.LayoutInflater;
|
33
|
import android.view.View;
|
34
|
import android.widget.AdapterView;
|
35
|
import android.widget.ArrayAdapter;
|
36
|
import android.widget.LinearLayout;
|
37
|
import android.widget.SeekBar;
|
38
|
import android.widget.Spinner;
|
39
|
import android.widget.TextView;
|
40
|
|
41
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
42
|
|
43
|
public class RubikSettings extends AppCompatDialogFragment implements SeekBar.OnSeekBarChangeListener, AdapterView.OnItemSelectedListener
|
44
|
{
|
45
|
public interface OnCompleteListener
|
46
|
{
|
47
|
void onComplete(int index, int position, int type);
|
48
|
}
|
49
|
|
50
|
private OnCompleteListener mListener;
|
51
|
|
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
|
|
69
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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
|
String name;
|
96
|
|
97
|
for (int i=0; i<RubikSettingsEnum.LENGTH; i++)
|
98
|
{
|
99
|
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
|
}
|
113
|
}
|
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
|
LinearLayout linearLayout = view.findViewById(R.id.main_settings_layout);
|
139
|
|
140
|
if( linearLayout!=null )
|
141
|
{
|
142
|
for (int i=0; i<RubikSettingsEnum.LENGTH; i++)
|
143
|
{
|
144
|
createSettingsSection(act,linearLayout,i);
|
145
|
}
|
146
|
}
|
147
|
else
|
148
|
{
|
149
|
android.util.Log.e("settings", "linearLayout NULL!");
|
150
|
}
|
151
|
|
152
|
return builder.create();
|
153
|
}
|
154
|
|
155
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
156
|
|
157
|
private void createSettingsSection(FragmentActivity act, LinearLayout layout, int index)
|
158
|
{
|
159
|
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
|
adapterType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
281
|
spinner.setAdapter(adapterType);
|
282
|
|
283
|
int type = mType[index];
|
284
|
|
285
|
if(type>=0 && type<appear.length)
|
286
|
{
|
287
|
spinner.setSelection(type);
|
288
|
}
|
289
|
}
|
290
|
|
291
|
|
292
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
293
|
|
294
|
private void saveOptions()
|
295
|
{
|
296
|
for (int i=0; i<RubikSettingsEnum.LENGTH; i++)
|
297
|
{
|
298
|
mListener.onComplete(i, mPos[i], mType[i]);
|
299
|
}
|
300
|
}
|
301
|
|
302
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
303
|
|
304
|
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
|
305
|
{
|
306
|
int parentID = parent.getId();
|
307
|
|
308
|
for (int i=0; i<RubikSettingsEnum.LENGTH; i++)
|
309
|
{
|
310
|
if( mSpinnerID[i] == parentID )
|
311
|
{
|
312
|
mType[i] = pos;
|
313
|
break;
|
314
|
}
|
315
|
}
|
316
|
}
|
317
|
|
318
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
319
|
|
320
|
public void onProgressChanged(SeekBar bar, int progress, boolean fromUser)
|
321
|
{
|
322
|
int barID = bar.getId();
|
323
|
|
324
|
for (int i=0; i<RubikSettingsEnum.LENGTH; i++)
|
325
|
{
|
326
|
if( mSeekBarID[i] == barID )
|
327
|
{
|
328
|
mPos[i] = progress;
|
329
|
int ms = RubikActivity.translateDuration(mPos[i]);
|
330
|
mDurationText[i].setText(getString(R.string.ms_placeholder,ms));
|
331
|
break;
|
332
|
}
|
333
|
}
|
334
|
}
|
335
|
|
336
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
337
|
|
338
|
public void onNothingSelected(AdapterView<?> parent) { }
|
339
|
public void onStartTrackingTouch(SeekBar bar) { }
|
340
|
public void onStopTrackingTouch(SeekBar bar) { }
|
341
|
}
|