PDFViewerLeftPanel.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using UnityEngine;
  2. using UnityEngine.EventSystems;
  3. using UnityEngine.UI;
  4. namespace Paroxe.PdfRenderer.Internal.Viewer
  5. {
  6. public class PDFViewerLeftPanel : UIBehaviour
  7. {
  8. public RectTransform m_Bookmarks;
  9. public Image m_BookmarksTab;
  10. public Text m_BookmarksTabTitle;
  11. public Sprite m_ClosedTabSprite;
  12. public Sprite m_CloseSprite;
  13. public float m_MaxWidth = 500.0f;
  14. public float m_MinWidth = 250.0f;
  15. public Sprite m_OpenedTabSprite;
  16. public Sprite m_OpenSprite;
  17. public Texture2D m_ResizeCursor;
  18. public Image m_SideBarImage;
  19. public RectTransform m_Thumbnails;
  20. public Scrollbar m_ThumbnailsScrollbar;
  21. public Image m_ThumbnailsTab;
  22. public Text m_ThumbnailsTabTitle;
  23. public PDFThumbnailsViewer m_ThumbnailsViewer;
  24. private RectTransform m_HorizontalScrollBar;
  25. private float m_LastPanelWidth;
  26. private bool m_Opened = true;
  27. private RectTransform m_RectTransform;
  28. private Vector2 m_StartDragPointerPosition;
  29. private RectTransform m_ViewerViewport;
  30. private bool m_Drag = false;
  31. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
  32. private bool m_PointerIn = false;
  33. #endif
  34. public void OnBeginDrag()
  35. {
  36. if (!m_Opened)
  37. return;
  38. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
  39. Cursor.SetCursor(m_ResizeCursor, new Vector2(16.0f, 16.0f), CursorMode.Auto);
  40. m_Drag = true;
  41. #endif
  42. }
  43. public void OnBookmarksTabClicked()
  44. {
  45. m_BookmarksTab.sprite = m_OpenedTabSprite;
  46. m_BookmarksTabTitle.color = Color.black;
  47. m_ThumbnailsTab.sprite = m_ClosedTabSprite;
  48. m_ThumbnailsTabTitle.color = new Color(0.50f, 0.50f, 0.50f);
  49. m_Bookmarks.gameObject.GetComponent<CanvasGroup>().alpha = 1.0f;
  50. m_Bookmarks.gameObject.GetComponent<CanvasGroup>().interactable = true;
  51. m_Bookmarks.gameObject.GetComponent<CanvasGroup>().blocksRaycasts = true;
  52. m_Thumbnails.gameObject.GetComponent<CanvasGroup>().alpha = 0.0f;
  53. m_Thumbnails.gameObject.GetComponent<CanvasGroup>().interactable = false;
  54. m_Thumbnails.gameObject.GetComponent<CanvasGroup>().blocksRaycasts = false;
  55. }
  56. public void OnDrag(BaseEventData eventData)
  57. {
  58. if (!m_Drag)
  59. return;
  60. var pointerData = eventData as PointerEventData;
  61. if (pointerData == null)
  62. return;
  63. m_RectTransform.sizeDelta += new Vector2(pointerData.delta.x, 0.0f);
  64. m_RectTransform.sizeDelta = new Vector2(Mathf.Clamp(m_RectTransform.sizeDelta.x, m_MinWidth, m_MaxWidth),
  65. m_RectTransform.sizeDelta.y);
  66. m_LastPanelWidth = m_RectTransform.sizeDelta.x;
  67. UpdateViewport();
  68. }
  69. public void OnEndDrag()
  70. {
  71. if (!m_Drag)
  72. return;
  73. if (!m_Opened)
  74. return;
  75. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
  76. if (!m_PointerIn)
  77. {
  78. Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
  79. }
  80. m_Drag = false;
  81. #endif
  82. }
  83. public void OnPointerEnter()
  84. {
  85. if (!m_Opened)
  86. {
  87. return;
  88. }
  89. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
  90. Cursor.SetCursor(m_ResizeCursor, new Vector2(16.0f, 16.0f), CursorMode.Auto);
  91. m_PointerIn = true;
  92. #endif
  93. }
  94. public void OnPointerExit()
  95. {
  96. if (!m_Opened)
  97. {
  98. return;
  99. }
  100. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
  101. if (!m_Drag)
  102. {
  103. Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
  104. }
  105. m_PointerIn = false;
  106. #endif
  107. }
  108. public void OnThumbnailsTabClicked()
  109. {
  110. m_BookmarksTab.sprite = m_ClosedTabSprite;
  111. m_BookmarksTabTitle.color = new Color(0.50f, 0.50f, 0.50f);
  112. m_ThumbnailsTab.sprite = m_OpenedTabSprite;
  113. m_ThumbnailsTabTitle.color = Color.black;
  114. m_Bookmarks.gameObject.GetComponent<CanvasGroup>().alpha = 0.0f;
  115. m_Bookmarks.gameObject.GetComponent<CanvasGroup>().interactable = false;
  116. m_Bookmarks.gameObject.GetComponent<CanvasGroup>().blocksRaycasts = false;
  117. m_Thumbnails.gameObject.GetComponent<CanvasGroup>().alpha = 1.0f;
  118. m_Thumbnails.gameObject.GetComponent<CanvasGroup>().interactable = true;
  119. m_Thumbnails.gameObject.GetComponent<CanvasGroup>().blocksRaycasts = true;
  120. }
  121. public void SetActive(bool active)
  122. {
  123. if (m_RectTransform == null)
  124. {
  125. m_RectTransform = transform as RectTransform;
  126. }
  127. if (m_ViewerViewport == null)
  128. {
  129. m_ViewerViewport = GetComponentsInParent<PDFViewer>(true)[0].m_Internal.Viewport;
  130. }
  131. if (m_HorizontalScrollBar == null)
  132. {
  133. m_HorizontalScrollBar = GetComponentsInParent<PDFViewer>(true)[0].m_Internal.HorizontalScrollBar;
  134. }
  135. gameObject.SetActive(active);
  136. if (!active)
  137. {
  138. m_ViewerViewport.offsetMin = new Vector2(0.0f, m_ViewerViewport.offsetMin.y);
  139. m_HorizontalScrollBar.offsetMin = new Vector2(0.0f, m_HorizontalScrollBar.offsetMin.y);
  140. }
  141. else
  142. {
  143. UpdateViewport();
  144. }
  145. }
  146. public bool IsOpened
  147. {
  148. get { return m_Opened; }
  149. }
  150. public void SetOpened(bool opened)
  151. {
  152. m_Opened = opened;
  153. UpdateGraphics();
  154. UpdateViewport();
  155. }
  156. public void Toggle()
  157. {
  158. m_Opened = !m_Opened;
  159. UpdateGraphics();
  160. UpdateViewport();
  161. }
  162. protected override void OnEnable()
  163. {
  164. m_RectTransform = transform as RectTransform;
  165. m_ViewerViewport = GetComponentInParent<PDFViewer>().m_Internal.Viewport;
  166. m_HorizontalScrollBar = GetComponentInParent<PDFViewer>().m_Internal.HorizontalScrollBar;
  167. m_LastPanelWidth = 350.0f;
  168. UpdateViewport();
  169. }
  170. private void UpdateGraphics()
  171. {
  172. m_SideBarImage.sprite = m_Opened ? m_CloseSprite : m_OpenSprite;
  173. if (m_RectTransform == null)
  174. {
  175. m_RectTransform = transform as RectTransform;
  176. }
  177. if (m_ViewerViewport == null)
  178. {
  179. m_ViewerViewport = GetComponentInParent<PDFViewer>().m_Internal.Viewport;
  180. }
  181. if (m_HorizontalScrollBar == null)
  182. {
  183. m_HorizontalScrollBar = GetComponentInParent<PDFViewer>().m_Internal.HorizontalScrollBar;
  184. }
  185. if (m_Opened)
  186. {
  187. m_RectTransform.sizeDelta = new Vector2(m_LastPanelWidth, m_RectTransform.sizeDelta.y);
  188. }
  189. else
  190. {
  191. m_RectTransform.sizeDelta = new Vector2(24.0f, m_RectTransform.sizeDelta.y);
  192. }
  193. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
  194. if (m_Opened)
  195. {
  196. Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
  197. }
  198. #endif
  199. }
  200. private void UpdateViewport()
  201. {
  202. if (m_ViewerViewport.offsetMin.x != m_RectTransform.sizeDelta.x)
  203. {
  204. m_ViewerViewport.offsetMin = new Vector2(m_RectTransform.sizeDelta.x, m_ViewerViewport.offsetMin.y);
  205. m_HorizontalScrollBar.offsetMin = new Vector2(m_RectTransform.sizeDelta.x,
  206. m_HorizontalScrollBar.offsetMin.y);
  207. }
  208. }
  209. }
  210. }