ItemWidgetBasic.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using System;
  2. using Fort23.Core;
  3. using Fort23.Mono;
  4. using OfficeOpenXml.FormulaParsing.Excel.Functions.Logical;
  5. using UnityEngine;
  6. using UnityEngine.EventSystems;
  7. public class ItemWidgetBasic : UIComponent, IScorllListWidget
  8. {
  9. public bool isAddLongPressBtn = true;
  10. public LongPressBtn longPressBtn;
  11. public ButtonAnimation animation = null;
  12. protected RectTransform _rectTransform;
  13. public System.Action<ItemWidgetBasic> OnClick
  14. {
  15. get { return onClick; }
  16. set
  17. {
  18. onClick = value;
  19. if (onContinueClick != null || onClick != null)
  20. {
  21. if (animation != null)
  22. animation.isEnabled = true;
  23. }
  24. else
  25. {
  26. if (animation != null)
  27. animation.isEnabled = false;
  28. }
  29. }
  30. }
  31. private System.Action<ItemWidgetBasic> onClick;
  32. public System.Action<ItemWidgetBasic> OnContinueClick
  33. {
  34. get { return onContinueClick; }
  35. set
  36. {
  37. onContinueClick = value;
  38. if (onContinueClick != null || onClick != null)
  39. {
  40. if (animation != null)
  41. animation.isEnabled = true;
  42. }
  43. else
  44. {
  45. if (animation != null)
  46. animation.isEnabled = false;
  47. }
  48. }
  49. }
  50. /// <summary>
  51. /// 拖拽开始事件
  52. /// </summary>
  53. public Action<PointerEventData, ItemWidgetBasic> onDragStart;
  54. /// <summary>
  55. /// 拖拽进行中事件
  56. /// </summary>
  57. public Action<PointerEventData, ItemWidgetBasic> onDrag;
  58. /// <summary>
  59. /// 拖拽结束事件
  60. /// </summary>
  61. public Action<PointerEventData, ItemWidgetBasic> onDragEnd;
  62. public Action<ItemWidgetBasic> onPointerEnter;
  63. public Action<ItemWidgetBasic> onPointerExit;
  64. private System.Action<ItemWidgetBasic> onContinueClick;
  65. public override void AddButtonEvent()
  66. {
  67. if (isAddLongPressBtn)
  68. {
  69. longPressBtn = own.GetComponent<LongPressBtn>();
  70. if (longPressBtn == null)
  71. longPressBtn = own.AddComponent<LongPressBtn>();
  72. longPressBtn.onClick = () => OnClick?.Invoke(this);
  73. longPressBtn.longPress = () => OnContinueClick?.Invoke(this);
  74. longPressBtn.onDragStart = OnDragStart;
  75. longPressBtn.onDrag = OnDrag;
  76. longPressBtn.onDragEnd = OnDragEnd;
  77. longPressBtn.onPointerEnter = OnPointerEnter;
  78. longPressBtn.onPointerExit = OnPointerExit;
  79. }
  80. if (own.transform.Find("Button") != null)
  81. {
  82. animation = own.transform.Find("Button").gameObject.GetComponent<ButtonAnimation>();
  83. if (animation == null)
  84. animation = own.transform.Find("Button").gameObject.AddComponent<ButtonAnimation>();
  85. animation.isEnabled = false;
  86. }
  87. base.AddButtonEvent();
  88. }
  89. private void OnPointerExit(PointerEventData obj)
  90. {
  91. onPointerExit?.Invoke(this);
  92. }
  93. private void OnPointerEnter(PointerEventData obj)
  94. {
  95. onPointerEnter?.Invoke(this);
  96. }
  97. private void OnDragStart(PointerEventData obj)
  98. {
  99. onDragStart?.Invoke(obj, this);
  100. }
  101. private void OnDrag(PointerEventData obj)
  102. {
  103. onDrag?.Invoke(obj, this);
  104. }
  105. private void OnDragEnd(PointerEventData obj)
  106. {
  107. onDragEnd?.Invoke(obj, this);
  108. }
  109. public override CTask<bool> AsyncInit(object[] uiData)
  110. {
  111. if (_rectTransform == null)
  112. {
  113. _rectTransform = GObjectPoolInterface.GetComponent<RectTransform>();
  114. }
  115. return base.AsyncInit(uiData);
  116. }
  117. public void SetBtnAnimation(bool isEnable)
  118. {
  119. if (animation)
  120. animation.isEnabled = isEnable;
  121. }
  122. public RectTransform Transform
  123. {
  124. get { return _rectTransform; }
  125. }
  126. public int index { get; set; }
  127. public virtual Vector2 GetSize()
  128. {
  129. return _rectTransform.sizeDelta;
  130. }
  131. public override void DormancyObj()
  132. {
  133. transform.GetComponent<RectTransform>().anchorMax = Vector2.one * 0.5f;
  134. transform.GetComponent<RectTransform>().anchorMin = Vector2.one * 0.5f;
  135. OnContinueClick = null;
  136. OnClick = null;
  137. base.DormancyObj();
  138. }
  139. public void OnPointerClick()
  140. {
  141. OnClick?.Invoke(this);
  142. }
  143. public void OnPointerContinueClick()
  144. {
  145. OnContinueClick?.Invoke(this);
  146. }
  147. }