Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogEffects.java @ 3f7a4363

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube 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
// Magic Cube 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 Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.dialogs;
21

    
22
import android.app.Dialog;
23
import android.content.DialogInterface;
24
import android.os.Bundle;
25
import androidx.annotation.NonNull;
26
import androidx.fragment.app.FragmentActivity;
27
import androidx.core.content.ContextCompat;
28
import androidx.appcompat.app.AlertDialog;
29
import androidx.appcompat.app.AppCompatDialogFragment;
30
import android.util.DisplayMetrics;
31
import android.util.TypedValue;
32
import android.view.Gravity;
33
import android.view.LayoutInflater;
34
import android.view.View;
35
import android.view.ViewGroup;
36
import android.view.Window;
37
import android.view.WindowManager;
38
import android.widget.AdapterView;
39
import android.widget.ArrayAdapter;
40
import android.widget.Button;
41
import android.widget.LinearLayout;
42
import android.widget.SeekBar;
43
import android.widget.Spinner;
44
import android.widget.TextView;
45

    
46
import org.distorted.effects.BaseEffect;
47
import org.distorted.main.R;
48
import org.distorted.main.RubikActivity;
49

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
public class RubikDialogEffects extends AppCompatDialogFragment implements SeekBar.OnSeekBarChangeListener, AdapterView.OnItemSelectedListener
53
  {
54
  private final TextView[] mDurationText;
55
  private float mTextSize;
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

    
59
  private void addSettingsSection(FragmentActivity act, LinearLayout layout, int index)
60
    {
61
    BaseEffect.Type beType = BaseEffect.Type.getType(index);
62

    
63
    int textH= (int)(1.35f*mTextSize);
64
    float A= 1.2f;
65
    float B= 0.25f;
66

    
67
    ///// OUTER LAYOUT ///////////////////////////////////////////////////////////////////
68

    
69
    int margin = (int)(B*textH);
70
    int color  = ContextCompat.getColor(act, R.color.grey);
71
    LinearLayout outerLayout = new LinearLayout(act);
72
    LinearLayout.LayoutParams outerLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT, 0.5f);
73
    outerLayoutParams.topMargin    = index>0 ? margin : 0;
74
    outerLayoutParams.bottomMargin = 0;
75
    outerLayoutParams.leftMargin   = margin;
76
    outerLayoutParams.rightMargin  = margin;
77

    
78
    outerLayout.setLayoutParams(outerLayoutParams);
79
    outerLayout.setGravity(Gravity.CENTER|Gravity.FILL_HORIZONTAL);
80
    outerLayout.setBackgroundColor(color);
81
    outerLayout.setOrientation(LinearLayout.VERTICAL);
82
    layout.addView(outerLayout);
83

    
84
    ///// TEXT ///////////////////////////////////////////////////////////////////////////
85

    
86
    int layoutHeight = (int)(A*textH);
87
    int padding      = (int)(B*textH);
88

    
89
    LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,layoutHeight);
90

    
91
    TextView textView = new TextView(act);
92
    textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
93
    textView.setText(beType.getText());
94
    textView.setLayoutParams(textParams);
95
    textView.setGravity(Gravity.CENTER);
96
    textView.setPadding(padding,0,padding,0);
97
    outerLayout.addView(textView);
98

    
99
    ///// INNER LAYOUT1 //////////////////////////////////////////////////////////////////
100

    
101
    int innerLayout1Height = (int)((A+B)*textH);
102
    LinearLayout innerLayout1 = new LinearLayout(act);
103
    LinearLayout.LayoutParams innerLayout1Params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,innerLayout1Height);
104

    
105
    innerLayout1.setLayoutParams(innerLayout1Params);
106
    innerLayout1.setGravity(Gravity.CENTER|Gravity.FILL_HORIZONTAL);
107
    innerLayout1.setOrientation(LinearLayout.HORIZONTAL);
108
    outerLayout.addView(innerLayout1);
109

    
110
    ///// STUFF INSIDE INNER LAYOUT1 /////////////////////////////////////////////////////
111

    
112
    int text1Padding = (int)(B*textH);
113
    LinearLayout.LayoutParams text1LayoutParams = new LinearLayout.LayoutParams(0,layoutHeight,0.25f);
114

    
115
    TextView text1View = new TextView(act);
116
    text1View.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
117
    text1View.setText(R.string.duration);
