using SC.XR.Unity.Module_Keyboard; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using UnityEngine.EventSystems; namespace SC.XR.Unity.Module_PlatformAccount { [RequireComponent(typeof(UGUI_SCInputField))] [RequireComponent(typeof(EventTrigger))] public class SCInputFieldTrigger : MonoBehaviour { private UGUI_SCInputField _scInputField; private EventTrigger _eventTrigger; protected Dictionary entrys = new Dictionary(); private bool _onSelect = false; private bool _onError = false; public UGUI_SCInputField UGUI_SCInputField { get { if (_scInputField == null) { _scInputField = GetComponent(); } return _scInputField; } } public EventTrigger EventTrigger { get { if (_eventTrigger == null) { _eventTrigger = GetComponent(); } return _eventTrigger; } } public bool OnError { get { return _onError; } set { _onError = value; } } [Header("Target Graphic Color")] public Color normalColor; public Color highlightedColor; public Color pressedColor; public Color selectedColor; public Color disabledColor; public Color valueChangedColor; public Color errorColor; [Space(2)] [Header("Placeholder Color")] public Color placeholder_normal_color; public Color placeholder_error_color; [Space(2)] [Header("Text Component Color")] public Color textComponent_normal_color; public Color textComponent_error_color; private void OnEnable() { //RegisterTriggerEvents(OnDeselectEvent, EventTriggerType.Deselect); RegisterTriggerEvents(OnPointerDownEvent, EventTriggerType.PointerDown); RegisterTriggerEvents(OnPointerEnterEvent, EventTriggerType.PointerEnter); RegisterTriggerEvents(OnPointerExitEvent, EventTriggerType.PointerExit); GetComponent().onValueChanged.AddListener(OnValueChanged); GetComponent().onEndEdit.AddListener(OnEndEdit); } private void OnDisable() { //UnRegisterTriggerEvents(OnDeselectEvent, EventTriggerType.Deselect); UnRegisterTriggerEvents(OnPointerDownEvent, EventTriggerType.PointerDown); UnRegisterTriggerEvents(OnPointerEnterEvent, EventTriggerType.PointerEnter); UnRegisterTriggerEvents(OnPointerExitEvent, EventTriggerType.PointerExit); GetComponent().onValueChanged.RemoveListener(OnValueChanged); GetComponent().onEndEdit.RemoveListener(OnEndEdit); } //public void OnDeselectEvent(BaseEventData baseEventData) //{ // if (UGUI_SCInputField.scKeyboard != null) return; // _onSelect = false; // UGUI_SCInputField.targetGraphic.color = _onError ? errorColor : normalColor; //} public void OnPointerDownEvent(BaseEventData baseEventData) { _onSelect = true; UGUI_SCInputField.targetGraphic.color = selectedColor; } public void OnPointerEnterEvent(BaseEventData baseEventData) { if (UGUI_SCInputField.scKeyboard != null) return; if (_onSelect) return; UGUI_SCInputField.targetGraphic.color = highlightedColor; } public void OnPointerExitEvent(BaseEventData baseEventData) { if (UGUI_SCInputField.scKeyboard != null) return; if (_onSelect) return; UGUI_SCInputField.targetGraphic.color = _onError ? errorColor : normalColor; } public void OnValueChanged(string input) { if (input == "") { UGUI_SCInputField.targetGraphic.color = selectedColor; } else { UGUI_SCInputField.targetGraphic.color = valueChangedColor; } } public void OnEndEdit(string input) { _onSelect = false; if (input == "") { UGUI_SCInputField.targetGraphic.color = _onError ? errorColor : normalColor; } else { UGUI_SCInputField.targetGraphic.color = _onError ? errorColor : valueChangedColor; } } public void OnInputError(bool value) { _onError = value; if(value) UGUI_SCInputField.targetGraphic.color = errorColor; } public void OnTextComponentNormal() { UGUI_SCInputField.textComponent.color = textComponent_normal_color; } public void OnPlaceholderNormal() { UGUI_SCInputField.placeholder.color = placeholder_normal_color; } public void OnTextComponentError() { UGUI_SCInputField.textComponent.color = textComponent_error_color; } public void OnPlaceholderError() { UGUI_SCInputField.placeholder.color = placeholder_error_color; } private void RegisterTriggerEvents(UnityAction unityAction, EventTriggerType eventTriggerType) { UnityAction action = new UnityAction(unityAction); EventTrigger.Entry entry; if (entrys.ContainsKey(eventTriggerType)) { EventTrigger.triggers.Add(entrys[eventTriggerType]); } else { entry = new EventTrigger.Entry(); entry.eventID = eventTriggerType; entry.callback.AddListener(action); entrys.Add(eventTriggerType, entry); EventTrigger.triggers.Add(entry); } } private void UnRegisterTriggerEvents(UnityAction unityAction, EventTriggerType eventTriggerType) { EventTrigger.triggers.Remove(entrys[eventTriggerType]); } } }