1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2019 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.dialogs;
|
11
|
|
12
|
import android.app.Dialog;
|
13
|
import android.content.DialogInterface;
|
14
|
import android.os.Bundle;
|
15
|
import androidx.annotation.NonNull;
|
16
|
import androidx.fragment.app.FragmentActivity;
|
17
|
import androidx.core.content.ContextCompat;
|
18
|
import androidx.appcompat.app.AlertDialog;
|
19
|
import androidx.appcompat.app.AppCompatDialogFragment;
|
20
|
import android.util.DisplayMetrics;
|
21
|
import android.util.TypedValue;
|
22
|
import android.view.Gravity;
|
23
|
import android.view.LayoutInflater;
|
24
|
import android.view.View;
|
25
|
import android.view.ViewGroup;
|
26
|
import android.view.Window;
|
27
|
import android.widget.AdapterView;
|
28
|
import android.widget.ArrayAdapter;
|
29
|
import android.widget.Button;
|
30
|
import android.widget.LinearLayout;
|
31
|
import android.widget.SeekBar;
|
32
|
import android.widget.Spinner;
|
33
|
import android.widget.TextView;
|
34
|
|
35
|
import org.distorted.objectlib.effects.BaseEffect;
|
36
|
import org.distorted.main.R;
|
37
|
import org.distorted.main.RubikActivity;
|
38
|
|
39
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
40
|
|
41
|
public class RubikDialogEffects extends AppCompatDialogFragment implements SeekBar.OnSeekBarChangeListener, AdapterView.OnItemSelectedListener
|
42
|
{
|
43
|
private final TextView[] mDurationText;
|
44
|
private float mTextSize;
|
45
|
|
46
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
47
|
|
48
|
private void addSettingsSection(FragmentActivity act, LinearLayout layout, int index)
|
49
|
{
|
50
|
BaseEffect.Type beType = BaseEffect.Type.getType(index);
|
51
|
|
52
|
int textH= (int)(1.35f*mTextSize);
|
53
|
float A= 1.2f;
|
54
|
float B= 0.25f;
|
55
|
|
56
|
///// OUTER LAYOUT ///////////////////////////////////////////////////////////////////
|
57
|
|
58
|
int margin = (int)(B*textH);
|
59
|
int color = ContextCompat.getColor(act, R.color.grey);
|
60
|
LinearLayout outerLayout = new LinearLayout(act);
|
61
|
LinearLayout.LayoutParams outerLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT, 0.5f);
|
62
|
outerLayoutParams.topMargin = index>0 ? margin : 0;
|
63
|
outerLayoutParams.bottomMargin = 0;
|
64
|
outerLayoutParams.leftMargin = margin;
|
65
|
outerLayoutParams.rightMargin = margin;
|
66
|
|
67
|
outerLayout.setLayoutParams(outerLayoutParams);
|
68
|
outerLayout.setGravity(Gravity.CENTER|Gravity.FILL_HORIZONTAL);
|
69
|
outerLayout.setBackgroundColor(color);
|
70
|
outerLayout.setOrientation(LinearLayout.VERTICAL);
|
71
|
layout.addView(outerLayout);
|
72
|
|
73
|
///// TEXT ///////////////////////////////////////////////////////////////////////////
|
74
|
|
75
|
int layoutHeight = (int)(A*textH);
|
76
|
int padding = (int)(B*textH);
|
77
|
|
78
|
LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,layoutHeight);
|
79
|
|
80
|
TextView textView = new TextView(act);
|
81
|
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
|
82
|
textView.setText(beType.getText());
|
83
|
textView.setLayoutParams(textParams);
|
84
|
textView.setGravity(Gravity.CENTER);
|
85
|
textView.setPadding(padding,0,padding,0);
|
86
|
outerLayout.addView(textView);
|
87
|
|
88
|
///// INNER LAYOUT1 //////////////////////////////////////////////////////////////////
|
89
|
|
90
|
int innerLayout1Height = (int)((A+B)*textH);
|
91
|
LinearLayout innerLayout1 = new LinearLayout(act);
|
92
|
LinearLayout.LayoutParams innerLayout1Params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,innerLayout1Height);
|
93
|
|
94
|
innerLayout1.setLayoutParams(innerLayout1Params);
|
95
|
innerLayout1.setGravity(Gravity.CENTER|Gravity.FILL_HORIZONTAL);
|
96
|
innerLayout1.setOrientation(LinearLayout.HORIZONTAL);
|
97
|
outerLayout.addView(innerLayout1);
|
98
|
|
99
|
///// STUFF INSIDE INNER LAYOUT1 /////////////////////////////////////////////////////
|
100
|
|
101
|
int text1Padding = (int)(B*textH);
|
102
|
LinearLayout.LayoutParams text1LayoutParams = new LinearLayout.LayoutParams(0,layoutHeight,0.25f);
|
103
|
|
104
|
TextView text1View = new TextView(act);
|
105
|
text1View.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
|
106
|
text1View.setText(R.string.duration);
|
107
|
text1View.setLayoutParams(text1LayoutParams);
|
108
|
text1View.setGravity(Gravity.START|Gravity.CENTER);
|
109
|
text1View.setPadding(text1Padding,0,text1Padding,0);
|
110
|
innerLayout1.addView(text1View);
|
111
|
//////////////////////////////////////////////////////////////////
|
112
|
int text2Padding = (int)(B*textH);
|
113
|
LinearLayout.LayoutParams text2LayoutParams = new LinearLayout.LayoutParams(0,layoutHeight,0.25f);
|
114
|
|
115
|
mDurationText[index] = new TextView(act);
|
116
|
mDurationText[index].setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
|
117
|
mDurationText[index].setLayoutParams(text2LayoutParams);
|
118
|
mDurationText[index].setGravity(Gravity.START|Gravity.CENTER);
|
119
|
mDurationText[index].setPadding(text2Padding,0,text2Padding,0);
|
120
|
innerLayout1.addView(mDurationText[index]);
|
121
|
//////////////////////////////////////////////////////////////////
|
122
|
int seekPadding = (int)(B*textH);
|
123
|
LinearLayout.LayoutParams seekLayoutParams = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,0.5f);
|
124
|
|
125
|
SeekBar seekBar = new SeekBar(act);
|
126
|
seekBar.setLayoutParams(seekLayoutParams);
|
127
|
seekBar.setPadding(seekPadding,0,seekPadding,0);
|
128
|
seekBar.setId(index);
|
129
|
innerLayout1.addView(seekBar);
|
130
|
|
131
|
seekBar.setOnSeekBarChangeListener(this);
|
132
|
seekBar.setProgress(beType.getCurrentPos());
|
133
|
|
134
|
///// INNER LAYOUT2 //////////////////////////////////////////////////////////////////
|
135
|
|
136
|
int innerLayout2Height = (int)((A+B)*textH);
|
137
|
LinearLayout innerLayout2 = new LinearLayout(act);
|
138
|
LinearLayout.LayoutParams innerLayout2Params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,innerLayout2Height);
|
139
|
|
140
|
innerLayout2.setLayoutParams(innerLayout2Params);
|
141
|
innerLayout2.setGravity(Gravity.CENTER|Gravity.FILL_HORIZONTAL);
|
142
|
innerLayout2.setOrientation(LinearLayout.HORIZONTAL);
|
143
|
outerLayout.addView(innerLayout2);
|
144
|
|
145
|
///// STUFF INSIDE INNER LAYOUT2 /////////////////////////////////////////////////////
|
146
|
|
147
|
int text3Padding = (int)(B*textH);
|
148
|
LinearLayout.LayoutParams text3LayoutParams = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,0.25f);
|
149
|
|
150
|
TextView text3View = new TextView(act);
|
151
|
text3View.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
|
152
|
text3View.setText(R.string.type);
|
153
|
text3View.setLayoutParams(text3LayoutParams);
|
154
|
text3View.setGravity(Gravity.START|Gravity.CENTER);
|
155
|
text3View.setPadding(text3Padding,0,text3Padding,0);
|
156
|
innerLayout2.addView(text3View);
|
157
|
//////////////////////////////////////////////////////////////////
|
158
|
int spinnerPadding = (int)(B*textH);
|
159
|
int spinnerMargin = (int)(B*0.5f*textH);
|
160
|
LinearLayout.LayoutParams spinnerLayoutParams = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,0.75f);
|
161
|
spinnerLayoutParams.topMargin = spinnerMargin;
|
162
|
spinnerLayoutParams.bottomMargin = spinnerMargin;
|
163
|
spinnerLayoutParams.leftMargin = spinnerMargin;
|
164
|
spinnerLayoutParams.rightMargin = 2*spinnerMargin;
|
165
|
|
166
|
Spinner spinner = new Spinner(act);
|
167
|
spinner.setLayoutParams(spinnerLayoutParams);
|
168
|
spinner.setPadding(spinnerPadding,0,spinnerPadding,0);
|
169
|
spinner.setBackgroundResource(R.drawable.ui_small_spinner);
|
170
|
spinner.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
|
171
|
spinner.setId(index+BaseEffect.Type.LENGTH);
|
172
|
innerLayout2.addView(spinner);
|
173
|
|
174
|
spinner.setOnItemSelectedListener(this);
|
175
|
String[] appear = BaseEffect.Type.getType(index).getNames();
|
176
|
|
177
|
ArrayAdapter<String> adapterType = new ArrayAdapter<String>(act, android.R.layout.simple_spinner_item, appear)
|
178
|
{
|
179
|
@NonNull
|
180
|
public View getView(int position, View convertView, @NonNull ViewGroup parent)
|
181
|
{
|
182
|
View v = super.getView(position, convertView, parent);
|
183
|
TextView tv = ((TextView) v);
|
184
|
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
|
185
|
return v;
|
186
|
}
|
187
|
};
|
188
|
|
189
|
adapterType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
190
|
spinner.setAdapter(adapterType);
|
191
|
spinner.setSelection(beType.getCurrentType());
|
192
|
}
|
193
|
|
194
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
195
|
// PUBLIC API
|
196
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
197
|
|
198
|
public RubikDialogEffects()
|
199
|
{
|
200
|
mDurationText = new TextView[BaseEffect.Type.LENGTH];
|
201
|
}
|
202
|
|
203
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
204
|
|
205
|
@NonNull
|
206
|
@Override
|
207
|
public Dialog onCreateDialog(Bundle savedInstanceState)
|
208
|
{
|
209
|
FragmentActivity act = getActivity();
|
210
|
LayoutInflater inflater = act.getLayoutInflater();
|
211
|
AlertDialog.Builder builder = new AlertDialog.Builder(act);
|
212
|
TextView tv = (TextView) inflater.inflate(R.layout.dialog_title, null);
|
213
|
|
214
|
DisplayMetrics displaymetrics = new DisplayMetrics();
|
215
|
act.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
|
216
|
final float titleSize= displaymetrics.widthPixels * RubikActivity.MENU_BIG_TEXT_SIZE;
|
217
|
final float okSize = displaymetrics.widthPixels * RubikActivity.DIALOG_BUTTON_SIZE;
|
218
|
|
219
|
mTextSize = displaymetrics.widthPixels * RubikActivity.MENU_SMALL_TEXT_SIZE;
|
220
|
|
221
|
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
|
222
|
tv.setText(R.string.effects);
|
223
|
builder.setCustomTitle(tv);
|
224
|
|
225
|
builder.setCancelable(true);
|
226
|
builder.setPositiveButton( R.string.ok, new DialogInterface.OnClickListener()
|
227
|
{
|
228
|
@Override
|
229
|
public void onClick(DialogInterface dialog, int which)
|
230
|
{
|
231
|
|
232
|
}
|
233
|
});
|
234
|
|
235
|
final View view = inflater.inflate(R.layout.dialog_effects, null);
|
236
|
builder.setView(view);
|
237
|
|
238
|
LinearLayout linearLayout = view.findViewById(R.id.settingsLayout);
|
239
|
|
240
|
if( linearLayout!=null )
|
241
|
{
|
242
|
for (int i=0; i< BaseEffect.Type.LENGTH; i++)
|
243
|
{
|
244
|
addSettingsSection(act,linearLayout,i);
|
245
|
}
|
246
|
}
|
247
|
else
|
248
|
{
|
249
|
android.util.Log.e("dialog_settings", "linearLayout NULL!");
|
250
|
}
|
251
|
|
252
|
Dialog dialog = builder.create();
|
253
|
dialog.setCanceledOnTouchOutside(false);
|
254
|
Window window = dialog.getWindow();
|
255
|
|
256
|
if( window!=null )
|
257
|
{
|
258
|
window.getDecorView().setSystemUiVisibility(RubikActivity.FLAGS);
|
259
|
}
|
260
|
|
261
|
dialog.setOnShowListener(new DialogInterface.OnShowListener()
|
262
|
{
|
263
|
@Override
|
264
|
public void onShow(DialogInterface dialog)
|
265
|
{
|
266
|
Button btnPositive = ((AlertDialog)dialog).getButton(Dialog.BUTTON_POSITIVE);
|
267
|
btnPositive.setTextSize(TypedValue.COMPLEX_UNIT_PX, okSize);
|
268
|
}
|
269
|
});
|
270
|
|
271
|
return dialog;
|
272
|
}
|
273
|
|
274
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
275
|
|
276
|
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
|
277
|
{
|
278
|
int parentID = parent.getId();
|
279
|
int len = BaseEffect.Type.LENGTH;
|
280
|
|
281
|
if( parentID>=len && parentID< 2*len) // ith spinner's ID is equal to i+LENGTH (see createSettingSection)
|
282
|
{
|
283
|
BaseEffect.Type.getType(parentID-len).setCurrentType(pos);
|
284
|
}
|
285
|
}
|
286
|
|
287
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
288
|
|
289
|
public void onProgressChanged(SeekBar bar, int progress, boolean fromUser)
|
290
|
{
|
291
|
int barID = bar.getId();
|
292
|
|
293
|
if( barID>=0 && barID< BaseEffect.Type.LENGTH) // ith seekbar's ID is equal to i (see createSettingSection)
|
294
|
{
|
295
|
BaseEffect.Type.getType(barID).setCurrentPos(progress);
|
296
|
int ms = BaseEffect.Type.translatePos(progress);
|
297
|
mDurationText[barID].setText(getString(R.string.ms_placeholder,ms));
|
298
|
}
|
299
|
}
|
300
|
|
301
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
302
|
|
303
|
public void onNothingSelected(AdapterView<?> parent) { }
|
304
|
public void onStartTrackingTouch(SeekBar bar) { }
|
305
|
public void onStopTrackingTouch(SeekBar bar) { }
|
306
|
}
|