PDFBookmarkListItem.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.UI;
  6. namespace Paroxe.PdfRenderer.Internal.Viewer
  7. {
  8. public class PDFBookmarkListItem : UIBehaviour
  9. {
  10. public Sprite m_CollapseSprite;
  11. public Image m_ExpandImage;
  12. public Sprite m_ExpandSprite;
  13. public Image m_Highlighted;
  14. public RectTransform m_HorizontalLine;
  15. public RectTransform m_Internal;
  16. public RectTransform m_NextSibling;
  17. public Text m_Title;
  18. public RectTransform m_VerticalLine;
  19. public RectTransform m_VerticalLine2;
  20. #if !UNITY_WEBGL
  21. private CanvasGroup m_CanvasGroup;
  22. private List<PDFBookmarkListItem> m_ChildrenItems;
  23. private bool m_Expanded;
  24. private bool m_Initialized;
  25. private bool m_IsLastSibling = false;
  26. private float m_LastClickTimestamp = 0.0f;
  27. private LayoutElement m_LayoutElement;
  28. private PDFBookmark m_PDFBookmark;
  29. private RectTransform m_RectTransform;
  30. private int m_SizeAdjusted = 0;
  31. private PDFBookmarksViewer m_BooksmarkViewer;
  32. public void Initilize(PDFBookmark bookmark, int indent, bool lastSibling)
  33. {
  34. m_ChildrenItems = new List<PDFBookmarkListItem>();
  35. m_IsLastSibling = lastSibling;
  36. m_PDFBookmark = bookmark;
  37. m_HorizontalLine.gameObject.SetActive(true);
  38. m_VerticalLine.gameObject.SetActive(false);
  39. m_VerticalLine2.gameObject.SetActive(true);
  40. m_ExpandImage.gameObject.SetActive(true);
  41. if (m_PDFBookmark.IsTopLevelBookmark && m_PDFBookmark.ChildCount == 0)
  42. {
  43. m_HorizontalLine.gameObject.SetActive(false);
  44. m_ExpandImage.gameObject.SetActive(false);
  45. }
  46. else if (m_PDFBookmark.ChildCount > 0)
  47. {
  48. m_HorizontalLine.gameObject.SetActive(true);
  49. m_ExpandImage.gameObject.SetActive(true);
  50. }
  51. else if (!m_PDFBookmark.IsTopLevelBookmark)
  52. {
  53. m_HorizontalLine.gameObject.SetActive(true);
  54. m_HorizontalLine.offsetMin = new Vector2(m_HorizontalLine.offsetMin.x + 6, m_HorizontalLine.offsetMin.y);
  55. if (!m_IsLastSibling)
  56. {
  57. m_VerticalLine2.gameObject.SetActive(true);
  58. }
  59. m_ExpandImage.gameObject.SetActive(false);
  60. }
  61. m_Title.text = m_PDFBookmark.GetTitle();
  62. name = m_Title.text.Substring(0, Mathf.Min(24, m_Title.text.Length));
  63. for (int i = 0; i < m_PDFBookmark.ChildCount; ++i)
  64. {
  65. PDFBookmark child = m_PDFBookmark.GetChild(i);
  66. PDFBookmarkListItem item = Instantiate(gameObject).GetComponent<PDFBookmarkListItem>();
  67. m_ChildrenItems.Add(item);
  68. RectTransform itemTransform = item.transform as RectTransform;
  69. itemTransform.SetParent(transform.parent, false);
  70. itemTransform.localScale = Vector3.one;
  71. itemTransform.anchorMin = new Vector2(0.0f, 1.0f);
  72. itemTransform.anchorMax = new Vector2(0.0f, 1.0f);
  73. itemTransform.offsetMin = Vector2.zero;
  74. itemTransform.offsetMax = Vector2.zero;
  75. item.Initilize(child, indent + 1, i == m_PDFBookmark.ChildCount - 1);
  76. if (indent == 0)
  77. {
  78. StartCoroutine(SetVisible());
  79. item.m_CanvasGroup.alpha = 0.0f;
  80. }
  81. }
  82. for (int i = 0; i < m_ChildrenItems.Count - 1; ++i)
  83. {
  84. m_ChildrenItems[i].m_NextSibling = m_ChildrenItems[i + 1].gameObject.transform as RectTransform;
  85. }
  86. m_Internal.offsetMin = new Vector2(20.0f * indent, m_Internal.offsetMin.y);
  87. m_Initialized = true;
  88. m_SizeAdjusted = 1;
  89. }
  90. public void OnExpandButton()
  91. {
  92. if (m_PDFBookmark.ChildCount > 0)
  93. {
  94. m_Expanded = !m_Expanded;
  95. m_VerticalLine.gameObject.SetActive(m_Expanded);
  96. m_ExpandImage.sprite = m_Expanded ? m_CollapseSprite : m_ExpandSprite;
  97. foreach (PDFBookmarkListItem child in m_ChildrenItems)
  98. {
  99. child.SetState(m_Expanded);
  100. }
  101. }
  102. }
  103. public void OnItemClicked()
  104. {
  105. if (Time.fixedTime - m_LastClickTimestamp < 0.5f)
  106. {
  107. OnExpandButton();
  108. }
  109. m_LastClickTimestamp = Time.fixedTime;
  110. if (m_BooksmarkViewer == null)
  111. m_BooksmarkViewer = GetComponentInParent<PDFBookmarksViewer>();
  112. if (m_BooksmarkViewer.m_LastHighlightedImage != null)
  113. {
  114. m_BooksmarkViewer.m_LastHighlightedImage.gameObject.SetActive(false);
  115. }
  116. m_Highlighted.gameObject.SetActive(true);
  117. m_BooksmarkViewer.m_LastHighlightedImage = m_Highlighted;
  118. m_PDFBookmark.ExecuteBookmarkAction();
  119. }
  120. public void SetState(bool active)
  121. {
  122. gameObject.SetActive(active);
  123. if (active)
  124. {
  125. StartCoroutine(SetVisible());
  126. }
  127. if (m_ChildrenItems.Count > 0)
  128. {
  129. m_VerticalLine.gameObject.SetActive(!active);
  130. m_ExpandImage.sprite = !active ? m_CollapseSprite : m_ExpandSprite;
  131. }
  132. if (!active)
  133. {
  134. foreach (PDFBookmarkListItem child in m_ChildrenItems)
  135. {
  136. child.SetState(active);
  137. }
  138. }
  139. }
  140. protected override void OnEnable()
  141. {
  142. m_RectTransform = transform as RectTransform;
  143. m_LayoutElement = GetComponent<LayoutElement>();
  144. m_CanvasGroup = GetComponent<CanvasGroup>();
  145. if (m_CanvasGroup == null)
  146. {
  147. m_CanvasGroup = gameObject.AddComponent<CanvasGroup>();
  148. m_CanvasGroup.interactable = true;
  149. m_CanvasGroup.blocksRaycasts = true;
  150. }
  151. m_CanvasGroup.alpha = 1.0f;
  152. }
  153. protected override void OnDestroy()
  154. {
  155. base.OnDestroy();
  156. m_PDFBookmark = null;
  157. m_ChildrenItems = null;
  158. if (m_BooksmarkViewer != null)
  159. {
  160. if (m_BooksmarkViewer.m_LastHighlightedImage == this)
  161. m_BooksmarkViewer.m_LastHighlightedImage = null;
  162. m_BooksmarkViewer = null;
  163. }
  164. }
  165. private IEnumerator SetVisible()
  166. {
  167. m_CanvasGroup.alpha = 0.0f;
  168. yield return new WaitForSeconds(1.0f / 60.0f);
  169. m_CanvasGroup.alpha = 1.0f;
  170. }
  171. void Update()
  172. {
  173. if (m_Initialized && m_SizeAdjusted == 0)
  174. {
  175. m_LayoutElement.preferredHeight = m_Title.preferredHeight + 20.0f;
  176. if (m_VerticalLine.gameObject.activeInHierarchy)
  177. {
  178. m_VerticalLine.sizeDelta = new Vector2(1.0f, m_LayoutElement.preferredHeight);
  179. }
  180. if (!m_PDFBookmark.IsTopLevelBookmark)
  181. {
  182. gameObject.SetActive(false);
  183. }
  184. m_SizeAdjusted = -1;
  185. }
  186. else if (m_SizeAdjusted > 0)
  187. {
  188. m_SizeAdjusted--;
  189. }
  190. if (m_Initialized && m_SizeAdjusted <= -1)
  191. {
  192. m_LayoutElement.preferredHeight = m_Title.preferredHeight + 20.0f;
  193. if (m_VerticalLine.gameObject.activeInHierarchy)
  194. {
  195. m_VerticalLine.sizeDelta = new Vector2(1.0f, m_LayoutElement.preferredHeight);
  196. }
  197. if (m_NextSibling != null)
  198. {
  199. if (m_VerticalLine2.gameObject.activeInHierarchy)
  200. {
  201. float newHeight =
  202. Mathf.Abs((m_NextSibling.anchoredPosition - m_RectTransform.anchoredPosition).y);
  203. m_VerticalLine2.sizeDelta = new Vector2(1.0f, newHeight);
  204. }
  205. }
  206. else if (m_VerticalLine2.gameObject.activeInHierarchy)
  207. {
  208. m_VerticalLine2.gameObject.SetActive(false);
  209. }
  210. if (m_SizeAdjusted == -1)
  211. {
  212. m_SizeAdjusted = -2;
  213. }
  214. }
  215. }
  216. #endif
  217. }
  218. }