SCKeyboardBase.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using SC.XR.Unity;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Runtime.CompilerServices;
  5. using TMPro;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. using UnityEngine.UIElements;
  9. namespace SC.XR.Unity
  10. {
  11. public abstract class SCKeyboardBase
  12. {
  13. public SCKeyboardMono keyboardMono;
  14. //private TextMeshProUGUI keyboardText;
  15. public SCKeyboardBase(Transform parent, Vector3 position, Quaternion rotation, Vector3 scale)
  16. {
  17. GameObject keyboardPrefab = Resources.Load<GameObject>(PrefabResourceName);
  18. if (keyboardPrefab == null)
  19. {
  20. Debug.LogError("This prefab resource do not exist");
  21. return;
  22. }
  23. GameObject keyboardGameObject = GameObject.Instantiate(keyboardPrefab);
  24. this.KeyboardGameObject = keyboardGameObject;
  25. SetKeyboardTransform(parent, position, rotation, scale);
  26. keyboardMono = keyboardGameObject.GetComponent<SCKeyboardMono>();
  27. if (keyboardMono == null)
  28. {
  29. Debug.LogError("This keyboard prefab do not have scripts inhert from SCKeyboardMono!");
  30. return;
  31. }
  32. keyboardMono.Initialize();
  33. //keyboardText = keyboardMono.keyboardText;
  34. keyboardMono.OnDoneButtonClick += OnDoneButtonClick;
  35. keyboardMono.OnTextChange += OnTextChange;
  36. ResetKeyboard();
  37. }
  38. //TODO
  39. public static bool HideInput { get; set; }
  40. public abstract string PrefabResourceName { get; }
  41. public bool Done { get; set; }
  42. public bool WasCanceled { get; set; }
  43. private string text = string.Empty;
  44. public string Text {
  45. get
  46. {
  47. return text;
  48. }
  49. set
  50. {
  51. keyboardMono.keyboardPrompt.SetEnteredText(value);
  52. text = value;
  53. }
  54. }
  55. public bool Active {
  56. get
  57. {
  58. return KeyboardGameObject.activeSelf;
  59. }
  60. set
  61. {
  62. KeyboardGameObject.SetActive(value);
  63. }
  64. }
  65. protected GameObject KeyboardGameObject { get; set; }
  66. private static Dictionary<SCKeyboardEnum, Type> keyboardTypeDic = new Dictionary<SCKeyboardEnum, Type>()
  67. {
  68. { SCKeyboardEnum.SCKeyboard2D, typeof(SCKeyboard2D) },
  69. { SCKeyboardEnum.SCKeyboard3D, typeof(SCKeyboard3D) }
  70. };
  71. private static Dictionary<Transform, Dictionary<SCKeyboardEnum, SCKeyboardBase>> keyboardCacheDic = new Dictionary<Transform, Dictionary<SCKeyboardEnum, SCKeyboardBase>>();
  72. public static SCKeyboardBase Open(SCKeyboardEnum sckeyboardEnum, string text, TouchScreenKeyboardType touchScreenKeyboardType, Transform parent, Vector3 position, Quaternion rotation, Vector3 scale)
  73. {
  74. if (keyboardCacheDic.ContainsKey(parent))
  75. {
  76. Dictionary<SCKeyboardEnum, SCKeyboardBase> keyboardEnumDic = keyboardCacheDic[parent];
  77. if (keyboardEnumDic != null && keyboardEnumDic.ContainsKey(sckeyboardEnum))
  78. {
  79. SCKeyboardBase keyboardCache = keyboardEnumDic[sckeyboardEnum];
  80. keyboardCache.ResetKeyboard();
  81. keyboardCache.SetKeyboardTransform(parent, position, rotation, scale);
  82. return keyboardCache;
  83. }
  84. }
  85. if (!keyboardTypeDic.ContainsKey(sckeyboardEnum))
  86. {
  87. DebugMy.LogError("This SCKeyboardEnum do not exist", sckeyboardEnum);
  88. return null;
  89. }
  90. SCKeyboardBase keyboard = Activator.CreateInstance(keyboardTypeDic[sckeyboardEnum], parent, position, rotation, scale) as SCKeyboardBase;
  91. if (!keyboardCacheDic.ContainsKey(parent))
  92. {
  93. keyboardCacheDic[parent] = new Dictionary<SCKeyboardEnum, SCKeyboardBase>();
  94. }
  95. keyboardCacheDic[parent][sckeyboardEnum] = keyboard;
  96. return keyboard;
  97. }
  98. private void OnDoneButtonClick()
  99. {
  100. Debug.Log("OnDoneButtonClick");
  101. this.Done = true;
  102. }
  103. private void OnTextChange(string text)
  104. {
  105. this.Text = text;
  106. }
  107. private void ResetKeyboard()
  108. {
  109. this.Done = false;
  110. this.WasCanceled = false;
  111. //this.Text = string.Empty;
  112. }
  113. private void SetKeyboardTransform(Transform parent, Vector3 positon, Quaternion rotation, Vector3 scale)
  114. {
  115. if (KeyboardGameObject == null)
  116. {
  117. Debug.LogError("KeyboardGameObject is NULL");
  118. return;
  119. }
  120. this.KeyboardGameObject.transform.SetParent(null);
  121. this.KeyboardGameObject.transform.position = positon;
  122. this.KeyboardGameObject.transform.rotation = rotation;
  123. this.KeyboardGameObject.transform.localScale = scale;
  124. this.KeyboardGameObject.transform.SetParent(parent);
  125. }
  126. public void SetKeyboardTransform(Vector3 positon, Quaternion rotation, Vector3 localScale)
  127. {
  128. this.KeyboardGameObject.transform.position = positon;
  129. this.KeyboardGameObject.transform.rotation = rotation;
  130. this.KeyboardGameObject.transform.localScale = localScale;
  131. }
  132. }
  133. }