Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikSettings.java @ 4918f19c

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.widget.AdapterView;
35
import android.widget.ArrayAdapter;
36
import android.widget.LinearLayout;
37
import android.widget.SeekBar;
38
import android.widget.Spinner;
39
import android.widget.TextView;
40

    
41
import org.distorted.effect.BaseEffect;
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

    
45
public class RubikSettings extends AppCompatDialogFragment implements SeekBar.OnSeekBarChangeListener, AdapterView.OnItemSelectedListener
46
  {
47
  private TextView[] mDurationText;
48

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

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

    
57
    int textH;
58
    int layoH;
59

    
60
    if( metrics.widthPixels > metrics.heightPixels )
61
      {
62
      textH = 20;
63
      layoH = 26;
64
      }
65
    else
66
      {
67
      textH = 32;
68
      layoH = 36;
69
      }
70

    
71
    ///// OUTER LAYOUT ///////////////////////////////////////////////////////////////////
72

    
73
    int margin = (int)(scale*10 + 0.5f);
74
    int color  = ContextCompat.getColor(act, R.color.grey);
75
    LinearLayout outerLayout = new LinearLayout(act);
76
    LinearLayout.LayoutParams outerLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT, 0.5f);
77
    outerLayoutParams.topMargin    = margin;
78
    outerLayoutParams.bottomMargin = 0;
79
    outerLayoutParams.leftMargin   = margin;
80
    outerLayoutParams.rightMargin  = margin;
81

    
82
    outerLayout.setLayoutParams(outerLayoutParams);
83
    outerLayout.setGravity(Gravity.CENTER|Gravity.FILL_HORIZONTAL);
84
    outerLayout.setBackgroundColor(color);
85
    outerLayout.setOrientation(LinearLayout.VERTICAL);
86
    layout.addView(outerLayout);
87

    
88
    ///// TEXT ///////////////////////////////////////////////////////////////////////////
89

    
90
    int layoutHeight = (int)(scale*textH + 0.5f);
91
    int padding      = (int)(scale*10    + 0.5f);
92

    
93
    LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,layoutHeight);
94

    
95
    TextView textView = new TextView(act);
96
    textView.setText(beType.getText());
97
    textView.setLayoutParams(textParams);
98
    textView.setGravity(Gravity.CENTER);
99
    textView.setPadding(padding,0,padding,0);
100
    textView.setTextAppearance(android.R.style.TextAppearance_Small);
101
    outerLayout.addView(textView);
102

    
103
    ///// INNER LAYOUT1 //////////////////////////////////////////////////////////////////
104

    
105
    int innerLayout1Height = (int)(scale*layoH + 0.5f);
106
    LinearLayout innerLayout1 = new LinearLayout(act);
107
    LinearLayout.LayoutParams innerLayout1Params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,innerLayout1Height);
108

    
109
    innerLayout1.setLayoutParams(innerLayout1Params);
110
    innerLayout1.setGravity(Gravity.CENTER|Gravity.FILL_HORIZONTAL);
111
    innerLayout1.setOrientation(LinearLayout.HORIZONTAL);
112
    outerLayout.addView(innerLayout1);
113

    
114
    ///// STUFF INSIDE INNER LAYOUT1 /////////////////////////////////////////////////////
115

    
116
    int text1Padding = (int)(scale*5 + 0.5f);
117
    LinearLayout.LayoutParams text1LayoutParams = new LinearLayout.LayoutParams(0,layoutHeight,0.2f);
118

    
119
    TextView text1View = new TextView(act);
120
    text1View.setText(R.string.duration);
121
    text1View.setLayoutParams(text1LayoutParams);
122
    text1View.setGravity(Gravity.START|Gravity.CENTER);
123
    text1View.setPadding(text1Padding,0,text1Padding,0);
124
    text1View.setTextAppearance(android.R.style.TextAppearance_Small);
125
    innerLayout1.addView(text1View);
126
    //////////////////////////////////////////////////////////////////
127
    int text2Padding = (int)(scale*5 + 0.5f);
128
    LinearLayout.LayoutParams text2LayoutParams = new LinearLayout.LayoutParams(0,layoutHeight,0.2f);
129

    
130
    mDurationText[index] = new TextView(act);
131
    mDurationText[index].setLayoutParams(text2LayoutParams);
132
    mDurationText[index].setGravity(Gravity.END|Gravity.CENTER);
133
    mDurationText[index].setPadding(text2Padding,0,text2Padding,0);
134
    mDurationText[index].setTextAppearance(android.R.style.TextAppearance_Small);
135
    innerLayout1.addView(mDurationText[index]);
