BackgroundControl.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import { _decorator, Component, EventTouch, log, math, Node, Touch, UITransform, Vec2, Vec3, Widget } from 'cc';
  2. import EventMgr from 'db://assets/script/Manager/EventMgr';
  3. import { GDM } from '../dtta/JsonMgr';
  4. import { GameCfg } from 'db://assets/script/Config/GameCfg';
  5. const { ccclass, property } = _decorator;
  6. @ccclass('BgControl')
  7. export class BgControl extends Component {
  8. node_bg: Node = null;
  9. private bgTransform!: UITransform;
  10. private maskTransform!: UITransform;
  11. //单指相关
  12. startBgPos: Vec3 = null
  13. startMovePos: Vec2 = null
  14. endMovePos: Vec2 = null
  15. //双指相关
  16. startDistance: number = null
  17. startScale: number = null
  18. maxScale: number = 3;
  19. minScale: number = 0.5;
  20. start() {
  21. this.node_bg = this.node.getChildByName("bg");
  22. this.bgTransform = this.node_bg.getComponent(UITransform)!;
  23. this.maskTransform = this.node.getComponent(UITransform)!;
  24. console.log(this.maskTransform.height)
  25. // this.scheduleOnce(() => {
  26. // if (this.maskTransform.height > 960) {
  27. // this.node.getComponent(Widget).enabled = false
  28. // this.maskTransform.height = 960
  29. // }
  30. // }, 0.1)
  31. let data = GDM.gamecfgMgr.paixuLevelinfo.length > 0 ? GDM.gamecfgMgr.paixuLevelinfo : GDM.gamecfgMgr.data
  32. this.maxScale = data[GameCfg.CurUnit].maxScale
  33. this.minScale = data[GameCfg.CurUnit].minScale
  34. this.node_bg.setScale(this.minScale, this.minScale, 1)
  35. this.scheduleOnce(() => {
  36. if (this.maskTransform.height >= this.node_bg.getComponent(UITransform).height * this.minScale - 20) {
  37. this.maskTransform.height = this.node_bg.getComponent(UITransform).height * this.minScale - 20
  38. }
  39. }, 0.1)
  40. log(GameCfg.CurUnit)
  41. log("max", this.maxScale)
  42. log("min", this.minScale)
  43. this.node_bg.on(Node.EventType.TOUCH_START, this.onTouchStart, this);
  44. this.node_bg.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
  45. this.node_bg.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
  46. this.node_bg.on(Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
  47. }
  48. onTouchStart(event: EventTouch) {
  49. const touches = event.getTouches();
  50. this.startMovePos = event.getLocation();
  51. if (touches.length == 1) {
  52. this.startBgPos = this.node_bg.getPosition()
  53. } else if (touches.length == 2) {
  54. this.startDistance = touches[0].getLocation().subtract(touches[1].getLocation()).length()
  55. this.startScale = this.node_bg.scale.x
  56. }
  57. }
  58. onTouchMove(event: EventTouch) {
  59. const touches = event.getAllTouches();
  60. if (touches.length == 1) {
  61. this.singleFingerMove(event)
  62. } else if (touches.length == 2) {
  63. this.doubleFingerMove(touches)
  64. }
  65. }
  66. singleFingerMove(event: EventTouch) {
  67. // const position = event.getLocation();
  68. // let x = this.startMovePos.x - position.x
  69. // let y = this.startMovePos.y - position.y
  70. // this.node_bg.setPosition(this.startBgPos.x - x, this.startBgPos.y - y)
  71. const change = event.getDelta();
  72. this.node_bg.setPosition(this.node_bg.position.x + change.x, this.node_bg.position.y + change.y)
  73. this.clampPosition()
  74. if (this.startMovePos.subtract(event.getLocation()).length() > 10) {
  75. this.bool_needfire = true
  76. }
  77. }
  78. bool_needfire = false
  79. doubleFingerMove(etouches: Touch[]) {
  80. let dis = etouches[0].getLocation().subtract(etouches[1].getLocation()).length()
  81. if (0 == this.startDistance) {
  82. this.startDistance = dis
  83. return
  84. }
  85. let scale = this.startScale + ((dis - this.startDistance) / 1000)
  86. if (scale > this.maxScale) {
  87. scale = this.maxScale
  88. } else if (scale < this.minScale) {
  89. scale = this.minScale
  90. }
  91. this.node_bg.setScale(new Vec3(scale, scale, 1))
  92. this.clampPosition()
  93. }
  94. onTouchEnd(event: EventTouch) {
  95. const touches = event.getAllTouches();
  96. this.startDistance = 0
  97. this.startScale = this.node_bg.scale.x
  98. this.startScale = this.node_bg.scale.x
  99. this.clampPosition()
  100. if (this.bool_needfire) {
  101. EventMgr.ins.dispatchEvent("dontshow_x")
  102. this.bool_needfire = false
  103. return
  104. }
  105. if (touches.length == 1) {
  106. const change = event.getDelta();
  107. } else if (touches.length == 2) {
  108. EventMgr.ins.dispatchEvent("dontshow_x")
  109. } else {
  110. log("三指 无效")
  111. }
  112. }
  113. //#region 边界限制
  114. private clampPosition() {
  115. const bgPos = this.node_bg.position;
  116. const bgScale = this.node_bg.scale.x;
  117. // 获取实际尺寸
  118. const bgWidth = this.bgTransform.width * bgScale;
  119. const bgHeight = this.bgTransform.height * bgScale;
  120. const maskWidth = this.maskTransform.width;
  121. const maskHeight = this.maskTransform.height;
  122. // console.log("实际尺寸", bgWidth, bgHeight)
  123. // console.log("遮罩尺寸", maskWidth, maskHeight)
  124. // 计算边界限制
  125. // 计算边界限制
  126. const minX = (maskWidth / 2) - bgWidth / 2;
  127. const maxX = (-maskWidth / 2) + bgWidth / 2;
  128. const minY = (maskHeight / 2) - bgHeight / 2;
  129. const maxY = (-maskHeight / 2) + bgHeight / 2;
  130. // console.log("minX", minX, "maxX", maxX, "minY", minY, "maxY", maxY)
  131. // // 限制位置
  132. let x = bgPos.x;
  133. let y = bgPos.y;
  134. if (bgPos.x > maxX) {
  135. x = maxX
  136. } else if (bgPos.x < minX) {
  137. x = minX
  138. }
  139. if (bgPos.y > maxY) {
  140. y = maxY
  141. } else if (bgPos.y < minY) {
  142. y = minY
  143. }
  144. this.node_bg.setPosition(x, y, 0);
  145. }
  146. }