CustomUIKeyboard.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. namespace SongBrowserPlugin.UI
  8. {
  9. // https://github.com/andruzzzhka/BeatSaverDownloader/blob/master/BeatSaverDownloader/PluginUI/UIElements/CustomUIKeyboard.cs
  10. class CustomUIKeyboard : UIKeyboard
  11. {
  12. public void Awake()
  13. {
  14. UIKeyboard original = GetComponent<UIKeyboard>();
  15. System.Reflection.FieldInfo[] fields = original.GetType().GetFields(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
  16. foreach (System.Reflection.FieldInfo field in fields)
  17. {
  18. field.SetValue(this, field.GetValue(original));
  19. }
  20. Destroy(original);
  21. }
  22. public override void Start()
  23. {
  24. name = "CustomUIKeyboard";
  25. (transform as RectTransform).anchoredPosition -= new Vector2(0f, 5f);
  26. string[] array = new string[]
  27. {
  28. "q",
  29. "w",
  30. "e",
  31. "r",
  32. "t",
  33. "y",
  34. "u",
  35. "i",
  36. "o",
  37. "p",
  38. "a",
  39. "s",
  40. "d",
  41. "f",
  42. "g",
  43. "h",
  44. "j",
  45. "k",
  46. "l",
  47. "z",
  48. "x",
  49. "c",
  50. "v",
  51. "b",
  52. "n",
  53. "m",
  54. "<-",
  55. "space"
  56. };
  57. for (int i = 0; i < array.Length; i++)
  58. {
  59. TextMeshProButton textButton = Instantiate(_keyButtonPrefab);
  60. textButton.text.text = array[i];
  61. if (i < array.Length - 2)
  62. {
  63. string key = array[i];
  64. textButton.button.onClick.AddListener(delegate ()
  65. {
  66. KeyButtonWasPressed(key);
  67. });
  68. }
  69. else if (i == array.Length - 2)
  70. {
  71. textButton.button.onClick.AddListener(delegate ()
  72. {
  73. DeleteButtonWasPressed();
  74. });
  75. }
  76. else
  77. {
  78. textButton.button.onClick.AddListener(delegate ()
  79. {
  80. SpaceButtonWasPressed();
  81. });
  82. }
  83. RectTransform buttonRect = textButton.GetComponent<RectTransform>();
  84. RectTransform component2 = transform.GetChild(i).gameObject.GetComponent<RectTransform>();
  85. buttonRect.SetParent(component2, false);
  86. buttonRect.localPosition = Vector2.zero;
  87. buttonRect.localScale = Vector3.one;
  88. buttonRect.anchoredPosition = Vector2.zero;
  89. buttonRect.anchorMin = Vector2.zero;
  90. buttonRect.anchorMax = Vector3.one;
  91. buttonRect.offsetMin = Vector2.zero;
  92. buttonRect.offsetMax = Vector2.zero;
  93. }
  94. for (int i = 1; i <= 10; i++)
  95. {
  96. TextMeshProButton textButton = Instantiate(_keyButtonPrefab);
  97. textButton.text.text = i.ToString().Last().ToString();
  98. string key = i.ToString().Last().ToString();
  99. textButton.button.onClick.AddListener(delegate ()
  100. {
  101. KeyButtonWasPressed(key);
  102. });
  103. RectTransform buttonRect = textButton.GetComponent<RectTransform>();
  104. RectTransform component2 = transform.GetChild(i - 1).gameObject.GetComponent<RectTransform>();
  105. RectTransform buttonHolder = Instantiate(component2, component2.parent, false);
  106. Destroy(buttonHolder.GetComponentInChildren<Button>().gameObject);
  107. buttonHolder.anchoredPosition -= new Vector2(0f, -10.5f);
  108. buttonRect.SetParent(buttonHolder, false);
  109. buttonRect.localPosition = Vector2.zero;
  110. buttonRect.localScale = Vector3.one;
  111. buttonRect.anchoredPosition = Vector2.zero;
  112. buttonRect.anchorMin = Vector2.zero;
  113. buttonRect.anchorMax = Vector3.one;
  114. buttonRect.offsetMin = Vector2.zero;
  115. buttonRect.offsetMax = Vector2.zero;
  116. }
  117. }
  118. }
  119. }