Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogEffects.java @ 316889cf

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 android.support.annotation.NonNull;
26
import android.support.v4.app.FragmentActivity;
27
import android.support.v4.content.ContextCompat;
28
import android.support.v7.app.AlertDialog;
29
import android.support.v7.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
  private void addSettingsSection(FragmentActivity act, LinearLayout layout, int index)
55
    {
56
    BaseEffect.Type beType = BaseEffect.Type.getType(index);
57
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
58
    float scale = metrics.density;
59

    
60
    int textH=32;
61
    int layoH=36;
62
    int margH=10;
63

    
64
    ///// OUTER LAYOUT ///////////////////////////////////////////////////////////////////
65

    
66
    int margin = (int)(scale*margH + 0.5f);
67
    int color  = ContextCompat.getColor(act, R.color.grey);
68
    LinearLayout outerLayout = new LinearLayout(act);
69
    LinearLayout.LayoutParams outerLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT, 0.5f);
70
    outerLayoutParams.topMargin    = index>0 ? margin : 0;
71
    outerLayoutParams.bottomMargin = 0;
72
    outerLayoutParams.leftMargin   = margin;
73
    outerLayoutParams.rightMargin  = margin;
74

    
75
    outerLayout.setLayoutParams(outerLayoutParams);
76
    outerLayout.setGravity(Gravity.CENTER|Gravity.FILL_HORIZONTAL);
77
    outerLayout.setBackgroundColor(color);
78
    outerLayout.setOrientation(LinearLayout.VERTICAL);
79
    layout.addView(outerLayout);
80

    
81
    ///// TEXT ///////////////////////////////////////////////////////////////////////////
82

    
83
    int layoutHeight = (int)(scale*textH + 0.5f);
84
    int padding      = (int)(scale*10    + 0.5f);
85

    
86
    LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,layoutHeight);
87

    
88
    TextView textView = new TextView(act);
89
    textView.setText(beType.getText());
90
    textView.setLayoutParams(textParams);
91
    textView.setGravity(Gravity.CENTER);
92
    textView.setPadding(padding,0,padding,0);
93
    textView.setTextAppearance(act,android.R.style.TextAppearance_Small);
94
    outerLayout.addView(textView);
95

    
96
    ///// INNER LAYOUT1 //////////////////////////////////////////////////////////////////
97

    
98
    int innerLayout1Height = (int)(scale*layoH + 0.5f);
99
    LinearLayout innerLayout1 = new LinearLayout(act);
100
    LinearLayout.LayoutParams innerLayout1Params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,innerLayout1Height);
101

    
102
    innerLayout1.setLayoutParams(innerLayout1Params);
103
    innerLayout1.setGravity(Gravity.CENTER|Gravity.FILL_HORIZONTAL);
104
    innerLayout1.setOrientation(LinearLayout.HORIZONTAL);
105
    outerLayout.addView(innerLayout1);
106

    
107
    ///// STUFF INSIDE INNER LAYOUT1 /////////////////////////////////////////////////////
108

    
109
    int text1Padding = (int)(scale*5 + 0.5f);
110
    LinearLayout.LayoutParams text1LayoutParams = new LinearLayout.LayoutParams(0,layoutHeight,0.25f);
111

    
112
    TextView text1View = new TextView(act);
113
    text1View.setText(R.string.duration);
114
    text1View.setLayoutParams(text1LayoutParams);
115
    text1View.setGravity(Gravity.START|Gravity.CENTER);
116
    text1View.setPadding(text1Padding,0,text1Padding,0);
117
    text1View.setTextAppearance(act,android.R.style.TextAppearance_Small);
118
    innerLayout1.addView(text1View);
119
    //////////////////////////////////////////////////////////////////
120
    int text2Padding = (int)(scale*5 + 0.5f);
121
    LinearLayout.LayoutParams text2LayoutParams = new LinearLayout.LayoutParams(0,layoutHeight,0.25f);
122

    
123
    mDurationText[index] = new TextView(act);
124
    mDurationText[index].setLayoutParams(text2LayoutParams);
125
    mDurationText[index].setGravity(Gravity.START|Gravity.CENTER);
