PDFThumbnailsViewer.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. namespace Paroxe.PdfRenderer.Internal.Viewer
  6. {
  7. public class PDFThumbnailsViewer : UIBehaviour
  8. {
  9. public PDFThumbnailItem m_ThumbnailItemPrefab;
  10. public RectTransform m_ThumbnailsContainer;
  11. private PDFPageRange m_CurrentPageRange;
  12. private PDFThumbnailItem m_HighlightedItem;
  13. private RectTransform m_LeftPanel;
  14. private PDFPageTextureHolder[] m_PageTextureHolders;
  15. private PDFDocument m_Document;
  16. private PDFViewer m_PDFViewer;
  17. private RectTransform m_RectTransform;
  18. private List<PDFThumbnailItem> m_ThumbnailList;
  19. private PDFViewerLeftPanel m_ViewerLeftPanel;
  20. private bool m_IsLoaded = false;
  21. private int m_UpdateFramesDelay;
  22. private void Cleanup()
  23. {
  24. m_IsLoaded = false;
  25. m_Document = null;
  26. m_HighlightedItem = null;
  27. if (m_PageTextureHolders != null)
  28. {
  29. foreach (PDFPageTextureHolder holder in m_PageTextureHolders)
  30. {
  31. if (holder.Texture != null)
  32. {
  33. Texture2D tex = holder.Texture;
  34. holder.Texture = null;
  35. Destroy(tex);
  36. }
  37. }
  38. }
  39. m_PageTextureHolders = null;
  40. bool isNotFirst = false;
  41. foreach (Transform child in m_ThumbnailItemPrefab.transform.parent)
  42. {
  43. if (isNotFirst)
  44. Destroy(child.gameObject);
  45. else
  46. isNotFirst = true;
  47. }
  48. m_ThumbnailItemPrefab.gameObject.SetActive(false);
  49. }
  50. private static bool Intersect(Rect box0, Rect box1)
  51. {
  52. if (box0.xMax < box1.xMin || box0.xMin > box1.xMax) return false;
  53. if (box0.yMax < box1.yMin || box0.yMin > box1.yMax) return false;
  54. return true;
  55. }
  56. private PDFPageRange GetVisiblePageRange()
  57. {
  58. PDFPageRange pageRange = new PDFPageRange();
  59. int c = m_ThumbnailsContainer.childCount - 1;
  60. for (int i = 0; i < c; ++i)
  61. {
  62. RectTransform rt = m_ThumbnailsContainer.GetChild(i + 1) as RectTransform;
  63. Rect pageRect = new Rect(-m_ThumbnailsContainer.anchoredPosition - rt.anchoredPosition, rt.rect.size);
  64. Rect viewportRect = new Rect(new Vector2(0.0f, (transform as RectTransform).rect.size.y * 0.5f), (transform as RectTransform).rect.size);
  65. pageRect.position = Vector2.zero;
  66. viewportRect.position = Vector2.zero;
  67. pageRect.center = -m_ThumbnailsContainer.anchoredPosition - rt.anchoredPosition;
  68. viewportRect.center = new Vector2(0.0f, (transform as RectTransform).rect.size.y * 0.5f);
  69. if (Intersect(pageRect, viewportRect))
  70. {
  71. if (pageRange.m_From == -1)
  72. {
  73. pageRange.m_From = i;
  74. }
  75. else
  76. {
  77. pageRange.m_To = i + 1;
  78. }
  79. }
  80. else if (pageRange.m_From != -1)
  81. {
  82. break;
  83. }
  84. }
  85. if (pageRange.m_From != -1 && pageRange.m_To == -1)
  86. {
  87. pageRange.m_To = pageRange.m_From + 1;
  88. }
  89. return pageRange;
  90. }
  91. public void OnDocumentUnloaded()
  92. {
  93. Cleanup();
  94. }
  95. public void OnDocumentLoaded(PDFDocument document)
  96. {
  97. if (!m_IsLoaded && gameObject.activeInHierarchy)
  98. {
  99. m_Document = document;
  100. int c = m_Document.GetPageCount();
  101. m_PageTextureHolders = new PDFPageTextureHolder[c];
  102. m_ThumbnailList = new List<PDFThumbnailItem>();
  103. int currentPage = m_PDFViewer.CurrentPageIndex;
  104. m_ThumbnailItemPrefab.gameObject.SetActive(false);
  105. for (int i = 0; i < c; ++i)
  106. {
  107. PDFThumbnailItem item = null;
  108. item = ((GameObject)Instantiate(m_ThumbnailItemPrefab.gameObject)).GetComponent<PDFThumbnailItem>();
  109. item.transform.SetParent(m_ThumbnailItemPrefab.transform.parent, false);
  110. item.gameObject.SetActive(true);
  111. item.m_Highlighted.gameObject.SetActive(false);
  112. item.m_PageIndexLabel.text = (i + 1).ToString();
  113. m_ThumbnailList.Add(item);
  114. m_PageTextureHolders[i] = new PDFPageTextureHolder();
  115. m_PageTextureHolders[i].m_PageIndex = i;
  116. m_PageTextureHolders[i].m_Page = item.m_PageThumbnailRawImage.gameObject;
  117. m_PageTextureHolders[i].m_PDFViewer = m_PDFViewer;
  118. m_PageTextureHolders[i].Texture = null;
  119. }
  120. if (currentPage >= 0 && currentPage < m_PDFViewer.Document.GetPageCount())
  121. {
  122. m_HighlightedItem = m_ThumbnailList[currentPage];
  123. m_HighlightedItem.m_Highlighted.gameObject.SetActive(true);
  124. }
  125. m_CurrentPageRange = new PDFPageRange();
  126. m_UpdateFramesDelay = 2;
  127. m_IsLoaded = true;
  128. }
  129. }
  130. public void OnCurrentPageChanged(int newPageIndex)
  131. {
  132. if (!m_IsLoaded)
  133. return;
  134. if (m_HighlightedItem != null)
  135. {
  136. m_HighlightedItem.m_Highlighted.gameObject.SetActive(false);
  137. }
  138. if (newPageIndex >= 0)
  139. {
  140. m_HighlightedItem = m_ThumbnailList[newPageIndex];
  141. m_HighlightedItem.m_Highlighted.gameObject.SetActive(true);
  142. }
  143. UpdateHighlightedItem();
  144. }
  145. protected override void OnDisable()
  146. {
  147. base.OnDisable();
  148. if (m_IsLoaded)
  149. {
  150. Cleanup();
  151. }
  152. }
  153. protected override void OnEnable()
  154. {
  155. base.OnEnable();
  156. DoOnEnable();
  157. }
  158. public void DoOnEnable()
  159. {
  160. if (m_PDFViewer == null)
  161. m_PDFViewer = GetComponentInParent<PDFViewer>();
  162. if (m_RectTransform == null)
  163. m_RectTransform = transform as RectTransform;
  164. if (m_LeftPanel == null)
  165. m_LeftPanel = transform.parent as RectTransform;
  166. if (m_ViewerLeftPanel == null)
  167. m_ViewerLeftPanel = m_LeftPanel.GetComponent<PDFViewerLeftPanel>();
  168. if (m_CurrentPageRange == null)
  169. m_CurrentPageRange = new PDFPageRange();
  170. if (!m_IsLoaded
  171. && m_PDFViewer.Document != null
  172. && m_PDFViewer.Document.IsValid)
  173. {
  174. OnDocumentLoaded(m_PDFViewer.Document);
  175. }
  176. m_ThumbnailItemPrefab.gameObject.SetActive(false);
  177. }
  178. public void DoUpdate()
  179. {
  180. if (m_RectTransform.sizeDelta.x != m_LeftPanel.sizeDelta.x - 24.0f)
  181. m_RectTransform.sizeDelta = new Vector2(m_LeftPanel.sizeDelta.x - 24.0f, m_RectTransform.sizeDelta.y);
  182. if (!m_IsLoaded || !m_ViewerLeftPanel.IsOpened)
  183. {
  184. if (!m_ViewerLeftPanel.IsOpened)
  185. m_UpdateFramesDelay = 2;
  186. return;
  187. }
  188. if (m_UpdateFramesDelay > 0)
  189. {
  190. --m_UpdateFramesDelay;
  191. return;
  192. }
  193. PDFPageRange pageRange = GetVisiblePageRange();
  194. if (!Equals(pageRange, m_CurrentPageRange))
  195. {
  196. PDFPageRange.UpdatePageAgainstRanges(m_CurrentPageRange, pageRange, m_Document, m_PageTextureHolders, null, 0.25f, null, m_PDFViewer.GetCachedNormalPageSizes());
  197. m_CurrentPageRange = pageRange;
  198. }
  199. }
  200. private void UpdateHighlightedItem()
  201. {
  202. if (m_HighlightedItem != null)
  203. {
  204. m_HighlightedItem.m_Highlighted.color = new Color(152.0f / 255.0f, 192.0f / 255.0f, 217.0f / 255.0f, 1.0f);
  205. }
  206. else if (m_HighlightedItem != null)
  207. {
  208. m_HighlightedItem.m_Highlighted.color = new Color(200.0f / 255.0f, 200.0f / 255.0f, 200.0f / 255.0f, 1.0f);
  209. }
  210. }
  211. }
  212. }