babylon.virtualJoystick.ts 16 KB

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