babylon.virtualJoystick.ts 18 KB

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