project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/ChatFragment.java
author nemo
Sat, 23 Jan 2016 16:07:14 -0500
changeset 11532 44e646598e6a
parent 10017 de822cd3df3a
permissions -rw-r--r--
sparkles for dynamite at sheepy's request. he can tweak values if he wishes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     1
/*
7584
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
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     3
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     4
 *
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     5
 * This program is free software; you can redistribute it and/or
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     6
 * modify it under the terms of the GNU General Public License
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     7
 * as published by the Free Software Foundation; either version 2
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     8
 * of the License, or (at your option) any later version.
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     9
 *
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    13
 * GNU General Public License for more details.
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    14
 *
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    15
 * You should have received a copy of the GNU General Public License
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    16
 * along with this program; if not, write to the Free Software
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    18
 */
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    19
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    20
package org.hedgewars.hedgeroid;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    22
import org.hedgewars.hedgeroid.R;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    23
import org.hedgewars.hedgeroid.netplay.MessageLog;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
import org.hedgewars.hedgeroid.netplay.Netplay;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    25
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    26
import android.os.Bundle;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
import android.support.v4.app.Fragment;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
import android.view.KeyEvent;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
import android.view.LayoutInflater;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
import android.view.View;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
import android.view.ViewGroup;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
import android.widget.EditText;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
import android.widget.ListView;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
import android.widget.TextView;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    35
import android.widget.TextView.OnEditorActionListener;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    36
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    37
/**
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    38
 * This fragment displays a chatlog and text input field for chatting in either
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    39
 * the lobby or a room.
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    40
 */
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    41
public class ChatFragment extends Fragment {
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    42
    private ChatlogAdapter adapter;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    43
    private Netplay netplay;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    44
    private MessageLog messageLog;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    45
    private boolean inRoom;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    46
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    47
    public void setInRoom(boolean inRoom) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    48
        this.inRoom = inRoom;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    49
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    50
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    51
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    52
    public void onCreate(Bundle savedInstanceState) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    53
        super.onCreate(savedInstanceState);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    54
        netplay = Netplay.getAppInstance(getActivity().getApplicationContext());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    55
        adapter = new ChatlogAdapter(getActivity());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    56
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    57
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    58
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    59
    public void onStart() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    60
        super.onStart();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    61
        messageLog = inRoom ? netplay.roomChatlog : netplay.lobbyChatlog;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    62
        adapter.setLog(messageLog.getLog());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    63
        messageLog.addListener(adapter);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    64
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    65
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    66
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    67
    public void onStop() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    68
        super.onStop();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    69
        messageLog.removeListener(adapter);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    70
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    71
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    72
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    73
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    74
            Bundle savedInstanceState) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    75
        View view = inflater.inflate(R.layout.fragment_chat, container, false);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    76
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    77
        ListView listView = (ListView) view.findViewById(R.id.chatConsole);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    78
        listView.setAdapter(adapter);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    79
        listView.setDivider(null);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    80
        listView.setDividerHeight(0);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    81
        listView.setVerticalFadingEdgeEnabled(true);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    82
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    83
        EditText editText = (EditText) view.findViewById(R.id.chatInput);
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    84
        editText.setOnEditorActionListener(new ChatSendListener());
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    85
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    86
        return view;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    87
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    88
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    89
    private final class ChatSendListener implements OnEditorActionListener {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    90
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    91
            String text = v.getText().toString();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    92
            if(text.length()>0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    93
                v.setText("");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    94
                netplay.sendChat(text);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    95
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    96
            return true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    97
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    98
    }
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    99
}