babylon.virtualJoystick.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // Mainly based on these 2 articles :
  2. // Creating an universal virtual touch joystick working for all Touch models thanks to Hand.JS : http://blogs.msdn.com/b/davrous/archive/2013/02/22/creating-an-universal-virtual-touch-joystick-working-for-all-touch-models-thanks-to-hand-js.aspx
  3. // & on Seb Lee-Delisle original work: http://seb.ly/2011/04/multi-touch-game-controller-in-javascripthtml5-for-ipad/
  4. var BABYLON;
  5. (function (BABYLON) {
  6. (function (JoystickAxis) {
  7. JoystickAxis[JoystickAxis["X"] = 0] = "X";
  8. JoystickAxis[JoystickAxis["Y"] = 1] = "Y";
  9. JoystickAxis[JoystickAxis["Z"] = 2] = "Z";
  10. })(BABYLON.JoystickAxis || (BABYLON.JoystickAxis = {}));
  11. var JoystickAxis = BABYLON.JoystickAxis;
  12. var VirtualJoystick = (function () {
  13. function VirtualJoystick(leftJoystick) {
  14. var _this = this;
  15. if (leftJoystick) {
  16. this._leftJoystick = true;
  17. } else {
  18. this._leftJoystick = false;
  19. }
  20. this._joystickIndex = VirtualJoystick._globalJoystickIndex;
  21. VirtualJoystick._globalJoystickIndex++;
  22. // By default left & right arrow keys are moving the X
  23. // and up & down keys are moving the Y
  24. this._axisTargetedByLeftAndRight = 0 /* X */;
  25. this._axisTargetedByUpAndDown = 1 /* Y */;
  26. this.reverseLeftRight = false;
  27. this.reverseUpDown = false;
  28. // collections of pointers
  29. this._touches = new BABYLON.VirtualJoystick.Collection();
  30. this.deltaPosition = BABYLON.Vector3.Zero();
  31. this._joystickSensibility = 25;
  32. this._inversedSensibility = 1 / (this._joystickSensibility / 1000);
  33. this._rotationSpeed = 25;
  34. this._inverseRotationSpeed = 1 / (this._rotationSpeed / 1000);
  35. this._rotateOnAxisRelativeToMesh = false;
  36. // injecting a canvas element on top of the canvas 3D game
  37. if (!VirtualJoystick.vjCanvas) {
  38. window.addEventListener("resize", function () {
  39. VirtualJoystick.vjCanvasWidth = window.innerWidth;
  40. VirtualJoystick.vjCanvasHeight = window.innerHeight;
  41. VirtualJoystick.vjCanvas.width = VirtualJoystick.vjCanvasWidth;
  42. VirtualJoystick.vjCanvas.height = VirtualJoystick.vjCanvasHeight;
  43. VirtualJoystick.halfWidth = VirtualJoystick.vjCanvasWidth / 2;
  44. VirtualJoystick.halfHeight = VirtualJoystick.vjCanvasHeight / 2;
  45. }, false);
  46. VirtualJoystick.vjCanvas = document.createElement("canvas");
  47. VirtualJoystick.vjCanvasWidth = window.innerWidth;
  48. VirtualJoystick.vjCanvasHeight = window.innerHeight;
  49. VirtualJoystick.vjCanvas.width = window.innerWidth;
  50. VirtualJoystick.vjCanvas.height = window.innerHeight;
  51. VirtualJoystick.vjCanvas.style.width = "100%";
  52. VirtualJoystick.vjCanvas.style.height = "100%";
  53. VirtualJoystick.vjCanvas.style.position = "absolute";
  54. VirtualJoystick.vjCanvas.style.backgroundColor = "transparent";
  55. VirtualJoystick.vjCanvas.style.top = "0px";
  56. VirtualJoystick.vjCanvas.style.left = "0px";
  57. VirtualJoystick.vjCanvas.style.zIndex = "5";
  58. VirtualJoystick.vjCanvas.style.msTouchAction = "none";
  59. VirtualJoystick.vjCanvasContext = VirtualJoystick.vjCanvas.getContext('2d');
  60. VirtualJoystick.vjCanvasContext.strokeStyle = "#ffffff";
  61. VirtualJoystick.vjCanvasContext.lineWidth = 2;
  62. document.body.appendChild(VirtualJoystick.vjCanvas);
  63. }
  64. VirtualJoystick.halfWidth = VirtualJoystick.vjCanvas.width / 2;
  65. VirtualJoystick.halfHeight = VirtualJoystick.vjCanvas.height / 2;
  66. this.pressed = false;
  67. // default joystick color
  68. this._joystickColor = "cyan";
  69. this._joystickPointerID = -1;
  70. // current joystick position
  71. this._joystickPointerPos = new BABYLON.Vector2(0, 0);
  72. // origin joystick position
  73. this._joystickPointerStartPos = new BABYLON.Vector2(0, 0);
  74. this._deltaJoystickVector = new BABYLON.Vector2(0, 0);
  75. VirtualJoystick.vjCanvas.addEventListener('pointerdown', function (evt) {
  76. _this._onPointerDown(evt);
  77. }, false);
  78. VirtualJoystick.vjCanvas.addEventListener('pointermove', function (evt) {
  79. _this._onPointerMove(evt);
  80. }, false);
  81. VirtualJoystick.vjCanvas.addEventListener('pointerup', function (evt) {
  82. _this._onPointerUp(evt);
  83. }, false);
  84. VirtualJoystick.vjCanvas.addEventListener('pointerout', function (evt) {
  85. _this._onPointerUp(evt);
  86. }, false);
  87. VirtualJoystick.vjCanvas.addEventListener("contextmenu", function (evt) {
  88. evt.preventDefault(); // Disables system menu
  89. }, false);
  90. requestAnimationFrame(function () {
  91. _this._drawVirtualJoystick();
  92. });
  93. }
  94. VirtualJoystick.prototype.setJoystickSensibility = function (newJoystickSensibility) {
  95. this._joystickSensibility = newJoystickSensibility;
  96. this._inversedSensibility = 1 / (this._joystickSensibility / 1000);
  97. };
  98. VirtualJoystick.prototype._onPointerDown = function (e) {
  99. var positionOnScreenCondition;
  100. e.preventDefault();
  101. if (this._leftJoystick === true) {
  102. positionOnScreenCondition = (e.clientX < VirtualJoystick.halfWidth);
  103. } else {
  104. positionOnScreenCondition = (e.clientX > VirtualJoystick.halfWidth);
  105. }
  106. if (positionOnScreenCondition && this._joystickPointerID < 0) {
  107. // First contact will be dedicated to the virtual joystick
  108. this._joystickPointerID = e.pointerId;
  109. this._joystickPointerStartPos.x = e.clientX;
  110. this._joystickPointerStartPos.y = e.clientY;
  111. this._joystickPointerPos = this._joystickPointerStartPos.clone();
  112. this._deltaJoystickVector.x = 0;
  113. this._deltaJoystickVector.y = 0;
  114. this.pressed = true;
  115. this._touches.add(e.pointerId.toString(), e);
  116. } else {
  117. // You can only trigger the action buttons with a joystick declared
  118. if (VirtualJoystick._globalJoystickIndex < 2 && this._action) {
  119. this._action();
  120. this._touches.add(e.pointerId.toString(), e);
  121. }
  122. }
  123. };
  124. VirtualJoystick.prototype._onPointerMove = function (e) {
  125. // If the current pointer is the one associated to the joystick (first touch contact)
  126. if (this._joystickPointerID == e.pointerId) {
  127. this._joystickPointerPos.x = e.clientX;
  128. this._joystickPointerPos.y = e.clientY;
  129. this._deltaJoystickVector = this._joystickPointerPos.clone();
  130. this._deltaJoystickVector = this._deltaJoystickVector.subtract(this._joystickPointerStartPos);
  131. var directionLeftRight = this.reverseLeftRight ? -1 : 1;
  132. var deltaJoystickX = directionLeftRight * this._deltaJoystickVector.x / this._inversedSensibility;
  133. switch (this._axisTargetedByLeftAndRight) {
  134. case 0 /* X */:
  135. this.deltaPosition.x = Math.min(1, Math.max(-1, deltaJoystickX));
  136. break;
  137. case 1 /* Y */:
  138. this.deltaPosition.y = Math.min(1, Math.max(-1, deltaJoystickX));
  139. break;
  140. case 2 /* Z */:
  141. this.deltaPosition.z = Math.min(1, Math.max(-1, deltaJoystickX));
  142. break;
  143. }
  144. var directionUpDown = this.reverseUpDown ? 1 : -1;
  145. var deltaJoystickY = directionUpDown * this._deltaJoystickVector.y / this._inversedSensibility;
  146. switch (this._axisTargetedByUpAndDown) {
  147. case 0 /* X */:
  148. this.deltaPosition.x = Math.min(1, Math.max(-1, deltaJoystickY));
  149. break;
  150. case 1 /* Y */:
  151. this.deltaPosition.y = Math.min(1, Math.max(-1, deltaJoystickY));
  152. break;
  153. case 2 /* Z */:
  154. this.deltaPosition.z = Math.min(1, Math.max(-1, deltaJoystickY));
  155. break;
  156. }
  157. } else {
  158. if (this._touches.item(e.pointerId.toString())) {
  159. this._touches.item(e.pointerId.toString()).x = e.clientX;
  160. this._touches.item(e.pointerId.toString()).y = e.clientY;
  161. }
  162. }
  163. };
  164. VirtualJoystick.prototype._onPointerUp = function (e) {
  165. this._clearCanvas();
  166. if (this._joystickPointerID == e.pointerId) {
  167. this._joystickPointerID = -1;
  168. this.pressed = false;
  169. }
  170. this._deltaJoystickVector.x = 0;
  171. this._deltaJoystickVector.y = 0;
  172. this._touches.remove(e.pointerId.toString());
  173. };
  174. /**
  175. * Change the color of the virtual joystick
  176. * @param newColor a string that must be a CSS color value (like "red") or the hexa value (like "#FF0000")
  177. */
  178. VirtualJoystick.prototype.setJoystickColor = function (newColor) {
  179. this._joystickColor = newColor;
  180. };
  181. VirtualJoystick.prototype.setActionOnTouch = function (action) {
  182. this._action = action;
  183. };
  184. // Define which axis you'd like to control for left & right
  185. VirtualJoystick.prototype.setAxisForLeftRight = function (axis) {
  186. switch (axis) {
  187. case 0 /* X */:
  188. case 1 /* Y */:
  189. case 2 /* Z */:
  190. this._axisTargetedByLeftAndRight = axis;
  191. break;
  192. this._axisTargetedByLeftAndRight = axis;
  193. break;
  194. default:
  195. this._axisTargetedByLeftAndRight = 0 /* X */;
  196. break;
  197. }
  198. };
  199. // Define which axis you'd like to control for up & down
  200. VirtualJoystick.prototype.setAxisForUpDown = function (axis) {
  201. switch (axis) {
  202. case 0 /* X */:
  203. case 1 /* Y */:
  204. case 2 /* Z */:
  205. this._axisTargetedByUpAndDown = axis;
  206. break;
  207. default:
  208. this._axisTargetedByUpAndDown = 1 /* Y */;
  209. break;
  210. }
  211. };
  212. VirtualJoystick.prototype._clearCanvas = function () {
  213. if (this._leftJoystick) {
  214. VirtualJoystick.vjCanvasContext.clearRect(0, 0, VirtualJoystick.vjCanvasWidth / 2, VirtualJoystick.vjCanvasHeight);
  215. } else {
  216. VirtualJoystick.vjCanvasContext.clearRect(VirtualJoystick.vjCanvasWidth / 2, 0, VirtualJoystick.vjCanvasWidth, VirtualJoystick.vjCanvasHeight);
  217. }
  218. };
  219. VirtualJoystick.prototype._drawVirtualJoystick = function () {
  220. var _this = this;
  221. if (this.pressed) {
  222. this._clearCanvas();
  223. this._touches.forEach(function (touch) {
  224. if (touch.pointerId === _this._joystickPointerID) {
  225. VirtualJoystick.vjCanvasContext.beginPath();
  226. VirtualJoystick.vjCanvasContext.strokeStyle = _this._joystickColor;
  227. VirtualJoystick.vjCanvasContext.lineWidth = 6;
  228. VirtualJoystick.vjCanvasContext.arc(_this._joystickPointerStartPos.x, _this._joystickPointerStartPos.y, 40, 0, Math.PI * 2, true);
  229. VirtualJoystick.vjCanvasContext.stroke();
  230. VirtualJoystick.vjCanvasContext.beginPath();
  231. VirtualJoystick.vjCanvasContext.strokeStyle = _this._joystickColor;
  232. VirtualJoystick.vjCanvasContext.lineWidth = 2;
  233. VirtualJoystick.vjCanvasContext.arc(_this._joystickPointerStartPos.x, _this._joystickPointerStartPos.y, 60, 0, Math.PI * 2, true);
  234. VirtualJoystick.vjCanvasContext.stroke();
  235. VirtualJoystick.vjCanvasContext.beginPath();
  236. VirtualJoystick.vjCanvasContext.strokeStyle = _this._joystickColor;
  237. VirtualJoystick.vjCanvasContext.arc(_this._joystickPointerPos.x, _this._joystickPointerPos.y, 40, 0, Math.PI * 2, true);
  238. VirtualJoystick.vjCanvasContext.stroke();
  239. } else {
  240. VirtualJoystick.vjCanvasContext.beginPath();
  241. VirtualJoystick.vjCanvasContext.fillStyle = "white";
  242. VirtualJoystick.vjCanvasContext.beginPath();
  243. VirtualJoystick.vjCanvasContext.strokeStyle = "red";
  244. VirtualJoystick.vjCanvasContext.lineWidth = 6;
  245. VirtualJoystick.vjCanvasContext.arc(touch.x, touch.y, 40, 0, Math.PI * 2, true);
  246. VirtualJoystick.vjCanvasContext.stroke();
  247. }
  248. ;
  249. });
  250. }
  251. requestAnimationFrame(function () {
  252. _this._drawVirtualJoystick();
  253. });
  254. };
  255. VirtualJoystick.prototype.releaseCanvas = function () {
  256. if (VirtualJoystick.vjCanvas) {
  257. document.body.removeChild(VirtualJoystick.vjCanvas);
  258. VirtualJoystick.vjCanvas = null;
  259. }
  260. };
  261. VirtualJoystick._globalJoystickIndex = 0;
  262. return VirtualJoystick;
  263. })();
  264. BABYLON.VirtualJoystick = VirtualJoystick;
  265. })(BABYLON || (BABYLON = {}));
  266. var BABYLON;
  267. (function (BABYLON) {
  268. (function (VirtualJoystick) {
  269. var Collection = (function () {
  270. function Collection() {
  271. this._count = 0;
  272. this._collection = new Array();
  273. }
  274. Collection.prototype.Count = function () {
  275. return this._count;
  276. };
  277. Collection.prototype.add = function (key, item) {
  278. if (this._collection[key] != undefined) {
  279. return undefined;
  280. }
  281. this._collection[key] = item;
  282. return ++this._count;
  283. };
  284. Collection.prototype.remove = function (key) {
  285. if (this._collection[key] == undefined) {
  286. return undefined;
  287. }
  288. delete this._collection[key];
  289. return --this._count;
  290. };
  291. Collection.prototype.item = function (key) {
  292. return this._collection[key];
  293. };
  294. Collection.prototype.forEach = function (block) {
  295. var key;
  296. for (key in this._collection) {
  297. if (this._collection.hasOwnProperty(key)) {
  298. block(this._collection[key]);
  299. }
  300. }
  301. };
  302. return Collection;
  303. })();
  304. VirtualJoystick.Collection = Collection;
  305. })(BABYLON.VirtualJoystick || (BABYLON.VirtualJoystick = {}));
  306. var VirtualJoystick = BABYLON.VirtualJoystick;
  307. })(BABYLON || (BABYLON = {}));
  308. //# sourceMappingURL=babylon.virtualJoystick.js.map