Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogBandagedSettings.java @ 068f7c2f

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,true);
88
    View v1 = scramble.getSelectedView();
89
    ((TextView)v1).setTextSize(TypedValue.COMPLEX_UNIT_PX,mTextSize);
90

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

    
95
    ArrayAdapter<String> animationAdapter = new ArrayAdapter<>(act, R.layout.spinner_item, animationModes );
96
    animationAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
97
    animation.setAdapter(animationAdapter);
98
    animation.setSelection(mAnimPos,true);
99
    View v2 = animation.getSelectedView();
100
    ((TextView)v2).setTextSize(TypedValue.COMPLEX_UNIT_PX,mTextSize);
101
    }
102

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104

    
105
  @Override
106
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
107
    {
108
    int spinnerID = parent.getId();
109

    
110
    ((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_PX,mTextSize);
111

    
112
    if( spinnerID == R.id.bandaged_spinner_scramble )
113
      {
114
      mScraPos = pos;
115
      }
116
    else if( spinnerID == R.id.bandaged_spinner_animation )
117
      {
118
      mAnimPos = pos;
119
      }
120
    }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

    
124
  @Override
125
  public void onNothingSelected(AdapterView<?> parent) { }
126

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

    
129
  @NonNull
130
  @Override
131
  public Dialog onCreateDialog(Bundle savedInstanceState)
132
    {
133
    FragmentActivity act = getActivity();
134
    LayoutInflater inflater = act.getLayoutInflater();
135
    AlertDialog.Builder builder = new AlertDialog.Builder(act);
136

    
137
    DisplayMetrics displaymetrics = new DisplayMetrics();
138
    act.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
139
    final float titleSize= displaymetrics.widthPixels * RubikActivity.MENU_BIG_TEXT_SIZE;
140
    final float okSize   = displaymetrics.widthPixels * RubikActivity.DIALOG_BUTTON_SIZE;
141
    mTextSize = displaymetrics.widthPixels * RubikActivity.MENU_BIG_TEXT_SIZE;
142

    
143
    TextView tv = (TextView) inflater.inflate(R.layout.dialog_title, null);
144
    tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
145
    tv.setText(R.string.settings);
146
    builder.setCustomTitle(tv);
147

    
148
    Bundle args = getArguments();
149
    int scraPos, animPos;
150

    
151
    try
152
      {
153
      scraPos = args.getInt("scraPos");
154
      animPos = args.getInt("animPos");
155
      }
156
    catch(Exception e)
157
      {
158
      scraPos = 0;
159
      animPos = 0;
160
      }
161

    
162
    final BandagedPlayActivity bact = (BandagedPlayActivity)getContext();
163

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

    
172
        if( screen!=null )
173
          {
174
          screen.setScrambleDepth(mScraPos);
175
          screen.setAnimationMode(mAnimPos);
176
          }
177
        }
178
      });
179

    
180
    final View view = inflater.inflate(R.layout.dialog_settings, null);
181
    configureView(act,view,mTextSize,scraPos,animPos);
182
    builder.setView(view);
183

    
184
    Dialog dialog = builder.create();
185
    dialog.setCanceledOnTouchOutside(false);
186
    Window window = dialog.getWindow();
187

    
188
    if( window!=null )
189
      {
190
      window.getDecorView().setSystemUiVisibility(RubikActivity.FLAGS);
191
      }
192

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

    
203
    return dialog;
204
    }
205
  }
(5-5/25)