Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikSettings.java @ 47ba5ddc

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
  private void createSettingsSection(FragmentActivity act, LinearLayout layout, int index)
49
    {
50
    BaseEffect.Type beType = BaseEffect.Type.getType(index);
51
    float scale = act.getResources().getDisplayMetrics().density;
52

    
53
    ///// OUTER LAYOUT ///////////////////////////////////////////////////////////////////
54

    
55
    int margin = (int)(scale*10 + 0.5f);
56
    int color  = act.getResources().getColor(R.color.grey);
57
    LinearLayout outerLayout = new LinearLayout(act);
58
    LinearLayout.LayoutParams outerLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT, 0.5f);
59
    outerLayoutParams.topMargin    = margin;
60
    outerLayoutParams.bottomMargin = margin;
61
    outerLayoutParams.leftMargin   = margin;
62
    outerLayoutParams.rightMargin  = margin;
63

    
64
    outerLayout.setLayoutParams(outerLayoutParams);
65
    outerLayout.setGravity(Gravity.CENTER|Gravity.FILL_HORIZONTAL);
66
    outerLayout.setBackgroundColor(color);
67
    outerLayout.setOrientation(LinearLayout.VERTICAL);
68
    layout.addView(outerLayout);
69

    
70
    ///// TEXT ///////////////////////////////////////////////////////////////////////////
71

    
72
    int layoutHeight = (int)(scale*32 + 0.5f);
73
    int padding      = (int)(scale*10 + 0.5f);
74

    
75
    LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,layoutHeight);
76

    
77
    TextView textView = new TextView(act);
78
    textView.setText(beType.getText());
79
    textView.setLayoutParams(textParams);
80
    textView.setGravity(Gravity.CENTER);
81
    textView.setPadding(padding,0,padding,0);
82
    textView.setTextAppearance(android.R.style.TextAppearance_Small);
83
    outerLayout.addView(textView);
84

    
85
    ///// INNER LAYOUT1 //////////////////////////////////////////////////////////////////
86

    
87
    int innerLayout1Height = (int)(scale*36 + 0.5f);
88
    LinearLayout innerLayout1 = new LinearLayout(act);
89
    LinearLayout.LayoutParams innerLayout1Params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,innerLayout1Height);
90

    
91
    innerLayout1.setLayoutParams(innerLayout1Params);
92
    innerLayout1.setGravity(Gravity.CENTER|Gravity.FILL_HORIZONTAL);
93
    innerLayout1.setOrientation(LinearLayout.HORIZONTAL);
94
    outerLayout.addView(innerLayout1);
95

    
96
    ///// STUFF INSIDE INNER LAYOUT1 /////////////////////////////////////////////////////
97

    
98
    int text1Padding = (int)(scale*5 + 0.5f);
99
    LinearLayout.LayoutParams text1LayoutParams = new LinearLayout.LayoutParams(0,layoutHeight,0.2f);
100

    
101
    TextView text1View = new TextView(act);
102
    text1View.setText(R.string.duration);
103
    text1View.setLayoutParams(text1LayoutParams);
104
    text1View.setGravity(Gravity.START|Gravity.CENTER);
105
    text1View.setPadding(text1Padding,0,text1Padding,0);
106
    text1View.setTextAppearance(android.R.style.TextAppearance_Small);
107
    innerLayout1.addView(text1View);
108
    //////////////////////////////////////////////////////////////////
109
    int text2Padding = (int)(scale*5 + 0.5f);
110
    LinearLayout.LayoutParams text2LayoutParams = new LinearLayout.LayoutParams(0,layoutHeight,0.2f);
111

    
112
    mDurationText[index] = new TextView(act);
113
    mDurationText[index].setLayoutParams(text2LayoutParams);
114
    mDurationText[index].setGravity(Gravity.END|Gravity.CENTER);
115
    mDurationText[index].setPadding(text2Padding,0,text2Padding,0);
116
    mDurationText[index].setTextAppearance(android.R.style.TextAppearance_Small);
117
    innerLayout1.addView(mDurationText[index]);
118
    //////////////////////////////////////////////////////////////////
119
    int seekPadding = (int)(scale*10 + 0.5f);
120
    LinearLayout.LayoutParams seekLayoutParams = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,0.6f);
