PDFViewerLeftPanel.cs 8.1 KB

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