Draggable_HandCard.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.UI;
  7. public class Draggable_HandCard : Draggable
  8. {
  9. public HandCard handCard;
  10. float shrink = 0.6f;
  11. public bool isExpand = false;
  12. public Vector3 startScale = new Vector3(1.2f, 1.2f, 1);
  13. public float DefaultY = 0;
  14. public bool CanPointerEnterExitAction { get; set; } = true;
  15. public void OnPointerEnter(BaseEventData eventData)
  16. {
  17. if (!CanPointerEnterExitAction)
  18. {
  19. return;
  20. }
  21. if (handCard.cardSource != null)
  22. {
  23. if (!handCard.cardSource.Owner.isYou)
  24. {
  25. return;
  26. }
  27. if (handCard.cardSource.Owner.HandTransform.GetComponent<HandContoller>() != null)
  28. {
  29. if (handCard.cardSource.Owner.HandTransform.GetComponent<HandContoller>().isDragging)
  30. {
  31. return;
  32. }
  33. }
  34. for (int i = 0; i < this.transform.parent.childCount; i++)
  35. {
  36. if (this.transform.parent == handCard.cardSource.Owner.HandTransform || this.transform.parent == GManager.instance.selectCardPanel.scrollRect.content)
  37. {
  38. if (this.transform.parent.GetChild(i) == this.transform)
  39. {
  40. this.isExpand = true;
  41. this.transform.localScale = startScale * 1.2f;
  42. this.transform.GetComponent<RectTransform>().pivot = new Vector2(0.5f, 0.08f);
  43. }
  44. else
  45. {
  46. this.transform.parent.GetChild(i).GetComponent<Draggable_HandCard>().isExpand = false;
  47. this.transform.parent.GetChild(i).localScale = startScale;
  48. this.transform.parent.GetChild(i).GetComponent<RectTransform>().pivot = new Vector2(0.5f, 0.5f);
  49. }
  50. }
  51. }
  52. }
  53. }
  54. public void OnPointerExit(BaseEventData eventData)
  55. {
  56. if (!CanPointerEnterExitAction)
  57. {
  58. return;
  59. }
  60. if (handCard.cardSource != null)
  61. {
  62. if (!handCard.cardSource.Owner.isYou)
  63. {
  64. return;
  65. }
  66. if (handCard.cardSource.Owner.HandTransform.GetComponent<HandContoller>() != null)
  67. {
  68. if (handCard.cardSource.Owner.HandTransform.GetComponent<HandContoller>().isDragging)
  69. {
  70. return;
  71. }
  72. }
  73. if (isExpand)
  74. {
  75. if (this.transform.parent == handCard.cardSource.Owner.HandTransform || this.transform.parent == GManager.instance.selectCardPanel.scrollRect.content)
  76. {
  77. for (int i = 0; i < this.transform.parent.childCount; i++)
  78. {
  79. this.transform.parent.GetChild(i).GetComponent<Draggable_HandCard>().isExpand = false;
  80. this.transform.parent.GetChild(i).localScale = startScale;
  81. this.transform.parent.GetChild(i).GetComponent<RectTransform>().pivot = new Vector2(0.5f, 0.5f);
  82. this.transform.parent.GetChild(i).transform.localPosition = new Vector3(this.transform.parent.GetChild(i).transform.localPosition.x, DefaultY, 0);
  83. }
  84. }
  85. }
  86. }
  87. }
  88. public override void OnBeginDrag(BaseEventData eventData)
  89. {
  90. if (handCard.cardSource != null)
  91. {
  92. if (!handCard.cardSource.Owner.isYou)
  93. {
  94. return;
  95. }
  96. if (handCard.CanDrag)
  97. {
  98. base.OnBeginDrag(eventData);
  99. isExpand = false;
  100. handCard.cardSource.Owner.HandTransform.GetComponent<HandContoller>().isDragging = true;
  101. this.GetComponent<RectTransform>().pivot = new Vector2(0.5f, 0.5f);
  102. this.transform.localScale = startScale * shrink;
  103. handCard.CardImage.color = new Color(1, 1, 1, 0.27f);
  104. for (int i = 0; i < handCard.cardSource.Owner.HandTransform.childCount; i++)
  105. {
  106. if (handCard.cardSource.Owner.HandTransform.GetChild(i).GetComponent<Draggable_HandCard>() != this)
  107. {
  108. handCard.cardSource.Owner.HandTransform.GetChild(i).GetComponent<Draggable_HandCard>().isExpand = false;
  109. handCard.cardSource.Owner.HandTransform.GetChild(i).localScale = startScale;
  110. handCard.cardSource.Owner.HandTransform.GetChild(i).GetComponent<RectTransform>().pivot = new Vector2(0.5f, 0.5f);
  111. handCard.cardSource.Owner.HandTransform.GetChild(i).transform.localPosition = new Vector3(handCard.cardSource.Owner.HandTransform.GetChild(i).transform.localPosition.x, DefaultY, 0);
  112. }
  113. }
  114. oldChildIndex = this.transform.GetSiblingIndex();
  115. oldParent = this.transform.parent;
  116. if (oldParent != null)
  117. {
  118. if (oldParent.GetComponent<GridLayoutGroup>() != null)
  119. {
  120. oldParent.GetComponent<GridLayoutGroup>().enabled = false;
  121. }
  122. }
  123. this.transform.SetSiblingIndex(this.transform.parent.childCount - 1);
  124. handCard.BeginDragAction?.Invoke(handCard);
  125. }
  126. }
  127. }
  128. public override void OnDrag(BaseEventData eventData)
  129. {
  130. if (handCard.cardSource != null)
  131. {
  132. if (!handCard.cardSource.Owner.isYou)
  133. {
  134. return;
  135. }
  136. if (!handCard.cardSource.Owner.HandTransform.GetComponent<HandContoller>().isDragging)
  137. {
  138. return;
  139. }
  140. if (handCard.CanDrag)
  141. {
  142. base.OnDrag(eventData);
  143. this.transform.SetParent(GManager.instance.You.HandTransform);
  144. this.transform.localScale = startScale * shrink;
  145. handCard.CardImage.color = new Color(1, 1, 1, 0.27f);
  146. if (GetRaycastArea() != null)
  147. {
  148. handCard.OnDragAction?.Invoke(GetRaycastArea());
  149. }
  150. }
  151. }
  152. }
  153. public override void OnEndDrag(BaseEventData eventData)
  154. {
  155. if (handCard.cardSource != null)
  156. {
  157. if (!handCard.cardSource.Owner.isYou)
  158. {
  159. return;
  160. }
  161. if (!handCard.cardSource.Owner.HandTransform.GetComponent<HandContoller>().isDragging)
  162. {
  163. return;
  164. }
  165. if (handCard.CanDrag)
  166. {
  167. base.OnEndDrag(eventData);
  168. handCard.cardSource.Owner.HandTransform.GetComponent<HandContoller>().isDragging = false;
  169. this.GetComponent<RectTransform>().pivot = new Vector2(0.5f, 0.5f);
  170. this.transform.localScale = startScale;
  171. handCard.CardImage.color = new Color(1, 1, 1, 1);
  172. this.transform.SetParent(GManager.instance.canvas.transform);
  173. if (GetRaycastArea() != null)
  174. {
  175. handCard.EndDragAction?.Invoke(GetRaycastArea());
  176. }
  177. }
  178. }
  179. }
  180. }