123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using UnityEngine.UI;
- namespace Paroxe.PdfRenderer.Internal.Viewer
- {
- public class PDFBookmarksViewer : UIBehaviour
- {
- public RectTransform m_BooksmarksContainer;
- public PDFBookmarkListItem m_ItemPrefab;
- public Image m_LastHighlightedImage;
- #if !UNITY_WEBGL
- private CanvasGroup m_CanvasGroup;
- private bool m_Initialized = false;
- private RectTransform m_LeftPanel;
- private bool m_Loaded = false;
- private PDFDocument m_Document;
- private PDFViewer m_PDFViewer;
- private RectTransform m_RectTransform;
- private List<RectTransform> m_TopLevelItems;
- #endif
- private WeakDeviceReference m_WeakDeviceReference;
- #if !UNITY_WEBGL
- private PDFBookmark m_RootBookmark;
- #endif
- public void DoUpdate()
- {
- #if !UNITY_WEBGL
- if (m_Initialized)
- {
- float containerHeight = 0.0f;
- foreach (RectTransform child in m_TopLevelItems)
- {
- containerHeight += child.sizeDelta.y;
- }
- }
- if (m_RectTransform != null && m_LeftPanel != null &&
- m_RectTransform.sizeDelta.x != m_LeftPanel.sizeDelta.x - 24.0f)
- {
- m_RectTransform.sizeDelta = new Vector2(m_LeftPanel.sizeDelta.x - 24.0f, m_RectTransform.sizeDelta.y);
- }
- #endif
- }
- private void Cleanup()
- {
- #if !UNITY_WEBGL
- if (m_Loaded)
- {
- m_Loaded = false;
- m_Initialized = false;
- m_TopLevelItems = null;
- m_Document = null;
- m_WeakDeviceReference.Detach();
- m_WeakDeviceReference = null;
- m_RootBookmark = null;
- bool isNotFirst = false;
- foreach (Transform child in m_BooksmarksContainer.transform)
- {
- if (isNotFirst)
- Destroy(child.gameObject);
- else
- isNotFirst = true;
- }
- m_ItemPrefab.gameObject.SetActive(false);
- m_CanvasGroup.alpha = 0.0f;
- PDFLibrary.Instance.ForceGabageCollection();
- }
- #endif
- }
- public void OnDocumentLoaded(PDFDocument document)
- {
- #if !UNITY_WEBGL
- if (!m_Loaded && gameObject.activeInHierarchy)
- {
- m_Loaded = true;
- m_Document = document;
- m_TopLevelItems = new List<RectTransform>();
- m_RectTransform = transform as RectTransform;
- m_LeftPanel = transform.parent as RectTransform;
- PDFViewer viewer = GetComponentInParent<PDFViewer>();
- if (m_RootBookmark == null)
- {
- if (m_WeakDeviceReference == null)
- m_WeakDeviceReference = new WeakDeviceReference(viewer);
- m_RootBookmark = m_Document.GetRootBookmark(m_WeakDeviceReference);
- }
-
- if (m_RootBookmark != null)
- {
- m_ItemPrefab.gameObject.SetActive(true);
- foreach (PDFBookmark child in m_RootBookmark.EnumerateChildrenBookmarks())
- {
- PDFBookmarkListItem item = null;
- item = Instantiate(m_ItemPrefab.gameObject).GetComponent<PDFBookmarkListItem>();
- RectTransform itemTransform = item.transform as RectTransform;
- itemTransform.SetParent(m_BooksmarksContainer, false);
- itemTransform.localScale = Vector3.one;
- itemTransform.anchorMin = new Vector2(0.0f, 1.0f);
- itemTransform.anchorMax = new Vector2(0.0f, 1.0f);
- itemTransform.offsetMin = Vector2.zero;
- itemTransform.offsetMax = Vector2.zero;
- m_TopLevelItems.Add(item.transform as RectTransform);
- item.Initilize(child, 0, false);
- }
- m_ItemPrefab.gameObject.SetActive(false);
- m_Initialized = true;
- if (GetComponentInParent<PDFViewerLeftPanel>().m_Thumbnails.gameObject.GetComponent<CanvasGroup>().alpha == 0.0f)
- StartCoroutine(DelayedShow());
- }
- }
- #endif
- }
- #if !UNITY_WEBGL
- IEnumerator DelayedShow()
- {
- yield return null;
- yield return null;
- yield return null;
- m_CanvasGroup.alpha = 1.0f;
- }
- #endif
- public void OnDocumentUnloaded()
- {
- #if !UNITY_WEBGL
- Cleanup();
- #endif
- }
- #if !UNITY_WEBGL
- protected override void OnDisable()
- {
- base.OnDisable();
- if (m_Loaded)
- {
- Cleanup();
- }
- }
- #endif
- #if !UNITY_WEBGL
- protected override void OnEnable()
- {
- base.OnEnable();
- DoOnEnable();
- }
- #endif
- public void DoOnEnable()
- {
- #if !UNITY_WEBGL
- if (m_PDFViewer == null)
- m_PDFViewer = GetComponentInParent<PDFViewer>();
- if (m_CanvasGroup == null)
- m_CanvasGroup = GetComponent<CanvasGroup>();
- if (m_RectTransform == null)
- m_RectTransform = transform as RectTransform;
- m_ItemPrefab.gameObject.SetActive(false);
- m_CanvasGroup.alpha = 0.0f;
- if (!m_Loaded && m_PDFViewer.Document != null && m_PDFViewer.Document.IsValid)
- OnDocumentLoaded(m_PDFViewer.Document);
- #endif
- }
- private class WeakDeviceReference : IPDFDevice
- {
- private IPDFDevice m_Device;
- public WeakDeviceReference(IPDFDevice device)
- {
- m_Device = device;
- }
- public void Detach()
- {
- m_Device = null;
- }
- bool IPDFDevice.AllowOpenURL
- {
- get { return m_Device.AllowOpenURL; }
- set { m_Device.AllowOpenURL = value; }
- }
- IPDFDeviceActionHandler IPDFDevice.BookmarksActionHandler
- {
- get { return m_Device.BookmarksActionHandler; }
- set { m_Device.BookmarksActionHandler = value; }
- }
- PDFDocument IPDFDevice.Document
- {
- get { return m_Device.Document; }
- }
- IPDFDeviceActionHandler IPDFDevice.LinksActionHandler
- {
- get { return m_Device.LinksActionHandler; }
- set { m_Device.LinksActionHandler = value; }
- }
- Vector2 IPDFDevice.GetDevicePageSize(int pageIndex)
- {
- return m_Device.GetDevicePageSize(pageIndex);
- }
- void IPDFDevice.GoToPage(int pageIndex)
- {
- m_Device.GoToPage(pageIndex);
- }
- #if !UNITY_WEBGL
- void IPDFDevice.LoadDocument(PDFDocument document, int pageIndex)
- {
- m_Device.LoadDocument(document, pageIndex);
- }
- void IPDFDevice.LoadDocument(PDFDocument document, string password, int pageIndex)
- {
- m_Device.LoadDocument(document, password, pageIndex);
- }
- void IPDFDevice.LoadDocumentFromAsset(PDFAsset pdfAsset, int pageIndex)
- {
- m_Device.LoadDocumentFromAsset(pdfAsset, pageIndex);
- }
- void IPDFDevice.LoadDocumentFromAsset(PDFAsset pdfAsset, string password, int pageIndex)
- {
- m_Device.LoadDocumentFromAsset(pdfAsset, password, pageIndex);
- }
- void IPDFDevice.LoadDocumentFromBuffer(byte[] buffer, int pageIndex)
- {
- m_Device.LoadDocumentFromBuffer(buffer, pageIndex);
- }
- void IPDFDevice.LoadDocumentFromBuffer(byte[] buffer, string password, int pageIndex)
- {
- m_Device.LoadDocumentFromBuffer(buffer, password, pageIndex);
- }
- void IPDFDevice.LoadDocumentFromFile(string filePath, int pageIndex)
- {
- m_Device.LoadDocumentFromFile(filePath, pageIndex);
- }
- void IPDFDevice.LoadDocumentFromFile(string filePath, string password, int pageIndex)
- {
- m_Device.LoadDocumentFromFile(filePath, password, pageIndex);
- }
- void IPDFDevice.LoadDocumentFromPersistentData(string folder, string fileName, int pageIndex)
- {
- m_Device.LoadDocumentFromFile(folder, fileName, pageIndex);
- }
- void IPDFDevice.LoadDocumentFromPersistentData(string folder, string fileName, string password, int pageIndex)
- {
- m_Device.LoadDocumentFromPersistentData(folder, fileName, password, pageIndex);
- }
- void IPDFDevice.LoadDocumentFromResources(string folder, string fileName, int pageIndex)
- {
- m_Device.LoadDocumentFromResources(folder, fileName, pageIndex);
- }
- void IPDFDevice.LoadDocumentFromResources(string folder, string fileName, string password, int pageIndex)
- {
- m_Device.LoadDocumentFromResources(folder, fileName, password, pageIndex);
- }
- void IPDFDevice.LoadDocumentFromStreamingAssets(string folder, string fileName, int pageIndex)
- {
- m_Device.LoadDocumentFromStreamingAssets(folder, fileName, pageIndex);
- }
- void IPDFDevice.LoadDocumentFromStreamingAssets(string folder, string fileName, string password, int pageIndex)
- {
- m_Device.LoadDocumentFromStreamingAssets(folder, fileName, password, pageIndex);
- }
- void IPDFDevice.LoadDocumentFromWeb(string url, int pageIndex)
- {
- m_Device.LoadDocumentFromWeb(url, pageIndex);
- }
- void IPDFDevice.LoadDocumentFromWeb(string url, string password, int pageIndex)
- {
- m_Device.LoadDocumentFromWeb(url, password, pageIndex);
- }
- #endif
- }
- }
- }
|