Sc2ad

viewcontroller thing

Sep 29th, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.56 KB | None | 0 0
  1. // https://github.com/andruzzzhka/BeatSaverDownloader/blob/master/BeatSaverDownloader/PluginUI/ViewControllers/SearchKeyboardViewController.cs
  2.     class KeyboardViewController : HMUI.ViewController
  3.     {
  4.         GameObject keyboardGO;
  5.  
  6.         CustomUIKeyboard keyboard;
  7.  
  8.         TextMeshProUGUI _inputText;
  9.         public string _inputString = "";
  10.  
  11.         public event Action<string> confirmPressed;
  12.         public event Action cancelPressed;
  13.  
  14.         protected override void DidActivate(bool firstActivation, ActivationType type)
  15.         {
  16.             if (type == ActivationType.AddedToHierarchy && firstActivation)
  17.             {
  18.                 var kb = Resources.FindObjectsOfTypeAll<UIKeyboard>().First(x => x.name != "CustomUIKeyboard");
  19.                 Logger.log.Debug("Found UIKeyboard object: " + kb);
  20.                 keyboardGO = Instantiate(kb, rectTransform, false).gameObject;
  21.  
  22.                 Destroy(keyboardGO.GetComponent<UIKeyboard>());
  23.                 keyboard = keyboardGO.AddComponent<CustomUIKeyboard>();
  24.  
  25.                 keyboard.textKeyWasPressedEvent += delegate (char input) { _inputString += input; UpdateInputText(); };
  26.                 keyboard.deleteButtonWasPressedEvent += delegate () { _inputString = _inputString.Substring(0, _inputString.Length - 1); UpdateInputText(); };
  27.                 keyboard.cancelButtonWasPressedEvent += () => { cancelPressed?.Invoke(); };
  28.                 keyboard.okButtonWasPressedEvent += () => { confirmPressed?.Invoke(_inputString); };
  29.  
  30.                 _inputText = BeatSaberMarkupLanguage.BeatSaberUI.CreateText(rectTransform, "Enter PIN...", new Vector2(0f, 22f));
  31.                 _inputText.alignment = TextAlignmentOptions.Center;
  32.                 _inputText.fontSize = 6f;
  33.  
  34.             }
  35.             else
  36.             {
  37.                 _inputString = "";
  38.                 UpdateInputText();
  39.             }
  40.  
  41.         }
  42.  
  43.         void UpdateInputText()
  44.         {
  45.             if (_inputText != null)
  46.             {
  47.                 _inputText.text = _inputString?.ToUpper() ?? "";
  48.                 if (string.IsNullOrEmpty(_inputString) || _inputString.Length <= 5)
  49.                 {
  50.                     keyboard.OkButtonInteractivity = false;
  51.                 }
  52.                 else
  53.                 {
  54.                     keyboard.OkButtonInteractivity = true;
  55.                 }
  56.             }
  57.         }
  58.  
  59.         void ClearInput()
  60.         {
  61.             _inputString = "";
  62.         }
  63.  
  64.         void Back()
  65.         {
  66.             _inputString = "";
  67.             cancelPressed?.Invoke();
  68.         }
  69.     }
Add Comment
Please, Sign In to add comment