118
    text1View.setLayoutParams(text1LayoutParams);
119
    text1View.setGravity(Gravity.START|Gravity.CENTER);
120
    text1View.setPadding(text1Padding,0,text1Padding,0);
121
    innerLayout1.addView(text1View);
122
    //////////////////////////////////////////////////////////////////
123
    int text2Padding = (int)(B*textH);
124
    LinearLayout.LayoutParams text2LayoutParams = new LinearLayout.LayoutParams(0,layoutHeight,0.25f);
125

    
126
    mDurationText[index] = new TextView(act);
127
    mDurationText[index].setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
128
    mDurationText[index].setLayoutParams(text2LayoutParams);
129
    mDurationText[index].setGravity(Gravity.START|Gravity.CENTER);
130
    mDurationText[index].setPadding(text2Padding,0,text2Padding,0);
131
    innerLayout1.addView(mDurationText[index]);
132
    //////////////////////////////////////////////////////////////////
133
    int seekPadding = (int)(B*textH);
134
    LinearLayout.LayoutParams seekLayoutParams = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,0.5f);
135

    
136
    SeekBar seekBar = new SeekBar(act);
137
    seekBar.setLayoutParams(seekLayoutParams);
138
    seekBar.setPadding(seekPadding,0,seekPadding,0);
139
    seekBar.setId(index);
140
    innerLayout1.addView(seekBar);
141

    
142
    seekBar.setOnSeekBarChangeListener(this);
143
    seekBar.setProgress(beType.getCurrentPos());
144

    
145
    ///// INNER LAYOUT2 //////////////////////////////////////////////////////////////////
146

    
147
    int innerLayout2Height = (int)((A+B)*textH);
148
    LinearLayout innerLayout2 = new LinearLayout(act);
149
    LinearLayout.LayoutParams innerLayout2Params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,innerLayout2Height);
150

    
151
    innerLayout2.setLayoutParams(innerLayout2Params);
152
    innerLayout2.setGravity(Gravity.CENTER|Gravity.FILL_HORIZONTAL);
153
    innerLayout2.setOrientation(LinearLayout.HORIZONTAL);
154
    outerLayout.addView(innerLayout2);
155

    
156
    ///// STUFF INSIDE INNER LAYOUT2 /////////////////////////////////////////////////////
157

    
158
    int text3Padding = (int)(B*textH);
159
    LinearLayout.LayoutParams text3LayoutParams = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,0.25f);
160

    
161
    TextView text3View = new TextView(act);
162
    text3View.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
163
    text3View.setText(R.string.type);
164
    text3View.setLayoutParams(text3LayoutParams);
165
    text3View.setGravity(Gravity.START|Gravity.CENTER);
166
    text3View.setPadding(text3Padding,0,text3Padding,0);
167
    innerLayout2.addView(text3View);
168
    //////////////////////////////////////////////////////////////////
169
    int spinnerPadding = (int)(B*textH);
170
    int spinnerMargin  = (int)(B*0.5f*textH);
171
    LinearLayout.LayoutParams spinnerLayoutParams = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,0.75f);
172
    spinnerLayoutParams.topMargin    =   spinnerMargin;
173
    spinnerLayoutParams.bottomMargin =   spinnerMargin;
174
    spinnerLayoutParams.leftMargin   =   spinnerMargin;
175
    spinnerLayoutParams.rightMargin  = 2*spinnerMargin;
176

    
177
    Spinner spinner = new Spinner(act);
178
    spinner.setLayoutParams(spinnerLayoutParams);
179
    spinner.setPadding(spinnerPadding,0,spinnerPadding,0);
180
    spinner.setBackgroundResource(R.drawable.ui_small_spinner);
181
    spinner.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
182
    spinner.setId(index+BaseEffect.Type.LENGTH);
183
    innerLayout2.addView(spinner);
184

    
185
    spinner.setOnItemSelectedListener(this);
186
    String[] appear = BaseEffect.Type.getType(index).getNames();
187

    
188
    ArrayAdapter<String> adapterType = new ArrayAdapter<String>(act, android.R.layout.simple_spinner_item, appear)
189
      {
190
      @NonNull
191
      public View getView(int position, View convertView, @NonNull ViewGroup parent)
192
        {
193
        View v = super.getView(position, convertView, parent);
194
        TextView tv = ((TextView) v);
195
        tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
196
        return v;
197
        }
198
      };
