XJoystick.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. let thumbArea, puck, adt, xAddPos = 0, yAddPos = 0;
  2. export default class XJoystick{
  3. constructor(o, s) {
  4. this._config = {
  5. thumbAreaSize: 0,
  6. puckSize: 0,
  7. leftPadding: 0,
  8. bottomPadding: 0
  9. },
  10. this.degree = 0,
  11. this.distance = 0,
  12. this._updateCount = 0,
  13. this.styleThumbAreaDownAlpha = .5,
  14. this.styleThumbAreaUpAlpha = .2,
  15. this.stylePuckDownAlpha = 1,
  16. this.stylePuckUpAlpha = .8,
  17. this.styleThumbAreaChickness = 3,
  18. this.updateStep = 10,
  19. this._config.thumbAreaSize = o.thumbAreaSize,
  20. this._config.puckSize = o.puckSize,
  21. this._config.leftPadding = o.leftPadding,
  22. this._config.bottomPadding = -o.bottomPadding,
  23. this._scene = s.Scene,
  24. this._smgr = s,
  25. this.drawJoystick(),
  26. this.addObserver()
  27. }
  28. drawJoystick() {
  29. adt = BABYLON.GUI.AdvancedDynamicTexture.CreateFullscreenUI("UI"),
  30. adt.renderAtIdealSize = !0,
  31. adt.idealHeight = window.innerHeight,
  32. adt.idealWidth = window.innerWidth,
  33. adt.clipChildren = !1,
  34. thumbArea = new BABYLON.GUI.Ellipse,
  35. thumbArea.name = "thumbArea",
  36. thumbArea.thickness = this.styleThumbAreaChickness,
  37. thumbArea.color = "white",
  38. thumbArea.paddingLeft = "0px",
  39. thumbArea.paddingRight = "0px",
  40. thumbArea.paddingBottom = "0px",
  41. thumbArea.paddingTop = "0px",
  42. thumbArea.background = "black",
  43. thumbArea.height = this._config.thumbAreaSize == null ? 100 : this._config.thumbAreaSize + "px",
  44. thumbArea.width = thumbArea.height,
  45. thumbArea.isPointerBlocker = !0,
  46. thumbArea.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT,
  47. thumbArea.verticalAlignment = BABYLON.GUI.Control.VERTICAL_ALIGNMENT_BOTTOM,
  48. thumbArea.alpha = this.styleThumbAreaUpAlpha,
  49. thumbArea.left = this._config.leftPadding,
  50. thumbArea.top = this._config.bottomPadding,
  51. thumbArea.zIndex = 0,
  52. thumbArea.clipChildren = !1,
  53. thumbArea.clipContent = !1,
  54. puck = new BABYLON.GUI.Ellipse,
  55. puck.name = "puck",
  56. puck.paddingLeft = "0px",
  57. puck.paddingRight = "0px",
  58. puck.paddingBottom = "0px",
  59. puck.paddingTop = "0px",
  60. puck.height = this._config.puckSize == null ? 30 : this._config.puckSize + "px",
  61. puck.width = puck.height,
  62. puck.isPointerBlocker = !0,
  63. puck.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_CENTER,
  64. puck.verticalAlignment = BABYLON.GUI.Control.VERTICAL_ALIGNMENT_CENTER,
  65. puck.isDown = !1,
  66. puck.background = "white",
  67. puck.isVisible = !0,
  68. puck.zIndex = 1,
  69. puck.clipContent = !1,
  70. puck.alpha = this.stylePuckUpAlpha,
  71. adt.addControl(thumbArea),
  72. thumbArea.addControl(puck)
  73. }
  74. _updatePuckPosition(o) {
  75. const s = this._config.thumbAreaSize / thumbArea._currentMeasure.width;
  76. xAddPos = o.x - thumbArea._currentMeasure.width * .5 - thumbArea._currentMeasure.left,
  77. yAddPos = thumbArea._currentMeasure.top - o.y + thumbArea._currentMeasure.height * .5;
  78. const c = Math.sqrt(xAddPos * xAddPos + yAddPos * yAddPos);
  79. this._updateCount > this.updateStep && (this._updateCount = 0),
  80. this._updateCount == 0 && (c > thumbArea._currentMeasure.width / 2 ? (puck.floatLeft = s * .5 * xAddPos * thumbArea._currentMeasure.width / c,
  81. puck.floatTop = s * (.5 * yAddPos * thumbArea._currentMeasure.width / c) * -1,
  82. puck.left = puck.floatLeft,
  83. puck.top = puck.floatTop) : (puck.floatLeft = xAddPos * s,
  84. puck.floatTop = s * yAddPos * -1,
  85. puck.left = puck.floatLeft,
  86. puck.top = puck.floatTop)),
  87. this._updateCount++
  88. }
  89. addObserver() {
  90. this.onMoveObserver = adt.rootContainer.onPointerMoveObservable.add(o=>{
  91. this._onMoveFunc && this._onMoveFunc(),
  92. puck.isDown && this._updatePuckPosition(o)
  93. }
  94. ),
  95. this.onStopObserver = thumbArea.onPointerUpObservable.add(o=>{
  96. puck.isDown = !1,
  97. this._smgr.playerController.puckActive = !1,
  98. this._onEndFunc && this._onEndFunc(),
  99. thumbArea.alpha = this.styleThumbAreaUpAlpha,
  100. puck.alpha = this.stylePuckUpAlpha,
  101. this._resetPunkPosition()
  102. }
  103. ),
  104. this.onStartObserver = thumbArea.onPointerDownObservable.add(o=>{
  105. puck.isDown = !0,
  106. this._smgr.playerController.puckActive = !0,
  107. this._onStartFunc && this._onStartFunc(),
  108. this._updatePuckPosition(o),
  109. thumbArea.alpha = this.styleThumbAreaDownAlpha,
  110. puck.alpha = this.stylePuckDownAlpha
  111. }
  112. ),
  113. this._scene || console.error("[Engine] joystick cannot find root scene!"),
  114. this._scene.registerBeforeRender(()=>{
  115. this.distance = Math.sqrt(xAddPos * xAddPos + yAddPos * yAddPos),
  116. xAddPos > 0 && yAddPos > 0 ? this.degree = Math.atan(yAddPos / xAddPos) * 180 / Math.PI : xAddPos < 0 && yAddPos > 0 ? this.degree = Math.atan(yAddPos / xAddPos) * 180 / Math.PI + 180 : xAddPos < 0 && yAddPos < 0 ? this.degree = Math.atan(yAddPos / xAddPos) * 180 / Math.PI + 180 : xAddPos > 0 && yAddPos < 0 && (this.degree = Math.atan(yAddPos / xAddPos) * 180 / Math.PI + 360)
  117. }
  118. )
  119. }
  120. removeObserver() {
  121. this.onStartObserver && thumbArea.onPointerDownObservable.remove(this.onStartObserver),
  122. this.onMoveObserver && thumbArea.onPointerDownObservable.remove(this.onMoveObserver),
  123. this.onStopObserver && thumbArea.onPointerDownObservable.remove(this.onStopObserver)
  124. }
  125. _resetPunkPosition() {
  126. xAddPos = 0,
  127. yAddPos = 0,
  128. puck.floatLeft = 0,
  129. puck.left = 0,
  130. puck.floatTop = 0,
  131. puck.top = 0
  132. }
  133. show() {
  134. this.addObserver(),
  135. this._resetPunkPosition(),
  136. adt.rootContainer.isVisible = !0
  137. }
  138. hide() {
  139. this.removeObserver(),
  140. this._resetPunkPosition(),
  141. this._smgr.playerController.puckActive = !1,
  142. adt.rootContainer.isVisible = !1
  143. }
  144. onMove(o) {
  145. this._onMoveFunc = o
  146. }
  147. onStart(o) {
  148. this._onStartFunc = o
  149. }
  150. onEnd(o) {
  151. this._onEndFunc = o
  152. }
  153. dispose() {
  154. this.removeObserver(),
  155. adt.dispose(),
  156. puck.dispose(),
  157. thumbArea.dispose()
  158. }
  159. }