PDFBookmarkListItem.cs 8.5 KB

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