Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogSetName.java @ b1e9596b

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 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 androidx.annotation.NonNull;
26
import androidx.fragment.app.FragmentActivity;
27
import androidx.appcompat.app.AlertDialog;
28
import androidx.appcompat.app.AppCompatDialogFragment;
29
import android.text.Editable;
30
import android.text.TextWatcher;
31
import android.util.DisplayMetrics;
32
import android.util.TypedValue;
33
import android.view.LayoutInflater;
34
import android.view.View;
35
import android.view.Window;
36
import android.view.WindowManager;
37
import android.widget.Button;
38
import android.widget.EditText;
39
import android.widget.TextView;
40

    
41
import org.distorted.main.R;
42
import org.distorted.main.RubikActivity;
43
import org.distorted.objects.RubikObjectList;
44
import org.distorted.scores.RubikScores;
45
import org.distorted.states.RubikState;
46
import org.distorted.states.RubikStatePlay;
47

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

    
50
public class RubikDialogSetName extends AppCompatDialogFragment
51
  {
52
  private static final int MAX_NAME_LEN = 15;
53
  private EditText mEdit;
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

    
57
  @Override
58
  public void onStart()
59
    {
60
    super.onStart();
61

    
62
    AlertDialog dialog = (AlertDialog)getDialog();
63

    
64
    if( dialog!=null )
65
      {
66
      Button positiveButton = dialog.getButton(Dialog.BUTTON_POSITIVE);
67

    
68
      if( positiveButton!=null )
69
        {
70
        String editName = mEdit.getText().toString();
71
        positiveButton.setEnabled(editName.length()>0);
72
        }
73
      }
74
    }
75

    
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77

    
78
  @NonNull
79
  @Override
80
  public Dialog onCreateDialog(Bundle savedInstanceState)
81
    {
82
    FragmentActivity act = getActivity();
83
    LayoutInflater inflater = act.getLayoutInflater();
84
    AlertDialog.Builder builder = new AlertDialog.Builder(act);
85

    
86
    DisplayMetrics displaymetrics = new DisplayMetrics();
87
    act.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
88
    final float titleSize= displaymetrics.widthPixels * RubikActivity.MENU_BIG_TEXT_SIZE;
89
    final float okSize   = displaymetrics.widthPixels * RubikActivity.MENU_MEDIUM_TEXT_SIZE;
90
    final float textSize = displaymetrics.widthPixels * RubikActivity.MENU_SMALL_TEXT_SIZE;
91

    
92
    Bundle args = getArguments();
93
    String name;
94

    
95
    try
96
      {
97
      name = args.getString("name");
98
      }
99
    catch(Exception e)
100
      {
101
      name = "";
102
      }
103

    
104
    boolean first = name.length()==0;
105

    
106
    TextView tv = (TextView) inflater.inflate(R.layout.dialog_title, null);
107
    tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
108
    tv.setText( first ? R.string.choose_name : R.string.name_taken);
109
    builder.setCustomTitle(tv);
110

    
111
    final View view = inflater.inflate(R.layout.dialog_set_name, null);
112
    TextView text = view.findViewById(R.id.set_name_message);
113
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
114
    mEdit = view.findViewById(R.id.set_name);
115
    mEdit.setHeight( (int)(2*titleSize) );
116
    mEdit.setTextSize(TypedValue.COMPLEX_UNIT_PX, 1.5f*titleSize);
117

    
118
    if( first )
119
      {
120
      text.setText(R.string.new_name);
121
      }
122
    else
123
      {
124
      text.setText(act.getString(R.string.new_name_try_again, name));
125
      }
126

    
127
    builder.setCancelable(true);
128

    
129
    builder.setPositiveButton( R.string.ok, new DialogInterface.OnClickListener()
130
      {
131
      @Override
132
      public void onClick(DialogInterface dialog, int which)
133
        {
134
        String name = mEdit.getText().toString();
135
        int len = name.length();
136

    
137
        if( len>0 )
138
          {
139
          if( len>MAX_NAME_LEN )
140
            {
141
            name = name.substring(0,MAX_NAME_LEN);
142
            }
143

    
144
          RubikActivity act = (RubikActivity)getActivity();
145
          RubikState.switchState(act,RubikState.PLAY);
146
          RubikScores.getInstance().setName(name);
147
          RubikStatePlay play = (RubikStatePlay) RubikState.PLAY.getStateClass();
148

    
149
          int object = play.getObject();
150
          int size   = play.getSize();
151
          int sizeIndex = RubikObjectList.getSizeIndex(object,size);
152

    
153
          Bundle bundle = new Bundle();
154
          bundle.putInt("tab", RubikObjectList.pack(object,sizeIndex) );
155
          bundle.putBoolean("submitting", true);
156

    
157
          RubikDialogScores scores = new RubikDialogScores();
158
          scores.setArguments(bundle);
159
          scores.show(act.getSupportFragmentManager(), null);
160
          }
161
        }
162
      });
163

    
164
    builder.setView(view);
165
    final Dialog dialog = builder.create();
166
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
167

    
168
    mEdit.requestFocus();
169

    
170
    mEdit.addTextChangedListener(new TextWatcher()
171
      {
172
      @Override
173
      public void afterTextChanged(Editable s) {}
174

    
175
      @Override
176
      public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
177

    
178
      @Override
179
      public void onTextChanged(CharSequence s, int start, int before, int count)
180
        {
181
        ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(count>0);
182
        }
183
      });
184

    
185
    dialog.setCanceledOnTouchOutside(false);
186

    
187
    Window window = dialog.getWindow();
188

    
189
    if( window!=null )
190
      {
191
      window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
192
      }
193

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

    
204
    return dialog;
205
    }
206
  }
(13-13/15)