DragDropManager.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace FairyGUI
  5. {
  6. /// <summary>
  7. /// Helper for drag and drop.
  8. /// 这是一个提供特殊拖放功能的功能类。与GObject.draggable不同,拖动开始后,他使用一个替代的图标作为拖动对象。
  9. /// 当玩家释放鼠标/手指,目标组件会发出一个onDrop事件。
  10. /// </summary>
  11. public class DragDropManager
  12. {
  13. private GLoader _agent;
  14. private object _sourceData;
  15. private GObject _source;
  16. private static DragDropManager _inst;
  17. public static DragDropManager inst
  18. {
  19. get
  20. {
  21. if (_inst == null)
  22. _inst = new DragDropManager();
  23. return _inst;
  24. }
  25. }
  26. public DragDropManager()
  27. {
  28. _agent = (GLoader)UIObjectFactory.NewObject(ObjectType.Loader);
  29. _agent.gameObjectName = "DragDropAgent";
  30. _agent.SetHome(GRoot.inst);
  31. _agent.touchable = false;//important
  32. _agent.draggable = true;
  33. _agent.SetSize(100, 100);
  34. _agent.SetPivot(0.5f, 0.5f, true);
  35. _agent.align = AlignType.Center;
  36. _agent.verticalAlign = VertAlignType.Middle;
  37. _agent.sortingOrder = int.MaxValue;
  38. _agent.onDragEnd.Add(__dragEnd);
  39. }
  40. /// <summary>
  41. /// Loader object for real dragging.
  42. /// 用于实际拖动的Loader对象。你可以根据实际情况设置loader的大小,对齐等。
  43. /// </summary>
  44. public GLoader dragAgent
  45. {
  46. get { return _agent; }
  47. }
  48. /// <summary>
  49. /// Is dragging?
  50. /// 返回当前是否正在拖动。
  51. /// </summary>
  52. public bool dragging
  53. {
  54. get { return _agent.parent != null; }
  55. }
  56. /// <summary>
  57. /// Start dragging.
  58. /// 开始拖动。
  59. /// </summary>
  60. /// <param name="source">Source object. This is the object which initiated the dragging.</param>
  61. /// <param name="icon">Icon to be used as the dragging sign.</param>
  62. /// <param name="sourceData">Custom data. You can get it in the onDrop event data.</param>
  63. /// <param name="touchPointID">Copy the touchId from InputEvent to here, if has one.</param>
  64. public void StartDrag(GObject source, string icon, object sourceData, int touchPointID = -1)
  65. {
  66. if (_agent.parent != null)
  67. return;
  68. _sourceData = sourceData;
  69. _source = source;
  70. _agent.url = icon;
  71. GRoot.inst.AddChild(_agent);
  72. _agent.xy = GRoot.inst.GlobalToLocal(Stage.inst.GetTouchPosition(touchPointID));
  73. _agent.StartDrag(touchPointID);
  74. }
  75. /// <summary>
  76. /// Cancel dragging.
  77. /// 取消拖动。
  78. /// </summary>
  79. public void Cancel()
  80. {
  81. if (_agent.parent != null)
  82. {
  83. _agent.StopDrag();
  84. GRoot.inst.RemoveChild(_agent);
  85. _sourceData = null;
  86. }
  87. }
  88. private void __dragEnd(EventContext evt)
  89. {
  90. if (_agent.parent == null) //cancelled
  91. return;
  92. GRoot.inst.RemoveChild(_agent);
  93. object sourceData = _sourceData;
  94. GObject source = _source;
  95. _sourceData = null;
  96. _source = null;
  97. GObject obj = GRoot.inst.touchTarget;
  98. while (obj != null)
  99. {
  100. if (obj.hasEventListeners("onDrop"))
  101. {
  102. obj.RequestFocus();
  103. obj.DispatchEvent("onDrop", sourceData, source);
  104. return;
  105. }
  106. obj = obj.parent;
  107. }
  108. }
  109. }
  110. }