PDFViewerPage.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 UnityEngine;
  10. using UnityEngine.EventSystems;
  11. namespace Paroxe.PdfRenderer.Internal.Viewer
  12. {
  13. #if UNITY_WEBGL
  14. public class PDFViewerPage : UIBehaviour
  15. {
  16. public Texture2D m_HandCursor;
  17. }
  18. #endif
  19. #if !UNITY_WEBGL
  20. public class PDFViewerPage : UIBehaviour, IPointerClickHandler
  21. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
  22. , IPointerEnterHandler, IPointerExitHandler
  23. #endif
  24. {
  25. public Texture2D m_HandCursor;
  26. private PDFViewer m_PDFViewer;
  27. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
  28. private bool m_PointerInside = false;
  29. private bool m_HandCursorSettedByMe = false;
  30. #endif
  31. private Camera m_CanvasCamera;
  32. public PDFPage m_Page;
  33. public void OnPointerClick(PointerEventData eventData)
  34. {
  35. if (m_PDFViewer == null)
  36. m_PDFViewer = GetComponentInParent<PDFViewer>();
  37. if (m_PDFViewer.Document == null)
  38. return;
  39. if (m_PDFViewer.LinksActionHandler == null)
  40. return;
  41. if (m_Page == null)
  42. m_PDFViewer.Document.GetPage(transform.GetSiblingIndex());
  43. PDFLink link = GetLinkAtPoint(eventData.pressPosition, eventData.pressEventCamera);
  44. if (link != null)
  45. PDFActionHandlerHelper.ExecuteLinkAction(m_PDFViewer, link);
  46. else if (m_PDFViewer.ParagraphZoomingEnable && eventData.clickCount == 2)
  47. {
  48. using (PDFTextPage textPage = m_Page.GetTextPage())
  49. {
  50. Rect pageRect = new Rect();
  51. Vector2 pos = eventData.pressPosition;
  52. RectTransformUtility.ScreenPointToLocalPointInRectangle(transform as RectTransform, pos, GetComponent<Camera>(), out pos);
  53. RectTransform rt = transform as RectTransform;
  54. pos += rt.sizeDelta.x * 0.5f * Vector2.right;
  55. pos += rt.sizeDelta.y * 0.5f * Vector2.up;
  56. pos = pos.x * (rt.sizeDelta.y / rt.sizeDelta.x) * Vector2.right + pos.y * Vector2.up;
  57. Vector2 pagePoint = m_Page.DeviceToPage(0, 0, (int)rt.sizeDelta.y, (int)rt.sizeDelta.y, PDFPage.PageRotation.Normal, (int)pos.x, (int)pos.y);
  58. Vector2 pageSize = m_Page.GetPageSize();
  59. float threshold = m_PDFViewer.ParagraphDetectionThreshold;
  60. string text = GetBoundedText(textPage, 0.0f, pagePoint.y + 0.0f, pageSize.x, pagePoint.y - 1.0f);
  61. if (!string.IsNullOrEmpty(text.Trim()) && text.Trim().Length > 4)
  62. {
  63. string prevText = text;
  64. float bottomOffset = 0.0f;
  65. float topOffset = 0.0f;
  66. float t = 0.0f;
  67. while (true)
  68. {
  69. bottomOffset += 2.0f;
  70. text = GetBoundedText(textPage, 0.0f, pagePoint.y + 0.0f, pageSize.x, pagePoint.y - bottomOffset);
  71. if (text == prevText)
  72. t += 2.0f;
  73. else
  74. t = 0.0f;
  75. if (t >= threshold)
  76. break;
  77. prevText = text;
  78. }
  79. t = 0.0f;
  80. while (true)
  81. {
  82. topOffset += 2.0f;
  83. text = GetBoundedText(textPage, 0.0f, pagePoint.y + topOffset, pageSize.x, pagePoint.y - bottomOffset);
  84. if (text == prevText)
  85. t += 2.0f;
  86. else
  87. t = 0.0f;
  88. if (t >= threshold)
  89. break;
  90. prevText = text;
  91. }
  92. pageRect = new Rect(0.0f, pagePoint.y + topOffset, pageSize.x, (pagePoint.y + topOffset) - (pagePoint.y - bottomOffset));
  93. m_PDFViewer.ZoomOnParagraph(this, pageRect);
  94. }
  95. }
  96. }
  97. }
  98. private string GetBoundedText(PDFTextPage textPage, float left, float top, float right, float bottom)
  99. {
  100. return textPage.GetBoundedText(left, top, right, bottom, 4096);
  101. }
  102. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
  103. public void OnPointerEnter(PointerEventData eventData)
  104. {
  105. if (m_PDFViewer == null)
  106. {
  107. m_PDFViewer = GetComponentInParent<PDFViewer>();
  108. }
  109. if (m_PDFViewer.Document == null)
  110. {
  111. return;
  112. }
  113. m_PointerInside = true;
  114. if (m_Page == null)
  115. m_PDFViewer.Document.GetPage(transform.GetSiblingIndex());
  116. }
  117. public void OnPointerExit(PointerEventData eventData)
  118. {
  119. m_PointerInside = false;
  120. if (m_HandCursorSettedByMe)
  121. {
  122. Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
  123. }
  124. if (m_Page != null)
  125. m_Page.Dispose();
  126. m_Page = null;
  127. }
  128. #endif
  129. protected override void OnEnable()
  130. {
  131. if (m_PDFViewer == null)
  132. m_PDFViewer = GetComponentInParent<PDFViewer>();
  133. if (m_CanvasCamera == null)
  134. m_CanvasCamera = FindCanvasCamera(transform as RectTransform);
  135. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
  136. m_PointerInside = false;
  137. #endif
  138. }
  139. protected override void OnDisable()
  140. {
  141. if (m_Page != null)
  142. m_Page.Dispose();
  143. m_Page = null;
  144. }
  145. private Camera FindCanvasCamera(RectTransform rt)
  146. {
  147. RectTransform parent = rt.parent as RectTransform;
  148. if (parent != null)
  149. {
  150. Canvas canvas = GetComponent<Canvas>();
  151. if (canvas != null && canvas.worldCamera != null)
  152. {
  153. return canvas.worldCamera;
  154. }
  155. return FindCanvasCamera(parent);
  156. }
  157. return null;
  158. }
  159. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
  160. void Update()
  161. {
  162. if (m_HandCursor == null)
  163. {
  164. return;
  165. }
  166. if (m_CanvasCamera == null)
  167. {
  168. m_CanvasCamera = FindCanvasCamera(transform as RectTransform);
  169. }
  170. if (m_PointerInside)
  171. {
  172. PDFLink link = GetLinkAtPoint(Input.mousePosition, m_CanvasCamera);
  173. if (link != null)
  174. {
  175. Cursor.SetCursor(m_HandCursor, new Vector2(6.0f, 0.0f), CursorMode.Auto);
  176. m_HandCursorSettedByMe = true;
  177. }
  178. else
  179. {
  180. Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
  181. }
  182. }
  183. }
  184. #endif
  185. private PDFLink GetLinkAtPoint(Vector2 point, Camera camera)
  186. {
  187. if (m_PDFViewer == null)
  188. m_PDFViewer = GetComponentInParent<PDFViewer>();
  189. if (m_Page == null)
  190. m_Page = m_PDFViewer.Document.GetPage(transform.GetSiblingIndex());
  191. Vector2 localPointerPosition;
  192. if (RectTransformUtility.ScreenPointToLocalPointInRectangle(transform as RectTransform, point, camera,
  193. out localPointerPosition))
  194. {
  195. RectTransform rt = transform as RectTransform;
  196. localPointerPosition += rt.sizeDelta.x*0.5f*Vector2.right;
  197. localPointerPosition += rt.sizeDelta.y*0.5f*Vector2.up;
  198. localPointerPosition = localPointerPosition.x*(rt.sizeDelta.y/rt.sizeDelta.x)*Vector2.right +
  199. localPointerPosition.y*Vector2.up;
  200. Vector2 pagePoint = m_Page.DeviceToPage(0, 0, (int) rt.sizeDelta.y, (int) rt.sizeDelta.y,
  201. PDFPage.PageRotation.Normal,
  202. (int) localPointerPosition.x, (int) localPointerPosition.y);
  203. return m_Page.GetLinkAtPoint(pagePoint);
  204. }
  205. return null;
  206. }
  207. }
  208. #endif
  209. }