Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikSettings.java @ b8b38548

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.os.Bundle;
24
import android.support.annotation.NonNull;
25
import android.support.v4.app.FragmentActivity;
26
import android.support.v4.content.ContextCompat;
27
import android.support.v7.app.AlertDialog;
28
import android.support.v7.app.AppCompatDialogFragment;
29
import android.util.DisplayMetrics;
30
import android.view.Gravity;
31
import android.view.LayoutInflater;
32
import android.view.View;
33
import android.widget.AdapterView;
34
import android.widget.ArrayAdapter;
35
import android.widget.LinearLayout;
36
import android.widget.SeekBar;
37
import android.widget.Spinner;
38
import android.widget.TextView;
39

    
40
import org.distorted.effect.BaseEffect;
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

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

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

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

    
56
    int textH;
57
    int layoH;
58

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

    
70
    ///// OUTER LAYOUT ///////////////////////////////////////////////////////////////////
71

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

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

    
87
    ///// TEXT ///////////////////////////////////////////////////////////////////////////
88

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

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

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

    
102
    ///// INNER LAYOUT1 //////////////////////////////////////////////////////////////////
103

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

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

    
113
    ///// STUFF INSIDE INNER LAYOUT1 /////////////////////////////////////////////////////
114

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

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

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

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

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

    
148
    ///// INNER LAYOUT2 //////////////////////////////////////////////////////////////////
149

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

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

    
159
    ///// STUFF INSIDE INNER LAYOUT2 /////////////////////////////////////////////////////
160

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

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

    
175
    Spinner spinner = new Spinner(act);
176
    spinner.setLayoutParams(spinnerLayoutParams);
177
    spinner.setPadding(spinnerPadding,0,spinnerPadding,0);
178
    spinner.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
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 RubikSettings()
196
    {
197
    mDurationText = new TextView[BaseEffect.Type.LENGTH];
198
    }
199

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

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

    
210
    LayoutInflater inflater = act.getLayoutInflater();
211
    final View view = inflater.inflate(R.layout.settings, null);
212
    builder.setView(view);
213

    
214
    LinearLayout linearLayout = view.findViewById(R.id.main_settings_layout);
215

    
216
    if( linearLayout!=null )
217
      {
218
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
219
        {
220
        createSettingsSection(act,linearLayout,i);
221
        }
222
      }
223
    else
224
      {
225
      android.util.Log.e("settings", "linearLayout NULL!");
226
      }
227

    
228
    return builder.create();
229
    }
230

    
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232

    
233
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
234
    {
235
    int parentID = parent.getId();
236
    int len = BaseEffect.Type.LENGTH;
237

    
238
    if( parentID>=len && parentID< 2*len) // ith spinner's ID is equal to i+LENGTH (see createSettingSection)
239
      {
240
      BaseEffect.Type.getType(parentID-len).setCurrentType(pos);
241
      }
242
    }
243

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

    
246
  public void onProgressChanged(SeekBar bar, int progress, boolean fromUser)
247
    {
248
    int barID = bar.getId();
249

    
250
    if( barID>=0 && barID< BaseEffect.Type.LENGTH) // ith seekbar's ID is equal to i (see createSettingSection)
251
      {
252
      BaseEffect.Type.getType(barID).setCurrentPos(progress);
253
      int ms = BaseEffect.Type.translatePos(progress);
254
      mDurationText[barID].setText(getString(R.string.ms_placeholder,ms));
255
      }
256
    }
257

    
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259

    
260
  public void onNothingSelected(AdapterView<?> parent) { }
261
  public void onStartTrackingTouch(SeekBar bar) { }
262
  public void onStopTrackingTouch(SeekBar bar)  { }
263
  }
(9-9/11)