input.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * ===========================================================================
  3. *
  4. * Wolf3D Browser Version GPL Source Code
  5. * Copyright (C) 2012 id Software LLC, a ZeniMax Media company.
  6. *
  7. * This file is part of the Wolf3D Browser Version GPL Source Code ("Wolf3D Browser Source Code").
  8. *
  9. * Wolf3D Browser Source Code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * Wolf3D Browser Source Code is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License version 2
  20. * along with Wolf3D Browser Source Code. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * If you have questions concerning this license, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  23. *
  24. * ===========================================================================
  25. */
  26. /**
  27. * @namespace
  28. * @description Functions for capturing keyboard/mouse input
  29. */
  30. Wolf.Input = (function() {
  31. var keys,
  32. lmbDown = false,
  33. rmbDown = false,
  34. bindings = [],
  35. hasFocus = false,
  36. mouseX = -1, mouseY = -1,
  37. mouseMoveX = 0, mouseMoveY = 0;
  38. function init() {
  39. var game = $("#game"),
  40. main = $("#main"),
  41. renderer = $("#game .renderer");
  42. if (!keys) {
  43. keys = [];
  44. $(document)
  45. .on("keydown", function(e) {
  46. e.preventDefault();
  47. if (!Wolf.Game.isPlaying()) {
  48. return;
  49. }
  50. keys[e.keyCode] = true;
  51. if (bindings[e.keyCode]) {
  52. for (var i=0,n=bindings[e.keyCode].length;i<n;i++) {
  53. bindings[e.keyCode][i](e);
  54. }
  55. }
  56. })
  57. .on("keyup", function(e) {
  58. e.preventDefault();
  59. if (!Wolf.Game.isPlaying()) {
  60. return;
  61. }
  62. keys[e.keyCode] = false;
  63. })
  64. .on("keypress", function(e) {
  65. e.preventDefault();
  66. })
  67. .on("contextmenu", function(e) {
  68. e.preventDefault();
  69. })
  70. .on("onrightclick", function(e) {
  71. e.preventDefault();
  72. })
  73. .on("mousedown", function(e) {
  74. window.focus();
  75. e.preventDefault();
  76. })
  77. .on("mouseup", function(e) {
  78. e.preventDefault();
  79. });
  80. $("#game")
  81. .on("mousedown", function(e) {
  82. if (hasFocus) {
  83. if (e.which == 1) {
  84. lmbDown = true;
  85. } else if (e.which == 3) {
  86. rmbDown = true;
  87. }
  88. } else {
  89. window.focus();
  90. }
  91. e.preventDefault();
  92. })
  93. .on("mouseup", function(e) {
  94. if (hasFocus) {
  95. if (e.which == 1) {
  96. lmbDown = false;
  97. } else if (e.which == 3) {
  98. rmbDown = false;
  99. }
  100. }
  101. e.preventDefault();
  102. })
  103. .on("mousemove", function(e) {
  104. if (!hasFocus) {
  105. return;
  106. }
  107. if (isPointerLocked()) {
  108. if ("webkitMovementX" in e.originalEvent) {
  109. mouseMoveX += e.originalEvent.webkitMovementX;
  110. mouseMoveY += e.originalEvent.webkitMovementY;
  111. } else if ("mozMovementX" in e.originalEvent) {
  112. mouseMoveX += e.originalEvent.mozMovementX;
  113. mouseMoveY += e.originalEvent.mozMovementY;
  114. } else if ("movementX" in e.originalEvent) {
  115. mouseMoveX += e.originalEvent.movementX;
  116. mouseMoveY += e.originalEvent.movementY;
  117. }
  118. } else {
  119. if (Wolf.Game.isFullscreen()) {
  120. mouseX = e.pageX / window.innerWidth;
  121. mouseY = e.pageY / window.innerHeight;
  122. } else {
  123. var offset = main.offset();
  124. mouseX = (e.pageX - offset.left) / main.width();
  125. mouseY = (e.pageY - offset.top) / main.height();
  126. }
  127. }
  128. e.preventDefault();
  129. });
  130. // reset keys and mouse if window/tab loses focus
  131. $(window).on("blur", function(e) {
  132. hasFocus = false;
  133. reset();
  134. });
  135. $(window).on("focus", function(e) {
  136. hasFocus = true;
  137. });
  138. }
  139. }
  140. function reset() {
  141. resetMouse();
  142. keys = [];
  143. }
  144. function resetMouse() {
  145. lmbDown = false;
  146. rmbDown = false;
  147. mouseX = mouseY = 0.5;
  148. }
  149. function bindKey(k, handler) {
  150. var keyCode = Wolf.Keys[k];
  151. if (!bindings[keyCode]) {
  152. bindings[keyCode] = [];
  153. }
  154. bindings[keyCode].push(handler);
  155. }
  156. /**
  157. * @memberOf Wolf.Input
  158. * @description Check if one of the specified keys is pressed.
  159. * @param {array} keys Array of key names.
  160. * @returns {boolean} True if a key is pressed, otherwise false.
  161. */
  162. function checkKeys(ckeys) {
  163. for (var i=0;i<ckeys.length;i++) {
  164. var k = ckeys[i];
  165. if (!!keys[Wolf.Keys[k]]) {
  166. return true;
  167. }
  168. }
  169. return false;
  170. }
  171. /**
  172. * @memberOf Wolf.Input
  173. * @description Clear status for keys.
  174. * @param {array} keys Array of key names.
  175. */
  176. function clearKeys(ckeys) {
  177. for (var i=0;i<ckeys.length;i++) {
  178. var k = ckeys[i];
  179. keys[Wolf.Keys[k]] = false;
  180. }
  181. return false;
  182. }
  183. function leftMouseDown() {
  184. return lmbDown;
  185. }
  186. function rightMouseDown() {
  187. return rmbDown;
  188. }
  189. function getMouseCoords() {
  190. if (mouseX < 0 || mouseX > 1 || mouseY < 0 || mouseY > 1) {
  191. return null;
  192. } else {
  193. return {
  194. x : (mouseX - 0.5) * 2,
  195. y : (mouseY - 0.5) * 2
  196. };
  197. }
  198. }
  199. function getMouseMovement() {
  200. var x = mouseMoveX,
  201. y = mouseMoveY;
  202. mouseMoveX = 0;
  203. mouseMoveY = 0;
  204. return {
  205. x : x / screen.width,
  206. y : y / screen.height
  207. };
  208. }
  209. function getPointer() {
  210. var pointer = navigator.pointer ||
  211. navigator.webkitPointer ||
  212. navigator.mozPointer ||
  213. navigator.msPointer ||
  214. navigator.oPointer;
  215. return pointer;
  216. }
  217. function isPointerLocked() {
  218. var pointer = getPointer();
  219. return pointer && pointer.isLocked && pointer.isLocked();
  220. }
  221. function lockPointer() {
  222. var pointer = getPointer();
  223. if (!pointer) {
  224. return;
  225. }
  226. if (Wolf.Game.isFullscreen()) {
  227. pointer.lock($("#game")[0],
  228. function(e) {
  229. Wolf.log("Pointer locked")
  230. }, function(e) {
  231. Wolf.log("Could not lock pointer: " + e);
  232. }
  233. );
  234. }
  235. }
  236. function unlockPointer() {
  237. var pointer = getPointer();
  238. if (!pointer) {
  239. return;
  240. }
  241. pointer.unlock($("#game")[0]);
  242. }
  243. return {
  244. init : init,
  245. reset : reset,
  246. resetMouse : resetMouse,
  247. checkKeys : checkKeys,
  248. clearKeys : clearKeys,
  249. bindKey : bindKey,
  250. leftMouseDown : leftMouseDown,
  251. rightMouseDown : rightMouseDown,
  252. getMouseCoords : getMouseCoords,
  253. getMouseMovement : getMouseMovement,
  254. isPointerLocked : isPointerLocked,
  255. lockPointer : lockPointer,
  256. unlockPointer : unlockPointer
  257. };
  258. })();
  259. Wolf.Keys = {
  260. LEFT : 37,
  261. UP : 38,
  262. RIGHT : 39,
  263. DOWN : 40,
  264. ENTER : 13,
  265. SPACE : 32,
  266. SHIFT : 16,
  267. CTRL : 17,
  268. ALT : 18,
  269. ESC : 27,
  270. HOME : 36,
  271. END : 35,
  272. DEL : 46,
  273. INS : 45,
  274. PGUP : 33,
  275. PGDN : 34,
  276. SLASH : 111,
  277. MINUS : 109,
  278. PLUS : 107,
  279. COMMA : 188,
  280. PERIOD : 190,
  281. 1 : 49,
  282. 2 : 50,
  283. 3 : 51,
  284. 4 : 52,
  285. 5 : 53,
  286. 6 : 54,
  287. 7 : 55,
  288. 8 : 56,
  289. 9 : 57,
  290. 0 : 58,
  291. A : 65,
  292. B : 66,
  293. C : 67,
  294. D : 68,
  295. E : 69,
  296. F : 70,
  297. G : 71,
  298. H : 72,
  299. I : 73,
  300. J : 74,
  301. K : 75,
  302. L : 76,
  303. M : 77,
  304. N : 78,
  305. O : 79,
  306. P : 80,
  307. Q : 81,
  308. R : 82,
  309. S : 83,
  310. T : 84,
  311. U : 85,
  312. V : 86,
  313. W : 87,
  314. X : 88,
  315. Y : 89,
  316. Z : 90,
  317. F1 : 112,
  318. F2 : 113,
  319. F3 : 114,
  320. F4 : 115,
  321. F5 : 116,
  322. F6 : 117,
  323. F7 : 118,
  324. F8 : 119,
  325. F9 : 120,
  326. F10 : 121,
  327. F11 : 122,
  328. F12 : 123
  329. };