123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- 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<EventTriggerType, EventTrigger.Entry> entrys = new Dictionary<EventTriggerType, EventTrigger.Entry>();
- private bool _onSelect = false;
- private bool _onError = false;
- public UGUI_SCInputField UGUI_SCInputField
- {
- get
- {
- if (_scInputField == null)
- {
- _scInputField = GetComponent<UGUI_SCInputField>();
- }
- return _scInputField;
- }
- }
- public EventTrigger EventTrigger
- {
- get
- {
- if (_eventTrigger == null)
- {
- _eventTrigger = GetComponent<EventTrigger>();
- }
- 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<UGUI_SCInputField>().onValueChanged.AddListener(OnValueChanged);
- GetComponent<UGUI_SCInputField>().onEndEdit.AddListener(OnEndEdit);
- }
- private void OnDisable()
- {
- //UnRegisterTriggerEvents(OnDeselectEvent, EventTriggerType.Deselect);
- UnRegisterTriggerEvents(OnPointerDownEvent, EventTriggerType.PointerDown);
- UnRegisterTriggerEvents(OnPointerEnterEvent, EventTriggerType.PointerEnter);
- UnRegisterTriggerEvents(OnPointerExitEvent, EventTriggerType.PointerExit);
- GetComponent<UGUI_SCInputField>().onValueChanged.RemoveListener(OnValueChanged);
- GetComponent<UGUI_SCInputField>().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<BaseEventData> unityAction, EventTriggerType eventTriggerType)
- {
- UnityAction<BaseEventData> action = new UnityAction<BaseEventData>(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<BaseEventData> unityAction, EventTriggerType eventTriggerType)
- {
- EventTrigger.triggers.Remove(entrys[eventTriggerType]);
- }
- }
- }
|