babylon.virtualJoystick.js 14 KB

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