babylon.virtualJoystick.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. e.preventDefault();
  100. var newPointer = { identifier: e.pointerId, x: e.clientX, y: e.clientY, type: this.givePointerType(e) };
  101. var positionOnScreenCondition;
  102. if (this._leftJoystick === true) {
  103. positionOnScreenCondition = (e.clientX < VirtualJoystick.halfWidth);
  104. } else {
  105. positionOnScreenCondition = (e.clientX > VirtualJoystick.halfWidth);
  106. }
  107. if (positionOnScreenCondition && this.joystickPointerID < 0) {
  108. // First contact will be dedicated to the virtual joystick
  109. this.joystickPointerID = e.pointerId;
  110. this.joystickPointerStartPos.x = e.clientX;
  111. this.joystickPointerStartPos.y = e.clientY;
  112. this.joystickPointerPos = this.joystickPointerStartPos.clone();
  113. this.deltaJoystickVector.x = 0;
  114. this.deltaJoystickVector.y = 0;
  115. this.pressed = true;
  116. this._touches.add(e.pointerId.toString(), newPointer);
  117. } else {
  118. // You can only trigger the action buttons with a joystick declared
  119. if (VirtualJoystick._globalJoystickIndex < 2 && this._action) {
  120. this._action();
  121. this._touches.add(e.pointerId.toString(), newPointer);
  122. }
  123. }
  124. };
  125. VirtualJoystick.prototype.onPointerMove = function (e) {
  126. // If the current pointer is the one associated to the joystick (first touch contact)
  127. if (this.joystickPointerID == e.pointerId) {
  128. this.joystickPointerPos.x = e.clientX;
  129. this.joystickPointerPos.y = e.clientY;
  130. this.deltaJoystickVector = this.joystickPointerPos.clone();
  131. this.deltaJoystickVector = this.deltaJoystickVector.subtract(this.joystickPointerStartPos);
  132. var directionLeftRight = this.reverseLeftRight ? -1 : 1;
  133. var deltaJoystickX = directionLeftRight * this.deltaJoystickVector.x / this._inversedSensibility;
  134. switch (this._axisTargetedByLeftAndRight) {
  135. case 0 /* X */:
  136. this.deltaPosition.x = Math.min(1, Math.max(-1, deltaJoystickX));
  137. break;
  138. case 1 /* Y */:
  139. this.deltaPosition.y = Math.min(1, Math.max(-1, deltaJoystickX));
  140. break;
  141. case 2 /* Z */:
  142. this.deltaPosition.z = Math.min(1, Math.max(-1, deltaJoystickX));
  143. break;
  144. }
  145. var directionUpDown = this.reverseUpDown ? 1 : -1;
  146. var deltaJoystickY = directionUpDown * this.deltaJoystickVector.y / this._inversedSensibility;
  147. switch (this._axisTargetedByUpAndDown) {
  148. case 0 /* X */:
  149. this.deltaPosition.x = Math.min(1, Math.max(-1, deltaJoystickY));
  150. break;
  151. case 1 /* Y */:
  152. this.deltaPosition.y = Math.min(1, Math.max(-1, deltaJoystickY));
  153. break;
  154. case 2 /* Z */:
  155. this.deltaPosition.z = Math.min(1, Math.max(-1, deltaJoystickY));
  156. break;
  157. }
  158. } else {
  159. if (this._touches.item(e.pointerId.toString())) {
  160. this._touches.item(e.pointerId.toString()).x = e.clientX;
  161. this._touches.item(e.pointerId.toString()).y = e.clientY;
  162. }
  163. }
  164. };
  165. VirtualJoystick.prototype.onPointerUp = function (e) {
  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. VirtualJoystick.prototype.setJoystickColor = function (newColor) {
  175. this._joystickColor = newColor;
  176. };
  177. VirtualJoystick.prototype.setActionOnTouch = function (action) {
  178. this._action = action;
  179. };
  180. // Define which axis you'd like to control for left & right
  181. VirtualJoystick.prototype.setAxisForLR = function (axis) {
  182. switch (axis) {
  183. case 0 /* X */:
  184. case 1 /* Y */:
  185. case 2 /* Z */:
  186. this._axisTargetedByLeftAndRight = axis;
  187. break;
  188. this._axisTargetedByLeftAndRight = axis;
  189. break;
  190. default:
  191. this._axisTargetedByLeftAndRight = 0 /* X */;
  192. break;
  193. }
  194. };
  195. // Define which axis you'd like to control for up & down
  196. VirtualJoystick.prototype.setAxisForUD = function (axis) {
  197. switch (axis) {
  198. case 0 /* X */:
  199. case 1 /* Y */:
  200. case 2 /* Z */:
  201. this._axisTargetedByUpAndDown = axis;
  202. break;
  203. default:
  204. this._axisTargetedByUpAndDown = 1 /* Y */;
  205. break;
  206. }
  207. };
  208. VirtualJoystick.prototype.drawVirtualJoystick = function () {
  209. var _this = this;
  210. if (this._leftJoystick) {
  211. VirtualJoystick.vjCanvasContext.clearRect(0, 0, VirtualJoystick.vjCanvasWidth / 2, VirtualJoystick.vjCanvasHeight);
  212. } else {
  213. VirtualJoystick.vjCanvasContext.clearRect(VirtualJoystick.vjCanvasWidth / 2, 0, VirtualJoystick.vjCanvasWidth, VirtualJoystick.vjCanvasHeight);
  214. }
  215. this._touches.forEach(function (touch) {
  216. if (touch.identifier === _this.joystickPointerID) {
  217. VirtualJoystick.vjCanvasContext.beginPath();
  218. VirtualJoystick.vjCanvasContext.strokeStyle = _this._joystickColor;
  219. VirtualJoystick.vjCanvasContext.lineWidth = 6;
  220. VirtualJoystick.vjCanvasContext.arc(_this.joystickPointerStartPos.x, _this.joystickPointerStartPos.y, 40, 0, Math.PI * 2, true);
  221. VirtualJoystick.vjCanvasContext.stroke();
  222. VirtualJoystick.vjCanvasContext.beginPath();
  223. VirtualJoystick.vjCanvasContext.strokeStyle = _this._joystickColor;
  224. VirtualJoystick.vjCanvasContext.lineWidth = 2;
  225. VirtualJoystick.vjCanvasContext.arc(_this.joystickPointerStartPos.x, _this.joystickPointerStartPos.y, 60, 0, Math.PI * 2, true);
  226. VirtualJoystick.vjCanvasContext.stroke();
  227. VirtualJoystick.vjCanvasContext.beginPath();
  228. VirtualJoystick.vjCanvasContext.strokeStyle = _this._joystickColor;
  229. VirtualJoystick.vjCanvasContext.arc(_this.joystickPointerPos.x, _this.joystickPointerPos.y, 40, 0, Math.PI * 2, true);
  230. VirtualJoystick.vjCanvasContext.stroke();
  231. } else {
  232. VirtualJoystick.vjCanvasContext.beginPath();
  233. VirtualJoystick.vjCanvasContext.fillStyle = "white";
  234. VirtualJoystick.vjCanvasContext.beginPath();
  235. VirtualJoystick.vjCanvasContext.strokeStyle = "red";
  236. VirtualJoystick.vjCanvasContext.lineWidth = 6;
  237. VirtualJoystick.vjCanvasContext.arc(touch.x, touch.y, 40, 0, Math.PI * 2, true);
  238. VirtualJoystick.vjCanvasContext.stroke();
  239. }
  240. ;
  241. });
  242. requestAnimationFrame(function () {
  243. _this.drawVirtualJoystick();
  244. });
  245. };
  246. VirtualJoystick.prototype.givePointerType = function (event) {
  247. switch (event.pointerType) {
  248. case event.POINTER_TYPE_MOUSE:
  249. return "MOUSE";
  250. break;
  251. case event.POINTER_TYPE_PEN:
  252. return "PEN";
  253. break;
  254. case event.POINTER_TYPE_TOUCH:
  255. return "TOUCH";
  256. break;
  257. }
  258. };
  259. VirtualJoystick.prototype.releaseCanvas = function () {
  260. if (VirtualJoystick.vjCanvas) {
  261. document.body.removeChild(VirtualJoystick.vjCanvas);
  262. VirtualJoystick.vjCanvas = null;
  263. }
  264. };
  265. VirtualJoystick._globalJoystickIndex = 0;
  266. return VirtualJoystick;
  267. })();
  268. BABYLON.VirtualJoystick = VirtualJoystick;
  269. })(BABYLON || (BABYLON = {}));
  270. var BABYLON;
  271. (function (BABYLON) {
  272. (function (VirtualJoystick) {
  273. var Collection = (function () {
  274. function Collection() {
  275. this.count = 0;
  276. this.collection = new Array();
  277. }
  278. Collection.prototype.add = function (key, item) {
  279. if (this.collection[key] != undefined) {
  280. return undefined;
  281. }
  282. this.collection[key] = item;
  283. return ++this.count;
  284. };
  285. Collection.prototype.remove = function (key) {
  286. if (this.collection[key] == undefined) {
  287. return undefined;
  288. }
  289. delete this.collection[key];
  290. return --this.count;
  291. };
  292. Collection.prototype.item = function (key) {
  293. return this.collection[key];
  294. };
  295. Collection.prototype.forEach = function (block) {
  296. var key;
  297. for (key in this.collection) {
  298. if (this.collection.hasOwnProperty(key)) {
  299. block(this.collection[key]);
  300. }
  301. }
  302. };
  303. return Collection;
  304. })();
  305. VirtualJoystick.Collection = Collection;
  306. })(BABYLON.VirtualJoystick || (BABYLON.VirtualJoystick = {}));
  307. var VirtualJoystick = BABYLON.VirtualJoystick;
  308. })(BABYLON || (BABYLON = {}));
  309. //# sourceMappingURL=babylon.virtualJoystick.js.map