project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/util/TextInputDialog.java
author Wuzzy <almikes@aol.com>
Wed, 13 Apr 2016 12:17:30 +0200
changeset 11765 10860d4bca22
parent 10017 de822cd3df3a
permissions -rw-r--r--
Add sound effects for: cleaver impact, air mine impact, using extra time
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     1
/*
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     2
 * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     3
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     4
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     5
 * This program is free software; you can redistribute it and/or
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     6
 * modify it under the terms of the GNU General Public License
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     7
 * as published by the Free Software Foundation; either version 2
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     8
 * of the License, or (at your option) any later version.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     9
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    13
 * GNU General Public License for more details.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    14
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    15
 * You should have received a copy of the GNU General Public License
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    16
 * along with this program; if not, write to the Free Software
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    18
 */
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    19
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7461
diff changeset
    20
package org.hedgewars.hedgeroid.util;
7461
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
diff changeset
    22
import android.app.Activity;
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
diff changeset
    23
import android.app.AlertDialog;
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
import android.app.Dialog;
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
diff changeset
    25
import android.content.DialogInterface;
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
diff changeset
    26
import android.os.Bundle;
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
import android.support.v4.app.DialogFragment;
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
import android.view.KeyEvent;
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
import android.view.inputmethod.EditorInfo;
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
import android.widget.EditText;
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
import android.widget.TextView;
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
import android.widget.TextView.OnEditorActionListener;
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
/**
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
diff changeset
    35
 * A generic text input dialog with configurable text. The Activity must implement the callback
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
diff changeset
    36
 * interface TextInputDialogListener, which will be called by the dialog if it is submitted or cancelled.
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
diff changeset
    37
 */
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
diff changeset
    38
public class TextInputDialog extends DialogFragment {
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    39
    private static final String BUNDLE_DIALOG_ID = "dialogId";
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    40
    private static final String BUNDLE_TITLE_TEXT = "title";
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    41
    private static final String BUNDLE_MESSAGE_TEXT = "message";
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    42
    private static final String BUNDLE_HINT_TEXT = "hint";
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    43
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    44
    private int dialogId, titleText, messageText, hintText;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    45
    private TextInputDialogListener listener;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    46
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    47
    public interface TextInputDialogListener {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    48
        void onTextInputDialogSubmitted(int dialogId, String text);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    49
        void onTextInputDialogCancelled(int dialogId);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    50
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    51
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    52
    /**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    53
     * The dialogId is only used for passing back to the callback on the activity, the
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    54
     * other parameters are text resource IDs. Pass 0 for any of them to not use this
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    55
     * text.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    56
     */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    57
    public TextInputDialog(int dialogId, int titleText, int messageText, int hintText) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    58
        this.dialogId = dialogId;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    59
        this.titleText = titleText;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    60
        this.messageText = messageText;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    61
        this.hintText = hintText;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    62
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    63
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    64
    public TextInputDialog() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    65
        // Only for reflection-based instantiation by the framework
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    66
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    67
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    68
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    69
    public void onAttach(Activity activity) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    70
        super.onAttach(activity);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    71
        try {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    72
            listener = (TextInputDialogListener) activity;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    73
        } catch(ClassCastException e) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    74
            throw new ClassCastException("Activity " + activity + " must implement TextInputDialogListener to use TextInputDialog.");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    75
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    76
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    77
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    78
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    79
    public void onDetach() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    80
        super.onDetach();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    81
        listener = null;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    82
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    83
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    84
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    85
    public Dialog onCreateDialog(Bundle savedInstanceState) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    86
        if(savedInstanceState != null) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    87
            dialogId = savedInstanceState.getInt(BUNDLE_DIALOG_ID, dialogId);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    88
            titleText = savedInstanceState.getInt(BUNDLE_TITLE_TEXT, titleText);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    89
            messageText = savedInstanceState.getInt(BUNDLE_MESSAGE_TEXT, messageText);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    90
            hintText = savedInstanceState.getInt(BUNDLE_HINT_TEXT, hintText);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    91
        }
7461
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
diff changeset
    92
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    93
        final EditText editText = new EditText(getActivity());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    94
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    95
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    96
        if(titleText != 0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    97
            builder.setTitle(titleText);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    98
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    99
        if(messageText != 0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   100
            builder.setTitle(messageText);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   101
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   102
        if(hintText != 0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   103
            editText.setHint(hintText);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   104
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   105
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   106
        editText.setId(android.R.id.text1);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   107
        editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   108
        editText.setSingleLine();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   109
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   110
        builder.setView(editText);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   111
        builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   112
            public void onClick(DialogInterface dialog, int which) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   113
                dialog.cancel();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   114
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   115
        });
7461
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
diff changeset
   116
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   117
        editText.setOnEditorActionListener(new OnEditorActionListener() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   118
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   119
                listener.onTextInputDialogSubmitted(dialogId, v.getText().toString());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   120
                dismiss();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   121
                return true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   122
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   123
        });
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   124
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   125
        builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   126
            public void onClick(DialogInterface dialog, int which) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   127
                listener.onTextInputDialogSubmitted(dialogId, editText.getText().toString());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   128
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   129
        });
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   130
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   131
        return builder.create();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   132
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   133
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   134
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   135
    public void onSaveInstanceState(Bundle icicle) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   136
        super.onSaveInstanceState(icicle);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   137
        icicle.putInt(BUNDLE_DIALOG_ID, dialogId);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   138
        icicle.putInt(BUNDLE_TITLE_TEXT, titleText);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   139
        icicle.putInt(BUNDLE_MESSAGE_TEXT, messageText);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   140
        icicle.putInt(BUNDLE_HINT_TEXT, hintText);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   141
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   142
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   143
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   144
    public void onCancel(DialogInterface dialog) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   145
        super.onCancel(dialog);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   146
        listener.onTextInputDialogCancelled(dialogId);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   147
    }
7461
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
diff changeset
   148
}