| | 1 | | using DCL.Models; |
| | 2 | | using DCL.Components; |
| | 3 | | using UnityEngine; |
| | 4 | | using TMPro; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using DCL; |
| | 7 | |
|
| | 8 | | public class InteractionHoverCanvasController : MonoBehaviour |
| | 9 | | { |
| 0 | 10 | | public static InteractionHoverCanvasController i { get; private set; } |
| | 11 | | public Canvas canvas; |
| | 12 | | public RectTransform backgroundTransform; |
| | 13 | | public TextMeshProUGUI text; |
| | 14 | | public GameObject[] icons; |
| | 15 | |
|
| | 16 | | bool isHovered = false; |
| | 17 | | Camera mainCamera; |
| | 18 | | GameObject hoverIcon; |
| | 19 | | Vector3 meshCenteredPos; |
| | 20 | | IDCLEntity entity; |
| | 21 | |
|
| | 22 | | const string ACTION_BUTTON_POINTER = "POINTER"; |
| | 23 | | const string ACTION_BUTTON_PRIMARY = "PRIMARY"; |
| | 24 | | const string ACTION_BUTTON_SECONDARY = "SECONDARY"; |
| | 25 | |
|
| | 26 | | void Awake() |
| | 27 | | { |
| 123 | 28 | | i = this; |
| 123 | 29 | | mainCamera = Camera.main; |
| 123 | 30 | | } |
| | 31 | |
|
| | 32 | | public void Setup(string button, string feedbackText, IDCLEntity entity) |
| | 33 | | { |
| 35 | 34 | | text.text = feedbackText; |
| 35 | 35 | | this.entity = entity; |
| | 36 | |
|
| 35 | 37 | | ConfigureIcon(button); |
| | 38 | |
|
| 35 | 39 | | canvas.enabled = enabled && isHovered; |
| 35 | 40 | | } |
| | 41 | |
|
| | 42 | | void ConfigureIcon(string button) |
| | 43 | | { |
| 35 | 44 | | hoverIcon?.SetActive(false); |
| | 45 | |
|
| | 46 | | switch (button) |
| | 47 | | { |
| | 48 | | case ACTION_BUTTON_POINTER: |
| 0 | 49 | | hoverIcon = icons[0]; |
| 0 | 50 | | break; |
| | 51 | | case ACTION_BUTTON_PRIMARY: |
| 2 | 52 | | hoverIcon = icons[1]; |
| 2 | 53 | | break; |
| | 54 | | case ACTION_BUTTON_SECONDARY: |
| 0 | 55 | | hoverIcon = icons[2]; |
| 0 | 56 | | break; |
| | 57 | | default: // ANY |
| 33 | 58 | | hoverIcon = icons[3]; |
| | 59 | | break; |
| | 60 | | } |
| | 61 | |
|
| 35 | 62 | | hoverIcon.SetActive(true); |
| 35 | 63 | | } |
| | 64 | |
|
| | 65 | | public void SetHoverState(bool hoverState) |
| | 66 | | { |
| 13432 | 67 | | if (!enabled || hoverState == isHovered) |
| 13406 | 68 | | return; |
| | 69 | |
|
| 26 | 70 | | isHovered = hoverState; |
| | 71 | |
|
| 26 | 72 | | canvas.enabled = isHovered; |
| 26 | 73 | | } |
| | 74 | |
|
| 0 | 75 | | public GameObject GetCurrentHoverIcon() { return hoverIcon; } |
| | 76 | |
|
| | 77 | | // This method will be used when we apply a "loose aim" for the 3rd person camera |
| | 78 | | void CalculateMeshCenteredPos(DCLTransform.Model transformModel = null) |
| | 79 | | { |
| 0 | 80 | | if (!canvas.enabled) |
| 0 | 81 | | return; |
| | 82 | |
|
| 0 | 83 | | if (entity.meshesInfo.renderers == null || entity.meshesInfo.renderers.Length == 0) |
| | 84 | | { |
| 0 | 85 | | meshCenteredPos = transform.parent.position; |
| 0 | 86 | | } |
| | 87 | | else |
| | 88 | | { |
| 0 | 89 | | Vector3 sum = Vector3.zero; |
| 0 | 90 | | for (int i = 0; i < entity.meshesInfo.renderers.Length; i++) |
| | 91 | | { |
| 0 | 92 | | sum += entity.meshesInfo.renderers[i].bounds.center; |
| | 93 | | } |
| | 94 | |
|
| 0 | 95 | | meshCenteredPos = sum / entity.meshesInfo.renderers.Length; |
| | 96 | | } |
| 0 | 97 | | } |
| | 98 | |
|
| | 99 | | // This method will be used when we apply a "loose aim" for the 3rd person camera |
| | 100 | | public void UpdateSizeAndPos() |
| | 101 | | { |
| 0 | 102 | | if (mainCamera == null) |
| 0 | 103 | | mainCamera = Camera.main; |
| | 104 | |
|
| 0 | 105 | | Vector3 screenPoint = mainCamera.WorldToViewportPoint(meshCenteredPos); |
| | 106 | |
|
| 0 | 107 | | if (screenPoint.z > 0) |
| | 108 | | { |
| 0 | 109 | | RectTransform canvasRect = (RectTransform) canvas.transform; |
| 0 | 110 | | float width = canvasRect.rect.width; |
| 0 | 111 | | float height = canvasRect.rect.height; |
| 0 | 112 | | screenPoint.Scale(new Vector3(width, height, 0)); |
| | 113 | |
|
| 0 | 114 | | ((RectTransform) backgroundTransform).anchoredPosition = screenPoint; |
| | 115 | | } |
| 0 | 116 | | } |
| | 117 | | } |