Project

General

Profile

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

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

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.ObjectList;
44
import org.distorted.network.RubikScores;
45
import org.distorted.screens.ScreenList;
46
import org.distorted.screens.RubikScreenPlay;
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.DIALOG_BUTTON_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
          name = name.replace(' ', '_');
145

    
146
          RubikActivity act = (RubikActivity)getActivity();
147
          ScreenList.switchState(act, ScreenList.PLAY);
148
          RubikScores.getInstance().setName(name);
149
          RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getStateClass();
150

    
151
          int object = play.getObject();
152
          int size   = play.getSize();
153
          int sizeIndex = ObjectList.getSizeIndex(object,size);
154

    
155
          Bundle bundle = new Bundle();
156
          bundle.putInt("tab", ObjectList.pack(object,sizeIndex) );
157
          bundle.putBoolean("submitting", true);
158

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

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

    
170
    mEdit.requestFocus();
171

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

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

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

    
187
    dialog.setCanceledOnTouchOutside(false);
188
    Window window = dialog.getWindow();
189

    
190
    if( window!=null )
191
      {
192
      window.getDecorView().setSystemUiVisibility(RubikActivity.FLAGS2);
193
      }
194

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

    
205
    return dialog;
206
    }
207
  }
(13-13/18)