Advertisement
Guest User

Untitled

a guest
Dec 14th, 2011
237
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. using System.Collections.Generic;
  5. public class ChatScreen : MonoBehaviour {
  6.  
  7. private Game engine;
  8. public GameObject gameManager_GObj;
  9. public GUISkin SkinChat;
  10. public string TextMessage;
  11. public string lastmessage = "lastmessage";
  12. public bool Pause = false;
  13.  
  14. public GUIStyle TextChatStyle;
  15. public GUIStyle boxStyle;
  16. public GUIStyle textFieldStyle;
  17. GUIStyle createStyle;
  18. private Vector2 chatScroll;
  19. public string NameSender;
  20. public static bool chat = false;
  21. internal const byte EV_CHAT = 105;
  22. internal const byte CHAT_MESSAGE = 64;
  23.  
  24. public List<string> TextChat = new List<string>();
  25. private string pending;
  26. private bool pass1;
  27. private bool flag2 = false;
  28. private float nextChange;
  29. private bool init = true;
  30.  
  31.  
  32. void Start ()
  33. {
  34. TextChatStyle = SkinChat.GetStyle("TextChat");
  35. nextChange = Time.time + 0.1f;
  36. boxStyle = SkinChat.GetStyle("box");
  37. textFieldStyle = SkinChat.GetStyle("textField");
  38. createStyle = SkinChat.GetStyle("Create");
  39. }
  40.  
  41. public void Initialize(Game engine)
  42. {
  43. this.engine = engine;
  44. this.init = true;
  45. }
  46.  
  47. public void IncomingMessage(Hashtable _hashtable)
  48. {
  49.  
  50. string message = (string)_hashtable[CHAT_MESSAGE];
  51. TextChat.Add(message);
  52. chatScroll = new Vector2(0, TextChat.Count * 19);
  53. }
  54.  
  55.  
  56. void SendingMessage(string message)
  57. {
  58. // block repeated messages (TODO: for a certain time?)
  59. if (lastmessage == message)
  60. return;
  61.  
  62. if (message != string.Empty)
  63. {
  64. pending = NameSender + ": " + message;
  65. Hashtable evInfo = new Hashtable();
  66. evInfo.Add((System.Object)CHAT_MESSAGE, (string)NameSender + ": " + message);
  67. engine.peer.OpRaiseEvent(EV_CHAT, evInfo, true);
  68. lastmessage = NameSender + ": " + message;
  69. }
  70.  
  71. }
  72.  
  73. void StatusPauseChat(bool _pause)
  74. {
  75. flag2 = chat;
  76. chat = _pause;
  77. }
  78.  
  79. void StatusPause(bool _pause)
  80. {
  81. Pause = _pause;
  82. }
  83.  
  84. void Update()
  85. {
  86. if (!Pause && !chat)
  87. {
  88. if (this.init)
  89. {
  90. gameManager_GObj.SendMessage("SetChat", false);
  91. chat = false;
  92. flag2 = false;
  93. TextMessage = string.Empty;
  94. nextChange = Time.time + 0.1f;
  95. this.init = false;
  96. }
  97. else if(Input.GetKeyUp(KeyCode.KeypadEnter) || Input.GetKeyUp(KeyCode.Return))
  98. {
  99. if (nextChange > Time.time) return;
  100. gameManager_GObj.SendMessage("SetChat", true);
  101. chat = true;
  102. flag2 = true;
  103. this.nextChange = Time.time + 0.1f;
  104. }
  105. }
  106. }
  107.  
  108. void OnGUI ()
  109. {
  110.  
  111.  
  112. if (!Pause)
  113. {
  114.  
  115. GUILayout.BeginArea(new Rect((int)((Screen.width -340)), (int)(Screen.height - 90), 340, 80));
  116.  
  117. GUILayout.BeginArea(new Rect(0, 0, 340, 45), boxStyle);
  118.  
  119. pass1 = !pass1;
  120. if (pass1)
  121. {
  122. if (pending != null)
  123. {
  124. TextChat.Add(pending);
  125. pending = null;
  126. chatScroll = new Vector2(0, TextChat.Count * 19);
  127. }
  128.  
  129.  
  130. }
  131.  
  132.  
  133. DisplayMessages();
  134. GUILayout.EndScrollView();
  135. GUILayout.EndArea();
  136.  
  137. GUILayout.BeginArea(new Rect(0, 50, 310, 60));
  138. GUILayout.BeginHorizontal();
  139.  
  140.  
  141. if (chat)
  142. {
  143. DisplaySenderMessage();
  144. }
  145. else if (flag2 != chat)
  146. {
  147. SetFocus();
  148. flag2 = chat;
  149. }
  150.  
  151. GUILayout.EndHorizontal();
  152. GUILayout.EndArea();
  153.  
  154. GUILayout.EndArea();
  155.  
  156.  
  157.  
  158.  
  159. }
  160. }
  161.  
  162. void SetFocus()
  163. {
  164. if (chat)
  165. {
  166. GUI.FocusControl("ChatField");
  167. }
  168. else
  169. {
  170. GUI.FocusControl("Null");
  171. }
  172. }
  173.  
  174. void DisplayMessages()
  175. {
  176. chatScroll = GUILayout.BeginScrollView(chatScroll, GUILayout.Width(330));
  177.  
  178. if (TextChat.Count <= 0) return;
  179. for(int i = 0; i<TextChat.Count; i++)
  180. {
  181. string textChat = TextChat[i];
  182. GUILayout.Label(textChat, TextChatStyle);
  183. }
  184. }
  185.  
  186. void DisplaySenderMessage()
  187. {
  188. GUI.SetNextControlName("ChatField");
  189. TextMessage = GUILayout.TextField(TextMessage, textFieldStyle,GUILayout.Width(260));
  190. GUI.SetNextControlName("Null");
  191.  
  192. if (chat)
  193. {
  194. if (
  195. GUILayout.Button("Send", createStyle, GUILayout.Width(50))
  196. || Input.GetKeyUp(KeyCode.KeypadEnter)
  197. || Input.GetKeyUp(KeyCode.Return)
  198. )
  199. {
  200. if (nextChange > Time.time) return;
  201. SendingMessage(TextMessage);
  202. gameManager_GObj.SendMessage("SetChat", false);
  203. chat = false;
  204. flag2 = false;
  205. TextMessage = string.Empty;
  206. this.nextChange = Time.time + 0.1f;
  207. }
  208. }
  209.  
  210. SetFocus();
  211. }
  212. }
  213.  
  214.  
Advertisement
RAW Paste Data Copied
Advertisement