121

    
122
    SeekBar seekBar = new SeekBar(act);
123
    seekBar.setLayoutParams(seekLayoutParams);
124
    seekBar.setPadding(seekPadding,0,seekPadding,0);
125
    seekBar.setId(index);
126
    innerLayout1.addView(seekBar);
127

    
128
    seekBar.setOnSeekBarChangeListener(this);
129
    seekBar.setProgress(beType.getCurrentPos());
130

    
131
    ///// INNER LAYOUT2 //////////////////////////////////////////////////////////////////
132

    
133
    int innerLayout2Height = (int)(scale*36 + 0.5f);
134
    LinearLayout innerLayout2 = new LinearLayout(act);
135
    LinearLayout.LayoutParams innerLayout2Params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,innerLayout2Height);
136

    
137
    innerLayout2.setLayoutParams(innerLayout2Params);
138
    innerLayout2.setGravity(Gravity.CENTER|Gravity.FILL_HORIZONTAL);
139
    innerLayout2.setOrientation(LinearLayout.HORIZONTAL);
140
    outerLayout.addView(innerLayout2);
141

    
142
    ///// STUFF INSIDE INNER LAYOUT2 /////////////////////////////////////////////////////
143

    
144
    int text3Padding = (int)(scale*5 + 0.5f);
145
    LinearLayout.LayoutParams text3LayoutParams = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,0.4f);
146

    
147
    TextView text3View = new TextView(act);
148
    text3View.setText(R.string.type);
149
    text3View.setLayoutParams(text3LayoutParams);
150
    text3View.setGravity(Gravity.START|Gravity.CENTER);
151
    text3View.setPadding(text3Padding,0,text3Padding,0);
152
    text3View.setTextAppearance(android.R.style.TextAppearance_Small);
153
    innerLayout2.addView(text3View);
154
    //////////////////////////////////////////////////////////////////
155
    int spinnerPadding = (int)(scale*10 + 0.5f);
156
    LinearLayout.LayoutParams spinnerLayoutParams = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,0.6f);
157

    
158
    Spinner spinner = new Spinner(act);
159
    spinner.setLayoutParams(spinnerLayoutParams);
160
    spinner.setPadding(spinnerPadding,0,spinnerPadding,0);
161
    spinner.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
162
    spinner.setId(index);
163
    innerLayout2.addView(spinner);
164

    
165
    spinner.setOnItemSelectedListener(this);
166
    String[] appear = BaseEffect.Type.getType(index).getNames();
167

    
168
    ArrayAdapter<String> adapterType = new ArrayAdapter<>(act,android.R.layout.simple_spinner_item, appear);
169
    adapterType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
170
    spinner.setAdapter(adapterType);
171
    spinner.setSelection(beType.getCurrentType());
172
    }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175
// PUBLIC API
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177

    
178
  public RubikSettings()
179
    {
180
    mDurationText = new TextView[BaseEffect.Type.LENGTH];
181
    }
182

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184

    
185
  @NonNull
186
  @Override
187
  public Dialog onCreateDialog(Bundle savedInstanceState)
188
    {
189
    FragmentActivity act = getActivity();
190
    AlertDialog.Builder builder = new AlertDialog.Builder(act);
191
    builder.setCancelable(false);
192

    
193
    LayoutInflater inflater = act.getLayoutInflater();
194
    final View view = inflater.inflate(R.layout.settings, null);
195
    builder.setView(view);
196

    
197
    LinearLayout linearLayout = view.findViewById(R.id.main_settings_layout);
198

    
199
    if( linearLayout!=null )
200
      {
201
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
202
        {
203
        createSettingsSection(act,linearLayout,i);
204
        }
205
      }
206
    else
207
      {
208
      android.util.Log.e("settings", "linearLayout NULL!");
209
      }
210

    
211
    return builder.create();
212
    }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

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

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

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227

    
228
  public void onProgressChanged(SeekBar bar, int progress, boolean fromUser)
229
    {
230
    int barID = bar.getId();
231

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

    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241

    
242
  public void onNothingSelected(AdapterView<?> parent) { }
243
  public void onStartTrackingTouch(SeekBar bar) { }
244
  public void onStopTrackingTouch(SeekBar bar)  { }
245
  }
(5-5/6)