SCKeyboardBase.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 SCKeyboardBase(Transform parent, Vector3 position, Quaternion rotation, Vector3 scale)
  14. {
  15. GameObject keyboardPrefab = Resources.Load<GameObject>(PrefabResourceName);
  16. if (keyboardPrefab == null)
  17. {
  18. Debug.LogError("This prefab resource do not exist");
  19. return;
  20. }
  21. GameObject keyboardGameObject = GameObject.Instantiate(keyboardPrefab);
  22. this.KeyboardGameObject = keyboardGameObject;
  23. SetKeyboardTransform(parent, position, rotation, scale);
  24. Init();
  25. ResetKeyboard();
  26. }
  27. public abstract void Init();
  28. public abstract void SetTextOnOpen(string openText);
  29. //TODO
  30. public static bool HideInput { get; set; }
  31. public abstract string PrefabResourceName { get; }
  32. public bool Done { get; set; }
  33. public bool WasCanceled { get; set; }
  34. protected string m_Text = string.Empty;
  35. public abstract string text {
  36. get;
  37. set;
  38. }
  39. public bool active {
  40. get
  41. {
  42. return KeyboardGameObject.activeSelf;
  43. }
  44. set
  45. {
  46. KeyboardGameObject.SetActive(value);
  47. }
  48. }
  49. protected GameObject KeyboardGameObject { get; set; }
  50. private static Dictionary<SCKeyboardEnum, Type> keyboardTypeDic = new Dictionary<SCKeyboardEnum, Type>()
  51. {
  52. { SCKeyboardEnum.SCKeyboard2D, typeof(SCKeyboard2D) },
  53. { SCKeyboardEnum.SCKeyboard3D, typeof(SCKeyboard3D) }
  54. };
  55. private static Dictionary<Transform, Dictionary<SCKeyboardEnum, SCKeyboardBase>> keyboardCacheDic = new Dictionary<Transform, Dictionary<SCKeyboardEnum, SCKeyboardBase>>();
  56. public static SCKeyboardBase Open(SCKeyboardEnum sckeyboardEnum, string text, TouchScreenKeyboardType touchScreenKeyboardType, Transform parent, Vector3 position, Quaternion rotation, Vector3 scale)
  57. {
  58. if (keyboardCacheDic.ContainsKey(parent))
  59. {
  60. Dictionary<SCKeyboardEnum, SCKeyboardBase> keyboardEnumDic = keyboardCacheDic[parent];
  61. if (keyboardEnumDic != null && keyboardEnumDic.ContainsKey(sckeyboardEnum))
  62. {
  63. SCKeyboardBase keyboardCache = keyboardEnumDic[sckeyboardEnum];
  64. keyboardCache.ResetKeyboard();
  65. keyboardCache.SetKeyboardTransform(parent, position, rotation, scale);
  66. return keyboardCache;
  67. }
  68. }
  69. if (!keyboardTypeDic.ContainsKey(sckeyboardEnum))
  70. {
  71. DebugMy.LogError("This SCKeyboardEnum do not exist", sckeyboardEnum);
  72. return null;
  73. }
  74. SCKeyboardBase keyboard = Activator.CreateInstance(keyboardTypeDic[sckeyboardEnum], parent, position, rotation, scale) as SCKeyboardBase;
  75. if (!keyboardCacheDic.ContainsKey(parent))
  76. {
  77. keyboardCacheDic[parent] = new Dictionary<SCKeyboardEnum, SCKeyboardBase>();
  78. }
  79. keyboardCacheDic[parent][sckeyboardEnum] = keyboard;
  80. return keyboard;
  81. }
  82. protected void OnDoneButtonClick()
  83. {
  84. Debug.Log("OnDoneButtonClick");
  85. this.Done = true;
  86. }
  87. protected void OnTextChange(string text)
  88. {
  89. this.text = text;
  90. }
  91. private void ResetKeyboard()
  92. {
  93. this.Done = false;
  94. this.WasCanceled = false;
  95. //this.Text = string.Empty;
  96. }
  97. private void SetKeyboardTransform(Transform parent, Vector3 positon, Quaternion rotation, Vector3 scale)
  98. {
  99. if (KeyboardGameObject == null)
  100. {
  101. Debug.LogError("KeyboardGameObject is NULL");
  102. return;
  103. }
  104. this.KeyboardGameObject.transform.SetParent(null);
  105. this.KeyboardGameObject.transform.position = positon;
  106. this.KeyboardGameObject.transform.rotation = rotation;
  107. this.KeyboardGameObject.transform.localScale = scale;
  108. this.KeyboardGameObject.transform.SetParent(parent);
  109. }
  110. public void SetKeyboardTransform(Vector3 positon, Quaternion rotation, Vector3 localScale)
  111. {
  112. this.KeyboardGameObject.transform.position = positon;
  113. this.KeyboardGameObject.transform.rotation = rotation;
  114. this.KeyboardGameObject.transform.localScale = localScale;
  115. }
  116. }
  117. }