Droppable.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. var t = require;
  2. var e = module;
  3. var i = exports;
  4. e =
  5. (this && this.__decorate) ||
  6. function(t, e, i, s) {
  7. var o,
  8. a = arguments.length,
  9. n = a < 3 ? e : null === s ? (s = Object.getOwnPropertyDescriptor(e, i)) : s;
  10. if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) n = Reflect.decorate(t, e, i, s);
  11. else
  12. for (var r = t.length - 1; 0 <= r; r--)
  13. (o = t[r]) && (n = (a < 3 ? o(n) : 3 < a ? o(e, i, n) : o(e, i)) || n);
  14. return 3 < a && n && Object.defineProperty(e, i, n), n;
  15. };
  16. Object.defineProperty(i, "__esModule", { value: !0 });
  17. const s = t("App"),
  18. o = t("Draggable"),
  19. { ccclass: a, menu: n } = cc._decorator;
  20. (t = class extends cc.Component {
  21. constructor() {
  22. super(...arguments),
  23. (this.canDrop = !1),
  24. (this.delegate = {
  25. checkCanDrop: function() {
  26. return !0;
  27. },
  28. onDragIn: function() {
  29. return null;
  30. },
  31. onDragOut: function() {
  32. return null;
  33. },
  34. onDrop: function() {
  35. return null;
  36. },
  37. onDropCancel: function() {
  38. return null;
  39. },
  40. onDropEnd: function() {
  41. return null;
  42. }
  43. }),
  44. (this.isDragingIn = !1);
  45. }
  46. start() {}
  47. onEnable() {
  48. s.default.ViewManager.uiRoot.on(o.DragEventType.DRAG_START, this.onDragStart, this),
  49. s.default.ViewManager.uiRoot.on(o.DragEventType.DRAG_END, this.onDragEnd, this);
  50. }
  51. onDisable() {
  52. s.default.ViewManager.uiRoot.off(o.DragEventType.DRAG_START, this.onDragStart, this),
  53. s.default.ViewManager.uiRoot.off(o.DragEventType.DRAG_END, this.onDragEnd, this);
  54. }
  55. setTouchNode(t) {
  56. s.default.ViewManager.uiRoot.off(o.DragEventType.DRAG_START, this.onDragStart, this),
  57. s.default.ViewManager.uiRoot.off(o.DragEventType.DRAG_END, this.onDragEnd, this),
  58. s.default.ViewManager.uiRoot.on(o.DragEventType.DRAG_START, this.onDragStart, this),
  59. s.default.ViewManager.uiRoot.on(o.DragEventType.DRAG_END, this.onDragEnd, this);
  60. }
  61. setDelegate(t) {
  62. this.delegate = t;
  63. }
  64. onDragStart(t, e) {
  65. t.node != this.node &&
  66. ((this.dragingSource = t),
  67. (this.dragingData = e),
  68. s.default.ViewManager.uiRoot.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this, !0),
  69. (this.canDrop = this.delegate.checkCanDrop(this.dragingSource, this.dragingData)));
  70. }
  71. onDragEnd(t, e, i) {
  72. s.default.ViewManager.uiRoot.off(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
  73. i = this.node.getBoundingBoxToWorld().contains(i.getLocation());
  74. i && this.isDragingIn && this.canDrop ?
  75. (this.delegate.onDrop && this.delegate.onDrop(this.dragingSource, this.dragingData),
  76. this.node.emit(o.DragEventType.DROP, this, this.dragingData),
  77. t.onDragDone(e, this)) :
  78. i ||
  79. (this.delegate.onDropCancel && this.delegate.onDropCancel(this.dragingSource, this.dragingData),
  80. this.node.emit(o.DragEventType.CANCEL, this, this.dragingData)),
  81. this.canDrop &&
  82. (this.delegate.onDropEnd && this.delegate.onDropEnd(this.dragingSource, this.dragingData),
  83. this.node.emit(o.DragEventType.DRAG_END, this, this.dragingData)),
  84. (this.isDragingIn = !1),
  85. (this.dragingData = null),
  86. (this.dragingSource = null),
  87. (this.canDrop = !1);
  88. }
  89. onTouchMove(t) {
  90. t = this.node.getBoundingBoxToWorld().contains(t.getLocation());
  91. t
  92. ?
  93. this.canDrop &&
  94. (this.isDragingIn ?
  95. this.node.emit(o.DragEventType.DRAGING, this, this.dragingData) :
  96. ((this.isDragingIn = !0),
  97. this.delegate.onDragIn && this.delegate.onDragIn(this.dragingSource, this.dragingData),
  98. this.node.emit(o.DragEventType.DRAGING_IN, this, this.dragingData))) :
  99. !t &&
  100. this.isDragingIn &&
  101. this.canDrop &&
  102. ((this.isDragingIn = !1),
  103. this.delegate.onDragOut && this.delegate.onDragOut(this.dragingSource, this.dragingData),
  104. this.node.emit(o.DragEventType.DRAGING_OUT, this, this.dragingData));
  105. }
  106. }),
  107. (t = e([a], t));
  108. i.default = t;