| | 1 | | using DCL; |
| | 2 | | using DCL.Models; |
| | 3 | | using TMPro; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.EventSystems; |
| | 6 | | using UnityEngine.UI; |
| | 7 | |
|
| | 8 | | public class EntityListAdapter : MonoBehaviour |
| | 9 | | { |
| | 10 | | public Color entitySelectedColor; |
| | 11 | | public Color entityUnselectedColor; |
| | 12 | | public Color entityWithoutErrorsColor; |
| | 13 | | public Color entityErrorColor; |
| | 14 | | public Color iconsSelectedColor; |
| | 15 | | public Color iconsUnselectedColor; |
| | 16 | | public TMP_InputField nameInputField; |
| | 17 | | public TextMeshProUGUI nameInputField_Text; |
| | 18 | | public Image selectedImg; |
| | 19 | | public RawImage entityThumbnailImg; |
| | 20 | | public Button unlockButton; |
| | 21 | | public Button lockButton; |
| | 22 | | public Image showImg; |
| | 23 | | public Image textBoxImage; |
| | 24 | | public System.Action<EntityAction, BIWEntity, EntityListAdapter> OnActionInvoked; |
| | 25 | | public System.Action<BIWEntity, string> OnEntityRename; |
| | 26 | | BIWEntity currentEntity; |
| | 27 | | internal AssetPromise_Texture loadedThumbnailPromise; |
| | 28 | |
|
| | 29 | | private void Start() |
| | 30 | | { |
| 1 | 31 | | if (nameInputField != null) |
| | 32 | | { |
| 0 | 33 | | nameInputField.onSelect.AddListener((currentText) => SetTextboxActive(true)); |
| | 34 | |
|
| 0 | 35 | | nameInputField.onEndEdit.AddListener((newText) => |
| | 36 | | { |
| 0 | 37 | | Rename(newText); |
| 0 | 38 | | SetTextboxActive(false); |
| | 39 | |
|
| 0 | 40 | | if (EventSystem.current != null && !EventSystem.current.alreadySelecting) |
| 0 | 41 | | EventSystem.current.SetSelectedGameObject(null); |
| 0 | 42 | | }); |
| | 43 | |
|
| 0 | 44 | | nameInputField.onSubmit.AddListener((newText) => EventSystem.current?.SetSelectedGameObject(null)); |
| | 45 | | } |
| | 46 | |
|
| 1 | 47 | | SetTextboxActive(false); |
| 1 | 48 | | } |
| | 49 | |
|
| | 50 | | private void OnDestroy() |
| | 51 | | { |
| 1 | 52 | | if (nameInputField != null) |
| | 53 | | { |
| 0 | 54 | | nameInputField.onSelect.RemoveAllListeners(); |
| 0 | 55 | | nameInputField.onEndEdit.RemoveAllListeners(); |
| 0 | 56 | | nameInputField.onSubmit.RemoveAllListeners(); |
| | 57 | | } |
| | 58 | |
|
| 1 | 59 | | if (currentEntity != null) |
| | 60 | | { |
| 0 | 61 | | currentEntity.OnStatusUpdate -= SetInfo; |
| 0 | 62 | | currentEntity.OnDelete -= DeleteAdapter; |
| 0 | 63 | | currentEntity.OnErrorStatusChange -= SetEntityError; |
| | 64 | | } |
| 1 | 65 | | } |
| | 66 | |
|
| | 67 | | public void SetContent(BIWEntity decentrelandEntity) |
| | 68 | | { |
| 0 | 69 | | if (currentEntity != null) |
| | 70 | | { |
| 0 | 71 | | currentEntity.OnStatusUpdate -= SetInfo; |
| 0 | 72 | | currentEntity.OnDelete -= DeleteAdapter; |
| 0 | 73 | | currentEntity.OnErrorStatusChange -= SetEntityError; |
| | 74 | | } |
| | 75 | |
|
| 0 | 76 | | currentEntity = decentrelandEntity; |
| 0 | 77 | | currentEntity.OnStatusUpdate += SetInfo; |
| 0 | 78 | | currentEntity.OnDelete += DeleteAdapter; |
| 0 | 79 | | currentEntity.OnErrorStatusChange += SetEntityError; |
| | 80 | |
|
| 0 | 81 | | AllowNameEdition(false); |
| 0 | 82 | | SetInfo(decentrelandEntity); |
| | 83 | |
|
| 0 | 84 | | entityThumbnailImg.enabled = false; |
| 0 | 85 | | CatalogItem entitySceneObject = decentrelandEntity.GetCatalogItemAssociated(); |
| 0 | 86 | | GetThumbnail(entitySceneObject); |
| 0 | 87 | | } |
| | 88 | |
|
| | 89 | | public void SelectOrDeselect() |
| | 90 | | { |
| 0 | 91 | | if (currentEntity.isVisible) |
| 0 | 92 | | OnActionInvoked?.Invoke(EntityAction.SELECT, currentEntity, this); |
| 0 | 93 | | } |
| | 94 | |
|
| 0 | 95 | | public void ShowOrHide() { OnActionInvoked?.Invoke(EntityAction.SHOW, currentEntity, this); } |
| | 96 | |
|
| 0 | 97 | | public void LockOrUnlock() { OnActionInvoked?.Invoke(EntityAction.LOCK, currentEntity, this); } |
| | 98 | |
|
| 0 | 99 | | public void DeleteEntity() { OnActionInvoked?.Invoke(EntityAction.DELETE, currentEntity, this); } |
| | 100 | |
|
| | 101 | | void SetInfo(BIWEntity entityToEdit) |
| | 102 | | { |
| 0 | 103 | | if (this == null) |
| 0 | 104 | | return; |
| | 105 | |
|
| 0 | 106 | | if (string.IsNullOrEmpty(entityToEdit.GetDescriptiveName())) |
| 0 | 107 | | nameInputField.text = entityToEdit.rootEntity.entityId; |
| | 108 | | else |
| 0 | 109 | | nameInputField.text = entityToEdit.GetDescriptiveName(); |
| | 110 | |
|
| | 111 | | //NOTE (Adrian): this is done to force the text component to update, otherwise it won't show the text, seems lik |
| 0 | 112 | | nameInputField.textComponent.enabled = true; |
| | 113 | |
|
| | 114 | |
|
| 0 | 115 | | if (entityToEdit.isVisible) |
| 0 | 116 | | showImg.color = iconsSelectedColor; |
| | 117 | | else |
| 0 | 118 | | showImg.color = iconsUnselectedColor; |
| | 119 | |
|
| 0 | 120 | | CheckEntityNameColor(entityToEdit); |
| | 121 | |
|
| 0 | 122 | | unlockButton.gameObject.SetActive(!entityToEdit.isLocked); |
| 0 | 123 | | lockButton.gameObject.SetActive(entityToEdit.isLocked); |
| | 124 | |
|
| 0 | 125 | | if (entityToEdit.isSelected) |
| | 126 | | { |
| 0 | 127 | | AllowNameEdition(true); |
| 0 | 128 | | selectedImg.color = entitySelectedColor; |
| 0 | 129 | | } |
| | 130 | | else |
| | 131 | | { |
| 0 | 132 | | AllowNameEdition(false); |
| 0 | 133 | | selectedImg.color = entityUnselectedColor; |
| | 134 | | } |
| | 135 | |
|
| 0 | 136 | | SetEntityError(entityToEdit); |
| 0 | 137 | | } |
| | 138 | |
|
| | 139 | | internal void GetThumbnail(CatalogItem catalogItem) |
| | 140 | | { |
| 0 | 141 | | if (catalogItem == null) |
| 0 | 142 | | return; |
| | 143 | |
|
| 0 | 144 | | var url = catalogItem.thumbnailURL; |
| | 145 | |
|
| 0 | 146 | | if (string.IsNullOrEmpty(url)) |
| 0 | 147 | | return; |
| | 148 | |
|
| 0 | 149 | | var newLoadedThumbnailPromise = new AssetPromise_Texture(url); |
| 0 | 150 | | newLoadedThumbnailPromise.OnSuccessEvent += SetThumbnail; |
| 0 | 151 | | newLoadedThumbnailPromise.OnFailEvent += (x, error) => { Debug.Log($"Error downloading: {url}, Exception: {error |
| 0 | 152 | | AssetPromiseKeeper_Texture.i.Keep(newLoadedThumbnailPromise); |
| 0 | 153 | | AssetPromiseKeeper_Texture.i.Forget(loadedThumbnailPromise); |
| 0 | 154 | | loadedThumbnailPromise = newLoadedThumbnailPromise; |
| 0 | 155 | | } |
| | 156 | |
|
| | 157 | | internal void SetThumbnail(Asset_Texture texture) |
| | 158 | | { |
| 0 | 159 | | if (entityThumbnailImg == null) |
| 0 | 160 | | return; |
| 0 | 161 | | entityThumbnailImg.enabled = true; |
| 0 | 162 | | entityThumbnailImg.texture = texture.texture; |
| 0 | 163 | | } |
| | 164 | |
|
| | 165 | | public void Rename(string newName) |
| | 166 | | { |
| 0 | 167 | | if (!string.IsNullOrEmpty(newName)) |
| 0 | 168 | | OnEntityRename?.Invoke(currentEntity, newName); |
| | 169 | | else |
| 0 | 170 | | nameInputField.text = currentEntity.GetDescriptiveName(); |
| 0 | 171 | | } |
| | 172 | |
|
| 0 | 173 | | public void AllowNameEdition(bool isAllowed) { nameInputField.enabled = isAllowed; } |
| | 174 | |
|
| | 175 | | void DeleteAdapter(BIWEntity entityToEdit) |
| | 176 | | { |
| 0 | 177 | | if (this != null && entityToEdit.entityUniqueId == currentEntity.entityUniqueId) |
| 0 | 178 | | Destroy(gameObject); |
| 0 | 179 | | } |
| | 180 | |
|
| | 181 | | private void SetEntityError(BIWEntity entity) |
| | 182 | | { |
| 0 | 183 | | if (entity != currentEntity) |
| 0 | 184 | | return; |
| | 185 | |
|
| 0 | 186 | | CheckEntityNameColor(entity); |
| 0 | 187 | | } |
| | 188 | |
|
| | 189 | | private void SetTextboxActive(bool isActive) |
| | 190 | | { |
| 1 | 191 | | if (textBoxImage == null) |
| 1 | 192 | | return; |
| | 193 | |
|
| 0 | 194 | | textBoxImage.enabled = isActive; |
| 0 | 195 | | } |
| | 196 | |
|
| | 197 | | private void CheckEntityNameColor(BIWEntity entity) |
| | 198 | | { |
| 0 | 199 | | if (entity.hasError) |
| 0 | 200 | | nameInputField_Text.color = entityErrorColor; |
| 0 | 201 | | else if (!entity.isVisible || entity.isLocked) |
| 0 | 202 | | nameInputField_Text.color = iconsUnselectedColor; |
| | 203 | | else |
| 0 | 204 | | nameInputField_Text.color = entityWithoutErrorsColor; |
| 0 | 205 | | } |
| | 206 | | } |