PDFBookmarksViewer.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.UI;
  7. namespace Paroxe.PdfRenderer.Internal.Viewer
  8. {
  9. public class PDFBookmarksViewer : UIBehaviour
  10. {
  11. public RectTransform m_BooksmarksContainer;
  12. public PDFBookmarkListItem m_ItemPrefab;
  13. public Image m_LastHighlightedImage;
  14. #if !UNITY_WEBGL
  15. private CanvasGroup m_CanvasGroup;
  16. private bool m_Initialized = false;
  17. private RectTransform m_LeftPanel;
  18. private bool m_Loaded = false;
  19. private PDFDocument m_Document;
  20. private PDFViewer m_PDFViewer;
  21. private RectTransform m_RectTransform;
  22. private List<RectTransform> m_TopLevelItems;
  23. #endif
  24. private WeakDeviceReference m_WeakDeviceReference;
  25. #if !UNITY_WEBGL
  26. private PDFBookmark m_RootBookmark;
  27. #endif
  28. public void DoUpdate()
  29. {
  30. #if !UNITY_WEBGL
  31. if (m_Initialized)
  32. {
  33. float containerHeight = 0.0f;
  34. foreach (RectTransform child in m_TopLevelItems)
  35. {
  36. containerHeight += child.sizeDelta.y;
  37. }
  38. }
  39. if (m_RectTransform != null && m_LeftPanel != null &&
  40. m_RectTransform.sizeDelta.x != m_LeftPanel.sizeDelta.x - 24.0f)
  41. {
  42. m_RectTransform.sizeDelta = new Vector2(m_LeftPanel.sizeDelta.x - 24.0f, m_RectTransform.sizeDelta.y);
  43. }
  44. #endif
  45. }
  46. private void Cleanup()
  47. {
  48. #if !UNITY_WEBGL
  49. if (m_Loaded)
  50. {
  51. m_Loaded = false;
  52. m_Initialized = false;
  53. m_TopLevelItems = null;
  54. m_Document = null;
  55. m_WeakDeviceReference.Detach();
  56. m_WeakDeviceReference = null;
  57. m_RootBookmark = null;
  58. bool isNotFirst = false;
  59. foreach (Transform child in m_BooksmarksContainer.transform)
  60. {
  61. if (isNotFirst)
  62. Destroy(child.gameObject);
  63. else
  64. isNotFirst = true;
  65. }
  66. m_ItemPrefab.gameObject.SetActive(false);
  67. m_CanvasGroup.alpha = 0.0f;
  68. PDFLibrary.Instance.ForceGabageCollection();
  69. }
  70. #endif
  71. }
  72. public void OnDocumentLoaded(PDFDocument document)
  73. {
  74. #if !UNITY_WEBGL
  75. if (!m_Loaded && gameObject.activeInHierarchy)
  76. {
  77. m_Loaded = true;
  78. m_Document = document;
  79. m_TopLevelItems = new List<RectTransform>();
  80. m_RectTransform = transform as RectTransform;
  81. m_LeftPanel = transform.parent as RectTransform;
  82. PDFViewer viewer = GetComponentInParent<PDFViewer>();
  83. if (m_RootBookmark == null)
  84. {
  85. if (m_WeakDeviceReference == null)
  86. m_WeakDeviceReference = new WeakDeviceReference(viewer);
  87. m_RootBookmark = m_Document.GetRootBookmark(m_WeakDeviceReference);
  88. }
  89. if (m_RootBookmark != null)
  90. {
  91. m_ItemPrefab.gameObject.SetActive(true);
  92. foreach (PDFBookmark child in m_RootBookmark.EnumerateChildrenBookmarks())
  93. {
  94. PDFBookmarkListItem item = null;
  95. item = Instantiate(m_ItemPrefab.gameObject).GetComponent<PDFBookmarkListItem>();
  96. RectTransform itemTransform = item.transform as RectTransform;
  97. itemTransform.SetParent(m_BooksmarksContainer, false);
  98. itemTransform.localScale = Vector3.one;
  99. itemTransform.anchorMin = new Vector2(0.0f, 1.0f);
  100. itemTransform.anchorMax = new Vector2(0.0f, 1.0f);
  101. itemTransform.offsetMin = Vector2.zero;
  102. itemTransform.offsetMax = Vector2.zero;
  103. m_TopLevelItems.Add(item.transform as RectTransform);
  104. item.Initilize(child, 0, false);
  105. }
  106. m_ItemPrefab.gameObject.SetActive(false);
  107. m_Initialized = true;
  108. if (GetComponentInParent<PDFViewerLeftPanel>().m_Thumbnails.gameObject.GetComponent<CanvasGroup>().alpha == 0.0f)
  109. StartCoroutine(DelayedShow());
  110. }
  111. }
  112. #endif
  113. }
  114. #if !UNITY_WEBGL
  115. IEnumerator DelayedShow()
  116. {
  117. yield return null;
  118. yield return null;
  119. yield return null;
  120. m_CanvasGroup.alpha = 1.0f;
  121. }
  122. #endif
  123. public void OnDocumentUnloaded()
  124. {
  125. #if !UNITY_WEBGL
  126. Cleanup();
  127. #endif
  128. }
  129. #if !UNITY_WEBGL
  130. protected override void OnDisable()
  131. {
  132. base.OnDisable();
  133. if (m_Loaded)
  134. {
  135. Cleanup();
  136. }
  137. }
  138. #endif
  139. #if !UNITY_WEBGL
  140. protected override void OnEnable()
  141. {
  142. base.OnEnable();
  143. DoOnEnable();
  144. }
  145. #endif
  146. public void DoOnEnable()
  147. {
  148. #if !UNITY_WEBGL
  149. if (m_PDFViewer == null)
  150. m_PDFViewer = GetComponentInParent<PDFViewer>();
  151. if (m_CanvasGroup == null)
  152. m_CanvasGroup = GetComponent<CanvasGroup>();
  153. if (m_RectTransform == null)
  154. m_RectTransform = transform as RectTransform;
  155. m_ItemPrefab.gameObject.SetActive(false);
  156. m_CanvasGroup.alpha = 0.0f;
  157. if (!m_Loaded && m_PDFViewer.Document != null && m_PDFViewer.Document.IsValid)
  158. OnDocumentLoaded(m_PDFViewer.Document);
  159. #endif
  160. }
  161. private class WeakDeviceReference : IPDFDevice
  162. {
  163. private IPDFDevice m_Device;
  164. public WeakDeviceReference(IPDFDevice device)
  165. {
  166. m_Device = device;
  167. }
  168. public void Detach()
  169. {
  170. m_Device = null;
  171. }
  172. bool IPDFDevice.AllowOpenURL
  173. {
  174. get { return m_Device.AllowOpenURL; }
  175. set { m_Device.AllowOpenURL = value; }
  176. }
  177. IPDFDeviceActionHandler IPDFDevice.BookmarksActionHandler
  178. {
  179. get { return m_Device.BookmarksActionHandler; }
  180. set { m_Device.BookmarksActionHandler = value; }
  181. }
  182. PDFDocument IPDFDevice.Document
  183. {
  184. get { return m_Device.Document; }
  185. }
  186. IPDFDeviceActionHandler IPDFDevice.LinksActionHandler
  187. {
  188. get { return m_Device.LinksActionHandler; }
  189. set { m_Device.LinksActionHandler = value; }
  190. }
  191. Vector2 IPDFDevice.GetDevicePageSize(int pageIndex)
  192. {
  193. return m_Device.GetDevicePageSize(pageIndex);
  194. }
  195. void IPDFDevice.GoToPage(int pageIndex)
  196. {
  197. m_Device.GoToPage(pageIndex);
  198. }
  199. #if !UNITY_WEBGL
  200. void IPDFDevice.LoadDocument(PDFDocument document, int pageIndex)
  201. {
  202. m_Device.LoadDocument(document, pageIndex);
  203. }
  204. void IPDFDevice.LoadDocument(PDFDocument document, string password, int pageIndex)
  205. {
  206. m_Device.LoadDocument(document, password, pageIndex);
  207. }
  208. void IPDFDevice.LoadDocumentFromAsset(PDFAsset pdfAsset, int pageIndex)
  209. {
  210. m_Device.LoadDocumentFromAsset(pdfAsset, pageIndex);
  211. }
  212. void IPDFDevice.LoadDocumentFromAsset(PDFAsset pdfAsset, string password, int pageIndex)
  213. {
  214. m_Device.LoadDocumentFromAsset(pdfAsset, password, pageIndex);
  215. }
  216. void IPDFDevice.LoadDocumentFromBuffer(byte[] buffer, int pageIndex)
  217. {
  218. m_Device.LoadDocumentFromBuffer(buffer, pageIndex);
  219. }
  220. void IPDFDevice.LoadDocumentFromBuffer(byte[] buffer, string password, int pageIndex)
  221. {
  222. m_Device.LoadDocumentFromBuffer(buffer, password, pageIndex);
  223. }
  224. void IPDFDevice.LoadDocumentFromFile(string filePath, int pageIndex)
  225. {
  226. m_Device.LoadDocumentFromFile(filePath, pageIndex);
  227. }
  228. void IPDFDevice.LoadDocumentFromFile(string filePath, string password, int pageIndex)
  229. {
  230. m_Device.LoadDocumentFromFile(filePath, password, pageIndex);
  231. }
  232. void IPDFDevice.LoadDocumentFromPersistentData(string folder, string fileName, int pageIndex)
  233. {
  234. m_Device.LoadDocumentFromFile(folder, fileName, pageIndex);
  235. }
  236. void IPDFDevice.LoadDocumentFromPersistentData(string folder, string fileName, string password, int pageIndex)
  237. {
  238. m_Device.LoadDocumentFromPersistentData(folder, fileName, password, pageIndex);
  239. }
  240. void IPDFDevice.LoadDocumentFromResources(string folder, string fileName, int pageIndex)
  241. {
  242. m_Device.LoadDocumentFromResources(folder, fileName, pageIndex);
  243. }
  244. void IPDFDevice.LoadDocumentFromResources(string folder, string fileName, string password, int pageIndex)
  245. {
  246. m_Device.LoadDocumentFromResources(folder, fileName, password, pageIndex);
  247. }
  248. void IPDFDevice.LoadDocumentFromStreamingAssets(string folder, string fileName, int pageIndex)
  249. {
  250. m_Device.LoadDocumentFromStreamingAssets(folder, fileName, pageIndex);
  251. }
  252. void IPDFDevice.LoadDocumentFromStreamingAssets(string folder, string fileName, string password, int pageIndex)
  253. {
  254. m_Device.LoadDocumentFromStreamingAssets(folder, fileName, password, pageIndex);
  255. }
  256. void IPDFDevice.LoadDocumentFromWeb(string url, int pageIndex)
  257. {
  258. m_Device.LoadDocumentFromWeb(url, pageIndex);
  259. }
  260. void IPDFDevice.LoadDocumentFromWeb(string url, string password, int pageIndex)
  261. {
  262. m_Device.LoadDocumentFromWeb(url, password, pageIndex);
  263. }
  264. #endif
  265. }
  266. }
  267. }