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.LayoutInflater;
|
32
|
import android.view.View;
|
33
|
import android.widget.AdapterView;
|
34
|
import android.widget.ArrayAdapter;
|
35
|
import android.widget.SeekBar;
|
36
|
import android.widget.Spinner;
|
37
|
import android.widget.TextView;
|
38
|
|
39
|
import org.distorted.effect.AppearEffect;
|
40
|
import org.distorted.effect.DisappearEffect;
|
41
|
|
42
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
43
|
|
44
|
public class RubikSettings extends AppCompatDialogFragment implements SeekBar.OnSeekBarChangeListener, AdapterView.OnItemSelectedListener
|
45
|
{
|
46
|
public interface OnCompleteListener
|
47
|
{
|
48
|
void onComplete(int aP, int dP, int aT, int dT);
|
49
|
}
|
50
|
|
51
|
private OnCompleteListener mListener;
|
52
|
|
53
|
private int mAppearPos, mDisappearPos;
|
54
|
private int mAppearType, mDisappearType;
|
55
|
|
56
|
private TextView mAppearDuration, mDisappearDuration;
|
57
|
|
58
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
59
|
|
60
|
@Override
|
61
|
public void onAttach(Context context)
|
62
|
{
|
63
|
super.onAttach(context);
|
64
|
|
65
|
try
|
66
|
{
|
67
|
mListener = (OnCompleteListener)context;
|
68
|
}
|
69
|
catch (final ClassCastException e)
|
70
|
{
|
71
|
throw new ClassCastException(context.toString() + " must implement OnCompleteListener");
|
72
|
}
|
73
|
}
|
74
|
|
75
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
76
|
|
77
|
@Override
|
78
|
public void onCreate(@Nullable Bundle savedInstanceState)
|
79
|
{
|
80
|
super.onCreate(savedInstanceState);
|
81
|
|
82
|
Bundle args = getArguments();
|
83
|
|
84
|
try
|
85
|
{
|
86
|
mAppearPos = args.getInt("appearPos");
|
87
|
mDisappearPos = args.getInt("disappearPos");
|
88
|
mAppearType = args.getInt("appearType");
|
89
|
mDisappearType = args.getInt("disappearType");
|
90
|
}
|
91
|
catch(NullPointerException ex)
|
92
|
{
|
93
|
mAppearPos = RubikActivity.DEFAULT_APPEAR_POS;
|
94
|
mDisappearPos = RubikActivity.DEFAULT_DISAPPEAR_POS;
|
95
|
mAppearType = RubikActivity.DEFAULT_APPEAR_TYPE;
|
96
|
mDisappearType = RubikActivity.DEFAULT_DISAPPEAR_TYPE;
|
97
|
}
|
98
|
}
|
99
|
|
100
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
101
|
|
102
|
@NonNull
|
103
|
@Override
|
104
|
public Dialog onCreateDialog(Bundle savedInstanceState)
|
105
|
{
|
106
|
FragmentActivity act = getActivity();
|
107
|
AlertDialog.Builder builder = new AlertDialog.Builder(act);
|
108
|
|
109
|
builder.setCancelable(false);
|
110
|
builder.setPositiveButton( R.string.save, new DialogInterface.OnClickListener()
|
111
|
{
|
112
|
@Override
|
113
|
public void onClick(DialogInterface dialog, int which)
|
114
|
{
|
115
|
saveOptions();
|
116
|
}
|
117
|
});
|
118
|
|
119
|
LayoutInflater inflater = act.getLayoutInflater();
|
120
|
final View view = inflater.inflate(R.layout.settings, null);
|
121
|
builder.setView(view);
|
122
|
|
123
|
mAppearDuration = view.findViewById(R.id.appearDurationText);
|
124
|
mDisappearDuration = view.findViewById(R.id.disappearDurationText);
|
125
|
|
126
|
Spinner appearTypeSpinner = view.findViewById(R.id.appearType);
|
127
|
|
128
|
if( appearTypeSpinner!=null )
|
129
|
{
|
130
|
appearTypeSpinner.setOnItemSelectedListener(this);
|
131
|
String[] appear = getAppearEffectNames();
|
132
|
ArrayAdapter<String> adapterType = new ArrayAdapter<>(act,android.R.layout.simple_spinner_item, appear);
|
133
|
adapterType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
134
|
appearTypeSpinner.setAdapter(adapterType);
|
135
|
|
136
|
if(mAppearType>=0 && mAppearType<appear.length)
|
137
|
{
|
138
|
appearTypeSpinner.setSelection(mAppearType);
|
139
|
}
|
140
|
}
|
141
|
else
|
142
|
{
|
143
|
android.util.Log.e("dialog", "APPEAR TYPE SPINNER NULL!!");
|
144
|
}
|
145
|
|
146
|
SeekBar appearBar = view.findViewById(R.id.appearDuration);
|
147
|
appearBar.setOnSeekBarChangeListener(this);
|
148
|
appearBar.setProgress(mAppearPos);
|
149
|
|
150
|
Spinner disappearTypeSpinner = view.findViewById(R.id.disappearType);
|
151
|
|
152
|
if( disappearTypeSpinner!=null )
|
153
|
{
|
154
|
disappearTypeSpinner.setOnItemSelectedListener(this);
|
155
|
String[] disappear = getDisappearEffectNames();
|
156
|
ArrayAdapter<String> adapterType = new ArrayAdapter<>(act,android.R.layout.simple_spinner_item, disappear);
|
157
|
adapterType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
158
|
disappearTypeSpinner.setAdapter(adapterType);
|
159
|
|
160
|
if(mDisappearType>=0 && mDisappearType<disappear.length)
|
161
|
{
|
162
|
disappearTypeSpinner.setSelection(mDisappearType);
|
163
|
}
|
164
|
}
|
165
|
else
|
166
|
{
|
167
|
android.util.Log.e("dialog", "DISAPPEAR TYPE SPINNER NULL!!");
|
168
|
}
|
169
|
|
170
|
SeekBar disappearBar = view.findViewById(R.id.disappearDuration);
|
171
|
disappearBar.setOnSeekBarChangeListener(this);
|
172
|
disappearBar.setProgress(mDisappearPos);
|
173
|
|
174
|
return builder.create();
|
175
|
}
|
176
|
|
177
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
178
|
|
179
|
private String[] getAppearEffectNames()
|
180
|
{
|
181
|
int length = AppearEffect.NUM_EFFECTS;
|
182
|
AppearEffect.Type[] types = AppearEffect.Type.values();
|
183
|
String[] names = new String[length];
|
184
|
|
185
|
for( int i=0; i<length; i++)
|
186
|
{
|
187
|
names[i] = types[i].name();
|
188
|
}
|
189
|
|
190
|
return names;
|
191
|
}
|
192
|
|
193
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
194
|
|
195
|
private String[] getDisappearEffectNames()
|
196
|
{
|
197
|
int length = DisappearEffect.NUM_EFFECTS;
|
198
|
DisappearEffect.Type[] types = DisappearEffect.Type.values();
|
199
|
String[] names = new String[length];
|
200
|
|
201
|
for( int i=0; i<length; i++)
|
202
|
{
|
203
|
names[i] = types[i].name();
|
204
|
}
|
205
|
|
206
|
return names;
|
207
|
}
|
208
|
|
209
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
210
|
|
211
|
private void saveOptions()
|
212
|
{
|
213
|
mListener.onComplete(mAppearPos, mDisappearPos, mAppearType, mDisappearType);
|
214
|
}
|
215
|
|
216
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
217
|
|
218
|
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
|
219
|
{
|
220
|
switch(parent.getId())
|
221
|
{
|
222
|
case R.id.appearType : mAppearType = pos; break;
|
223
|
case R.id.disappearType: mDisappearType= pos; break;
|
224
|
}
|
225
|
}
|
226
|
|
227
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
228
|
|
229
|
public void onProgressChanged(SeekBar bar, int progress, boolean fromUser)
|
230
|
{
|
231
|
switch (bar.getId())
|
232
|
{
|
233
|
case R.id.appearDuration : mAppearPos = progress;
|
234
|
mAppearDuration.setText(RubikActivity.translateDuration(mAppearPos)+" ms");
|
235
|
break;
|
236
|
case R.id.disappearDuration: mDisappearPos= progress;
|
237
|
mDisappearDuration.setText(RubikActivity.translateDuration(mDisappearPos)+" ms");
|
238
|
break;
|
239
|
}
|
240
|
}
|
241
|
|
242
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
243
|
|
244
|
public void onNothingSelected(AdapterView<?> parent) { }
|
245
|
public void onStartTrackingTouch(SeekBar bar) { }
|
246
|
public void onStopTrackingTouch(SeekBar bar) { }
|
247
|
}
|