babylon.virtualJoystick.ts 16 KB

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