| | 1 | | using DCL; |
| | 2 | | using DCL.Components; |
| | 3 | | using DCL.Controllers; |
| | 4 | | using DCL.Models; |
| | 5 | | using System; |
| | 6 | | using UnityEngine; |
| | 7 | |
|
| | 8 | | public interface IEntityInformationController |
| | 9 | | { |
| | 10 | | event Action<Vector3> OnPositionChange; |
| | 11 | | event Action<Vector3> OnRotationChange; |
| | 12 | | event Action<Vector3> OnScaleChange; |
| | 13 | | event Action<BIWEntity, string> OnNameChange; |
| | 14 | | event Action<BIWEntity> OnSmartItemComponentUpdate; |
| | 15 | | event Action OnDisable; |
| | 16 | |
|
| | 17 | | void Initialize(IEntityInformationView view); |
| | 18 | | void Dispose(); |
| | 19 | | void PositionChanged(Vector3 pos); |
| | 20 | | void RotationChanged(Vector3 rot); |
| | 21 | | void ScaleChanged(Vector3 scale); |
| | 22 | | void NameChanged(BIWEntity entity, string name); |
| | 23 | | void ToggleDetailsInfo(); |
| | 24 | | void ToggleBasicInfo(); |
| | 25 | | void StartChangingName(); |
| | 26 | | void EndChangingName(); |
| | 27 | | void SetEntity(BIWEntity entity, IParcelScene currentScene); |
| | 28 | | void Enable(); |
| | 29 | | void Disable(); |
| | 30 | | void UpdateInfo(BIWEntity entity); |
| | 31 | | void UpdateEntitiesSelection(int numberOfSelectedEntities); |
| | 32 | | void SetTransparencyMode(bool isOn); |
| | 33 | | } |
| | 34 | |
|
| | 35 | | public class EntityInformationController : IEntityInformationController |
| | 36 | | { |
| | 37 | | private const string TRIS_TEXT_FORMAT = "{0} TRIS"; |
| | 38 | | private const string MATERIALS_TEXT_FORMAT = "{0} MATERIALS"; |
| | 39 | | private const string TEXTURES_TEXT_FORMAT = "{0} TEXTURES"; |
| | 40 | | private const float TRANSPARENCY_MODE_ALPHA_VALUE = 0.5f; |
| | 41 | |
|
| | 42 | | public event Action<Vector3> OnPositionChange; |
| | 43 | | public event Action<Vector3> OnRotationChange; |
| | 44 | | public event Action<Vector3> OnScaleChange; |
| | 45 | | public event Action<BIWEntity, string> OnNameChange; |
| | 46 | | public event Action<BIWEntity> OnSmartItemComponentUpdate; |
| | 47 | | public event Action OnDisable; |
| | 48 | |
|
| | 49 | | internal IEntityInformationView entityInformationView; |
| | 50 | | internal IParcelScene parcelScene; |
| | 51 | | internal AssetPromise_Texture loadedThumbnailPromise; |
| | 52 | | internal bool isChangingName = false; |
| | 53 | | internal BIWEntity currentEntity; |
| | 54 | |
|
| | 55 | | public void Initialize(IEntityInformationView entityInformationView) |
| | 56 | | { |
| 21 | 57 | | this.entityInformationView = entityInformationView; |
| | 58 | |
|
| 21 | 59 | | if (entityInformationView.position != null) |
| 0 | 60 | | entityInformationView.position.OnChanged += PositionChanged; |
| | 61 | |
|
| 21 | 62 | | if (entityInformationView.rotation != null) |
| 0 | 63 | | entityInformationView.rotation.OnChanged += RotationChanged; |
| | 64 | |
|
| 21 | 65 | | if (entityInformationView.scale != null) |
| 0 | 66 | | entityInformationView.scale.OnChanged += ScaleChanged; |
| | 67 | |
|
| 21 | 68 | | entityInformationView.OnNameChange += NameChanged; |
| 21 | 69 | | entityInformationView.OnStartChangingName += StartChangingName; |
| 21 | 70 | | entityInformationView.OnEndChangingName += EndChangingName; |
| 21 | 71 | | entityInformationView.OnDisable += Disable; |
| 21 | 72 | | entityInformationView.OnUpdateInfo += UpdateInfo; |
| 21 | 73 | | } |
| | 74 | |
|
| | 75 | | public void Dispose() |
| | 76 | | { |
| 21 | 77 | | if (entityInformationView.position != null) |
| 0 | 78 | | entityInformationView.position.OnChanged -= PositionChanged; |
| | 79 | |
|
| 21 | 80 | | if (entityInformationView.rotation != null) |
| 0 | 81 | | entityInformationView.rotation.OnChanged -= RotationChanged; |
| | 82 | |
|
| 21 | 83 | | if (entityInformationView.scale != null) |
| 0 | 84 | | entityInformationView.scale.OnChanged -= ScaleChanged; |
| | 85 | |
|
| 21 | 86 | | entityInformationView.OnNameChange -= NameChanged; |
| 21 | 87 | | entityInformationView.OnUpdateInfo -= UpdateInfo; |
| 21 | 88 | | entityInformationView.OnStartChangingName -= StartChangingName; |
| 21 | 89 | | entityInformationView.OnEndChangingName -= EndChangingName; |
| 21 | 90 | | entityInformationView.OnDisable -= Disable; |
| 21 | 91 | | } |
| | 92 | |
|
| 2 | 93 | | public void PositionChanged(Vector3 pos) { OnPositionChange?.Invoke(pos); } |
| | 94 | |
|
| | 95 | | public void RotationChanged(Vector3 rot) |
| | 96 | | { |
| 1 | 97 | | currentEntity?.SetRotation(rot); |
| 1 | 98 | | OnRotationChange?.Invoke(rot); |
| 1 | 99 | | } |
| | 100 | |
|
| 2 | 101 | | public void ScaleChanged(Vector3 scale) { OnScaleChange?.Invoke(scale); } |
| | 102 | |
|
| 2 | 103 | | public void NameChanged(BIWEntity entity, string name) { OnNameChange?.Invoke(entity, name); } |
| | 104 | |
|
| 2 | 105 | | public void ToggleDetailsInfo() { entityInformationView.ToggleDetailsInfo(); } |
| | 106 | |
|
| 2 | 107 | | public void ToggleBasicInfo() { entityInformationView.ToggleBasicInfo(); } |
| | 108 | |
|
| 0 | 109 | | public void StartChangingName() { isChangingName = true; } |
| | 110 | |
|
| 0 | 111 | | public void EndChangingName() { isChangingName = false; } |
| | 112 | |
|
| | 113 | | public void SetEntity(BIWEntity entity, IParcelScene currentScene) |
| | 114 | | { |
| 1 | 115 | | currentEntity = entity; |
| 1 | 116 | | EntityDeselected(); |
| 1 | 117 | | entityInformationView.SetCurrentEntity(entity); |
| | 118 | |
|
| 1 | 119 | | if (entityInformationView.currentEntity != null) |
| | 120 | | { |
| 0 | 121 | | entity.OnStatusUpdate -= UpdateEntityName; |
| 0 | 122 | | entityInformationView.currentEntity.OnStatusUpdate += UpdateEntityName; |
| | 123 | | } |
| | 124 | |
|
| 1 | 125 | | parcelScene = currentScene; |
| | 126 | |
|
| 1 | 127 | | if (entity.HasSmartItemComponent()) |
| | 128 | | { |
| 0 | 129 | | entityInformationView.SetSmartItemListViewActive(false); |
| | 130 | | //TODO: Remove this comment when we implement smart items in builder in world |
| | 131 | | //if (entity.rootEntity.TryGetBaseComponent(CLASS_ID_COMPONENT.SMART_ITEM, out IEntityComponent baseComponen |
| | 132 | | // entityInformationView.smartItemList.SetSmartItemParameters(entity.GetSmartItemParameters(), ((SmartItem |
| 0 | 133 | | } |
| | 134 | | else |
| | 135 | | { |
| 1 | 136 | | entityInformationView.SetSmartItemListViewActive(false); |
| | 137 | | } |
| | 138 | |
|
| 1 | 139 | | entityInformationView.SetEntityThumbnailEnable(false); |
| 1 | 140 | | CatalogItem entitySceneObject = entity.GetCatalogItemAssociated(); |
| 1 | 141 | | GetThumbnail(entitySceneObject); |
| 1 | 142 | | UpdateLimitsInformation(entitySceneObject); |
| 1 | 143 | | UpdateEntityName(entityInformationView.currentEntity); |
| 1 | 144 | | UpdateInfo(entityInformationView.currentEntity); |
| 1 | 145 | | } |
| | 146 | |
|
| | 147 | | internal void GetThumbnail(CatalogItem catalogItem) |
| | 148 | | { |
| 2 | 149 | | if (catalogItem == null) |
| 1 | 150 | | return; |
| | 151 | |
|
| 1 | 152 | | var url = catalogItem.thumbnailURL; |
| | 153 | |
|
| 1 | 154 | | if (string.IsNullOrEmpty(url)) |
| 0 | 155 | | return; |
| | 156 | |
|
| 1 | 157 | | var newLoadedThumbnailPromise = new AssetPromise_Texture(url); |
| 1 | 158 | | newLoadedThumbnailPromise.OnSuccessEvent += SetThumbnail; |
| 3 | 159 | | newLoadedThumbnailPromise.OnFailEvent += (x, error) => { Debug.Log($"Error downloading: {url}, Exception: {error |
| 1 | 160 | | AssetPromiseKeeper_Texture.i.Keep(newLoadedThumbnailPromise); |
| 1 | 161 | | AssetPromiseKeeper_Texture.i.Forget(loadedThumbnailPromise); |
| 1 | 162 | | loadedThumbnailPromise = newLoadedThumbnailPromise; |
| 1 | 163 | | } |
| | 164 | |
|
| | 165 | | internal void SetThumbnail(Asset_Texture texture) |
| | 166 | | { |
| 1 | 167 | | entityInformationView.SetEntityThumbnailEnable(true); |
| 1 | 168 | | entityInformationView.SetEntityThumbnailTexture(texture.texture); |
| 1 | 169 | | } |
| | 170 | |
|
| | 171 | | internal void UpdateEntityName(BIWEntity entity) |
| | 172 | | { |
| 2 | 173 | | if (entity == null) |
| 1 | 174 | | return; |
| | 175 | |
|
| 1 | 176 | | string currentName = entity.GetDescriptiveName(); |
| | 177 | |
|
| 1 | 178 | | if (!isChangingName) |
| 1 | 179 | | entityInformationView.SetNameIFText(currentName); |
| 1 | 180 | | } |
| | 181 | |
|
| | 182 | | internal void UpdateLimitsInformation(CatalogItem catalogItem) |
| | 183 | | { |
| 3 | 184 | | if (catalogItem == null) |
| | 185 | | { |
| 2 | 186 | | entityInformationView.SeEntityLimitsText("", "", ""); |
| 2 | 187 | | return; |
| | 188 | | } |
| | 189 | |
|
| 1 | 190 | | string trisText = string.Format(TRIS_TEXT_FORMAT, catalogItem.metrics.triangles); |
| 1 | 191 | | string materialText = string.Format(MATERIALS_TEXT_FORMAT, catalogItem.metrics.materials); |
| 1 | 192 | | string textureText = string.Format(TEXTURES_TEXT_FORMAT, catalogItem.metrics.textures); |
| | 193 | |
|
| 1 | 194 | | entityInformationView.SeEntityLimitsText(trisText, materialText, textureText); |
| 1 | 195 | | } |
| | 196 | |
|
| 2 | 197 | | public void Enable() { entityInformationView.SetActive(true); } |
| | 198 | |
|
| | 199 | | public void Disable() |
| | 200 | | { |
| 1 | 201 | | entityInformationView.SetActive(false); |
| 1 | 202 | | EntityDeselected(); |
| 1 | 203 | | entityInformationView.SetCurrentEntity(null); |
| 1 | 204 | | OnDisable?.Invoke(); |
| 1 | 205 | | } |
| | 206 | |
|
| | 207 | | internal void EntityDeselected() |
| | 208 | | { |
| 2 | 209 | | if (entityInformationView.currentEntity == null) |
| 2 | 210 | | return; |
| | 211 | |
|
| 0 | 212 | | if (entityInformationView.currentEntity.rootEntity.TryGetBaseComponent(CLASS_ID_COMPONENT.SMART_ITEM, out IEntit |
| | 213 | | { |
| 0 | 214 | | SmartItemComponent smartItemComponent = (SmartItemComponent) component; |
| 0 | 215 | | OnSmartItemComponentUpdate?.Invoke(entityInformationView.currentEntity); |
| | 216 | | } |
| 0 | 217 | | } |
| | 218 | |
|
| | 219 | | public void UpdateInfo(BIWEntity entity) |
| | 220 | | { |
| 2 | 221 | | if (entity != null && entity.rootEntity.gameObject != null) |
| | 222 | | { |
| 1 | 223 | | Vector3 positionConverted = WorldStateUtils.ConvertUnityToScenePosition(entity.rootEntity.gameObject.transfo |
| 1 | 224 | | Vector3 currentRotation = entity.rootEntity.gameObject.transform.rotation.eulerAngles; |
| 1 | 225 | | Vector3 currentScale = entity.rootEntity.gameObject.transform.lossyScale; |
| | 226 | |
|
| 1 | 227 | | currentRotation = entity.GetEulerRotation(); |
| | 228 | |
|
| 1 | 229 | | entityInformationView.SetPositionAttribute(positionConverted); |
| 1 | 230 | | entityInformationView.SetRotationAttribute(currentRotation); |
| 1 | 231 | | entityInformationView.SetScaleAttribute(currentScale); |
| | 232 | | } |
| 2 | 233 | | } |
| | 234 | |
|
| 4 | 235 | | public void UpdateEntitiesSelection(int numberOfSelectedEntities) { entityInformationView.UpdateEntitiesSelection(nu |
| | 236 | |
|
| 4 | 237 | | public void SetTransparencyMode(bool isOn) { entityInformationView.SetTransparencyMode(isOn ? TRANSPARENCY_MODE_ALPH |
| | 238 | | } |