SCInputFieldTrigger.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using SC.XR.Unity.Module_Keyboard;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. using UnityEngine.EventSystems;
  6. namespace SC.XR.Unity.Module_PlatformAccount
  7. {
  8. [RequireComponent(typeof(UGUI_SCInputField))]
  9. [RequireComponent(typeof(EventTrigger))]
  10. public class SCInputFieldTrigger : MonoBehaviour
  11. {
  12. private UGUI_SCInputField _scInputField;
  13. private EventTrigger _eventTrigger;
  14. protected Dictionary<EventTriggerType, EventTrigger.Entry> entrys = new Dictionary<EventTriggerType, EventTrigger.Entry>();
  15. private bool _onSelect = false;
  16. private bool _onError = false;
  17. public UGUI_SCInputField UGUI_SCInputField
  18. {
  19. get
  20. {
  21. if (_scInputField == null)
  22. {
  23. _scInputField = GetComponent<UGUI_SCInputField>();
  24. }
  25. return _scInputField;
  26. }
  27. }
  28. public EventTrigger EventTrigger
  29. {
  30. get
  31. {
  32. if (_eventTrigger == null)
  33. {
  34. _eventTrigger = GetComponent<EventTrigger>();
  35. }
  36. return _eventTrigger;
  37. }
  38. }
  39. public bool OnError
  40. {
  41. get
  42. {
  43. return _onError;
  44. }
  45. set
  46. {
  47. _onError = value;
  48. }
  49. }
  50. [Header("Target Graphic Color")]
  51. public Color normalColor;
  52. public Color highlightedColor;
  53. public Color pressedColor;
  54. public Color selectedColor;
  55. public Color disabledColor;
  56. public Color valueChangedColor;
  57. public Color errorColor;
  58. [Space(2)]
  59. [Header("Placeholder Color")]
  60. public Color placeholder_normal_color;
  61. public Color placeholder_error_color;
  62. [Space(2)]
  63. [Header("Text Component Color")]
  64. public Color textComponent_normal_color;
  65. public Color textComponent_error_color;
  66. private void OnEnable()
  67. {
  68. //RegisterTriggerEvents(OnDeselectEvent, EventTriggerType.Deselect);
  69. RegisterTriggerEvents(OnPointerDownEvent, EventTriggerType.PointerDown);
  70. RegisterTriggerEvents(OnPointerEnterEvent, EventTriggerType.PointerEnter);
  71. RegisterTriggerEvents(OnPointerExitEvent, EventTriggerType.PointerExit);
  72. GetComponent<UGUI_SCInputField>().onValueChanged.AddListener(OnValueChanged);
  73. GetComponent<UGUI_SCInputField>().onEndEdit.AddListener(OnEndEdit);
  74. }
  75. private void OnDisable()
  76. {
  77. //UnRegisterTriggerEvents(OnDeselectEvent, EventTriggerType.Deselect);
  78. UnRegisterTriggerEvents(OnPointerDownEvent, EventTriggerType.PointerDown);
  79. UnRegisterTriggerEvents(OnPointerEnterEvent, EventTriggerType.PointerEnter);
  80. UnRegisterTriggerEvents(OnPointerExitEvent, EventTriggerType.PointerExit);
  81. GetComponent<UGUI_SCInputField>().onValueChanged.RemoveListener(OnValueChanged);
  82. GetComponent<UGUI_SCInputField>().onEndEdit.RemoveListener(OnEndEdit);
  83. }
  84. //public void OnDeselectEvent(BaseEventData baseEventData)
  85. //{
  86. // if (UGUI_SCInputField.scKeyboard != null) return;
  87. // _onSelect = false;
  88. // UGUI_SCInputField.targetGraphic.color = _onError ? errorColor : normalColor;
  89. //}
  90. public void OnPointerDownEvent(BaseEventData baseEventData)
  91. {
  92. _onSelect = true;
  93. UGUI_SCInputField.targetGraphic.color = selectedColor;
  94. }
  95. public void OnPointerEnterEvent(BaseEventData baseEventData)
  96. {
  97. if (UGUI_SCInputField.scKeyboard != null) return;
  98. if (_onSelect) return;
  99. UGUI_SCInputField.targetGraphic.color = highlightedColor;
  100. }
  101. public void OnPointerExitEvent(BaseEventData baseEventData)
  102. {
  103. if (UGUI_SCInputField.scKeyboard != null) return;
  104. if (_onSelect) return;
  105. UGUI_SCInputField.targetGraphic.color = _onError ? errorColor : normalColor;
  106. }
  107. public void OnValueChanged(string input)
  108. {
  109. if (input == "")
  110. {
  111. UGUI_SCInputField.targetGraphic.color = selectedColor;
  112. }
  113. else
  114. {
  115. UGUI_SCInputField.targetGraphic.color = valueChangedColor;
  116. }
  117. }
  118. public void OnEndEdit(string input)
  119. {
  120. _onSelect = false;
  121. if (input == "")
  122. {
  123. UGUI_SCInputField.targetGraphic.color = _onError ? errorColor : normalColor;
  124. }
  125. else
  126. {
  127. UGUI_SCInputField.targetGraphic.color = _onError ? errorColor : valueChangedColor;
  128. }
  129. }
  130. public void OnInputError(bool value)
  131. {
  132. _onError = value;
  133. if(value) UGUI_SCInputField.targetGraphic.color = errorColor;
  134. }
  135. public void OnTextComponentNormal()
  136. {
  137. UGUI_SCInputField.textComponent.color = textComponent_normal_color;
  138. }
  139. public void OnPlaceholderNormal()
  140. {
  141. UGUI_SCInputField.placeholder.color = placeholder_normal_color;
  142. }
  143. public void OnTextComponentError()
  144. {
  145. UGUI_SCInputField.textComponent.color = textComponent_error_color;
  146. }
  147. public void OnPlaceholderError()
  148. {
  149. UGUI_SCInputField.placeholder.color = placeholder_error_color;
  150. }
  151. private void RegisterTriggerEvents(UnityAction<BaseEventData> unityAction, EventTriggerType eventTriggerType)
  152. {
  153. UnityAction<BaseEventData> action = new UnityAction<BaseEventData>(unityAction);
  154. EventTrigger.Entry entry;
  155. if (entrys.ContainsKey(eventTriggerType))
  156. {
  157. EventTrigger.triggers.Add(entrys[eventTriggerType]);
  158. }
  159. else
  160. {
  161. entry = new EventTrigger.Entry();
  162. entry.eventID = eventTriggerType;
  163. entry.callback.AddListener(action);
  164. entrys.Add(eventTriggerType, entry);
  165. EventTrigger.triggers.Add(entry);
  166. }
  167. }
  168. private void UnRegisterTriggerEvents(UnityAction<BaseEventData> unityAction, EventTriggerType eventTriggerType)
  169. {
  170. EventTrigger.triggers.Remove(entrys[eventTriggerType]);
  171. }
  172. }
  173. }