123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- /*
- http://www.cgsoso.com/forum-211-1.html
- CG搜搜 Unity3d 每日Unity3d插件免费更新 更有VIP资源!
- CGSOSO 主打游戏开发,影视设计等CG资源素材。
- 插件如若商用,请务必官网购买!
- daily assets update for try.
- U should buy the asset from home store if u use it in your project!
- */
- using UnityEngine;
- using UnityEngine.EventSystems;
- namespace Paroxe.PdfRenderer.Internal.Viewer
- {
- #if UNITY_WEBGL
- public class PDFViewerPage : UIBehaviour
- {
- public Texture2D m_HandCursor;
- }
- #endif
- #if !UNITY_WEBGL
- public class PDFViewerPage : UIBehaviour, IPointerClickHandler
- #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
- , IPointerEnterHandler, IPointerExitHandler
- #endif
- {
- public Texture2D m_HandCursor;
- private PDFViewer m_PDFViewer;
- #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
- private bool m_PointerInside = false;
- private bool m_HandCursorSettedByMe = false;
- #endif
- private Camera m_CanvasCamera;
- public PDFPage m_Page;
- public void OnPointerClick(PointerEventData eventData)
- {
- if (m_PDFViewer == null)
- m_PDFViewer = GetComponentInParent<PDFViewer>();
- if (m_PDFViewer.Document == null)
- return;
- if (m_PDFViewer.LinksActionHandler == null)
- return;
- if (m_Page == null)
- m_PDFViewer.Document.GetPage(transform.GetSiblingIndex());
- PDFLink link = GetLinkAtPoint(eventData.pressPosition, eventData.pressEventCamera);
- if (link != null)
- PDFActionHandlerHelper.ExecuteLinkAction(m_PDFViewer, link);
- else if (m_PDFViewer.ParagraphZoomingEnable && eventData.clickCount == 2)
- {
- using (PDFTextPage textPage = m_Page.GetTextPage())
- {
- Rect pageRect = new Rect();
- Vector2 pos = eventData.pressPosition;
- RectTransformUtility.ScreenPointToLocalPointInRectangle(transform as RectTransform, pos, GetComponent<Camera>(), out pos);
- RectTransform rt = transform as RectTransform;
- pos += rt.sizeDelta.x * 0.5f * Vector2.right;
- pos += rt.sizeDelta.y * 0.5f * Vector2.up;
- pos = pos.x * (rt.sizeDelta.y / rt.sizeDelta.x) * Vector2.right + pos.y * Vector2.up;
- Vector2 pagePoint = m_Page.DeviceToPage(0, 0, (int)rt.sizeDelta.y, (int)rt.sizeDelta.y, PDFPage.PageRotation.Normal, (int)pos.x, (int)pos.y);
- Vector2 pageSize = m_Page.GetPageSize();
- float threshold = m_PDFViewer.ParagraphDetectionThreshold;
- string text = GetBoundedText(textPage, 0.0f, pagePoint.y + 0.0f, pageSize.x, pagePoint.y - 1.0f);
- if (!string.IsNullOrEmpty(text.Trim()) && text.Trim().Length > 4)
- {
- string prevText = text;
- float bottomOffset = 0.0f;
- float topOffset = 0.0f;
- float t = 0.0f;
- while (true)
- {
- bottomOffset += 2.0f;
- text = GetBoundedText(textPage, 0.0f, pagePoint.y + 0.0f, pageSize.x, pagePoint.y - bottomOffset);
- if (text == prevText)
- t += 2.0f;
- else
- t = 0.0f;
- if (t >= threshold)
- break;
- prevText = text;
- }
- t = 0.0f;
- while (true)
- {
- topOffset += 2.0f;
- text = GetBoundedText(textPage, 0.0f, pagePoint.y + topOffset, pageSize.x, pagePoint.y - bottomOffset);
- if (text == prevText)
- t += 2.0f;
- else
- t = 0.0f;
- if (t >= threshold)
- break;
- prevText = text;
- }
- pageRect = new Rect(0.0f, pagePoint.y + topOffset, pageSize.x, (pagePoint.y + topOffset) - (pagePoint.y - bottomOffset));
-
- m_PDFViewer.ZoomOnParagraph(this, pageRect);
- }
- }
- }
- }
- private string GetBoundedText(PDFTextPage textPage, float left, float top, float right, float bottom)
- {
- return textPage.GetBoundedText(left, top, right, bottom, 4096);
- }
- #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
- public void OnPointerEnter(PointerEventData eventData)
- {
- if (m_PDFViewer == null)
- {
- m_PDFViewer = GetComponentInParent<PDFViewer>();
- }
- if (m_PDFViewer.Document == null)
- {
- return;
- }
- m_PointerInside = true;
- if (m_Page == null)
- m_PDFViewer.Document.GetPage(transform.GetSiblingIndex());
- }
- public void OnPointerExit(PointerEventData eventData)
- {
- m_PointerInside = false;
- if (m_HandCursorSettedByMe)
- {
- Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
- }
- if (m_Page != null)
- m_Page.Dispose();
- m_Page = null;
- }
- #endif
- protected override void OnEnable()
- {
- if (m_PDFViewer == null)
- m_PDFViewer = GetComponentInParent<PDFViewer>();
- if (m_CanvasCamera == null)
- m_CanvasCamera = FindCanvasCamera(transform as RectTransform);
- #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
- m_PointerInside = false;
- #endif
- }
- protected override void OnDisable()
- {
- if (m_Page != null)
- m_Page.Dispose();
- m_Page = null;
- }
- private Camera FindCanvasCamera(RectTransform rt)
- {
- RectTransform parent = rt.parent as RectTransform;
- if (parent != null)
- {
- Canvas canvas = GetComponent<Canvas>();
- if (canvas != null && canvas.worldCamera != null)
- {
- return canvas.worldCamera;
- }
- return FindCanvasCamera(parent);
- }
- return null;
- }
- #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
- void Update()
- {
- if (m_HandCursor == null)
- {
- return;
- }
- if (m_CanvasCamera == null)
- {
- m_CanvasCamera = FindCanvasCamera(transform as RectTransform);
- }
- if (m_PointerInside)
- {
- PDFLink link = GetLinkAtPoint(Input.mousePosition, m_CanvasCamera);
- if (link != null)
- {
- Cursor.SetCursor(m_HandCursor, new Vector2(6.0f, 0.0f), CursorMode.Auto);
- m_HandCursorSettedByMe = true;
- }
- else
- {
- Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
- }
- }
- }
- #endif
- private PDFLink GetLinkAtPoint(Vector2 point, Camera camera)
- {
- if (m_PDFViewer == null)
- m_PDFViewer = GetComponentInParent<PDFViewer>();
- if (m_Page == null)
- m_Page = m_PDFViewer.Document.GetPage(transform.GetSiblingIndex());
- Vector2 localPointerPosition;
- if (RectTransformUtility.ScreenPointToLocalPointInRectangle(transform as RectTransform, point, camera,
- out localPointerPosition))
- {
- RectTransform rt = transform as RectTransform;
- localPointerPosition += rt.sizeDelta.x*0.5f*Vector2.right;
- localPointerPosition += rt.sizeDelta.y*0.5f*Vector2.up;
- localPointerPosition = localPointerPosition.x*(rt.sizeDelta.y/rt.sizeDelta.x)*Vector2.right +
- localPointerPosition.y*Vector2.up;
- Vector2 pagePoint = m_Page.DeviceToPage(0, 0, (int) rt.sizeDelta.y, (int) rt.sizeDelta.y,
- PDFPage.PageRotation.Normal,
- (int) localPointerPosition.x, (int) localPointerPosition.y);
- return m_Page.GetLinkAtPoint(pagePoint);
- }
- return null;
- }
- }
- #endif
- }
|