Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted 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
// Distorted 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 Distorted.  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.view.Gravity;
29
import android.view.LayoutInflater;
30
import android.view.View;
31
import android.widget.AdapterView;
32
import android.widget.ArrayAdapter;
33
import android.widget.LinearLayout;
34
import android.widget.SeekBar;
35
import android.widget.Spinner;
36
import android.widget.TextView;
37

    
38
import org.distorted.effect.BaseEffect;
39

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

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

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

    
48
  public RubikSettings()
49
    {
50
    mDurationText = new TextView[BaseEffect.Type.LENGTH];
51
    }
52

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

    
55
  @NonNull
56
  @Override
57
  public Dialog onCreateDialog(Bundle savedInstanceState)
58
    {
59
    FragmentActivity act = getActivity();
60
    AlertDialog.Builder builder = new AlertDialog.Builder(act);
61
    builder.setCancelable(false);
62

    
63
    LayoutInflater inflater = act.getLayoutInflater();
64
    final View view = inflater.inflate(R.layout.settings, null);
65
    builder.setView(view);
66

    
67
    LinearLayout linearLayout = view.findViewById(R.id.main_settings_layout);
68

    
69
    if( linearLayout!=null )
70
      {
71
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
72
        {
73
        createSettingsSection(act,linearLayout,i);
74
        }
75
      }
76
    else
77
      {
78
      android.util.Log.e("settings", "linearLayout NULL!");
79
      }
80

    
81
    return builder.create();
82
    }
83

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

    
86
  private void createSettingsSection(FragmentActivity act, LinearLayout layout, int index)
87
    {
88
    BaseEffect.Type beType = BaseEffect.Type.getType(index);
89
    float scale = act.getResources().getDisplayMetrics().density;
90

    
91
    ///// TEXT ///////////////////////////////////////////////////////////////////////////
92

    
93
    int layoutHeight = (int)(scale*48 + 0.5f);
94
    int padding      = (int)(scale*10 + 0.5f);
95

    
96
    LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,layoutHeight);
97

    
98
    TextView textView = new TextView(act);
99
    textView.setText(beType.getText());
100
    textView.setLayoutParams(textParams);
101
    textView.setGravity(Gravity.START|Gravity.CENTER);
102
    textView.setPadding(padding,0,padding,0);
103
    textView.setTextAppearance(android.R.style.TextAppearance_Medium);
104
    layout.addView(textView);
105

    
106
    ///// OUTER LAYOUT ///////////////////////////////////////////////////////////////////
107

    
108
    int margin = (int)(scale*10 + 0.5f);
109
    int color  = act.getResources().getColor(R.color.grey);
110
    LinearLayout outerLayout = new LinearLayout(act);
111
    LinearLayout.LayoutParams outerLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT, 0.5f);
112
    outerLayoutParams.bottomMargin = margin;
113
    outerLayoutParams.leftMargin   = margin;
114
    outerLayoutParams.rightMargin  = margin;
115

    
116
    outerLayout.setLayoutParams(outerLayoutParams);
117
    outerLayout.setGravity(Gravity.CENTER|Gravity.FILL_HORIZONTAL);
118
    outerLayout.setBackgroundColor(color);
119
    outerLayout.setOrientation(LinearLayout.VERTICAL);
120
    layout.addView(outerLayout);
121

    
122
    ///// INNER LAYOUT1 //////////////////////////////////////////////////////////////////
123

    
124
    int innerLayout1Height = (int)(scale*36 + 0.5f);
125
    LinearLayout innerLayout1 = new LinearLayout(act);
126
    LinearLayout.LayoutParams innerLayout1Params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,innerLayout1Height);
127

    
128
    innerLayout1.setLayoutParams(innerLayout1Params);
129
    innerLayout1.setGravity(Gravity.CENTER|Gravity.FILL_HORIZONTAL);
130
    innerLayout1.setOrientation(LinearLayout.HORIZONTAL);
131
    outerLayout.addView(innerLayout1);
132

    
133
    ///// STUFF INSIDE INNER LAYOUT1 /////////////////////////////////////////////////////
134

    
135
    int text1Padding = (int)(scale*5 + 0.5f);
136
    LinearLayout.LayoutParams text1LayoutParams = new LinearLayout.LayoutParams(0,layoutHeight,0.2f);
137

    
138
    TextView text1View = new TextView(act);
139
    text1View.setText(R.string.duration);
140
    text1View.setLayoutParams(text1LayoutParams);
141
    text1View.setGravity(Gravity.START|Gravity.CENTER);
142
    text1View.setPadding(text1Padding,0,text1Padding,0);
143
    text1View.setTextAppearance(android.R.style.TextAppearance_Small);
144
    innerLayout1.addView(text1View);
145
    //////////////////////////////////////////////////////////////////
