| | 1 | | using DCL; |
| | 2 | | using DCL.Components; |
| | 3 | | using System.Collections; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using TMPro; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.UI; |
| | 8 | |
|
| | 9 | | public class SmartItemEntityParameter : SmartItemUIParameterAdapter, IEntityListHandler |
| | 10 | | { |
| | 11 | | public TMP_Dropdown dropDown; |
| | 12 | |
|
| | 13 | | private List<DCLBuilderInWorldEntity> entitiesList; |
| | 14 | |
|
| 2 | 15 | | private Dictionary<DCLBuilderInWorldEntity, Sprite> entitySpriteDict = new Dictionary<DCLBuilderInWorldEntity, Sprit |
| 2 | 16 | | private Dictionary<string, DCLBuilderInWorldEntity> entityPromiseKeeperDict = new Dictionary<string, DCLBuilderInWor |
| | 17 | |
|
| 0 | 18 | | private void Start() { dropDown.onValueChanged.AddListener(OnValueChange); } |
| | 19 | |
|
| 0 | 20 | | public void SetEntityList(List<DCLBuilderInWorldEntity> entitiesList) { this.entitiesList = entitiesList; } |
| | 21 | |
|
| | 22 | | public override void SetInfo() |
| | 23 | | { |
| 0 | 24 | | base.SetInfo(); |
| | 25 | |
|
| 0 | 26 | | GenerateDropdownContent(); |
| 0 | 27 | | foreach (DCLBuilderInWorldEntity entity in entitiesList) |
| | 28 | | { |
| 0 | 29 | | GetThumbnail(entity); |
| | 30 | | } |
| 0 | 31 | | } |
| | 32 | |
|
| | 33 | | void GenerateDropdownContent() |
| | 34 | | { |
| 0 | 35 | | dropDown.ClearOptions(); |
| | 36 | |
|
| 0 | 37 | | dropDown.options = new List<TMP_Dropdown.OptionData>(); |
| | 38 | |
|
| 0 | 39 | | List<TMP_Dropdown.OptionData> optionsList = new List<TMP_Dropdown.OptionData>(); |
| 0 | 40 | | foreach (DCLBuilderInWorldEntity entity in entitiesList) |
| | 41 | | { |
| 0 | 42 | | var item = new TMP_Dropdown.OptionData(); |
| 0 | 43 | | item.text = entity.GetDescriptiveName(); |
| 0 | 44 | | if (entitySpriteDict.ContainsKey(entity)) |
| 0 | 45 | | item.image = entitySpriteDict[entity]; |
| 0 | 46 | | optionsList.Add(item); |
| | 47 | | } |
| | 48 | |
|
| 0 | 49 | | dropDown.AddOptions(optionsList); |
| | 50 | |
|
| | 51 | |
|
| 0 | 52 | | string value = (string)GetParameterValue(); |
| | 53 | |
|
| 0 | 54 | | for (int i = 0; i < entitiesList.Count; i++) |
| | 55 | | { |
| 0 | 56 | | if (entitiesList[i].rootEntity.entityId == value) |
| 0 | 57 | | dropDown.SetValueWithoutNotify(i); |
| | 58 | | } |
| 0 | 59 | | } |
| | 60 | |
|
| | 61 | | private void GetThumbnail(DCLBuilderInWorldEntity entity) |
| | 62 | | { |
| 0 | 63 | | var url = entity.GetCatalogItemAssociated()?.thumbnailURL; |
| | 64 | |
|
| 0 | 65 | | if (string.IsNullOrEmpty(url)) |
| 0 | 66 | | return; |
| | 67 | |
|
| 0 | 68 | | string newLoadedThumbnailURL = url; |
| 0 | 69 | | var newLoadedThumbnailPromise = new AssetPromise_Texture(url); |
| | 70 | |
|
| 0 | 71 | | string promiseId = newLoadedThumbnailPromise.GetId().ToString(); |
| 0 | 72 | | if (!entityPromiseKeeperDict.ContainsKey(promiseId)) |
| 0 | 73 | | entityPromiseKeeperDict.Add(promiseId, entity); |
| 0 | 74 | | newLoadedThumbnailPromise.OnSuccessEvent += SetThumbnail; |
| 0 | 75 | | newLoadedThumbnailPromise.OnFailEvent += x => { Debug.Log($"Error downloading: {url}"); }; |
| | 76 | |
|
| 0 | 77 | | AssetPromiseKeeper_Texture.i.Keep(newLoadedThumbnailPromise); |
| 0 | 78 | | } |
| | 79 | |
|
| | 80 | | public void SetThumbnail(Asset_Texture texture) |
| | 81 | | { |
| 0 | 82 | | if (!entityPromiseKeeperDict.ContainsKey(texture.id.ToString())) |
| 0 | 83 | | return; |
| | 84 | |
|
| 0 | 85 | | Vector2 pivot = new Vector2(0.5f, 0.5f); |
| 0 | 86 | | Sprite spriteToUse = Sprite.Create(texture.texture, new Rect(0, 0, texture.width, texture.height), pivot); |
| 0 | 87 | | entitySpriteDict.Add(entityPromiseKeeperDict[texture.id.ToString()], spriteToUse); |
| 0 | 88 | | GenerateDropdownContent(); |
| 0 | 89 | | } |
| | 90 | |
|
| | 91 | | private void OnValueChange(int currentIndex) |
| | 92 | | { |
| 0 | 93 | | foreach (DCLBuilderInWorldEntity entity in entitiesList) |
| | 94 | | { |
| 0 | 95 | | if (entity.GetDescriptiveName() == dropDown.options[currentIndex].text) |
| 0 | 96 | | SetParameterValue(entity.rootEntity.entityId); |
| | 97 | | } |
| 0 | 98 | | } |
| | 99 | | } |