Project

General

Profile

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

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

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.v7.app.AlertDialog;
27
import android.support.v7.app.AppCompatDialogFragment;
28
import android.util.DisplayMetrics;
29
import android.view.Gravity;
30
import android.view.LayoutInflater;
31
import android.view.View;
32
import android.widget.AdapterView;
33
import android.widget.ArrayAdapter;
34
import android.widget.LinearLayout;
35
import android.widget.SeekBar;
36
import android.widget.Spinner;
37
import android.widget.TextView;
38

    
39
import org.distorted.effect.BaseEffect;
40

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

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

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

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

    
55
    int textH;
56
    int layoH;
57

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

    
69
    ///// OUTER LAYOUT ///////////////////////////////////////////////////////////////////
70

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

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

    
86
    ///// TEXT ///////////////////////////////////////////////////////////////////////////
87

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

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

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

    
101
    ///// INNER LAYOUT1 //////////////////////////////////////////////////////////////////
102

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

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

    
112
    ///// STUFF INSIDE INNER LAYOUT1 /////////////////////////////////////////////////////
113

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

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

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

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

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

    
147
    ///// INNER LAYOUT2 //////////////////////////////////////////////////////////////////
148

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

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

    
158
    ///// STUFF INSIDE INNER LAYOUT2 /////////////////////////////////////////////////////
159

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

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

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

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

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

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191
// PUBLIC API
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193

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

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200

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

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

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

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

    
227
    return builder.create();
228
    }
229

    
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231

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

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

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

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

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

    
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258

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