Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogSetName.java @ 441f2db5

1 f895e77a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6 68191e7d Leszek Koltunski
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8 f895e77a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
9
10 1f9772f3 Leszek Koltunski
package org.distorted.dialogs;
11 f895e77a Leszek Koltunski
12
import android.app.Dialog;
13
import android.os.Bundle;
14 66e777b0 Leszek Koltunski
import androidx.fragment.app.FragmentActivity;
15
import androidx.appcompat.app.AlertDialog;
16 1bdf3dc2 Leszek Koltunski
import android.text.Editable;
17
import android.text.TextWatcher;
18 fb37b424 Leszek Koltunski
import android.util.TypedValue;
19 f895e77a Leszek Koltunski
import android.view.View;
20
import android.view.WindowManager;
21 1bdf3dc2 Leszek Koltunski
import android.widget.Button;
22 f895e77a Leszek Koltunski
import android.widget.EditText;
23
import android.widget.TextView;
24
25 1f9772f3 Leszek Koltunski
import org.distorted.main.R;
26
import org.distorted.main.RubikActivity;
27 acabdd83 Leszek Koltunski
import org.distorted.external.RubikScores;
28 fcd5b990 Leszek Koltunski
import org.distorted.screens.ScreenList;
29 f895e77a Leszek Koltunski
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31
32 c02fa107 Leszek Koltunski
public class RubikDialogSetName extends RubikDialogAbstract
33 f895e77a Leszek Koltunski
  {
34 f4ee4d70 Leszek Koltunski
  private static final int MAX_NAME_LEN = 15;
35 90fd47b0 Leszek Koltunski
  private EditText mEdit;
36
37 2d942f35 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
38
39
  @Override
40
  public void onStart()
41
    {
42
    super.onStart();
43
44
    AlertDialog dialog = (AlertDialog)getDialog();
45
46
    if( dialog!=null )
47
      {
48
      Button positiveButton = dialog.getButton(Dialog.BUTTON_POSITIVE);
49
50
      if( positiveButton!=null )
51
        {
52
        String editName = mEdit.getText().toString();
53
        positiveButton.setEnabled(editName.length()>0);
54
        }
55
      }
56
    }
57
58 f895e77a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
59
60 f8a21f6b Leszek Koltunski
  public int getResource()      { return R.layout.dialog_set_name; }
61
  public int getTitleResource() { return mArgument.length()==0 ? R.string.choose_name : R.string.name_taken; }
62
  public boolean hasArgument()  { return true; }
63
  public int getPositive()      { return R.string.ok; }
64
  public int getNegative()      { return -1; }
65 f895e77a Leszek Koltunski
66 c02fa107 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
67 f895e77a Leszek Koltunski
68 f8a21f6b Leszek Koltunski
  public void positiveAction()
69 c02fa107 Leszek Koltunski
    {
70 f8a21f6b Leszek Koltunski
    String name = mEdit.getText().toString();
71
    int len = name.length();
72 f895e77a Leszek Koltunski
73 f8a21f6b Leszek Koltunski
    if( len>0 )
74 f895e77a Leszek Koltunski
      {
75 f8a21f6b Leszek Koltunski
      if( len>MAX_NAME_LEN )
76 f895e77a Leszek Koltunski
        {
77 f8a21f6b Leszek Koltunski
        name = name.substring(0,MAX_NAME_LEN);
78 f895e77a Leszek Koltunski
        }
79 c02fa107 Leszek Koltunski
80 f8a21f6b Leszek Koltunski
      name = name.replace(' ', '_');
81 c02fa107 Leszek Koltunski
82 f8a21f6b Leszek Koltunski
      try
83
        {
84
        RubikActivity act = (RubikActivity)getActivity();
85
        ScreenList.switchScreen(act, ScreenList.PLAY);
86
        RubikScores.getInstance().setName(name);
87
88
        Bundle bundle = new Bundle();
89
        bundle.putString("argument", "true");
90
        RubikDialogScores scores = new RubikDialogScores();
91
        scores.setArguments(bundle);
92
        scores.show(act.getSupportFragmentManager(), null);
93
        }
94
      catch(IllegalStateException ex)
95
        {
96
        android.util.Log.e("D", "IllegalStateException trying to display SetName");
97
        }
98
      }
99 c02fa107 Leszek Koltunski
    }
100
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
103 f8a21f6b Leszek Koltunski
  public void negativeAction()
104 c02fa107 Leszek Koltunski
    {
105 f8a21f6b Leszek Koltunski
106 c02fa107 Leszek Koltunski
    }
107
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109
110
  public void prepareBody(Dialog dialog, View view, FragmentActivity act, float size)
111
    {
112
    TextView text = view.findViewById(R.id.set_name_message);
113
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
114
    mEdit = view.findViewById(R.id.set_name);
115
    mEdit.setHeight( (int)(2*mTitleSize) );
116
    mEdit.setTextSize(TypedValue.COMPLEX_UNIT_PX, 1.5f*mTitleSize);
117
118
    if( mArgument.length()==0 )
119
      {
120
      text.setText(R.string.new_name);
121
      }
122
    else
123
      {
124
      text.setText(act.getString(R.string.new_name_try_again, mArgument));
125
      }
126 f895e77a Leszek Koltunski
127 db266f25 Leszek Koltunski
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
128 f895e77a Leszek Koltunski
129 90fd47b0 Leszek Koltunski
    mEdit.requestFocus();
130 1bdf3dc2 Leszek Koltunski
131 90fd47b0 Leszek Koltunski
    mEdit.addTextChangedListener(new TextWatcher()
132 1bdf3dc2 Leszek Koltunski
      {
133
      @Override
134
      public void afterTextChanged(Editable s) {}
135
136
      @Override
137
      public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
138
139
      @Override
140
      public void onTextChanged(CharSequence s, int start, int before, int count)
141
        {
142
        ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(count>0);
143
        }
144
      });
145 f895e77a Leszek Koltunski
    }
146
  }