headsupdisplay.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. function HeadsUpDisplay(game, camera) {
  2. var ui = this;
  3. this.player = null;
  4. this.canvas = null;
  5. this.hudCanvas = null;
  6. this.hudContext = null;
  7. this.settingsButton = null;
  8. this.chatButton = null;
  9. this.inChat = false;
  10. this.chatTimeout = null;
  11. this.init = function(player, canvas) {
  12. this.player = player;
  13. this.canvas = canvas;
  14. this.settingsButton = new Button(canvas);
  15. this.settingsButton.addClickEvent(function(button) {
  16. if(button == 0) {
  17. var newName = prompt("Please enter your player name:", player.name);
  18. var newSprite = prompt("Please enter your desired race:", player.sprite);
  19. player.name = newName || player.name;
  20. player.sprite = newSprite || player.sprite;
  21. player.syncData();
  22. }
  23. });
  24. this.chatButton = new Button(canvas);
  25. this.chatButton.addClickEvent(function(button) {
  26. if(button == 0) {
  27. ui.toggleChat();
  28. }
  29. });
  30. };
  31. this.handleResize = function(canvas) {
  32. var buttonOptions = {position: {x: 12, y: 24, align: "left", baseline: "middle"}, background: {color: "gray", hover: "white"}, font: {size: 24, color: "white", hover: "black", family: "sans-serif"}, padding: 5};
  33. this.settingsButton.init("?", buttonOptions);
  34. var buttonOptions = {position: {x: 50, y: 24, align: "left", baseline: "middle"}, background: {color: "gray", hover: "white"}, font: {size: 24, color: "white", hover: "black", family: "sans-serif"}, padding: 5};
  35. this.chatButton.init("T", buttonOptions);
  36. }
  37. this.update = function(delta) {
  38. if(system.keyboard.isPressed(Keys.Enter)) {
  39. if(!this.inChat) {
  40. this.showChat();
  41. }
  42. }
  43. if(system.keyboard.isPressed(Keys.Escape)) {
  44. this.hideChat();
  45. }
  46. if(this.inChat) {
  47. document.getElementById("chat-input").focus();
  48. system.keyboard.clearBuffer();
  49. return;
  50. }
  51. if(system.keyboard.isPressed(Keys.T)) {
  52. this.showChat();
  53. }
  54. }
  55. this.sendChat = function() {
  56. var chatInput = document.getElementById("chat-input");
  57. var message = chatInput.value;
  58. if(message == "") {
  59. this.hideChat();
  60. return;
  61. }
  62. var chatAction = {action: "chat",
  63. data: {
  64. mobileid: this.player.id,
  65. message: message
  66. }
  67. };
  68. system.theater.socketSend(chatAction);
  69. chatInput.value = "";
  70. clearTimeout(this.chatTimeout);
  71. chatInput.blur();
  72. }
  73. this.draw = function(context) {
  74. this.settingsButton.draw(context);
  75. this.chatButton.draw(context);
  76. }
  77. this.mouseMove = function(canvas, x, y) {
  78. this.settingsButton.mouseMove(x, y);
  79. this.chatButton.mouseMove(x, y);
  80. }
  81. this.touchMove = function(canvas, x, y) {
  82. this.settingsButton.touchMove(x, y);
  83. this.chatButton.touchMove(x, y);
  84. }
  85. this.mouseDown = function(canvas, button, x, y) {
  86. this.settingsButton.mouseDown(button, x, y);
  87. this.chatButton.mouseDown(button, x, y);
  88. }
  89. this.mouseUp = function(canvas, button, x, y) {
  90. //this.settingsButton.mouseUp(button, x, y);
  91. //this.chatButton.mouseUp(button, x, y);
  92. }
  93. this.touchStart = function(canvas, x, y) {
  94. this.settingsButton.touchStart(x, y);
  95. this.chatButton.touchStart(x, y);
  96. };
  97. this.touchEnd = function(canvas, x, y) {
  98. //this.settingsButton.touchEnd(x, y);
  99. //this.chatButton.touchEnd(x, y);
  100. };
  101. this.showInventory = function() {
  102. var inventoryWindow = document.getElementById("inventory");
  103. if(inventoryWindow == null) {
  104. inventoryWindow = document.createElement("div");
  105. inventoryWindow.id = "inventory";
  106. inventoryWindow.style.display = "block";
  107. inventoryWindow.innerHTML = "Inventory Placeholder";
  108. document.getElementsByTagName("body")[0].appendChild(inventoryWindow);
  109. } else {
  110. if(inventoryWindow.style.display == "block") {
  111. inventoryWindow.style.display = "none";
  112. } else {
  113. inventoryWindow.style.display = "block";
  114. inventoryWindow.innerHTML = "Inventory Placeholder";
  115. }
  116. }
  117. }
  118. this.toggleChat = function() {
  119. if(this.inChat) {
  120. this.hideChat();
  121. } else {
  122. this.showChat();
  123. }
  124. }
  125. this.showChatInput = function() {
  126. }
  127. this.attachChatIfNonExistant = function() {
  128. var chatWindow = document.getElementById("chat");
  129. if(chatWindow == null) {
  130. chatWindow = document.createElement("div");
  131. chatWindow.id = "chat";
  132. chatWindow.style.display = "block";
  133. var chatOutput = document.createElement("div");
  134. chatOutput.id = "chat-output";
  135. chatWindow.appendChild(chatOutput);
  136. var form = document.createElement("form");
  137. form.addEventListener("submit", function(event) {
  138. ui.sendChat();
  139. event.preventDefault();
  140. }, false);
  141. var chatInput = document.createElement("input");
  142. chatInput.type = "text";
  143. chatInput.id = "chat-input";
  144. chatInput.setAttribute("autocomplete", "off");
  145. chatInput.setAttribute("autocorrect", "off");
  146. chatInput.setAttribute("autocapitalize", "off");
  147. chatInput.addEventListener("focus", function() {
  148. ui.inChat = true;
  149. });
  150. form.appendChild(chatInput);
  151. chatWindow.appendChild(form);
  152. document.getElementsByTagName("body")[0].appendChild(chatWindow);
  153. }
  154. return chatWindow;
  155. }
  156. this.showChat = function() {
  157. var chatWindow = this.attachChatIfNonExistant();
  158. chatWindow.style.display = "block";
  159. this.inChat = true;
  160. clearTimeout(this.chatTimeout);
  161. };
  162. this.hideChat = function() {
  163. this.inChat = false;
  164. var chatWindow = document.getElementById("chat");
  165. if(chatWindow == null) {
  166. return;
  167. }
  168. var chatInput = document.getElementById("chat-input");
  169. chatInput.blur();
  170. chatWindow.style.display = "none";
  171. };
  172. this.clearChat = function() {
  173. var chatOutput = document.getElementById("chat-output");
  174. if(chatOutput != null) {
  175. chatOutput.innerHTML = "";
  176. this.hideChat();
  177. }
  178. }
  179. this.receivedChatMessage = function(mobileId, message) {
  180. var tmpStatus = this.inChat;
  181. this.showChat();
  182. this.inChat = tmpStatus;
  183. var chatWindow = document.getElementById("chat");
  184. var chatOutput = document.getElementById("chat-output");
  185. var height = chatOutput.getElementsByTagName("span").length;
  186. while(height > 10) {
  187. chatOutput.removeChild(chatOutput.childNodes[0]);
  188. height = chatOutput.getElementsByTagName("span").length;
  189. }
  190. if(this.canvas.width < 768) {
  191. while(height > 5) {
  192. chatOutput.removeChild(chatOutput.childNodes[0]);
  193. height = chatOutput.getElementsByTagName("span").length;
  194. }
  195. }
  196. chatWindow.style.height = ((height * 20) + 60) + "px";
  197. var span = document.createElement("span");
  198. span.setAttribute("data-mobileid", mobileId);
  199. var text = document.createTextNode(message);
  200. span.appendChild(text);
  201. chatOutput.appendChild(span);
  202. clearTimeout(ui.chatTimeout);
  203. ui.chatTimeout = setTimeout(function() {
  204. if(!ui.inChat) {
  205. ui.hideChat();
  206. }
  207. }, 3000);
  208. }
  209. };