|
1 /* |
|
2 * 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 |
1 package org.hedgewars.hedgeroid; |
20 package org.hedgewars.hedgeroid; |
2 |
21 |
3 import org.hedgewars.hedgeroid.R; |
22 import org.hedgewars.hedgeroid.R; |
4 import org.hedgewars.hedgeroid.netplay.MessageLog; |
23 import org.hedgewars.hedgeroid.netplay.MessageLog; |
5 import org.hedgewars.hedgeroid.netplay.Netplay; |
24 import org.hedgewars.hedgeroid.netplay.Netplay; |
13 import android.widget.EditText; |
32 import android.widget.EditText; |
14 import android.widget.ListView; |
33 import android.widget.ListView; |
15 import android.widget.TextView; |
34 import android.widget.TextView; |
16 import android.widget.TextView.OnEditorActionListener; |
35 import android.widget.TextView.OnEditorActionListener; |
17 |
36 |
|
37 /** |
|
38 * This fragment displays a chatlog and text input field for chatting in either |
|
39 * the lobby or a room. |
|
40 */ |
18 public class ChatFragment extends Fragment { |
41 public class ChatFragment extends Fragment { |
19 public static final String ARGUMENT_INROOM = "inRoom"; |
|
20 |
|
21 private ChatlogAdapter adapter; |
42 private ChatlogAdapter adapter; |
22 private Netplay netconn; |
43 private Netplay netplay; |
23 private MessageLog messageLog; |
44 private MessageLog messageLog; |
24 private boolean inRoom; |
45 private boolean inRoom; |
25 |
46 |
26 public void setInRoom(boolean inRoom) { |
47 public void setInRoom(boolean inRoom) { |
27 this.inRoom = inRoom; |
48 this.inRoom = inRoom; |
28 } |
49 } |
29 |
50 |
30 @Override |
51 @Override |
31 public void onCreate(Bundle savedInstanceState) { |
52 public void onCreate(Bundle savedInstanceState) { |
32 super.onCreate(savedInstanceState); |
53 super.onCreate(savedInstanceState); |
33 netconn = Netplay.getAppInstance(getActivity().getApplicationContext()); |
54 netplay = Netplay.getAppInstance(getActivity().getApplicationContext()); |
34 adapter = new ChatlogAdapter(getActivity()); |
55 adapter = new ChatlogAdapter(getActivity()); |
35 } |
56 } |
36 |
57 |
37 @Override |
58 @Override |
38 public void onStart() { |
59 public void onStart() { |
39 super.onStart(); |
60 super.onStart(); |
40 messageLog = inRoom ? netconn.roomChatlog : netconn.lobbyChatlog; |
61 messageLog = inRoom ? netplay.roomChatlog : netplay.lobbyChatlog; |
41 adapter.setLog(messageLog.getLog()); |
62 adapter.setLog(messageLog.getLog()); |
42 messageLog.registerObserver(adapter); |
63 messageLog.addListener(adapter); |
43 } |
64 } |
44 |
65 |
45 @Override |
66 @Override |
46 public void onStop() { |
67 public void onStop() { |
47 super.onStop(); |
68 super.onStop(); |
48 messageLog.unregisterObserver(adapter); |
69 messageLog.removeListener(adapter); |
49 } |
70 } |
50 |
71 |
51 @Override |
72 @Override |
52 public View onCreateView(LayoutInflater inflater, ViewGroup container, |
73 public View onCreateView(LayoutInflater inflater, ViewGroup container, |
53 Bundle savedInstanceState) { |
74 Bundle savedInstanceState) { |
68 private final class ChatSendListener implements OnEditorActionListener { |
89 private final class ChatSendListener implements OnEditorActionListener { |
69 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
90 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
70 String text = v.getText().toString(); |
91 String text = v.getText().toString(); |
71 if(text.length()>0) { |
92 if(text.length()>0) { |
72 v.setText(""); |
93 v.setText(""); |
73 netconn.sendChat(text); |
94 netplay.sendChat(text); |
74 } |
95 } |
75 return true; |
96 return true; |
76 } |
97 } |
77 } |
98 } |
78 } |
99 } |