| | 1 | | using System; |
| | 2 | | using TMPro; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.EventSystems; |
| | 5 | | using UnityEngine.UI; |
| | 6 | |
|
| | 7 | | public interface IEntityInformationView |
| | 8 | | { |
| | 9 | | BIWEntity currentEntity { get; set; } |
| | 10 | | AttributeXYZ position { get; } |
| | 11 | | AttributeXYZ rotation { get; } |
| | 12 | | AttributeXYZ scale { get; } |
| | 13 | | SmartItemListView smartItemList { get; } |
| | 14 | |
|
| | 15 | | event Action OnDisable; |
| | 16 | | event Action OnEndChangingName; |
| | 17 | | event Action<BIWEntity, string> OnNameChange; |
| | 18 | | event Action OnStartChangingName; |
| | 19 | | event Action<BIWEntity> OnUpdateInfo; |
| | 20 | |
|
| | 21 | | void ChangeEntityName(string newName); |
| | 22 | | void Disable(); |
| | 23 | | void EndChangingName(); |
| | 24 | | void SeEntityLimitsText(string tris, string mats, string textures); |
| | 25 | | void SetActive(bool isActive); |
| | 26 | | void SetCurrentEntity(BIWEntity entity); |
| | 27 | | void SetEntityThumbnailEnable(bool isEnable); |
| | 28 | | void SetEntityThumbnailTexture(Texture2D texture); |
| | 29 | | void SetNameIFText(string text); |
| | 30 | | void SetPositionAttribute(Vector3 newPos); |
| | 31 | | void SetRotationAttribute(Vector3 newRotation); |
| | 32 | | void SetScaleAttribute(Vector3 newScale); |
| | 33 | | void SetSmartItemListViewActive(bool isActive); |
| | 34 | | void StartChangingName(); |
| | 35 | | void ToggleBasicInfo(); |
| | 36 | | void ToggleDetailsInfo(); |
| | 37 | | void UpdateEntitiesSelection(int numberOfSelectedEntities); |
| | 38 | | void SetTransparencyMode(float alphaValue, bool interactable = true); |
| | 39 | | } |
| | 40 | |
|
| | 41 | | public class EntityInformationView : MonoBehaviour, IEntityInformationView |
| | 42 | | { |
| | 43 | | [Header("Sprites")] |
| | 44 | | [SerializeField] internal Sprite openMenuSprite; |
| | 45 | |
|
| | 46 | | [SerializeField] internal Sprite closeMenuSprite; |
| | 47 | |
|
| | 48 | | [Header("Prefab references")] |
| | 49 | | [SerializeField] internal CanvasGroup canvasGroup; |
| | 50 | |
|
| | 51 | | [SerializeField] internal GameObject individualEntityPanel; |
| | 52 | | [SerializeField] internal GameObject multipleEntitiesPanel; |
| | 53 | | [SerializeField] internal TextMeshProUGUI multipleEntitiesText; |
| | 54 | | [SerializeField] internal TextMeshProUGUI entityLimitsTrisTxt; |
| | 55 | | [SerializeField] internal TextMeshProUGUI entityLimitsMaterialsTxt; |
| | 56 | | [SerializeField] internal TextMeshProUGUI entityLimitsTextureTxt; |
| | 57 | | [SerializeField] internal TMP_InputField nameIF; |
| | 58 | | [SerializeField] internal Image nameIFTextBoxImage; |
| | 59 | | [SerializeField] internal RawImage entitytTumbailImg; |
| | 60 | | [SerializeField] internal AttributeXYZ positionAttribute; |
| | 61 | | [SerializeField] internal AttributeXYZ rotationAttribute; |
| | 62 | | [SerializeField] internal AttributeXYZ scaleAttribute; |
| | 63 | | [SerializeField] internal GameObject detailsGO; |
| | 64 | | [SerializeField] internal GameObject basicsGO; |
| | 65 | | [SerializeField] internal Image detailsToggleBtn; |
| | 66 | | [SerializeField] internal Image basicToggleBtn; |
| | 67 | | [SerializeField] internal SmartItemListView smartItemListView; |
| | 68 | | [SerializeField] internal Button backButton; |
| | 69 | | [SerializeField] internal Button detailsToggleButton; |
| | 70 | | [SerializeField] internal Button basicInfoTogglekButton; |
| | 71 | |
|
| 0 | 72 | | public BIWEntity currentEntity { get; set; } |
| 0 | 73 | | public AttributeXYZ position => positionAttribute; |
| 0 | 74 | | public AttributeXYZ rotation => rotationAttribute; |
| 0 | 75 | | public AttributeXYZ scale => scaleAttribute; |
| 0 | 76 | | public SmartItemListView smartItemList => smartItemListView; |
| | 77 | |
|
| | 78 | | public event Action<BIWEntity, string> OnNameChange; |
| | 79 | | public event Action<BIWEntity> OnUpdateInfo; |
| | 80 | | public event Action OnStartChangingName; |
| | 81 | | public event Action OnEndChangingName; |
| | 82 | | public event Action OnDisable; |
| | 83 | |
|
| | 84 | | internal const int FRAMES_BETWEEN_UPDATES = 5; |
| | 85 | | private const string VIEW_PATH = "Common/EntityInformationView"; |
| | 86 | |
|
| | 87 | | internal static EntityInformationView Create() |
| | 88 | | { |
| 22 | 89 | | var view = Instantiate(Resources.Load<GameObject>(VIEW_PATH)).GetComponent<EntityInformationView>(); |
| 22 | 90 | | view.gameObject.name = "_EntityInformationView"; |
| | 91 | |
|
| 22 | 92 | | return view; |
| | 93 | | } |
| | 94 | |
|
| | 95 | | private void Awake() |
| | 96 | | { |
| 22 | 97 | | backButton.onClick.AddListener(Disable); |
| 22 | 98 | | detailsToggleButton.onClick.AddListener(ToggleDetailsInfo); |
| 22 | 99 | | basicInfoTogglekButton.onClick.AddListener(ToggleBasicInfo); |
| 22 | 100 | | nameIF.onEndEdit.AddListener((newName) => |
| | 101 | | { |
| 0 | 102 | | ChangeEntityName(newName); |
| 0 | 103 | | SetNameIFTextboxActive(false); |
| 0 | 104 | | }); |
| 22 | 105 | | nameIF.onSelect.AddListener((newName) => |
| | 106 | | { |
| 0 | 107 | | SetNameIFTextboxActive(true); |
| 0 | 108 | | StartChangingName(); |
| 0 | 109 | | }); |
| 22 | 110 | | nameIF.onSubmit.AddListener((newText) => EventSystem.current?.SetSelectedGameObject(null)); |
| 22 | 111 | | nameIF.onDeselect.AddListener((newName) => EndChangingName()); |
| | 112 | |
|
| 22 | 113 | | SetNameIFTextboxActive(false); |
| 22 | 114 | | } |
| | 115 | |
|
| | 116 | | private void OnDestroy() |
| | 117 | | { |
| 22 | 118 | | backButton.onClick.RemoveListener(Disable); |
| 22 | 119 | | detailsToggleButton.onClick.RemoveListener(ToggleDetailsInfo); |
| 22 | 120 | | basicInfoTogglekButton.onClick.RemoveListener(ToggleBasicInfo); |
| 22 | 121 | | nameIF.onEndEdit.RemoveAllListeners(); |
| 22 | 122 | | nameIF.onSelect.RemoveAllListeners(); |
| 22 | 123 | | nameIF.onSubmit.RemoveAllListeners(); |
| 22 | 124 | | nameIF.onDeselect.RemoveAllListeners(); |
| 22 | 125 | | } |
| | 126 | |
|
| | 127 | | private void LateUpdate() |
| | 128 | | { |
| 0 | 129 | | if (currentEntity == null) |
| 0 | 130 | | return; |
| | 131 | |
|
| 0 | 132 | | if (Time.frameCount % FRAMES_BETWEEN_UPDATES == 0) |
| 0 | 133 | | OnUpdateInfo?.Invoke(currentEntity); |
| 0 | 134 | | } |
| | 135 | |
|
| 0 | 136 | | public void SetCurrentEntity(BIWEntity entity) { currentEntity = entity; } |
| | 137 | |
|
| | 138 | | public void ToggleDetailsInfo() |
| | 139 | | { |
| 2 | 140 | | detailsGO.SetActive(!detailsGO.activeSelf); |
| 2 | 141 | | detailsToggleBtn.sprite = detailsGO.activeSelf ? openMenuSprite : closeMenuSprite; |
| 2 | 142 | | } |
| | 143 | |
|
| | 144 | | public void ToggleBasicInfo() |
| | 145 | | { |
| 2 | 146 | | basicsGO.SetActive(!basicsGO.activeSelf); |
| 2 | 147 | | basicToggleBtn.sprite = basicsGO.activeSelf ? openMenuSprite : closeMenuSprite; |
| 2 | 148 | | } |
| | 149 | |
|
| 2 | 150 | | public void StartChangingName() { OnStartChangingName?.Invoke(); } |
| | 151 | |
|
| 2 | 152 | | public void EndChangingName() { OnEndChangingName?.Invoke(); } |
| | 153 | |
|
| | 154 | | public void ChangeEntityName(string newName) |
| | 155 | | { |
| 1 | 156 | | if (!string.IsNullOrEmpty(newName)) |
| 1 | 157 | | OnNameChange?.Invoke(currentEntity, newName); |
| | 158 | | else |
| 0 | 159 | | SetNameIFText(currentEntity.GetDescriptiveName()); |
| 0 | 160 | | } |
| | 161 | |
|
| 2 | 162 | | public void Disable() { OnDisable?.Invoke(); } |
| | 163 | |
|
| | 164 | | public void SetEntityThumbnailEnable(bool isEnable) |
| | 165 | | { |
| 2 | 166 | | if (entitytTumbailImg == null) |
| 0 | 167 | | return; |
| | 168 | |
|
| 2 | 169 | | entitytTumbailImg.enabled = isEnable; |
| 2 | 170 | | } |
| | 171 | |
|
| | 172 | | public void SetEntityThumbnailTexture(Texture2D texture) |
| | 173 | | { |
| 1 | 174 | | if (entitytTumbailImg == null) |
| 0 | 175 | | return; |
| | 176 | |
|
| 1 | 177 | | entitytTumbailImg.texture = texture; |
| 1 | 178 | | } |
| | 179 | |
|
| | 180 | | public void SeEntityLimitsText(string tris, string mats, string textures) |
| | 181 | | { |
| 1 | 182 | | entityLimitsTrisTxt.text = tris; |
| 1 | 183 | | entityLimitsMaterialsTxt.text = mats; |
| 1 | 184 | | entityLimitsTextureTxt.text = textures; |
| 1 | 185 | | } |
| | 186 | |
|
| 4 | 187 | | public void SetSmartItemListViewActive(bool isActive) { smartItemListView.gameObject.SetActive(isActive); } |
| | 188 | |
|
| 2 | 189 | | public void SetNameIFText(string text) { nameIF.SetTextWithoutNotify(text); } |
| | 190 | |
|
| 4 | 191 | | public void SetActive(bool isActive) { gameObject.SetActive(isActive); } |
| | 192 | |
|
| 0 | 193 | | public void SetPositionAttribute(Vector3 newPos) { positionAttribute.SetValues(newPos); } |
| | 194 | |
|
| 0 | 195 | | public void SetRotationAttribute(Vector3 newRotation) { rotationAttribute.SetValues(newRotation); } |
| | 196 | |
|
| 0 | 197 | | public void SetScaleAttribute(Vector3 newScale) { scaleAttribute.SetValues(newScale); } |
| | 198 | |
|
| | 199 | | public void UpdateEntitiesSelection(int numberOfSelectedEntities) |
| | 200 | | { |
| 2 | 201 | | if (numberOfSelectedEntities > 1) |
| | 202 | | { |
| 1 | 203 | | individualEntityPanel.SetActive(false); |
| 1 | 204 | | multipleEntitiesPanel.SetActive(true); |
| 1 | 205 | | multipleEntitiesText.text = $"{numberOfSelectedEntities} entities selected"; |
| 1 | 206 | | } |
| | 207 | | else |
| | 208 | | { |
| 1 | 209 | | individualEntityPanel.SetActive(true); |
| 1 | 210 | | multipleEntitiesPanel.SetActive(false); |
| | 211 | | } |
| 1 | 212 | | } |
| | 213 | |
|
| | 214 | | public void SetTransparencyMode(float alphaValue, bool interactable = true) |
| | 215 | | { |
| 2 | 216 | | canvasGroup.alpha = alphaValue; |
| 2 | 217 | | canvasGroup.blocksRaycasts = interactable; |
| 2 | 218 | | canvasGroup.interactable = interactable; |
| 2 | 219 | | } |
| | 220 | |
|
| | 221 | | private void SetNameIFTextboxActive(bool isActive) |
| | 222 | | { |
| 22 | 223 | | if (nameIFTextBoxImage == null) |
| 0 | 224 | | return; |
| | 225 | |
|
| 22 | 226 | | nameIFTextBoxImage.enabled = isActive; |
| 22 | 227 | | } |
| | 228 | | } |