Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogEffects.java @ 90fd47b0

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
    dialog.setCanceledOnTouchOutside(false);
61

    
62
    Window window = dialog.getWindow();
63

    
64
    if( window!=null )
65
      {
66
      window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
67
      }
68
    }
69

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

    
72
  private void addSettingsSection(FragmentActivity act, LinearLayout layout, int index)
73
    {
74
    BaseEffect.Type beType = BaseEffect.Type.getType(index);
75
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
76
    float scale = metrics.density;
77

    
78
    int textH=32;
79
    int layoH=36;
80
    int margH=10;
81

    
82
    ///// OUTER LAYOUT ///////////////////////////////////////////////////////////////////
83

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

    
93
    outerLayout.setLayoutParams(outerLayoutParams);
94
    outerLayout.setGravity(Gravity.CENTER|Gravity.FILL_HORIZONTAL);
95
    outerLayout.setBackgroundColor(color);
96
    outerLayout.setOrientation(LinearLayout.VERTICAL);
97
    layout.addView(outerLayout);
98

    
99
    ///// TEXT ///////////////////////////////////////////////////////////////////////////
100

    
101
    int layoutHeight = (int)(scale*textH + 0.5f);
102
    int padding      = (int)(scale*10    + 0.5f);
103

    
104
    LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,layoutHeight);
105

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

    
114
    ///// INNER LAYOUT1 //////////////////////////////////////////////////////////////////
115

    
116
    int innerLayout1Height = (int)(scale*layoH + 0.5f);
117
    LinearLayout innerLayout1 = new LinearLayout(act);
118
    LinearLayout.LayoutParams innerLayout1Params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,innerLayout1Height);
119

    
120
    innerLayout1.setLayoutParams(innerLayout1Params);
121
    innerLayout1.setGravity(Gravity.CENTER|Gravity.FILL_HORIZONTAL);
122
    innerLayout1.setOrientation(LinearLayout.HORIZONTAL);
123
    outerLayout.addView(innerLayout1);
124

    
125
    ///// STUFF INSIDE INNER LAYOUT1 /////////////////////////////////////////////////////
126

    
127
    int text1Padding = (int)(scale*5 + 0.5f);
128
    LinearLayout.LayoutParams text1LayoutParams = new LinearLayout.LayoutParams(0,layoutHeight,0.25f);
129

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

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

    
151
    SeekBar seekBar = new SeekBar(act);
152
    seekBar.setLayoutParams(seekLayoutParams);
153
    seekBar.setPadding(seekPadding,0,seekPadding,0);
154
    seekBar.setId(index);
155
    innerLayout1.addView(seekBar);
156

    
157
    seekBar.setOnSeekBarChangeListener(this);
158
    seekBar.setProgress(beType.getCurrentPos());
159

    
160
    ///// INNER LAYOUT2 //////////////////////////////////////////////////////////////////
161

    
162
    int innerLayout2Height = (int)(scale*layoH + 0.5f);
163
    LinearLayout innerLayout2 = new LinearLayout(act);
164
    LinearLayout.LayoutParams innerLayout2Params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,innerLayout2Height);
165

    
166
    innerLayout2.setLayoutParams(innerLayout2Params);
167
    innerLayout2.setGravity(Gravity.CENTER|Gravity.FILL_HORIZONTAL);
168
    innerLayout2.setOrientation(LinearLayout.HORIZONTAL);
169
    outerLayout.addView(innerLayout2);
170

    
171
    ///// STUFF INSIDE INNER LAYOUT2 /////////////////////////////////////////////////////
172

    
173
    int text3Padding = (int)(scale*5 + 0.5f);
174
    LinearLayout.LayoutParams text3LayoutParams = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,0.25f);
175

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

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

    
200
    spinner.setOnItemSelectedListener(this);
201
    String[] appear = BaseEffect.Type.getType(index).getNames();
202

    
203
    ArrayAdapter<String> adapterType = new ArrayAdapter<>(act,android.R.layout.simple_spinner_item, appear);
204
    adapterType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
205
    spinner.setAdapter(adapterType);
206
    spinner.setSelection(beType.getCurrentType());
207
    }
208

    
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210
// PUBLIC API
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

    
213
  public RubikDialogEffects()
214
    {
215
    mDurationText = new TextView[BaseEffect.Type.LENGTH];
216
    }
217

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

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

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

    
238
        }
239
      });
240

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

    
244
    LinearLayout linearLayout = view.findViewById(R.id.settingsLayout);
245

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

    
258
    return builder.create();
259
    }
260

    
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262

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

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

    
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275

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

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

    
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289

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