126
    mDurationText[index].setPadding(text2Padding,0,text2Padding,0);
127
    mDurationText[index].setTextAppearance(act,android.R.style.TextAppearance_Small);
128
    innerLayout1.addView(mDurationText[index]);
129
    //////////////////////////////////////////////////////////////////
130
    int seekPadding = (int)(scale*10 + 0.5f);
131
    LinearLayout.LayoutParams seekLayoutParams = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,0.5f);
132

    
133
    SeekBar seekBar = new SeekBar(act);
134
    seekBar.setLayoutParams(seekLayoutParams);
135
    seekBar.setPadding(seekPadding,0,seekPadding,0);
136
    seekBar.setId(index);
137
    innerLayout1.addView(seekBar);
138

    
139
    seekBar.setOnSeekBarChangeListener(this);
140
    seekBar.setProgress(beType.getCurrentPos());
141

    
142
    ///// INNER LAYOUT2 //////////////////////////////////////////////////////////////////
143

    
144
    int innerLayout2Height = (int)(scale*layoH + 0.5f);
145
    LinearLayout innerLayout2 = new LinearLayout(act);
146
    LinearLayout.LayoutParams innerLayout2Params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,innerLayout2Height);
147

    
148
    innerLayout2.setLayoutParams(innerLayout2Params);
149
    innerLayout2.setGravity(Gravity.CENTER|Gravity.FILL_HORIZONTAL);
150
    innerLayout2.setOrientation(LinearLayout.HORIZONTAL);
151
    outerLayout.addView(innerLayout2);
152

    
153
    ///// STUFF INSIDE INNER LAYOUT2 /////////////////////////////////////////////////////
154

    
155
    int text3Padding = (int)(scale*5 + 0.5f);
156
    LinearLayout.LayoutParams text3LayoutParams = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,0.25f);
157

    
158
    TextView text3View = new TextView(act);
159
    text3View.setText(R.string.type);
160
    text3View.setLayoutParams(text3LayoutParams);
161
    text3View.setGravity(Gravity.START|Gravity.CENTER);
162
    text3View.setPadding(text3Padding,0,text3Padding,0);
163
    text3View.setTextAppearance(act,android.R.style.TextAppearance_Small);
164
    innerLayout2.addView(text3View);
165
    //////////////////////////////////////////////////////////////////
166
    int spinnerPadding = (int)(scale*10 + 0.5f);
167
    int spinnerMargin  = (int)(scale* 3 + 0.5f);
168
    LinearLayout.LayoutParams spinnerLayoutParams = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,0.75f);
169
    spinnerLayoutParams.topMargin    =   spinnerMargin;
170
    spinnerLayoutParams.bottomMargin =   spinnerMargin;
171
    spinnerLayoutParams.leftMargin   =   spinnerMargin;
172
    spinnerLayoutParams.rightMargin  = 2*spinnerMargin;
173

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

    
182
    spinner.setOnItemSelectedListener(this);
183
    String[] appear = BaseEffect.Type.getType(index).getNames();
184

    
185
    ArrayAdapter<String> adapterType = new ArrayAdapter<>(act,android.R.layout.simple_spinner_item, appear);
186
    adapterType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
187
    spinner.setAdapter(adapterType);
188
    spinner.setSelection(beType.getCurrentType());
189
    }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192
// PUBLIC API
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194

    
195
  public RubikDialogEffects()
196
    {
197
    mDurationText = new TextView[BaseEffect.Type.LENGTH];
198
    }
199

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201

    
202
  @Override
203
  public void onStart()
204
    {
205
    super.onStart();
206

    
207
    Dialog dialog = getDialog();
208
    dialog.setCanceledOnTouchOutside(false);
209

    
210
    Window window = dialog.getWindow();
211

    
212
    if( window!=null )
213
      {
214
      window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
215
                      WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
216
      window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
217
      }
218
    }
219

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221

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

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

    
240
        }
241
      });
242

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

    
246
    LinearLayout linearLayout = view.findViewById(R.id.settingsLayout);
247

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

    
260
    return builder.create();
261
    }
262

    
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264

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

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

    
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277

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

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

    
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291

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