DragSystem.ts 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { Component, EventTouch, SystemEventType, UITransform, _decorator, Node } from "cc";
  2. import { GeometryUtils } from "../../utils/GeometryUtils";
  3. import { DragSource } from "./DragSource";
  4. import { DragTarget } from "./DragTarget";
  5. const { ccclass, property } = _decorator;
  6. /**
  7. * 拖拽系统组件
  8. * @description 主场景中丢这个组件即可
  9. * @author 袁浩
  10. */
  11. @ccclass("DragSystem")
  12. export class DragSystem extends Component {
  13. @property({ tooltip: "可以出发拖拽的节点", type: Node })
  14. public touchNode: Node;
  15. private sourceList: DragSource[] = [];
  16. private targetList: DragTarget[] = [];
  17. private draggingSource: DragSource;//拖拽中的组件
  18. private static thisObject: DragSystem;
  19. start() {
  20. this.touchNode.on(SystemEventType.TOUCH_MOVE, this.onTouchMove, this);
  21. this.touchNode.on(SystemEventType.TOUCH_END, this.onTouchEnd, this);
  22. this.touchNode.on(SystemEventType.TOUCH_CANCEL, this.onTouchEnd, this);
  23. DragSystem.thisObject = this;
  24. }
  25. private onTouchMove(event: EventTouch) {
  26. if (!this.draggingSource) {//没有进行拖拽
  27. for (let i = 0; i < this.sourceList.length; i++) {
  28. if (this.sourceList[i].node.isValid && this.sourceList[i].node.active && this.sourceList[i].getComponent(UITransform).isHit(event.getUILocation())) {//如果跟触摸点碰撞
  29. this.draggingSource = this.sourceList[i];
  30. this.draggingSource.drag(event);
  31. return;
  32. }
  33. }
  34. }
  35. else {
  36. this.draggingSource.worldPosition = GeometryUtils.V2ToV3(event.getUILocation());
  37. for (let i = 0; i < this.targetList.length; i++) {
  38. let target = this.targetList[i];
  39. if (target.node.isValid && target.node.active && this.draggingSource.group == target.group) {
  40. target.draging(this.draggingSource,this.targetList[i].getComponent(UITransform).isHit(event.getUILocation()), event);
  41. }
  42. }
  43. this.draggingSource.draging(event);
  44. }
  45. }
  46. private onTouchEnd(event: EventTouch) {
  47. if (!this.draggingSource) {//没有进行拖拽
  48. return;
  49. }
  50. this.draggingSource.drop(event);
  51. for (let i = 0; i < this.targetList.length; i++) {
  52. let target = this.targetList[i];
  53. if (target.node.isValid && target.node.active && this.draggingSource.group == target.group && this.targetList[i].getComponent(UITransform).isHit(event.getUILocation())) {//如果分组相同且跟触摸点碰撞
  54. target.drop(this.draggingSource, event);
  55. }
  56. }
  57. this.draggingSource.worldPosition = null;
  58. this.draggingSource = null;
  59. }
  60. private addSource(source: DragSource) {
  61. this.sourceList.push(source);
  62. }
  63. private addTarget(target: DragTarget) {
  64. this.targetList.push(target);
  65. }
  66. private removeSource(source: DragSource) {
  67. let index = this.sourceList.indexOf(source);
  68. index != -1 && this.sourceList.splice(index, 1);
  69. }
  70. private removeTarget(target: DragTarget) {
  71. let index = this.targetList.indexOf(target);
  72. index != -1 && this.targetList.splice(index, 1);
  73. }
  74. onDestroy() {
  75. this.touchNode.off(SystemEventType.TOUCH_MOVE, this.onTouchMove, this);
  76. this.touchNode.off(SystemEventType.TOUCH_END, this.onTouchEnd, this);
  77. DragSystem.thisObject = null;
  78. }
  79. public static addSource(source: DragSource) {
  80. this.thisObject.addSource(source);
  81. }
  82. public static addTarget(target: DragTarget) {
  83. this.thisObject.addTarget(target);
  84. }
  85. public static removeSource(source: DragSource) {
  86. this.thisObject && this.thisObject.removeSource(source);
  87. }
  88. public static removeTarget(target: DragTarget) {
  89. this.thisObject && this.thisObject.removeTarget(target);
  90. }
  91. public static onTouchEnd(event: EventTouch) {
  92. this.thisObject && this.thisObject.onTouchEnd(event);
  93. }
  94. public static get touchNode() {
  95. return this.thisObject.touchNode;
  96. }
  97. }