Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. let chat =
  2. {
  3. size: 0,
  4. history_limit: 30, //Change this if you want to hold more/less chat history
  5. container: null,
  6. input: null,
  7. enabled: false,
  8. active: true
  9. };
  10.  
  11. function enableChatInput(enable)
  12. {
  13. if(chat.active == false
  14. && enable == true)
  15. return;
  16.  
  17. if (enable != (chat.input != null))
  18. {
  19. //chat_printing = enable;
  20.  
  21. mp.invoke("focus", enable);
  22.  
  23. if (enable)
  24. {
  25. chat.input = $("#chat").append('<div><input id="chat_msg" type="text" /></div>').children(":last");
  26. chat.input.children("input").focus();
  27. }
  28. else
  29. {
  30. chat.input.fadeOut('fast', function()
  31. {
  32. chat.input.remove();
  33. chat.input = null;
  34. });
  35. }
  36. }
  37. }
  38.  
  39. var chatAPI =
  40. {
  41. push: (text) =>
  42. {
  43. chat.container.prepend("<li>" + text + "</li>");
  44.  
  45. chat.size++;
  46.  
  47. if (chat.size >= chat.history_limit)
  48. {
  49. chat.container.children(":last").remove();
  50. }
  51. },
  52.  
  53. clear: () =>
  54. {
  55. chat.container.html("");
  56. },
  57.  
  58. activate: (toggle) =>
  59. {
  60. if (toggle == false
  61. && (chat.input != null))
  62. enableChatInput(false);
  63.  
  64. chat.active = toggle;
  65. },
  66.  
  67. show: (toggle) =>
  68. {
  69. if(toggle)
  70. $("#chat").show();
  71. else
  72. $("#chat").hide();
  73.  
  74. chat.active = toggle;
  75. }
  76. };
  77.  
  78. $(document).ready(function()
  79. {
  80. chat.container = $("#chat ul#chat_messages");
  81.  
  82. $(".ui_element").show();
  83. chatAPI.push("Multiplayer started");
  84.  
  85. $("body").keydown(function(event)
  86. {
  87. if (event.which == 84 && chat.input == null // si preme T, apertura chat
  88. && chat.active == true)
  89. {
  90. enableChatInput(true);
  91. event.preventDefault();
  92. }
  93. else if (event.which == 13 && chat.input != null) // si preme INVIO per mandare l'input
  94. {
  95. var value = chat.input.children("input").val();
  96.  
  97. if (value.length > 0)
  98. {
  99. if (value[0] == "/")
  100. {
  101. value = value.substr(1);
  102.  
  103. if (value.length > 0)
  104. mp.invoke("command", value);
  105. }
  106. else
  107. {
  108. mp.invoke("chatMessage", value);
  109. }
  110. }
  111.  
  112. enableChatInput(false);
  113. }
  114. });
  115. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement