Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogFreePlaySettings.java @ adbd16d9

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.main.RubikActivity;
42
import org.distorted.screens.RubikScreenFreePlay;
43
import org.distorted.main.R;
44
import org.distorted.screens.ScreenList;
45

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

    
48
public class RubikDialogFreePlaySettings 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 = RubikScreenFreePlay.DEPTHS.length;
58
    String[] ret = new String[len];
59

    
60
    for(int i=0; i<len; i++)
61
      {
62
      ret[i] = String.valueOf(RubikScreenFreePlay.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
    builder.setCancelable(true);
159
    builder.setPositiveButton( R.string.ok, new DialogInterface.OnClickListener()
160
      {
161
      @Override
162
      public void onClick(DialogInterface dialog, int which)
163
        {
164
        RubikScreenFreePlay free = (RubikScreenFreePlay) ScreenList.FREE.getScreenClass();
165

    
166
        if( free!=null )
167
          {
168
          free.setScrambleDepth(mScraPos);
169
          free.setAnimationMode(mAnimPos);
170
          }
171
        }
172
      });
173

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

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

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

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

    
197
    return dialog;
198
    }
199
  }
(8-8/25)