| | 1 | | using System; |
| | 2 | | using UnityEngine; |
| | 3 | | using UnityEngine.UI; |
| | 4 | |
|
| | 5 | | public class ExpressionsHUDView : MonoBehaviour |
| | 6 | | { |
| | 7 | | private const string PATH = "ExpressionsHUD"; |
| | 8 | |
|
| | 9 | | public delegate void ExpressionClicked(string expressionId); |
| | 10 | |
|
| | 11 | | [Serializable] |
| | 12 | | public class ButtonToExpression |
| | 13 | | { |
| | 14 | | public string expressionId; |
| | 15 | | public Button_OnPointerDown button; // When the button is used to lock/unlock the mouse we have to use onPointer |
| | 16 | | } |
| | 17 | |
|
| | 18 | | [SerializeField] internal ButtonToExpression[] buttonToExpressionMap; |
| | 19 | | [SerializeField] internal Button showContentButton; |
| | 20 | | [SerializeField] internal Button_OnPointerDown[] hideContentButtons; |
| | 21 | | [SerializeField] internal RectTransform content; |
| | 22 | | [SerializeField] internal InputAction_Trigger openExpressionsAction; |
| | 23 | | [SerializeField] internal RawImage avatarPic; |
| | 24 | |
|
| 7 | 25 | | public static ExpressionsHUDView Create() { return Instantiate(Resources.Load<GameObject>(PATH)).GetComponent<Expres |
| | 26 | |
|
| | 27 | | void OnOpenExpressions(DCLAction_Trigger trigger) |
| | 28 | | { |
| 2 | 29 | | if (!IsVisible()) |
| 0 | 30 | | return; |
| | 31 | |
|
| 2 | 32 | | if (Input.GetKeyDown(KeyCode.Escape) && !IsContentVisible()) |
| 0 | 33 | | return; |
| | 34 | |
|
| 2 | 35 | | ToggleContent(); |
| 2 | 36 | | } |
| | 37 | |
|
| | 38 | | private void Awake() |
| | 39 | | { |
| 7 | 40 | | openExpressionsAction.OnTriggered += OnOpenExpressions; |
| 7 | 41 | | showContentButton.onClick.AddListener(ToggleContent); |
| | 42 | |
|
| 42 | 43 | | for (int i = 0; i < hideContentButtons.Length; i++) |
| | 44 | | { |
| 14 | 45 | | hideContentButtons[i].onPointerDown += HideContent; |
| | 46 | | } |
| 7 | 47 | | } |
| | 48 | |
|
| | 49 | | internal void Initialize(ExpressionClicked clickedDelegate) |
| | 50 | | { |
| 9 | 51 | | content.gameObject.SetActive(false); |
| | 52 | |
|
| 144 | 53 | | foreach (var buttonToExpression in buttonToExpressionMap) |
| | 54 | | { |
| 65 | 55 | | buttonToExpression.button.onPointerDown += () => clickedDelegate?.Invoke(buttonToExpression.expressionId); |
| | 56 | | } |
| 9 | 57 | | } |
| | 58 | |
|
| | 59 | | public void UpdateAvatarSprite(Texture2D avatarTexture) |
| | 60 | | { |
| 0 | 61 | | if (avatarTexture == null) |
| 0 | 62 | | return; |
| | 63 | |
|
| 0 | 64 | | avatarPic.texture = avatarTexture; |
| 0 | 65 | | } |
| | 66 | |
|
| | 67 | | internal void ToggleContent() |
| | 68 | | { |
| 5 | 69 | | if (IsContentVisible()) |
| | 70 | | { |
| 2 | 71 | | HideContent(); |
| 2 | 72 | | } |
| | 73 | | else |
| | 74 | | { |
| 3 | 75 | | ShowContent(); |
| | 76 | | } |
| 3 | 77 | | } |
| | 78 | |
|
| | 79 | | internal void ShowContent() |
| | 80 | | { |
| 3 | 81 | | content.gameObject.SetActive(true); |
| 3 | 82 | | DCL.Helpers.Utils.UnlockCursor(); |
| 3 | 83 | | AudioScriptableObjects.dialogOpen.Play(true); |
| 3 | 84 | | } |
| | 85 | |
|
| | 86 | | internal void HideContent() |
| | 87 | | { |
| 2 | 88 | | content.gameObject.SetActive(false); |
| 2 | 89 | | DCL.Helpers.Utils.LockCursor(); |
| 2 | 90 | | AudioScriptableObjects.dialogClose.Play(true); |
| 2 | 91 | | } |
| | 92 | |
|
| 7 | 93 | | public bool IsContentVisible() { return content.gameObject.activeSelf; } |
| | 94 | |
|
| 2 | 95 | | public void SetVisiblity(bool visible) { gameObject.SetActive(visible); } |
| | 96 | |
|
| 2 | 97 | | public bool IsVisible() { return gameObject.activeSelf; } |
| | 98 | |
|
| 14 | 99 | | public void OnDestroy() { CleanUp(); } |
| | 100 | |
|
| | 101 | | public void CleanUp() |
| | 102 | | { |
| 14 | 103 | | openExpressionsAction.OnTriggered -= OnOpenExpressions; |
| | 104 | |
|
| 84 | 105 | | for (int i = 0; i < hideContentButtons.Length; i++) |
| | 106 | | { |
| 28 | 107 | | hideContentButtons[i].onPointerDown -= HideContent; |
| | 108 | | } |
| 14 | 109 | | } |
| | 110 | | } |