Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikDialogSettings.java @ 4235de9b

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.magic;
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.effect.BaseEffect;
44

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

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

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
  private void addSettingsSection(FragmentActivity act, LinearLayout layout, int index)
54
    {
55
    BaseEffect.Type beType = BaseEffect.Type.getType(index);
56
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
57
    float scale = metrics.density;
58

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

    
63
    ///// OUTER LAYOUT ///////////////////////////////////////////////////////////////////
64

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

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

    
80
    ///// TEXT ///////////////////////////////////////////////////////////////////////////
81

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

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

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

    
95
    ///// INNER LAYOUT1 //////////////////////////////////////////////////////////////////
96

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

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

    
106
    ///// STUFF INSIDE INNER LAYOUT1 /////////////////////////////////////////////////////
107

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

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

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

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

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

    
141
    ///// INNER LAYOUT2 //////////////////////////////////////////////////////////////////
142

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

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

    
152
    ///// STUFF INSIDE INNER LAYOUT2 /////////////////////////////////////////////////////
153

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

    
157
    TextView text3View = new TextView(act);
158
    text3View.setText(R.string.type);
159
    text3View.setLayoutParams(text3LayoutParams);
160
    text3View.setGravity(Gravity.START|Gravity.CENTER);
161
    text3View.setPadding(text3Padding,0,text3Padding,0);
162
    text3View.setTextAppearance(android.R.style.TextAppearance_Small);
163
    innerLayout2.addView(text3View);
164
    //////////////////////////////////////////////////////////////////
165
    int spinnerPadding = (int)(scale*10 + 0.5f);
166
    LinearLayout.LayoutParams spinnerLayoutParams = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,0.6f);
167

    
168
    Spinner spinner = new Spinner(act);
169
    spinner.setLayoutParams(spinnerLayoutParams);
170
    spinner.setPadding(spinnerPadding,0,spinnerPadding,0);
171
    spinner.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
172
    spinner.setId(index+BaseEffect.Type.LENGTH);
173
    innerLayout2.addView(spinner);
174

    
175
    spinner.setOnItemSelectedListener(this);
176
    String[] appear = BaseEffect.Type.getType(index).getNames();
177

    
178
    ArrayAdapter<String> adapterType = new ArrayAdapter<>(act,android.R.layout.simple_spinner_item, appear);
179
    adapterType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
180
    spinner.setAdapter(adapterType);
181
    spinner.setSelection(beType.getCurrentType());
182
    }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185
// PUBLIC API
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187

    
188
  public RubikDialogSettings()
189
    {
190
    mDurationText = new TextView[BaseEffect.Type.LENGTH];
191
    }
192

    
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194

    
195
  @Override
196
  public void onStart()
197
    {
198
    super.onStart();
199

    
200
    Window window = getDialog().getWindow();
201
    window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
202
                    WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
203
    window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
204
    }
205

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

    
208
  @NonNull
209
  @Override
210
  public Dialog onCreateDialog(Bundle savedInstanceState)
211
    {
212
    FragmentActivity act = getActivity();
213
    LayoutInflater inflater = act.getLayoutInflater();
214
    AlertDialog.Builder builder = new AlertDialog.Builder(act);
215
    TextView tv = (TextView) inflater.inflate(R.layout.dialog_title, null);
216
    tv.setText(R.string.settings);
217
    builder.setCustomTitle(tv);
218

    
219
    builder.setCancelable(true);
220
    builder.setPositiveButton( R.string.ok, new DialogInterface.OnClickListener()
221
      {
222
      @Override
223
      public void onClick(DialogInterface dialog, int which)
224
        {
225

    
226
        }
227
      });
228

    
229
    final View view = inflater.inflate(R.layout.dialog_settings, null);
230
    builder.setView(view);
231

    
232
    LinearLayout linearLayout = view.findViewById(R.id.settingsLayout);
233

    
234
    if( linearLayout!=null )
235
      {
236
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
237
        {
238
        addSettingsSection(act,linearLayout,i);
239
        }
240
      }
241
    else
242
      {
243
      android.util.Log.e("dialog_settings", "linearLayout NULL!");
244
      }
245

    
246
    return builder.create();
247
    }
248

    
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250

    
251
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
252
    {
253
    int parentID = parent.getId();
254
    int len = BaseEffect.Type.LENGTH;
255

    
256
    if( parentID>=len && parentID< 2*len) // ith spinner's ID is equal to i+LENGTH (see createSettingSection)
257
      {
258
      BaseEffect.Type.getType(parentID-len).setCurrentType(pos);
259
      }
260
    }
261

    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263

    
264
  public void onProgressChanged(SeekBar bar, int progress, boolean fromUser)
265
    {
266
    int barID = bar.getId();
267

    
268
    if( barID>=0 && barID< BaseEffect.Type.LENGTH) // ith seekbar's ID is equal to i (see createSettingSection)
269
      {
270
      BaseEffect.Type.getType(barID).setCurrentPos(progress);
271
      int ms = BaseEffect.Type.translatePos(progress);
272
      mDurationText[barID].setText(getString(R.string.ms_placeholder,ms));
273
      }
274
    }
275

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

    
278
  public void onNothingSelected(AdapterView<?> parent) { }
279
  public void onStartTrackingTouch(SeekBar bar) { }
280
  public void onStopTrackingTouch(SeekBar bar)  { }
281
  }
(7-7/11)