Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogEffects.java @ 801b16db

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.view.Gravity;
32
import android.view.LayoutInflater;
33
import android.view.View;
34
import android.view.Window;
35
import android.view.WindowManager;
36
import android.widget.AdapterView;
37
import android.widget.ArrayAdapter;
38
import android.widget.LinearLayout;
39
import android.widget.SeekBar;
40
import android.widget.Spinner;
41
import android.widget.TextView;
42

    
43
import org.distorted.effects.BaseEffect;
44
import org.distorted.main.R;
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

    
48
public class RubikDialogEffects extends AppCompatDialogFragment implements SeekBar.OnSeekBarChangeListener, AdapterView.OnItemSelectedListener
49
  {
50
  private TextView[] mDurationText;
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

    
54
  @Override
55
  public void onStart()
56
    {
57
    super.onStart();
58

    
59
    Dialog dialog = getDialog();
60

    
61
    if( dialog!=null )
62
      {
63
      dialog.setCanceledOnTouchOutside(false);
64

    
65
      Window window = dialog.getWindow();
66

    
67
      if( window!=null )
68
        {
69
        window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
70
        }
71
      }
72
    }
73

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75

    
76
  private void addSettingsSection(FragmentActivity act, LinearLayout layout, int index)
77
    {
78
    BaseEffect.Type beType = BaseEffect.Type.getType(index);
79
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
80
    float scale = metrics.density;
81

    
82
    int textH=32;
83
    int layoH=36;
84
    int margH=10;
85

    
86
    ///// OUTER LAYOUT ///////////////////////////////////////////////////////////////////
87

    
88
    int margin = (int)(scale*margH + 0.5f);
89
    int color  = ContextCompat.getColor(act, R.color.grey);
90
    LinearLayout outerLayout = new LinearLayout(act);
91
    LinearLayout.LayoutParams outerLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT, 0.5f);
92
    outerLayoutParams.topMargin    = index>0 ? margin : 0;
93
    outerLayoutParams.bottomMargin = 0;
94
    outerLayoutParams.leftMargin   = margin;
95
    outerLayoutParams.rightMargin  = margin;
96

    
97
    outerLayout.setLayoutParams(outerLayoutParams);
98
    outerLayout.setGravity(Gravity.CENTER|Gravity.FILL_HORIZONTAL);
99
    outerLayout.setBackgroundColor(color);
100
    outerLayout.setOrientation(LinearLayout.VERTICAL);
101
    layout.addView(outerLayout);
102

    
103
    ///// TEXT ///////////////////////////////////////////////////////////////////////////
104

    
105
    int layoutHeight = (int)(scale*textH + 0.5f);
106
    int padding      = (int)(scale*10    + 0.5f);
107

    
108
    LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,layoutHeight);
109

    
110
    TextView textView = new TextView(act);
111
    textView.setText(beType.getText());
112
    textView.setLayoutParams(textParams);
113
    textView.setGravity(Gravity.CENTER);
114
    textView.setPadding(padding,0,padding,0);
115
    textView.setTextAppearance(act,android.R.style.TextAppearance_Small);
116
    outerLayout.addView(textView);
117

    
118
    ///// INNER LAYOUT1 //////////////////////////////////////////////////////////////////
119

    
120
    int innerLayout1Height = (int)(scale*layoH + 0.5f);
121
    LinearLayout innerLayout1 = new LinearLayout(act);
122
    LinearLayout.LayoutParams innerLayout1Params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,innerLayout1Height);
123

    
124
    innerLayout1.setLayoutParams(innerLayout1Params);
125
    innerLayout1.setGravity(Gravity.CENTER|Gravity.FILL_HORIZONTAL);
126
    innerLayout1.setOrientation(LinearLayout.HORIZONTAL);
127
    outerLayout.addView(innerLayout1);
128

    
129
    ///// STUFF INSIDE INNER LAYOUT1 /////////////////////////////////////////////////////
130

    
131
    int text1Padding = (int)(scale*5 + 0.5f);
132
    LinearLayout.LayoutParams text1LayoutParams = new LinearLayout.LayoutParams(0,layoutHeight,0.25f);
133

    
134
    TextView text1View = new TextView(act);
135
    text1View.setText(R.string.duration);
136
    text1View.setLayoutParams(text1LayoutParams);
137
    text1View.setGravity(Gravity.START|Gravity.CENTER);
138
    text1View.setPadding(text1Padding,0,text1Padding,0);
139
    text1View.setTextAppearance(act,android.R.style.TextAppearance_Small);
140
    innerLayout1.addView(text1View);
141
    //////////////////////////////////////////////////////////////////
142
    int text2Padding = (int)(scale*5 + 0.5f);
143
    LinearLayout.LayoutParams text2LayoutParams = new LinearLayout.LayoutParams(0,layoutHeight,0.25f);
144

    
145
    mDurationText[index] = new TextView(act);
146
    mDurationText[index].setLayoutParams(text2LayoutParams);
147
    mDurationText[index].setGravity(Gravity.START|Gravity.CENTER);
148
    mDurationText[index].setPadding(text2Padding,0,text2Padding,0);
149
    mDurationText[index].setTextAppearance(act,android.R.style.TextAppearance_Small);
150
    innerLayout1.addView(mDurationText[index]);
151
    //////////////////////////////////////////////////////////////////
152
    int seekPadding = (int)(scale*10 + 0.5f);
153
    LinearLayout.LayoutParams seekLayoutParams = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,0.5f);
