Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogEffects.java @ d2556e79

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.widget.AdapterView;
38
import android.widget.ArrayAdapter;
39
import android.widget.Button;
40
import android.widget.LinearLayout;
41
import android.widget.SeekBar;
42
import android.widget.Spinner;
43
import android.widget.TextView;
44

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

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

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

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

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

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

    
66
    ///// OUTER LAYOUT ///////////////////////////////////////////////////////////////////
67

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

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

    
83
    ///// TEXT ///////////////////////////////////////////////////////////////////////////
84

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

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

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

    
98
    ///// INNER LAYOUT1 //////////////////////////////////////////////////////////////////
99

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

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

    
109
    ///// STUFF INSIDE INNER LAYOUT1 /////////////////////////////////////////////////////
110

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

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

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

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

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

    
144
    ///// INNER LAYOUT2 //////////////////////////////////////////////////////////////////
145

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

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

    
155
    ///// STUFF INSIDE INNER LAYOUT2 /////////////////////////////////////////////////////
156

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

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

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

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

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

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

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205
// PUBLIC API
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

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

    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214

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

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

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

    
231
    tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
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
    Dialog dialog = builder.create();
263
    dialog.setCanceledOnTouchOutside(false);
264
    Window window = dialog.getWindow();
265

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

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

    
281
    return dialog;
282
    }
283

    
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285

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

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

    
297
///////////////////////////////////////////////////////////////////////////////////////////////////
298

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

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

    
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312

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