146
    int text2Padding = (int)(scale*5 + 0.5f);
147
    LinearLayout.LayoutParams text2LayoutParams = new LinearLayout.LayoutParams(0,layoutHeight,0.2f);
148

    
149
    mDurationText[index] = new TextView(act);
150
    mDurationText[index].setLayoutParams(text2LayoutParams);
151
    mDurationText[index].setGravity(Gravity.END|Gravity.CENTER);
152
    mDurationText[index].setPadding(text2Padding,0,text2Padding,0);
153
    mDurationText[index].setTextAppearance(android.R.style.TextAppearance_Small);
154
    innerLayout1.addView(mDurationText[index]);
155
    //////////////////////////////////////////////////////////////////
156
    int seekPadding = (int)(scale*10 + 0.5f);
157
    LinearLayout.LayoutParams seekLayoutParams = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,0.6f);
158

    
159
    SeekBar seekBar = new SeekBar(act);
160
    seekBar.setLayoutParams(seekLayoutParams);
161
    seekBar.setPadding(seekPadding,0,seekPadding,0);
162
    seekBar.setId(index);
163
    innerLayout1.addView(seekBar);
164

    
165
    seekBar.setOnSeekBarChangeListener(this);
166
    seekBar.setProgress(beType.getCurrentPos());
167

    
168
    ///// INNER LAYOUT2 //////////////////////////////////////////////////////////////////
169

    
170
    int innerLayout2Height = (int)(scale*36 + 0.5f);
171
    LinearLayout innerLayout2 = new LinearLayout(act);
172
    LinearLayout.LayoutParams innerLayout2Params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,innerLayout2Height);
173

    
174
    innerLayout2.setLayoutParams(innerLayout2Params);
175
    innerLayout2.setGravity(Gravity.CENTER|Gravity.FILL_HORIZONTAL);
176
    innerLayout2.setOrientation(LinearLayout.HORIZONTAL);
177
    outerLayout.addView(innerLayout2);
178

    
179
    ///// STUFF INSIDE INNER LAYOUT2 /////////////////////////////////////////////////////
180

    
181
    int text3Padding = (int)(scale*5 + 0.5f);
182
    LinearLayout.LayoutParams text3LayoutParams = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,0.4f);
183

    
184
    TextView text3View = new TextView(act);
185
    text3View.setText(R.string.type);
186
    text3View.setLayoutParams(text3LayoutParams);
187
    text3View.setGravity(Gravity.START|Gravity.CENTER);
188
    text3View.setPadding(text3Padding,0,text3Padding,0);
189
    text3View.setTextAppearance(android.R.style.TextAppearance_Small);
190
    innerLayout2.addView(text3View);
191
    //////////////////////////////////////////////////////////////////
192
    int spinnerPadding = (int)(scale*10 + 0.5f);
193
    LinearLayout.LayoutParams spinnerLayoutParams = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,0.6f);
194

    
195
    Spinner spinner = new Spinner(act);
196
    spinner.setLayoutParams(spinnerLayoutParams);
197
    spinner.setPadding(spinnerPadding,0,spinnerPadding,0);
198
    spinner.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
199
    spinner.setId(index);
200
    innerLayout2.addView(spinner);
201

    
202
    spinner.setOnItemSelectedListener(this);
203
    String[] appear = BaseEffect.Type.getType(index).getNames();
204

    
205
    ArrayAdapter<String> adapterType = new ArrayAdapter<>(act,android.R.layout.simple_spinner_item, appear);
206
    adapterType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
207
    spinner.setAdapter(adapterType);
208
    spinner.setSelection(beType.getCurrentType());
209
    }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

    
213
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
214
    {
215
    int parentID = parent.getId();
216

    
217
    if( parentID>=0 && parentID< BaseEffect.Type.LENGTH) // ith spinner's ID is equal to i (see createSettingSection)
218
      {
219
      BaseEffect.Type.getType(parentID).setCurrentType(pos);
220
      }
221
    }
222

    
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224

    
225
  public void onProgressChanged(SeekBar bar, int progress, boolean fromUser)
226
    {
227
    int barID = bar.getId();
228

    
229
    if( barID>=0 && barID< BaseEffect.Type.LENGTH) // ith seekbar's ID is equal to i (see createSettingSection)
230
      {
231
      BaseEffect.Type.getType(barID).setCurrentPos(progress);
232
      int ms = BaseEffect.Type.translatePos(progress);
233
      mDurationText[barID].setText(getString(R.string.ms_placeholder,ms));
234
      }
235
    }
236

    
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238

    
239
  public void onNothingSelected(AdapterView<?> parent) { }
240
  public void onStartTrackingTouch(SeekBar bar) { }
241
  public void onStopTrackingTouch(SeekBar bar)  { }
242
  }
(5-5/6)