PDFSearchPanel.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 System;
  10. using System.Collections.Generic;
  11. using UnityEngine;
  12. using UnityEngine.EventSystems;
  13. using UnityEngine.UI;
  14. namespace Paroxe.PdfRenderer.Internal.Viewer
  15. {
  16. public class PDFSearchPanel : UIBehaviour
  17. {
  18. public Button m_CloseButton;
  19. public RectTransform m_ContentPanel;
  20. public InputField m_InputField;
  21. public Image m_MatchCaseCheckBox;
  22. public Image m_MatchWholeWordCheckBox;
  23. public Button m_NextButton;
  24. public Button m_PreviousButton;
  25. public Text m_TotalResultText;
  26. public Image m_ValidatorImage;
  27. #if !UNITY_WEBGL
  28. private float m_AnimationDuration = 0.40f;
  29. private float m_AnimationPosition = 1.0f;
  30. private int m_CurrentSearchIndex = 0;
  31. private PDFSearchHandle.MatchOption m_Flags = PDFSearchHandle.MatchOption.NONE;
  32. private bool m_Opened = false;
  33. private PDFViewer m_PDFViewer;
  34. private string m_PreviousSearch;
  35. private PDFProgressiveSearch m_ProgressiveSearch;
  36. private bool m_SearchFinished = false;
  37. private int m_Total = 0;
  38. private bool m_Registered;
  39. public void Close()
  40. {
  41. if (m_Opened)
  42. {
  43. m_Opened = false;
  44. m_AnimationPosition = 0.00f;
  45. if (m_ProgressiveSearch != null)
  46. m_ProgressiveSearch.Abort();
  47. m_PDFViewer.SetSearchResults(null);
  48. if (EventSystem.current.currentSelectedGameObject == m_InputField.gameObject)
  49. {
  50. EventSystem.current.SetSelectedGameObject(null, null);
  51. }
  52. }
  53. }
  54. public void OnCloseButton()
  55. {
  56. Close();
  57. }
  58. public void OnInputFieldEndEdit()
  59. {
  60. if (m_ProgressiveSearch != null)
  61. {
  62. if (m_InputField.text.Trim() != m_PreviousSearch)
  63. {
  64. m_SearchFinished = false;
  65. m_PreviousSearch = m_InputField.text.Trim();
  66. m_ProgressiveSearch.StartSearch(m_InputField.text.Trim(), m_Flags);
  67. }
  68. }
  69. }
  70. public void OnMatchCaseClicked()
  71. {
  72. m_MatchCaseCheckBox.enabled = !m_MatchCaseCheckBox.enabled;
  73. if (m_MatchCaseCheckBox.enabled && m_MatchWholeWordCheckBox.enabled)
  74. m_Flags = PDFSearchHandle.MatchOption.MATCH_CASE_AND_WHOLE_WORD;
  75. else if (m_MatchCaseCheckBox.enabled && !m_MatchWholeWordCheckBox.enabled)
  76. m_Flags = PDFSearchHandle.MatchOption.MATCH_CASE;
  77. else if (!m_MatchCaseCheckBox.enabled && m_MatchWholeWordCheckBox.enabled)
  78. m_Flags = PDFSearchHandle.MatchOption.MATCH_WHOLE_WORD;
  79. else
  80. m_Flags = PDFSearchHandle.MatchOption.NONE;
  81. if (m_ProgressiveSearch != null)
  82. {
  83. m_SearchFinished = false;
  84. m_ProgressiveSearch.StartSearch(m_InputField.text.Trim(), m_Flags);
  85. }
  86. }
  87. public void OnMatchWholeWordCliked()
  88. {
  89. m_MatchWholeWordCheckBox.enabled = !m_MatchWholeWordCheckBox.enabled;
  90. if (m_MatchCaseCheckBox.enabled && m_MatchWholeWordCheckBox.enabled)
  91. m_Flags = PDFSearchHandle.MatchOption.MATCH_CASE_AND_WHOLE_WORD;
  92. else if (m_MatchCaseCheckBox.enabled && !m_MatchWholeWordCheckBox.enabled)
  93. m_Flags = PDFSearchHandle.MatchOption.MATCH_CASE;
  94. else if (!m_MatchCaseCheckBox.enabled && m_MatchWholeWordCheckBox.enabled)
  95. m_Flags = PDFSearchHandle.MatchOption.MATCH_WHOLE_WORD;
  96. else
  97. m_Flags = PDFSearchHandle.MatchOption.NONE;
  98. if (m_ProgressiveSearch != null)
  99. {
  100. m_SearchFinished = false;
  101. m_ProgressiveSearch.StartSearch(m_InputField.text.Trim(), m_Flags);
  102. }
  103. }
  104. public void OnNextButton()
  105. {
  106. m_PDFViewer.GoToNextSearchResult();
  107. }
  108. public void OnPreviousButton()
  109. {
  110. m_PDFViewer.GoToPreviousSearchResult();
  111. }
  112. public void Open()
  113. {
  114. if (!m_Opened && m_PDFViewer.IsLoaded)
  115. {
  116. m_Opened = true;
  117. m_AnimationPosition = 0.00f;
  118. m_PreviousSearch = "";
  119. }
  120. }
  121. public void Toggle()
  122. {
  123. if (m_Opened)
  124. Close();
  125. else
  126. Open();
  127. }
  128. protected override void OnDisable()
  129. {
  130. base.OnDisable();
  131. DestroyProgressiveSearch();
  132. m_PDFViewer.OnDocumentLoaded -= OnDocumentLoaded;
  133. m_PDFViewer.OnDocumentUnloaded -= OnDocumentUnloaded;
  134. }
  135. protected override void OnEnable()
  136. {
  137. base.OnEnable();
  138. if (m_PDFViewer == null)
  139. m_PDFViewer = GetComponentInParent<PDFViewer>();
  140. m_PDFViewer.OnDocumentLoaded += OnDocumentLoaded;
  141. m_PDFViewer.OnDocumentUnloaded += OnDocumentUnloaded;
  142. }
  143. private void OnDocumentLoaded(PDFViewer sender, PDFDocument document)
  144. {
  145. if (m_ProgressiveSearch == null)
  146. m_ProgressiveSearch = PDFProgressiveSearch.CreateSearch(m_PDFViewer.Document, sender.SearchTimeBudgetPerFrame);
  147. m_ProgressiveSearch.OnSearchFinished += OnSearchFinished;
  148. m_ProgressiveSearch.OnProgressChanged += OnProgressChanged;
  149. m_Registered = true;
  150. }
  151. private void OnDocumentUnloaded(PDFViewer sender, PDFDocument document)
  152. {
  153. DestroyProgressiveSearch();
  154. }
  155. private void DestroyProgressiveSearch()
  156. {
  157. if (m_ProgressiveSearch != null)
  158. {
  159. if (m_Registered)
  160. {
  161. m_ProgressiveSearch.OnSearchFinished -= OnSearchFinished;
  162. m_ProgressiveSearch.OnProgressChanged -= OnProgressChanged;
  163. m_Registered = false;
  164. }
  165. Destroy(m_ProgressiveSearch.gameObject);
  166. m_ProgressiveSearch = null;
  167. }
  168. }
  169. private void OnProgressChanged(PDFProgressiveSearch sender, int total)
  170. {
  171. m_Total = total;
  172. UpdateSearchTotal();
  173. }
  174. private void OnSearchFinished(PDFProgressiveSearch sender, IList<PDFSearchResult>[] searchResults)
  175. {
  176. m_SearchFinished = true;
  177. m_PDFViewer.SetSearchResults(searchResults);
  178. }
  179. void Update()
  180. {
  181. if (!m_PDFViewer.IsLoaded)
  182. {
  183. Close();
  184. }
  185. if (m_Opened)
  186. {
  187. if (m_CurrentSearchIndex != m_PDFViewer.CurrentSearchResultIndex)
  188. {
  189. m_CurrentSearchIndex = m_PDFViewer.CurrentSearchResultIndex;
  190. UpdateSearchTotal();
  191. }
  192. }
  193. if (m_Opened && m_AnimationPosition < 1.0f)
  194. {
  195. m_AnimationPosition = Mathf.Clamp01(m_AnimationPosition + Time.deltaTime/m_AnimationDuration);
  196. m_ContentPanel.anchoredPosition = Vector2.Lerp(
  197. new Vector2(m_ContentPanel.anchoredPosition.x, 135.0f),
  198. new Vector2(m_ContentPanel.anchoredPosition.x, 0.0f),
  199. PDFInternalUtils.CubicEaseIn(m_AnimationPosition, 0.0f, 1.0f, 1.0f));
  200. if (m_AnimationPosition == 1.0f)
  201. {
  202. EventSystem.current.SetSelectedGameObject(m_InputField.gameObject, null);
  203. m_InputField.OnPointerClick(new PointerEventData(EventSystem.current));
  204. OnInputFieldEndEdit();
  205. }
  206. }
  207. else if (!m_Opened && m_AnimationPosition < 1.0f)
  208. {
  209. m_AnimationPosition = Mathf.Clamp01(m_AnimationPosition + Time.deltaTime/m_AnimationDuration);
  210. m_ContentPanel.anchoredPosition = Vector2.Lerp(
  211. new Vector2(m_ContentPanel.anchoredPosition.x, 135.0f),
  212. new Vector2(m_ContentPanel.anchoredPosition.x, 0.0f),
  213. PDFInternalUtils.CubicEaseIn(1.0f - m_AnimationPosition, 0.0f, 1.0f, 1.0f));
  214. if (m_AnimationPosition == 1.0f)
  215. {
  216. m_TotalResultText.text = "0 of 0";
  217. }
  218. }
  219. }
  220. private void UpdateSearchTotal()
  221. {
  222. if (m_Total > 0)
  223. {
  224. m_TotalResultText.text = (m_SearchFinished ? m_PDFViewer.CurrentSearchResultIndex + 1 : 1) + " of " + m_Total;
  225. }
  226. else
  227. {
  228. m_TotalResultText.text = "0 of 0";
  229. }
  230. if (string.IsNullOrEmpty(m_InputField.text.Trim()))
  231. {
  232. m_ValidatorImage.color = new Color(m_ValidatorImage.color.r, m_ValidatorImage.color.g,
  233. m_ValidatorImage.color.b, 0.0f);
  234. m_TotalResultText.color = new Color(m_TotalResultText.color.r, m_TotalResultText.color.g,
  235. m_TotalResultText.color.b, 0.0f);
  236. }
  237. else if (m_Total > 0)
  238. {
  239. m_ValidatorImage.color = new Color(m_ValidatorImage.color.r, m_ValidatorImage.color.g,
  240. m_ValidatorImage.color.b, 0.0f);
  241. m_TotalResultText.color = new Color(m_TotalResultText.color.r, m_TotalResultText.color.g,
  242. m_TotalResultText.color.b, 1.0f);
  243. }
  244. else
  245. {
  246. m_ValidatorImage.color = new Color(m_ValidatorImage.color.r, m_ValidatorImage.color.g,
  247. m_ValidatorImage.color.b, m_SearchFinished ? 0.4f : 0.0f);
  248. m_TotalResultText.color = new Color(m_TotalResultText.color.r, m_TotalResultText.color.g,
  249. m_TotalResultText.color.b, 1.0f);
  250. }
  251. RectTransform validatorTransform = (m_ValidatorImage.transform as RectTransform);
  252. validatorTransform.sizeDelta = new Vector2(m_TotalResultText.preferredWidth + 18.0f, validatorTransform.sizeDelta.y);
  253. }
  254. #endif
  255. }
  256. }