KeyboardInputs.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // /******************************************************************************
  2. // * File: KeyboardInputs.cs
  3. // * Copyright (c) 2023 Qualcomm Technologies, Inc. and/or its subsidiaries. All rights reserved.
  4. // *
  5. // *
  6. // ******************************************************************************/
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using UnityEngine;
  10. using UnityEngine.Events;
  11. using UnityEngine.UI;
  12. namespace QCHT.Samples.XRKeyboard
  13. {
  14. public class KeyboardInputs : MonoBehaviour
  15. {
  16. [SerializeField] private Text _keyboardInputsText;
  17. public Text KeyboardInputsText
  18. {
  19. get => _keyboardInputsText;
  20. set => _keyboardInputsText = value;
  21. }
  22. private bool _maj;
  23. public bool IsMaj => _maj;
  24. public UnityEvent<KeyButton> OnKeyButtonPressed = new UnityEvent<KeyButton>();
  25. private List<KeyButton> _keyButtons;
  26. private void Start()
  27. {
  28. _keyButtons = GetComponentsInChildren<KeyButton>(true).ToList();
  29. foreach (KeyButton button in _keyButtons)
  30. {
  31. button.inputEvent.AddListener(RegisterInput);
  32. }
  33. }
  34. private void RegisterInput(KeyButton keyButton)
  35. {
  36. ProcessText(keyButton);
  37. OnKeyButtonPressed?.Invoke(keyButton);
  38. }
  39. private void ProcessText(KeyButton keyButton)
  40. {
  41. if (_keyboardInputsText == null)
  42. return;
  43. var keyStringCharButton = keyButton as KeyStringCharButton;
  44. if (keyStringCharButton != null && keyStringCharButton.StrInput.Length > 0)
  45. {
  46. _keyboardInputsText.text += _maj ? keyStringCharButton.StrInput.ToUpper() : keyStringCharButton.StrInput.ToLower();
  47. return;
  48. }
  49. var keyStringButton = keyButton as KeyStringButton;
  50. if (keyStringButton != null && keyStringButton.StrInput.Length > 0)
  51. {
  52. _keyboardInputsText.text += keyStringButton.StrInput;
  53. return;
  54. }
  55. var keySpecialButton = keyButton as KeySpecialButton;
  56. if (keySpecialButton != null)
  57. {
  58. switch (keySpecialButton.KeySpecial)
  59. {
  60. case KeySpecial.Delete:
  61. if (_keyboardInputsText.text.Length > 3 && _keyboardInputsText.text.Substring(_keyboardInputsText.text.Length - 2, 2) == "\n")
  62. _keyboardInputsText.text = _keyboardInputsText.text.Substring(0, _keyboardInputsText.text.Length - 2);
  63. else if (_keyboardInputsText.text.Length > 0)
  64. _keyboardInputsText.text = _keyboardInputsText.text.Substring(0, _keyboardInputsText.text.Length - 1);
  65. break;
  66. case KeySpecial.Enter:
  67. _keyboardInputsText.text += "\n";
  68. break;
  69. case KeySpecial.Shift:
  70. SetMaj(!_maj);
  71. break;
  72. case KeySpecial.DeleteAll:
  73. _keyboardInputsText.text = string.Empty;
  74. break;
  75. case KeySpecial.SwitchObject:
  76. keySpecialButton.SwitchObject();
  77. break;
  78. }
  79. }
  80. }
  81. private void SetMaj(bool maj)
  82. {
  83. foreach (var key in _keyButtons)
  84. {
  85. var keyChar = key as KeyStringCharButton;
  86. if (keyChar != null)
  87. keyChar.SetMaj(maj);
  88. }
  89. _maj = maj;
  90. }
  91. }
  92. }