Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogBandagedSettings.java @ 05f6d7bd

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

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

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

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

    
64
    return ret;
65
    }
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

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

    
76
    mScraPos = scraPos;
77
    mAnimPos = animPos;
78

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

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

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

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

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

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

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

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116

    
117
  @Override
118
  public void onNothingSelected(AdapterView<?> parent) { }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

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

    
130
    DisplayMetrics displaymetrics = new DisplayMetrics();
131
    act.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
132
    final float titleSize= displaymetrics.widthPixels * RubikActivity.MENU_BIG_TEXT_SIZE;
133
    final float okSize   = displaymetrics.widthPixels * RubikActivity.DIALOG_BUTTON_SIZE;
134
    final float textSize = displaymetrics.widthPixels * RubikActivity.MENU_BIG_TEXT_SIZE;
135

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

    
141
    Bundle args = getArguments();
142
    int scraPos, animPos;
143

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

    
155
    final BandagedPlayActivity bact = (BandagedPlayActivity)getContext();
156

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

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

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

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

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

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

    
196
    return dialog;
197
    }
198
  }
(5-5/25)