PDFThumbnailsViewer.cs 7.9 KB

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