PDFSearchPanel.cs 10 KB

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