Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogBandagedSettings.java @ 39176a1f

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.dialogs;
21

    
22
import android.app.Dialog;
23
import android.content.DialogInterface;
24
import android.os.Bundle;
25
import android.util.DisplayMetrics;
26
import android.util.TypedValue;
27
import android.view.LayoutInflater;
28
import android.view.View;
29
import android.view.Window;
30
import android.widget.AdapterView;
31
import android.widget.ArrayAdapter;
32
import android.widget.Button;
33
import android.widget.Spinner;
34
import android.widget.TextView;
35

    
36
import androidx.annotation.NonNull;
37
import androidx.appcompat.app.AlertDialog;
38
import androidx.appcompat.app.AppCompatDialogFragment;
39
import androidx.fragment.app.FragmentActivity;
40

    
41
import org.distorted.bandaged.BandagedPlayActivity;
42
import org.distorted.bandaged.BandagedPlayScreen;
43
import org.distorted.main.R;
44
import org.distorted.main.RubikActivity;
45

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

    
48
public class RubikDialogBandagedSettings extends AppCompatDialogFragment implements AdapterView.OnItemSelectedListener
49
  {
50
  private int mAnimPos, mScraPos;
51
  private float mTextSize;
52

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

    
55
  private String[] createScrambleModes()
56
    {
57
    int len = BandagedPlayScreen.DEPTHS.length;
58
    String[] ret = new String[len];
59

    
60
    for(int i=0; i<len; i++)
61
      {
62
      ret[i] = String.valueOf(BandagedPlayScreen.DEPTHS[i]);
63
      }
64

    
65
    return ret;
66
    }
67

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

    
70
  private void configureView(FragmentActivity act, View view, float textSize, int scraPos, int animPos)
71
    {
72
    TextView text1 = view.findViewById(R.id.bandaged_text_depth);
73
    text1.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
74
    TextView text2 = view.findViewById(R.id.bandaged_text_animation);
75
    text2.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
76

    
77
    mScraPos = scraPos;
78
    mAnimPos = animPos;
79

    
80
    Spinner scramble = view.findViewById(R.id.bandaged_spinner_scramble);
81
    scramble.setOnItemSelectedListener(this);
82
    String[] scrambleModes = createScrambleModes();
83

    
84
    ArrayAdapter<String> scrambleAdapter = new ArrayAdapter<>(act, R.layout.spinner_item, scrambleModes );
85
    scrambleAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
86
    scramble.setAdapter(scrambleAdapter);
87
    scramble.setSelection(mScraPos);
88

    
89
    Spinner animation = view.findViewById(R.id.bandaged_spinner_animation);
90
    animation.setOnItemSelectedListener(this);
91
    String[] animationModes = { "ON" , "OFF" };
92

    
93
    ArrayAdapter<String> animationAdapter = new ArrayAdapter<>(act, R.layout.spinner_item, animationModes );
94
    animationAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
95
    animation.setAdapter(animationAdapter);
96
    animation.setSelection(mAnimPos);
97
    }
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

    
101
  @Override
102
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
103
    {
104
    int spinnerID = parent.getId();
105

    
106
    ((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_PX,mTextSize);
107

    
108
    if( spinnerID == R.id.bandaged_spinner_scramble )
109
      {
110
      mScraPos = pos;
111
      }
112
    else if( spinnerID == R.id.bandaged_spinner_animation )
113
      {
114
      mAnimPos = pos;
115
      }
116
    }
117

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

    
120
  @Override
121
  public void onNothingSelected(AdapterView<?> parent) { }
122

    
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124

    
125
  @NonNull
126
  @Override
127
  public Dialog onCreateDialog(Bundle savedInstanceState)
128
    {
129
    FragmentActivity act = getActivity();
130
    LayoutInflater inflater = act.getLayoutInflater();
131
    AlertDialog.Builder builder = new AlertDialog.Builder(act);
132

    
133
    DisplayMetrics displaymetrics = new DisplayMetrics();
134
    act.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
135
    final float titleSize= displaymetrics.widthPixels * RubikActivity.MENU_BIG_TEXT_SIZE;
136
    final float okSize   = displaymetrics.widthPixels * RubikActivity.DIALOG_BUTTON_SIZE;
137
    mTextSize = displaymetrics.widthPixels * RubikActivity.MENU_BIG_TEXT_SIZE;
138

    
139
    TextView tv = (TextView) inflater.inflate(R.layout.dialog_title, null);
140
    tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
141
    tv.setText(R.string.settings);
142
    builder.setCustomTitle(tv);
143

    
144
    Bundle args = getArguments();
145
    int scraPos, animPos;
146

    
147
    try
148
      {
149
      scraPos = args.getInt("scraPos");
150
      animPos = args.getInt("animPos");
151
      }
152
    catch(Exception e)
153
      {
154
      scraPos = 0;
155
      animPos = 0;
156
      }
157

    
158
    final BandagedPlayActivity bact = (BandagedPlayActivity)getContext();
159

    
160
    builder.setCancelable(true);
161
    builder.setPositiveButton( R.string.ok, new DialogInterface.OnClickListener()
162
      {
163
      @Override
164
      public void onClick(DialogInterface dialog, int which)
165
        {
166
        BandagedPlayScreen screen = bact!=null ? bact.getScreen() : null;
167

    
168
        if( screen!=null )
169
          {
170
          screen.setScrambleDepth(mScraPos);
171
          screen.setAnimationMode(mAnimPos);
172
          }
173
        }
174
      });
175

    
176
    final View view = inflater.inflate(R.layout.dialog_settings, null);
177
    configureView(act,view,mTextSize,scraPos,animPos);
178
    builder.setView(view);
179

    
180
    Dialog dialog = builder.create();
181
    dialog.setCanceledOnTouchOutside(false);
182
    Window window = dialog.getWindow();
183

    
184
    if( window!=null )
185
      {
186
      window.getDecorView().setSystemUiVisibility(RubikActivity.FLAGS);
187
      }
188

    
189
    dialog.setOnShowListener(new DialogInterface.OnShowListener()
190
      {
191
      @Override
192
      public void onShow(DialogInterface dialog)
193
        {
194
        Button btnPositive = ((AlertDialog)dialog).getButton(Dialog.BUTTON_POSITIVE);
195
        btnPositive.setTextSize(TypedValue.COMPLEX_UNIT_PX, okSize);
196
        }
197
      });
198

    
199
    return dialog;
200
    }
201
  }
(5-5/24)