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