| | 1 | | using DCL; |
| | 2 | | using DCL.Components; |
| | 3 | | using DCL.Models; |
| | 4 | | using System.Collections; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using TMPro; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.UI; |
| | 9 | |
|
| | 10 | | public class SmartItemActionEventAdapter : MonoBehaviour |
| | 11 | | { |
| | 12 | | public TMP_Dropdown entityDropDown; |
| | 13 | | public TMP_Dropdown actionDropDown; |
| | 14 | | public TMP_Dropdown optionsDropDown; |
| | 15 | | public SmartItemListView smartItemListView; |
| | 16 | |
|
| | 17 | | public System.Action<SmartItemActionEventAdapter> OnActionableRemove; |
| | 18 | |
|
| | 19 | | private SmartItemActionEvent actionEvent; |
| | 20 | |
|
| | 21 | | private DCLBuilderInWorldEntity selectedEntity; |
| 2 | 22 | | private List<DCLBuilderInWorldEntity> filteredList = new List<DCLBuilderInWorldEntity>(); |
| | 23 | |
|
| 2 | 24 | | private Dictionary<DCLBuilderInWorldEntity, Sprite> entitySpriteDict = new Dictionary<DCLBuilderInWorldEntity, Sprit |
| 2 | 25 | | private Dictionary<string, DCLBuilderInWorldEntity> entityPromiseKeeperDict = new Dictionary<string, DCLBuilderInWor |
| | 26 | |
|
| | 27 | | private void Start() |
| | 28 | | { |
| 0 | 29 | | entityDropDown.onValueChanged.AddListener(SelectedEntity); |
| 0 | 30 | | actionDropDown.onValueChanged.AddListener(GenerateParametersFromIndex); |
| 0 | 31 | | optionsDropDown.onValueChanged.AddListener(OptionSelected); |
| 0 | 32 | | optionsDropDown.SetValueWithoutNotify(-1); |
| 0 | 33 | | } |
| | 34 | |
|
| | 35 | | private void OptionSelected(int index) |
| | 36 | | { |
| | 37 | | switch (index) |
| | 38 | | { |
| | 39 | | case 0: |
| 0 | 40 | | ResetActionable(); |
| 0 | 41 | | break; |
| | 42 | | case 1: |
| 0 | 43 | | RemoveActionable(); |
| | 44 | | break; |
| | 45 | | } |
| | 46 | |
|
| 0 | 47 | | optionsDropDown.SetValueWithoutNotify(1); |
| 0 | 48 | | } |
| | 49 | |
|
| 0 | 50 | | public SmartItemActionEvent GetContent() { return actionEvent; } |
| | 51 | |
|
| | 52 | | public void RemoveActionable() |
| | 53 | | { |
| 0 | 54 | | OnActionableRemove?.Invoke(this); |
| 0 | 55 | | Destroy(gameObject); |
| 0 | 56 | | } |
| | 57 | |
|
| | 58 | | public void ResetActionable() |
| | 59 | | { |
| 0 | 60 | | actionEvent.smartItemActionable.values.Clear(); |
| 0 | 61 | | SetContent(actionEvent); |
| 0 | 62 | | } |
| | 63 | |
|
| | 64 | | public void SetContent(SmartItemActionEvent actionEvent) |
| | 65 | | { |
| 0 | 66 | | this.actionEvent = actionEvent; |
| 0 | 67 | | filteredList = BuilderInWorldUtils.FilterEntitiesBySmartItemComponentAndActions(actionEvent.entityList); |
| | 68 | |
|
| 0 | 69 | | GenerateEntityDropdownContent(); |
| 0 | 70 | | foreach (DCLBuilderInWorldEntity entity in filteredList) |
| | 71 | | { |
| 0 | 72 | | GetThumbnail(entity); |
| | 73 | | } |
| | 74 | |
|
| 0 | 75 | | SelectedEntity(entityDropDown.value); |
| 0 | 76 | | } |
| | 77 | |
|
| | 78 | | private void SelectedEntity(int number) |
| | 79 | | { |
| 0 | 80 | | if (!filteredList[number].rootEntity.TryGetBaseComponent(CLASS_ID_COMPONENT.SMART_ITEM, out IEntityComponent com |
| | 81 | | { |
| 0 | 82 | | return; |
| | 83 | | } |
| | 84 | |
|
| 0 | 85 | | actionEvent.smartItemActionable.entityId = filteredList[number].rootEntity.entityId; |
| 0 | 86 | | selectedEntity = filteredList[number]; |
| 0 | 87 | | GenerateActionDropdownContent(filteredList[number].GetSmartItemActions()); |
| | 88 | |
|
| 0 | 89 | | GenerateParametersFromSelectedOption(); |
| 0 | 90 | | } |
| | 91 | |
|
| 0 | 92 | | private void GenerateParametersFromSelectedOption() { GenerateParametersFromIndex(actionDropDown.value); } |
| | 93 | |
|
| | 94 | | private void GenerateParametersFromIndex(int index) |
| | 95 | | { |
| 0 | 96 | | string label = actionDropDown.options[index].text; |
| | 97 | |
|
| 0 | 98 | | SmartItemAction selectedAction = null; |
| 0 | 99 | | foreach (SmartItemAction action in selectedEntity.GetSmartItemActions()) |
| | 100 | | { |
| 0 | 101 | | if (action.label == label) |
| | 102 | | { |
| 0 | 103 | | selectedAction = action; |
| 0 | 104 | | break; |
| | 105 | | } |
| | 106 | | } |
| | 107 | |
|
| 0 | 108 | | actionEvent.smartItemActionable.actionId = selectedAction.id; |
| 0 | 109 | | smartItemListView.SetEntityList(actionEvent.entityList); |
| 0 | 110 | | smartItemListView.SetSmartItemParameters(selectedAction.parameters, actionEvent.smartItemActionable.values); |
| 0 | 111 | | } |
| | 112 | |
|
| | 113 | | private void GenerateActionDropdownContent(SmartItemAction[] actions) |
| | 114 | | { |
| 0 | 115 | | actionDropDown.ClearOptions(); |
| | 116 | |
|
| 0 | 117 | | actionDropDown.options = new List<TMP_Dropdown.OptionData>(); |
| | 118 | |
|
| 0 | 119 | | List<string> optionsLabelList = new List<string>(); |
| 0 | 120 | | int index = 0; |
| 0 | 121 | | int indexToUse = 0; |
| | 122 | |
|
| 0 | 123 | | foreach (SmartItemAction action in actions) |
| | 124 | | { |
| 0 | 125 | | optionsLabelList.Add(action.label); |
| 0 | 126 | | if (!string.IsNullOrEmpty(actionEvent.smartItemActionable.actionId) && |
| | 127 | | action.id == actionEvent.smartItemActionable.actionId) |
| 0 | 128 | | indexToUse = index; |
| | 129 | |
|
| 0 | 130 | | index++; |
| | 131 | | } |
| | 132 | |
|
| 0 | 133 | | actionDropDown.AddOptions(optionsLabelList); |
| 0 | 134 | | actionDropDown.SetValueWithoutNotify(indexToUse); |
| 0 | 135 | | } |
| | 136 | |
|
| | 137 | | private void GenerateEntityDropdownContent() |
| | 138 | | { |
| 0 | 139 | | entityDropDown.ClearOptions(); |
| | 140 | |
|
| 0 | 141 | | entityDropDown.options = new List<TMP_Dropdown.OptionData>(); |
| | 142 | |
|
| 0 | 143 | | List<TMP_Dropdown.OptionData> optionsList = new List<TMP_Dropdown.OptionData>(); |
| | 144 | |
|
| 0 | 145 | | int index = 0; |
| 0 | 146 | | int indexToUse = 0; |
| | 147 | |
|
| 0 | 148 | | foreach (DCLBuilderInWorldEntity entity in filteredList) |
| | 149 | | { |
| 0 | 150 | | var item = new TMP_Dropdown.OptionData(); |
| 0 | 151 | | item.text = entity.GetDescriptiveName(); |
| 0 | 152 | | if (entitySpriteDict.ContainsKey(entity)) |
| 0 | 153 | | item.image = entitySpriteDict[entity]; |
| 0 | 154 | | optionsList.Add(item); |
| | 155 | |
|
| 0 | 156 | | if (!string.IsNullOrEmpty(actionEvent.smartItemActionable.entityId) && |
| | 157 | | entity.rootEntity.entityId == actionEvent.smartItemActionable.entityId) |
| 0 | 158 | | indexToUse = index; |
| | 159 | |
|
| 0 | 160 | | index++; |
| | 161 | | } |
| | 162 | |
|
| 0 | 163 | | entityDropDown.AddOptions(optionsList); |
| 0 | 164 | | entityDropDown.SetValueWithoutNotify(indexToUse); |
| 0 | 165 | | } |
| | 166 | |
|
| | 167 | | private void GetThumbnail(DCLBuilderInWorldEntity entity) |
| | 168 | | { |
| 0 | 169 | | var url = entity.GetCatalogItemAssociated()?.thumbnailURL; |
| | 170 | |
|
| 0 | 171 | | if (string.IsNullOrEmpty(url)) |
| 0 | 172 | | return; |
| | 173 | |
|
| 0 | 174 | | string newLoadedThumbnailURL = url; |
| 0 | 175 | | var newLoadedThumbnailPromise = new AssetPromise_Texture(url); |
| | 176 | |
|
| 0 | 177 | | string promiseId = newLoadedThumbnailPromise.GetId().ToString(); |
| 0 | 178 | | if (!entityPromiseKeeperDict.ContainsKey(promiseId)) |
| 0 | 179 | | entityPromiseKeeperDict.Add(promiseId, entity); |
| 0 | 180 | | newLoadedThumbnailPromise.OnSuccessEvent += SetThumbnail; |
| 0 | 181 | | newLoadedThumbnailPromise.OnFailEvent += x => { Debug.Log($"Error downloading: {url}"); }; |
| | 182 | |
|
| 0 | 183 | | AssetPromiseKeeper_Texture.i.Keep(newLoadedThumbnailPromise); |
| 0 | 184 | | } |
| | 185 | |
|
| | 186 | | public void SetThumbnail(Asset_Texture texture) |
| | 187 | | { |
| 0 | 188 | | if (!entityPromiseKeeperDict.ContainsKey(texture.id.ToString())) |
| 0 | 189 | | return; |
| 0 | 190 | | Sprite spriteToUse = Sprite.Create(texture.texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0 |
| 0 | 191 | | entitySpriteDict.Add(entityPromiseKeeperDict[texture.id.ToString()], spriteToUse); |
| 0 | 192 | | GenerateEntityDropdownContent(); |
| 0 | 193 | | } |
| | 194 | | } |