136
    //////////////////////////////////////////////////////////////////
137
    int seekPadding = (int)(scale*10 + 0.5f);
138
    LinearLayout.LayoutParams seekLayoutParams = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,0.6f);
139

    
140
    SeekBar seekBar = new SeekBar(act);
141
    seekBar.setLayoutParams(seekLayoutParams);
142
    seekBar.setPadding(seekPadding,0,seekPadding,0);
143
    seekBar.setId(index);
144
    innerLayout1.addView(seekBar);
145

    
146
    seekBar.setOnSeekBarChangeListener(this);
147
    seekBar.setProgress(beType.getCurrentPos());
148

    
149
    ///// INNER LAYOUT2 //////////////////////////////////////////////////////////////////
150

    
151
    int innerLayout2Height = (int)(scale*layoH + 0.5f);
152
    LinearLayout innerLayout2 = new LinearLayout(act);
153
    LinearLayout.LayoutParams innerLayout2Params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,innerLayout2Height);
154

    
155
    innerLayout2.setLayoutParams(innerLayout2Params);
156
    innerLayout2.setGravity(Gravity.CENTER|Gravity.FILL_HORIZONTAL);
157
    innerLayout2.setOrientation(LinearLayout.HORIZONTAL);
158
    outerLayout.addView(innerLayout2);
159

    
160
    ///// STUFF INSIDE INNER LAYOUT2 /////////////////////////////////////////////////////
161

    
162
    int text3Padding = (int)(scale*5 + 0.5f);
163
    LinearLayout.LayoutParams text3LayoutParams = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,0.4f);
164

    
165
    TextView text3View = new TextView(act);
166
    text3View.setText(R.string.type);
167
    text3View.setLayoutParams(text3LayoutParams);
168
    text3View.setGravity(Gravity.START|Gravity.CENTER);
169
    text3View.setPadding(text3Padding,0,text3Padding,0);
170
    text3View.setTextAppearance(android.R.style.TextAppearance_Small);
171
    innerLayout2.addView(text3View);
172
    //////////////////////////////////////////////////////////////////
173
    int spinnerPadding = (int)(scale*10 + 0.5f);
174
    LinearLayout.LayoutParams spinnerLayoutParams = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,0.6f);
175

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

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

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

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193
// PUBLIC API
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

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

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

    
203
  @NonNull
204
  @Override
205
  public Dialog onCreateDialog(Bundle savedInstanceState)
206
    {
207
    FragmentActivity act = getActivity();
208
    AlertDialog.Builder builder = new AlertDialog.Builder(act);
209

    
210
    builder.setCancelable(true);
211
    builder.setPositiveButton( R.string.ok, new DialogInterface.OnClickListener()
212
      {
213
      @Override
214
      public void onClick(DialogInterface dialog, int which)
215
        {
216

    
217
        }
218
      });
219

    
220
    LayoutInflater inflater = act.getLayoutInflater();
221
    final View view = inflater.inflate(R.layout.settings, null);
222
    builder.setView(view);
223

    
224
    LinearLayout linearLayout = view.findViewById(R.id.main_settings_layout);
225

    
226
    if( linearLayout!=null )
227
      {
228
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
229
        {
230
        createSettingsSection(act,linearLayout,i);
231
        }
232
      }
233
    else
234
      {
235
      android.util.Log.e("settings", "linearLayout NULL!");
236
      }
237

    
238
    return builder.create();
239
    }
240

    
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242

    
243
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
244
    {
245
    int parentID = parent.getId();
246
    int len = BaseEffect.Type.LENGTH;
247

    
248
    if( parentID>=len && parentID< 2*len) // ith spinner's ID is equal to i+LENGTH (see createSettingSection)
249
      {
250
      BaseEffect.Type.getType(parentID-len).setCurrentType(pos);
251
      }
252
    }
253

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

    
256
  public void onProgressChanged(SeekBar bar, int progress, boolean fromUser)
257
    {
258
    int barID = bar.getId();
259

    
260
    if( barID>=0 && barID< BaseEffect.Type.LENGTH) // ith seekbar's ID is equal to i (see createSettingSection)
261
      {
262
      BaseEffect.Type.getType(barID).setCurrentPos(progress);
263
      int ms = BaseEffect.Type.translatePos(progress);
264
      mDurationText[barID].setText(getString(R.string.ms_placeholder,ms));
265
      }
266
    }
267

    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269

    
270
  public void onNothingSelected(AdapterView<?> parent) { }
271
  public void onStartTrackingTouch(SeekBar bar) { }
272
  public void onStopTrackingTouch(SeekBar bar)  { }
273
  }
(9-9/11)