Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogSetName.java @ 7ac0ee88

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.objectlib.main.ObjectList;
42

    
43
import org.distorted.main.R;
44
import org.distorted.main.RubikActivity;
45
import org.distorted.network.RubikScores;
46
import org.distorted.screens.ScreenList;
47
import org.distorted.screens.RubikScreenPlay;
48

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

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

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

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

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

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

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

    
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78

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

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

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

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

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

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

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

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

    
128
    builder.setCancelable(true);
129

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

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

    
145
          name = name.replace(' ', '_');
146

    
147
          RubikActivity act = (RubikActivity)getActivity();
148
          ScreenList.switchScreen(act, ScreenList.PLAY);
149
          RubikScores.getInstance().setName(name);
150
          RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
151

    
152
          Bundle bundle = new Bundle();
153
          bundle.putInt("tab", play.getObject() );
154
          bundle.putBoolean("submitting", true);
155

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

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

    
167
    mEdit.requestFocus();
168

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

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

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

    
184
    dialog.setCanceledOnTouchOutside(false);
185
    Window window = dialog.getWindow();
186

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

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

    
202
    return dialog;
203
    }
204
  }
(14-14/19)