SymbolTextEvent.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using UnityEngine;
  2. using UnityEngine.EventSystems;
  3. using UnityEngine.Events;
  4. using LuaInterface;
  5. namespace WXB
  6. {
  7. [RequireComponent(typeof(SymbolText))]
  8. public class SymbolTextEvent : MonoBehaviour, /*IPointerEnterHandler, IPointerExitHandler,*/ IPointerDownHandler, IPointerUpHandler
  9. {
  10. SymbolText d_symbolText;
  11. //RenderCache.BaseData d_baseData;
  12. [System.Serializable]
  13. public class OnClickEvent : UnityEvent<NodeBase>
  14. {
  15. }
  16. public OnClickEvent OnClick = new OnClickEvent(); // 点击了此结点
  17. LuaTable table;
  18. LuaFunction func;
  19. LuaTable param;
  20. void OnEnable()
  21. {
  22. if (d_symbolText == null)
  23. {
  24. d_symbolText = GetComponent<SymbolText>();
  25. }
  26. }
  27. void OnDisable()
  28. {
  29. //isEnter = false;
  30. //d_baseData = null;
  31. d_down_basedata = null;
  32. localPosition = Vector2.zero;
  33. }
  34. //bool isEnter = false;
  35. //public void OnPointerEnter(PointerEventData eventData)
  36. //{
  37. // isEnter = true;
  38. //}
  39. //public void OnPointerExit(PointerEventData eventData)
  40. //{
  41. // isEnter = false;
  42. //}
  43. RenderCache.BaseData d_down_basedata;
  44. public void RegisterClickEvent(LuaTable table, LuaFunction func)
  45. {
  46. this.table = table;
  47. this.func = func;
  48. }
  49. public void OnPointerDown(PointerEventData eventData)
  50. {
  51. if (!Tools.ScreenPointToWorldPointInRectangle(d_symbolText.rectTransform, eventData.position, d_symbolText.canvas.worldCamera, out localPosition))
  52. return;
  53. //localPosition *= d_symbolText.pixelsPerUnit;
  54. d_down_basedata = d_symbolText.renderCache.Get(localPosition);
  55. }
  56. public void OnPointerUp(PointerEventData eventData)
  57. {
  58. if (!Tools.ScreenPointToWorldPointInRectangle(d_symbolText.rectTransform, eventData.position, d_symbolText.canvas.worldCamera, out localPosition))
  59. return;
  60. //localPosition *= d_symbolText.pixelsPerUnit;
  61. var up_node = d_symbolText.renderCache.Get(localPosition);
  62. if (d_down_basedata != up_node)
  63. return;
  64. if (d_down_basedata != null)
  65. {
  66. OnClick.Invoke(d_down_basedata.node);
  67. if (func != null)
  68. {
  69. HyperlinkNode node = d_down_basedata.node as HyperlinkNode;
  70. if (node != null && !string.IsNullOrEmpty(node.d_link))
  71. {
  72. if (table != null)
  73. {
  74. func.Call(table, node.d_link);
  75. }
  76. else
  77. {
  78. func.Call(node.d_link);
  79. }
  80. }
  81. }
  82. }
  83. }
  84. Vector2 localPosition;
  85. //void Update()
  86. //{
  87. // if (isEnter)
  88. // {
  89. // Tools.ScreenPointToWorldPointInRectangle(d_symbolText.rectTransform, Input.mousePosition, d_symbolText.canvas.worldCamera, out localPosition);
  90. // localPosition *= d_symbolText.pixelsPerUnit;
  91. // //Debug.LogFormat("Pos:{0}", localPosition);
  92. // RenderCache.BaseData bd = d_symbolText.renderCache.Get(localPosition);
  93. // if (d_baseData != bd)
  94. // {
  95. // if (d_baseData != null)
  96. // {
  97. // d_baseData.OnMouseLevel();
  98. // }
  99. // d_baseData = bd;
  100. // if (d_baseData != null)
  101. // d_baseData.OnMouseEnter();
  102. // }
  103. // }
  104. // else
  105. // {
  106. // if (d_baseData != null)
  107. // {
  108. // d_baseData.OnMouseLevel();
  109. // d_baseData = null;
  110. // }
  111. // }
  112. //}
  113. }
  114. }