| | 1 | | using System; |
| | 2 | | using UnityEngine; |
| | 3 | | using UnityEngine.EventSystems; |
| | 4 | |
|
| | 5 | | public class VoiceChatButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler |
| | 6 | | { |
| | 7 | | [SerializeField] InputAction_Hold voiceChatAction; |
| | 8 | | [SerializeField] Animator buttonAnimator; |
| | 9 | | [SerializeField] private Animator disabledTooltipAnimator; |
| | 10 | |
|
| 1 | 11 | | private static readonly int talkingAnimation = Animator.StringToHash("Talking"); |
| 1 | 12 | | private static readonly int disabledAnimation = Animator.StringToHash("Disabled"); |
| 1 | 13 | | private static readonly int showDisabledTooltipAnimation = Animator.StringToHash("ShowDisabledTooltip"); |
| 1 | 14 | | private static readonly int hideDisabledTooltipAnimation = Animator.StringToHash("HideDisabledTooltip"); |
| | 15 | |
|
| | 16 | | private bool isRecording = false; |
| 5 | 17 | | private bool isEnabledByScene = true; |
| | 18 | |
|
| 0 | 19 | | public void OnPointerDown(PointerEventData eventData) { voiceChatAction.RaiseOnStarted(); } |
| | 20 | |
|
| 0 | 21 | | public void OnPointerUp(PointerEventData eventData) { voiceChatAction.RaiseOnFinished(); } |
| | 22 | |
|
| | 23 | | public void SetOnRecording(bool recording) |
| | 24 | | { |
| 0 | 25 | | isRecording = recording; |
| | 26 | |
|
| 0 | 27 | | if (!gameObject.activeInHierarchy) |
| 0 | 28 | | return; |
| | 29 | |
|
| 0 | 30 | | buttonAnimator.SetBool(talkingAnimation, recording); |
| 0 | 31 | | } |
| | 32 | |
|
| | 33 | | public void SetEnabledByScene(bool enabledByScene) |
| | 34 | | { |
| 0 | 35 | | bool hasChangedToDisable = !enabledByScene && isEnabledByScene; |
| 0 | 36 | | if (hasChangedToDisable) |
| | 37 | | { |
| 0 | 38 | | if (isRecording) |
| | 39 | | { |
| 0 | 40 | | ShowDisabledTooltip(); |
| | 41 | | } |
| 0 | 42 | | voiceChatAction.OnStarted -= OnVoiceChatInput; |
| 0 | 43 | | voiceChatAction.OnStarted += OnVoiceChatInput; |
| 0 | 44 | | } |
| | 45 | | else |
| | 46 | | { |
| 0 | 47 | | voiceChatAction.OnStarted -= OnVoiceChatInput; |
| 0 | 48 | | disabledTooltipAnimator.SetTrigger(hideDisabledTooltipAnimation); |
| | 49 | | } |
| | 50 | |
|
| 0 | 51 | | isEnabledByScene = enabledByScene; |
| 0 | 52 | | buttonAnimator.SetBool(disabledAnimation, !isEnabledByScene); |
| 0 | 53 | | } |
| | 54 | |
|
| | 55 | | private void OnVoiceChatInput(DCLAction_Hold action) |
| | 56 | | { |
| 0 | 57 | | if (!isEnabledByScene) |
| | 58 | | { |
| 0 | 59 | | ShowDisabledTooltip(); |
| | 60 | | } |
| 0 | 61 | | } |
| | 62 | |
|
| | 63 | | private void ShowDisabledTooltip() |
| | 64 | | { |
| 0 | 65 | | if (disabledTooltipAnimator is null) |
| 0 | 66 | | return; |
| | 67 | |
|
| 0 | 68 | | disabledTooltipAnimator.SetTrigger(hideDisabledTooltipAnimation); |
| 0 | 69 | | disabledTooltipAnimator.SetTrigger(showDisabledTooltipAnimation); |
| 0 | 70 | | } |
| | 71 | | } |