| | 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 | | { |
| 667 | 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 | | { |
| 89 | 28 | | i = this; |
| 89 | 29 | | mainCamera = Camera.main; |
| 89 | 30 | | backgroundTransform.gameObject.SetActive(false); |
| 89 | 31 | | } |
| | 32 | |
|
| | 33 | | public void Setup(string button, string feedbackText, IDCLEntity entity) |
| | 34 | | { |
| 278 | 35 | | text.text = feedbackText; |
| 278 | 36 | | this.entity = entity; |
| | 37 | |
|
| 278 | 38 | | ConfigureIcon(button); |
| | 39 | |
|
| 278 | 40 | | canvas.enabled = enabled && isHovered; |
| 278 | 41 | | } |
| | 42 | |
|
| | 43 | | void ConfigureIcon(string button) |
| | 44 | | { |
| 278 | 45 | | hoverIcon?.SetActive(false); |
| | 46 | |
|
| | 47 | | switch (button) |
| | 48 | | { |
| | 49 | | case ACTION_BUTTON_POINTER: |
| 0 | 50 | | hoverIcon = icons[0]; |
| 0 | 51 | | break; |
| | 52 | | case ACTION_BUTTON_PRIMARY: |
| 1 | 53 | | hoverIcon = icons[1]; |
| 1 | 54 | | break; |
| | 55 | | case ACTION_BUTTON_SECONDARY: |
| 0 | 56 | | hoverIcon = icons[2]; |
| 0 | 57 | | break; |
| | 58 | | default: // ANY |
| 277 | 59 | | hoverIcon = icons[3]; |
| | 60 | | break; |
| | 61 | | } |
| | 62 | |
|
| 278 | 63 | | hoverIcon.SetActive(true); |
| 278 | 64 | | backgroundTransform.gameObject.SetActive(true); |
| 278 | 65 | | } |
| | 66 | |
|
| | 67 | | public void SetHoverState(bool hoverState) |
| | 68 | | { |
| 603 | 69 | | if (!enabled || hoverState == isHovered) |
| 579 | 70 | | return; |
| | 71 | |
|
| 24 | 72 | | isHovered = hoverState; |
| | 73 | |
|
| 24 | 74 | | canvas.enabled = isHovered; |
| 24 | 75 | | } |
| | 76 | |
|
| 0 | 77 | | public GameObject GetCurrentHoverIcon() { return hoverIcon; } |
| | 78 | |
|
| | 79 | | // This method will be used when we apply a "loose aim" for the 3rd person camera |
| | 80 | | void CalculateMeshCenteredPos(DCLTransform.Model transformModel = null) |
| | 81 | | { |
| 0 | 82 | | if (!canvas.enabled) |
| 0 | 83 | | return; |
| | 84 | |
|
| 0 | 85 | | if (entity.meshesInfo.renderers == null || entity.meshesInfo.renderers.Length == 0) |
| | 86 | | { |
| 0 | 87 | | meshCenteredPos = transform.parent.position; |
| 0 | 88 | | } |
| | 89 | | else |
| | 90 | | { |
| 0 | 91 | | Vector3 sum = Vector3.zero; |
| 0 | 92 | | for (int i = 0; i < entity.meshesInfo.renderers.Length; i++) |
| | 93 | | { |
| 0 | 94 | | sum += entity.meshesInfo.renderers[i].bounds.center; |
| | 95 | | } |
| | 96 | |
|
| 0 | 97 | | meshCenteredPos = sum / entity.meshesInfo.renderers.Length; |
| | 98 | | } |
| 0 | 99 | | } |
| | 100 | |
|
| | 101 | | // This method will be used when we apply a "loose aim" for the 3rd person camera |
| | 102 | | public void UpdateSizeAndPos() |
| | 103 | | { |
| 0 | 104 | | if (mainCamera == null) |
| 0 | 105 | | mainCamera = Camera.main; |
| | 106 | |
|
| 0 | 107 | | Vector3 screenPoint = mainCamera.WorldToViewportPoint(meshCenteredPos); |
| | 108 | |
|
| 0 | 109 | | if (screenPoint.z > 0) |
| | 110 | | { |
| 0 | 111 | | RectTransform canvasRect = (RectTransform) canvas.transform; |
| 0 | 112 | | float width = canvasRect.rect.width; |
| 0 | 113 | | float height = canvasRect.rect.height; |
| 0 | 114 | | screenPoint.Scale(new Vector3(width, height, 0)); |
| | 115 | |
|
| 0 | 116 | | ((RectTransform) backgroundTransform).anchoredPosition = screenPoint; |
| | 117 | | } |
| 0 | 118 | | } |
| | 119 | | } |