SearchKeyboardViewController.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using SongBrowser.Internals;
  2. using System;
  3. using System.Linq;
  4. using TMPro;
  5. using UnityEngine;
  6. namespace SongBrowser.UI
  7. {
  8. // https://github.com/andruzzzhka/BeatSaverDownloader/blob/master/BeatSaverDownloader/PluginUI/ViewControllers/SearchKeyboardViewController.cs
  9. class SearchKeyboardViewController : HMUI.ViewController
  10. {
  11. GameObject _searchKeyboardGO;
  12. CustomUIKeyboard _searchKeyboard;
  13. TextMeshProUGUI _inputText;
  14. public string _inputString = "";
  15. public event Action<string> searchButtonPressed;
  16. public event Action backButtonPressed;
  17. protected override void DidActivate(bool firstActivation, ActivationType type)
  18. {
  19. if (type == ActivationType.AddedToHierarchy && firstActivation)
  20. {
  21. _searchKeyboardGO = Instantiate(Resources.FindObjectsOfTypeAll<UIKeyboard>().First(x => x.name != "CustomUIKeyboard"), rectTransform, false).gameObject;
  22. Destroy(_searchKeyboardGO.GetComponent<UIKeyboard>());
  23. _searchKeyboard = _searchKeyboardGO.AddComponent<CustomUIKeyboard>();
  24. _searchKeyboard.textKeyWasPressedEvent += delegate (char input) { _inputString += input; UpdateInputText(); };
  25. _searchKeyboard.deleteButtonWasPressedEvent += delegate () { _inputString = _inputString.Substring(0, _inputString.Length - 1); UpdateInputText(); };
  26. _searchKeyboard.cancelButtonWasPressedEvent += () => { backButtonPressed?.Invoke(); };
  27. _searchKeyboard.okButtonWasPressedEvent += () => { searchButtonPressed?.Invoke(_inputString); };
  28. _inputText = BeatSaberUI.CreateText(rectTransform, "Search...", new Vector2(0f, 22f));
  29. _inputText.alignment = TextAlignmentOptions.Center;
  30. _inputText.fontSize = 6f;
  31. }
  32. else
  33. {
  34. _inputString = "";
  35. UpdateInputText();
  36. }
  37. }
  38. void UpdateInputText()
  39. {
  40. if (_inputText != null)
  41. {
  42. _inputText.text = _inputString?.ToUpper() ?? "";
  43. if (string.IsNullOrEmpty(_inputString))
  44. {
  45. _searchKeyboard.OkButtonInteractivity = false;
  46. }
  47. else
  48. {
  49. _searchKeyboard.OkButtonInteractivity = true;
  50. }
  51. }
  52. }
  53. void ClearInput()
  54. {
  55. _inputString = "";
  56. }
  57. void Back()
  58. {
  59. _inputString = "";
  60. backButtonPressed?.Invoke();
  61. }
  62. }
  63. }