SearchKeyboardViewController.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using TMPro;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. using VRUI;
  8. namespace SongBrowserPlugin.UI
  9. {
  10. // https://github.com/andruzzzhka/BeatSaverDownloader/blob/master/BeatSaverDownloader/PluginUI/ViewControllers/SearchKeyboardViewController.cs
  11. class SearchKeyboardViewController : VRUIViewController
  12. {
  13. GameObject _searchKeyboardGO;
  14. CustomUIKeyboard _searchKeyboard;
  15. Button _searchButton;
  16. Button _backButton;
  17. TextMeshProUGUI _inputText;
  18. public string _inputString = "";
  19. public event Action<string> searchButtonPressed;
  20. public event Action backButtonPressed;
  21. protected override void DidActivate(bool firstActivation, ActivationType type)
  22. {
  23. if (_searchKeyboard == null)
  24. {
  25. _searchKeyboardGO = Instantiate(Resources.FindObjectsOfTypeAll<UIKeyboard>().First(x => x.name != "CustomUIKeyboard"), rectTransform, false).gameObject;
  26. _searchKeyboard = _searchKeyboardGO.AddComponent<CustomUIKeyboard>();
  27. _searchKeyboard.uiKeyboardKeyEvent = delegate (char input) { _inputString += input; UpdateInputText(); };
  28. _searchKeyboard.uiKeyboardDeleteEvent = delegate () { _inputString = _inputString.Substring(0, _inputString.Length - 1); UpdateInputText(); };
  29. }
  30. if (_inputText == null)
  31. {
  32. _inputText = UIBuilder.CreateText(rectTransform, "Search...", new Vector2(0f, -11.5f));
  33. _inputText.alignment = TextAlignmentOptions.Center;
  34. _inputText.fontSize = 6f;
  35. }
  36. else
  37. {
  38. _inputString = "";
  39. UpdateInputText();
  40. }
  41. if (_searchButton == null)
  42. {
  43. _searchButton = UIBuilder.CreateUIButton(rectTransform, "ApplyButton");
  44. UIBuilder.SetButtonText(ref _searchButton, "Search");
  45. (_searchButton.transform as RectTransform).sizeDelta = new Vector2(30f, 10f);
  46. (_searchButton.transform as RectTransform).anchoredPosition = new Vector2(-15f, 1.5f);
  47. _searchButton.onClick.RemoveAllListeners();
  48. _searchButton.onClick.AddListener(delegate ()
  49. {
  50. searchButtonPressed?.Invoke(_inputString);
  51. DismissModalViewController(null, false);
  52. });
  53. }
  54. if (_backButton == null)
  55. {
  56. _backButton = UIBuilder.CreateBackButton(rectTransform);
  57. _backButton.onClick.AddListener(delegate ()
  58. {
  59. _inputString = "";
  60. backButtonPressed?.Invoke();
  61. DismissModalViewController(null, false);
  62. });
  63. }
  64. }
  65. void UpdateInputText()
  66. {
  67. if (_inputText != null)
  68. {
  69. _inputText.text = _inputString.ToUpper();
  70. }
  71. }
  72. void ClearInput()
  73. {
  74. _inputString = "";
  75. }
  76. /// <summary>
  77. /// Emulate keyboard support.
  78. /// </summary>
  79. private void LateUpdate()
  80. {
  81. if (!this.isInViewControllerHierarchy) return;
  82. if (Input.GetKeyDown(KeyCode.KeypadEnter) || Input.GetKeyDown(KeyCode.Return))
  83. {
  84. _searchButton.onClick.Invoke();
  85. }
  86. else if (Input.GetKeyDown(KeyCode.Backspace))
  87. {
  88. this._searchKeyboard.DeleteButtonWasPressed();
  89. }
  90. else if (Input.GetKeyDown(KeyCode.Space))
  91. {
  92. this._searchKeyboard.SpaceButtonWasPressed();
  93. }
  94. IEnumerable<KeyCode> keycodeIterator = Enum.GetValues(typeof(KeyCode)).Cast<KeyCode>();
  95. foreach (KeyCode keycode in keycodeIterator)
  96. {
  97. if (!((keycode >= KeyCode.A && keycode <= KeyCode.Z) || (keycode >= KeyCode.Alpha0 && keycode <= KeyCode.Alpha9))) continue;
  98. if (Input.GetKeyDown(keycode))
  99. {
  100. this._searchKeyboard.KeyButtonWasPressed(keycode.ToString());
  101. }
  102. }
  103. }
  104. }
  105. }