154

    
155
    SeekBar seekBar = new SeekBar(act);
156
    seekBar.setLayoutParams(seekLayoutParams);
157
    seekBar.setPadding(seekPadding,0,seekPadding,0);
158
    seekBar.setId(index);
159
    innerLayout1.addView(seekBar);
160

    
161
    seekBar.setOnSeekBarChangeListener(this);
162
    seekBar.setProgress(beType.getCurrentPos());
163

    
164
    ///// INNER LAYOUT2 //////////////////////////////////////////////////////////////////
165

    
166
    int innerLayout2Height = (int)(scale*layoH + 0.5f);
167
    LinearLayout innerLayout2 = new LinearLayout(act);
168
    LinearLayout.LayoutParams innerLayout2Params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,innerLayout2Height);
169

    
170
    innerLayout2.setLayoutParams(innerLayout2Params);
171
    innerLayout2.setGravity(Gravity.CENTER|Gravity.FILL_HORIZONTAL);
172
    innerLayout2.setOrientation(LinearLayout.HORIZONTAL);
173
    outerLayout.addView(innerLayout2);
174

    
175
    ///// STUFF INSIDE INNER LAYOUT2 /////////////////////////////////////////////////////
176

    
177
    int text3Padding = (int)(scale*5 + 0.5f);
178
    LinearLayout.LayoutParams text3LayoutParams = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,0.25f);
179

    
180
    TextView text3View = new TextView(act);
181
    text3View.setText(R.string.type);
182
    text3View.setLayoutParams(text3LayoutParams);
183
    text3View.setGravity(Gravity.START|Gravity.CENTER);
184
    text3View.setPadding(text3Padding,0,text3Padding,0);
185
    text3View.setTextAppearance(act,android.R.style.TextAppearance_Small);
186
    innerLayout2.addView(text3View);
187
    //////////////////////////////////////////////////////////////////
188
    int spinnerPadding = (int)(scale*10 + 0.5f);
189
    int spinnerMargin  = (int)(scale* 3 + 0.5f);
190
    LinearLayout.LayoutParams spinnerLayoutParams = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,0.75f);
191
    spinnerLayoutParams.topMargin    =   spinnerMargin;
192
    spinnerLayoutParams.bottomMargin =   spinnerMargin;
193
    spinnerLayoutParams.leftMargin   =   spinnerMargin;
194
    spinnerLayoutParams.rightMargin  = 2*spinnerMargin;
195

    
196
    Spinner spinner = new Spinner(act);
197
    spinner.setLayoutParams(spinnerLayoutParams);
198
    spinner.setPadding(spinnerPadding,0,spinnerPadding,0);
199
    spinner.setBackgroundResource(R.drawable.spinner);
200
    spinner.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
201
    spinner.setId(index+BaseEffect.Type.LENGTH);
202
    innerLayout2.addView(spinner);
203

    
204
    spinner.setOnItemSelectedListener(this);
205
    String[] appear = BaseEffect.Type.getType(index).getNames();
206

    
207
    ArrayAdapter<String> adapterType = new ArrayAdapter<>(act,android.R.layout.simple_spinner_item, appear);
208
    adapterType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
209
    spinner.setAdapter(adapterType);
210
    spinner.setSelection(beType.getCurrentType());
211
    }
212

    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214
// PUBLIC API
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

    
217
  public RubikDialogEffects()
218
    {
219
    mDurationText = new TextView[BaseEffect.Type.LENGTH];
220
    }
221

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

    
224
  @NonNull
225
  @Override
226
  public Dialog onCreateDialog(Bundle savedInstanceState)
227
    {
228
    FragmentActivity act = getActivity();
229
    LayoutInflater inflater = act.getLayoutInflater();
230
    AlertDialog.Builder builder = new AlertDialog.Builder(act);
231
    TextView tv = (TextView) inflater.inflate(R.layout.dialog_title, null);
232
    tv.setText(R.string.effects);
233
    builder.setCustomTitle(tv);
234

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

    
242
        }
243
      });
244

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

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

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

    
262
    return builder.create();
263
    }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266

    
267
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
268
    {
269
    int parentID = parent.getId();
270
    int len = BaseEffect.Type.LENGTH;
271

    
272
    if( parentID>=len && parentID< 2*len) // ith spinner's ID is equal to i+LENGTH (see createSettingSection)
273
      {
274
      BaseEffect.Type.getType(parentID-len).setCurrentType(pos);
275
      }
276
    }
277

    
278
///////////////////////////////////////////////////////////////////////////////////////////////////
279

    
280
  public void onProgressChanged(SeekBar bar, int progress, boolean fromUser)
281
    {
282
    int barID = bar.getId();
283

    
284
    if( barID>=0 && barID< BaseEffect.Type.LENGTH) // ith seekbar's ID is equal to i (see createSettingSection)
285
      {
286
      BaseEffect.Type.getType(barID).setCurrentPos(progress);
287
      int ms = BaseEffect.Type.translatePos(progress);
288
      mDurationText[barID].setText(getString(R.string.ms_placeholder,ms));
289
      }
290
    }
291

    
292
///////////////////////////////////////////////////////////////////////////////////////////////////
293

    
294
  public void onNothingSelected(AdapterView<?> parent) { }
295
  public void onStartTrackingTouch(SeekBar bar) { }
296
  public void onStopTrackingTouch(SeekBar bar)  { }
297
  }
(2-2/13)