199

    
200
    adapterType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
201
    spinner.setAdapter(adapterType);
202
    spinner.setSelection(beType.getCurrentType());
203
    }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206
// PUBLIC API
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

    
209
  public RubikDialogEffects()
210
    {
211
    mDurationText = new TextView[BaseEffect.Type.LENGTH];
212
    }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

    
216
  @NonNull
217
  @Override
218
  public Dialog onCreateDialog(Bundle savedInstanceState)
219
    {
220
    FragmentActivity act = getActivity();
221
    LayoutInflater inflater = act.getLayoutInflater();
222
    AlertDialog.Builder builder = new AlertDialog.Builder(act);
223
    TextView tv = (TextView) inflater.inflate(R.layout.dialog_title, null);
224

    
225
    DisplayMetrics displaymetrics = new DisplayMetrics();
226
    act.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
227
    final float titleSize= displaymetrics.widthPixels * RubikActivity.MENU_BIG_TEXT_SIZE;
228
    final float okSize   = displaymetrics.widthPixels * RubikActivity.DIALOG_BUTTON_SIZE;
229

    
230
    mTextSize = displaymetrics.widthPixels * RubikActivity.MENU_SMALL_TEXT_SIZE;
231

    
232
    tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
233
    tv.setText(R.string.effects);
234
    builder.setCustomTitle(tv);
235

    
236
    builder.setCancelable(true);
237
    builder.setPositiveButton( R.string.ok, new DialogInterface.OnClickListener()
238
      {
239
      @Override
240
      public void onClick(DialogInterface dialog, int which)
241
        {
242

    
243
        }
244
      });
245

    
246
    final View view = inflater.inflate(R.layout.dialog_effects, null);
247
    builder.setView(view);
248

    
249
    LinearLayout linearLayout = view.findViewById(R.id.settingsLayout);
250

    
251
    if( linearLayout!=null )
252
      {
253
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
254
        {
255
        addSettingsSection(act,linearLayout,i);
256
        }
257
      }
258
    else
259
      {
260
      android.util.Log.e("dialog_settings", "linearLayout NULL!");
261
      }
262

    
263
    Dialog dialog = builder.create();
264
    dialog.setCanceledOnTouchOutside(false);
265
    Window window = dialog.getWindow();
266

    
267
    if( window!=null )
268
      {
269
      window.getDecorView().setSystemUiVisibility(RubikActivity.FLAGS);
270
      }
271

    
272
    dialog.setOnShowListener(new DialogInterface.OnShowListener()
273
      {
274
      @Override
275
      public void onShow(DialogInterface dialog)
276
        {
277
        Button btnPositive = ((AlertDialog)dialog).getButton(Dialog.BUTTON_POSITIVE);
278
        btnPositive.setTextSize(TypedValue.COMPLEX_UNIT_PX, okSize);
279
        }
280
      });
281

    
282
    return dialog;
283
    }
284

    
285
///////////////////////////////////////////////////////////////////////////////////////////////////
286

    
287
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
288
    {
289
    int parentID = parent.getId();
290
    int len = BaseEffect.Type.LENGTH;
291

    
292
    if( parentID>=len && parentID< 2*len) // ith spinner's ID is equal to i+LENGTH (see createSettingSection)
293
      {
294
      BaseEffect.Type.getType(parentID-len).setCurrentType(pos);
295
      }
296
    }
297

    
298
///////////////////////////////////////////////////////////////////////////////////////////////////
299

    
300
  public void onProgressChanged(SeekBar bar, int progress, boolean fromUser)
301
    {
302
    int barID = bar.getId();
303

    
304
    if( barID>=0 && barID< BaseEffect.Type.LENGTH) // ith seekbar's ID is equal to i (see createSettingSection)
305
      {
306
      BaseEffect.Type.getType(barID).setCurrentPos(progress);
307
      int ms = BaseEffect.Type.translatePos(progress);
308
      mDurationText[barID].setText(getString(R.string.ms_placeholder,ms));
309
      }
310
    }
311

    
312
///////////////////////////////////////////////////////////////////////////////////////////////////
313

    
314
  public void onNothingSelected(AdapterView<?> parent) { }
315
  public void onStartTrackingTouch(SeekBar bar) { }
316
  public void onStopTrackingTouch(SeekBar bar)  { }
317
  }
(3-3/19)