SearchKeyboardViewController.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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), new Vector2(60f, 10f));
  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, "SettingsButton");
  44. UIBuilder.SetButtonText(ref _searchButton, "Search");
  45. (_searchButton.transform as RectTransform).sizeDelta = new Vector2(30f, 10f);
  46. (_searchButton.transform as RectTransform).anchoredPosition = new Vector2(-65f, 1.5f);
  47. _searchButton.onClick.RemoveAllListeners();
  48. _searchButton.onClick.AddListener(delegate () {
  49. searchButtonPressed?.Invoke(_inputString);
  50. DismissModalViewController(null, false);
  51. });
  52. }
  53. if (_backButton == null)
  54. {
  55. _backButton = UIBuilder.CreateBackButton(rectTransform);
  56. _backButton.onClick.AddListener(delegate ()
  57. {
  58. _inputString = "";
  59. backButtonPressed?.Invoke();
  60. DismissModalViewController(null, false);
  61. });
  62. }
  63. }
  64. void UpdateInputText()
  65. {
  66. if (_inputText != null)
  67. {
  68. _inputText.text = _inputString.ToUpper();
  69. }
  70. }
  71. void ClearInput()
  72. {
  73. _inputString = "";
  74. }
  75. /// <summary>
  76. /// Emulate keyboard support.
  77. /// </summary>
  78. private void LateUpdate()
  79. {
  80. if (!this.isInViewControllerHierarchy) return;
  81. if (Input.GetKeyDown(KeyCode.KeypadEnter) || Input.GetKeyDown(KeyCode.Return))
  82. {
  83. _searchButton.onClick.Invoke();
  84. }
  85. else if (Input.GetKeyDown(KeyCode.Backspace))
  86. {
  87. this._searchKeyboard.DeleteButtonWasPressed();
  88. }
  89. else if (Input.GetKeyDown(KeyCode.Space))
  90. {
  91. this._searchKeyboard.SpaceButtonWasPressed();
  92. }
  93. IEnumerable<KeyCode> keycodeIterator = Enum.GetValues(typeof(KeyCode)).Cast<KeyCode>();
  94. foreach (KeyCode keycode in keycodeIterator)
  95. {
  96. if (!((keycode >= KeyCode.A && keycode <= KeyCode.Z) || (keycode >= KeyCode.Alpha0 && keycode <= KeyCode.Alpha9))) continue;
  97. if (Input.GetKeyDown(keycode))
  98. {
  99. this._searchKeyboard.KeyButtonWasPressed(keycode.ToString());
  100. }
  101. }
  102. }
  103. }
  104. }