| | 1 | | using DCL.Components; |
| | 2 | | using DCL.Models; |
| | 3 | | using System; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Linq; |
| | 6 | | using DCL.Builder; |
| | 7 | | using DCL.Controllers; |
| | 8 | | using UnityEngine; |
| | 9 | |
|
| | 10 | | namespace DCL.Builder |
| | 11 | | { |
| | 12 | | public class BIWCreatorController : BIWController, IBIWCreatorController |
| | 13 | | { |
| | 14 | | private const float SECONDS_TO_SEND_ANALYTICS = 5f; |
| | 15 | | public event Action OnInputDone; |
| | 16 | | public event Action OnCatalogItemPlaced; |
| | 17 | |
|
| | 18 | | private IBIWModeController modeController; |
| | 19 | |
|
| | 20 | | private IBIWFloorHandler floorHandler; |
| | 21 | | private IBIWEntityHandler entityHandler; |
| | 22 | |
|
| | 23 | | private GameObject loadingObjectPrefab; |
| | 24 | | private GameObject errorPrefab; |
| | 25 | |
|
| | 26 | | private CatalogItem lastCatalogItemCreated; |
| | 27 | |
|
| 14 | 28 | | private readonly Dictionary<string, BIWLoadingPlaceHolder> loadingGameObjects = new Dictionary<string, BIWLoadin |
| 14 | 29 | | private readonly Dictionary<BIWEntity, GameObject> errorGameObjects = new Dictionary<BIWEntity, GameObject>(); |
| | 30 | |
|
| 14 | 31 | | private readonly List<KeyValuePair<CatalogItem, string>> itemsToSendAnalytics = new List<KeyValuePair<CatalogIte |
| | 32 | |
|
| | 33 | | private float lastAnalyticsSentTimestamp = 0; |
| | 34 | |
|
| | 35 | | public override void Initialize(IContext context) |
| | 36 | | { |
| 14 | 37 | | base.Initialize(context); |
| | 38 | |
|
| 14 | 39 | | modeController = context.editorContext.modeController; |
| 14 | 40 | | floorHandler = context.editorContext.floorHandler; |
| 14 | 41 | | entityHandler = context.editorContext.entityHandler; |
| 14 | 42 | | loadingObjectPrefab = context.projectReferencesAsset.loadingPrefab; |
| 14 | 43 | | errorPrefab = context.projectReferencesAsset.errorPrefab; |
| | 44 | |
|
| 14 | 45 | | if ( context.editorContext.editorHUD != null) |
| | 46 | | { |
| 14 | 47 | | context.editorContext.editorHUD.OnCatalogItemSelected += OnCatalogItemSelected; |
| 14 | 48 | | context.editorContext.editorHUD.OnCatalogItemDropped += OnCatalogItemDropped; |
| | 49 | | } |
| 14 | 50 | | } |
| | 51 | |
|
| | 52 | | public override void Dispose() |
| | 53 | | { |
| 14 | 54 | | base.Dispose(); |
| 14 | 55 | | if ( context.editorContext.editorHUD != null) |
| | 56 | | { |
| 14 | 57 | | context.editorContext.editorHUD.OnCatalogItemSelected -= OnCatalogItemSelected; |
| 14 | 58 | | context.editorContext.editorHUD.OnCatalogItemDropped -= OnCatalogItemDropped; |
| | 59 | | } |
| | 60 | |
|
| 14 | 61 | | CleanUp(); |
| 14 | 62 | | } |
| | 63 | |
|
| | 64 | | public override void Update() |
| | 65 | | { |
| 0 | 66 | | base.Update(); |
| 0 | 67 | | if (Time.realtimeSinceStartup >= lastAnalyticsSentTimestamp) |
| | 68 | | { |
| 0 | 69 | | SendAnalytics(); |
| 0 | 70 | | lastAnalyticsSentTimestamp = Time.realtimeSinceStartup + SECONDS_TO_SEND_ANALYTICS; |
| | 71 | | } |
| 0 | 72 | | } |
| | 73 | |
|
| | 74 | | private void SendAnalytics() |
| | 75 | | { |
| 0 | 76 | | if (itemsToSendAnalytics.Count == 0) |
| 0 | 77 | | return; |
| | 78 | |
|
| 0 | 79 | | BIWAnalytics.NewObjectPlacedChunk(itemsToSendAnalytics); |
| 0 | 80 | | itemsToSendAnalytics.Clear(); |
| 0 | 81 | | } |
| | 82 | |
|
| | 83 | | public void CleanUp() |
| | 84 | | { |
| 42 | 85 | | foreach (BIWLoadingPlaceHolder placeHolder in loadingGameObjects.Values) |
| | 86 | | { |
| 7 | 87 | | placeHolder.Dispose(); |
| | 88 | | } |
| | 89 | |
|
| 38 | 90 | | foreach (BIWEntity entity in errorGameObjects.Keys.ToArray()) |
| | 91 | | { |
| 5 | 92 | | DeleteErrorOnEntity(entity); |
| | 93 | | } |
| | 94 | |
|
| 14 | 95 | | loadingGameObjects.Clear(); |
| 14 | 96 | | errorGameObjects.Clear(); |
| 14 | 97 | | } |
| | 98 | |
|
| 0 | 99 | | public bool IsAnyErrorOnEntities() { return errorGameObjects.Count > 0; } |
| | 100 | |
|
| | 101 | | private bool IsInsideTheLimits(CatalogItem sceneObject) |
| | 102 | | { |
| 15 | 103 | | if ( context.editorContext.editorHUD == null) |
| 0 | 104 | | return false; |
| | 105 | |
|
| 15 | 106 | | SceneMetricsModel limits = sceneToEdit.metricsCounter.maxCount; |
| 15 | 107 | | SceneMetricsModel usage = sceneToEdit.metricsCounter.currentCount; |
| | 108 | |
|
| 15 | 109 | | if (limits.bodies < usage.bodies + sceneObject.metrics.bodies) |
| | 110 | | { |
| 0 | 111 | | context.editorContext.editorHUD.ShowSceneLimitsPassed(); |
| 0 | 112 | | return false; |
| | 113 | | } |
| | 114 | |
|
| 15 | 115 | | if (limits.entities < usage.entities + sceneObject.metrics.entities) |
| | 116 | | { |
| 0 | 117 | | context.editorContext.editorHUD.ShowSceneLimitsPassed(); |
| 0 | 118 | | return false; |
| | 119 | | } |
| | 120 | |
|
| 15 | 121 | | if (limits.materials < usage.materials + sceneObject.metrics.materials) |
| | 122 | | { |
| 0 | 123 | | context.editorContext.editorHUD.ShowSceneLimitsPassed(); |
| 0 | 124 | | return false; |
| | 125 | | } |
| | 126 | |
|
| 15 | 127 | | if (limits.meshes < usage.meshes + sceneObject.metrics.meshes) |
| | 128 | | { |
| 0 | 129 | | context.editorContext.editorHUD.ShowSceneLimitsPassed(); |
| 0 | 130 | | return false; |
| | 131 | | } |
| | 132 | |
|
| 15 | 133 | | if (limits.textures < usage.textures + sceneObject.metrics.textures) |
| | 134 | | { |
| 0 | 135 | | context.editorContext.editorHUD.ShowSceneLimitsPassed(); |
| 0 | 136 | | return false; |
| | 137 | | } |
| | 138 | |
|
| 15 | 139 | | if (limits.triangles < usage.triangles + sceneObject.metrics.triangles) |
| | 140 | | { |
| 0 | 141 | | context.editorContext.editorHUD.ShowSceneLimitsPassed(); |
| 0 | 142 | | return false; |
| | 143 | | } |
| | 144 | |
|
| 15 | 145 | | return true; |
| | 146 | | } |
| | 147 | |
|
| | 148 | | public void CreateErrorOnEntity(BIWEntity entity) |
| | 149 | | { |
| 7 | 150 | | if (errorGameObjects.ContainsKey(entity)) |
| 0 | 151 | | return; |
| | 152 | |
|
| 7 | 153 | | GameObject instantiatedError = UnityEngine.Object.Instantiate(errorPrefab, Vector3.zero, errorPrefab.transfo |
| 7 | 154 | | instantiatedError.transform.SetParent(entity.rootEntity.gameObject.transform, true); |
| 7 | 155 | | instantiatedError.transform.localPosition = Vector3.zero; |
| | 156 | |
|
| 7 | 157 | | var missingAsset = instantiatedError.GetComponent<MissingAsset>(); |
| 7 | 158 | | missingAsset.Configure(entity); |
| | 159 | |
|
| 7 | 160 | | errorGameObjects.Add(entity, instantiatedError); |
| 7 | 161 | | entity.OnDelete += DeleteErrorOnEntity; |
| 7 | 162 | | } |
| | 163 | |
|
| | 164 | | public void DeleteErrorOnEntity(BIWEntity entity) |
| | 165 | | { |
| 7 | 166 | | if (!errorGameObjects.ContainsKey(entity)) |
| 0 | 167 | | return; |
| | 168 | |
|
| 7 | 169 | | entity.OnDelete -= DeleteErrorOnEntity; |
| 7 | 170 | | GameObject.Destroy(errorGameObjects[entity]); |
| 7 | 171 | | errorGameObjects.Remove(entity); |
| 7 | 172 | | } |
| | 173 | |
|
| 16 | 174 | | public void CreateCatalogItem(CatalogItem catalogItem, bool autoSelect = true, bool isFloor = false) { CreateCat |
| | 175 | |
|
| | 176 | | public BIWEntity CreateCatalogItem(CatalogItem catalogItem, Vector3 startPosition, bool autoSelect = true, bool |
| | 177 | | { |
| 15 | 178 | | if (catalogItem.IsNFT() && BIWNFTController.i.IsNFTInUse(catalogItem.id)) |
| 0 | 179 | | return null; |
| | 180 | |
|
| 15 | 181 | | IsInsideTheLimits(catalogItem); |
| | 182 | |
|
| 15 | 183 | | BIWUtils.AddSceneMappings(catalogItem.contents, BIWUrlUtils.GetUrlSceneObjectContent(), sceneToEdit.sceneDat |
| 15 | 184 | | DCL.Environment.i.world.sceneController.UpdateParcelScenesExecute(sceneToEdit.sceneData); |
| | 185 | |
|
| 15 | 186 | | Vector3 editionPosition = modeController.GetCurrentEditionPosition(); |
| | 187 | |
|
| 15 | 188 | | BIWEntity entity = entityHandler.CreateEmptyEntity(sceneToEdit, startPosition, editionPosition, false); |
| 15 | 189 | | entity.isFloor = isFloor; |
| 15 | 190 | | entity.SetRotation(Vector3.zero); |
| | 191 | |
|
| 15 | 192 | | if (!isFloor) |
| 8 | 193 | | CreateLoadingObject(entity); |
| | 194 | |
|
| 20 | 195 | | entity.rootEntity.OnShapeUpdated += (entity) => onFloorLoadedAction?.Invoke(entity); |
| | 196 | |
|
| 15 | 197 | | AddShape(catalogItem, entity); |
| 15 | 198 | | AddEntityNameComponent(catalogItem, entity); |
| 15 | 199 | | AddLockedComponent(entity); |
| | 200 | |
|
| 15 | 201 | | if (catalogItem.IsSmartItem()) |
| | 202 | | { |
| 0 | 203 | | AddSmartItemComponent(entity); |
| | 204 | | } |
| | 205 | |
|
| 15 | 206 | | if (catalogItem.IsVoxel()) |
| 0 | 207 | | entity.isVoxel = true; |
| | 208 | |
|
| 15 | 209 | | if (autoSelect) |
| | 210 | | { |
| 8 | 211 | | entityHandler.DeselectEntities(); |
| 8 | 212 | | entityHandler.Select(entity.rootEntity); |
| | 213 | | } |
| | 214 | |
|
| 15 | 215 | | entity.rootEntity.gameObject.transform.eulerAngles = Vector3.zero; |
| | 216 | |
|
| 15 | 217 | | modeController.CreatedEntity(entity); |
| | 218 | |
|
| 15 | 219 | | lastCatalogItemCreated = catalogItem; |
| | 220 | |
|
| 15 | 221 | | entityHandler.EntityListChanged(); |
| 15 | 222 | | entityHandler.NotifyEntityIsCreated(entity.rootEntity); |
| 15 | 223 | | OnInputDone?.Invoke(); |
| 15 | 224 | | OnCatalogItemPlaced?.Invoke(); |
| 15 | 225 | | return entity; |
| | 226 | | } |
| | 227 | |
|
| | 228 | | #region LoadingObjects |
| | 229 | |
|
| 2 | 230 | | public bool ExistsLoadingGameObjectForEntity(string entityId) { return loadingGameObjects.ContainsKey(entityId); |
| | 231 | |
|
| | 232 | | public void CreateLoadingObject(BIWEntity entity) |
| | 233 | | { |
| 10 | 234 | | if (loadingGameObjects.ContainsKey(entity.rootEntity.entityId)) |
| 1 | 235 | | return; |
| | 236 | |
|
| 9 | 237 | | BIWLoadingPlaceHolder loadingPlaceHolder = UnityEngine.Object.Instantiate(loadingObjectPrefab, entity.rootEn |
| 9 | 238 | | loadingGameObjects.Add(entity.rootEntity.entityId, loadingPlaceHolder); |
| 9 | 239 | | entity.OnShapeFinishLoading += OnShapeLoadFinish; |
| 9 | 240 | | } |
| | 241 | |
|
| | 242 | | private void OnShapeLoadFinish(BIWEntity entity) |
| | 243 | | { |
| 0 | 244 | | entity.OnShapeFinishLoading -= OnShapeLoadFinish; |
| 0 | 245 | | RemoveLoadingObject(entity.rootEntity.entityId); |
| 0 | 246 | | } |
| | 247 | |
|
| | 248 | | public void RemoveLoadingObject(string entityId) |
| | 249 | | { |
| 1 | 250 | | if (!loadingGameObjects.ContainsKey(entityId)) |
| 0 | 251 | | return; |
| 1 | 252 | | BIWLoadingPlaceHolder loadingPlaceHolder = loadingGameObjects[entityId]; |
| 1 | 253 | | loadingGameObjects.Remove(entityId); |
| 1 | 254 | | loadingPlaceHolder.DestroyAfterAnimation(); |
| 1 | 255 | | } |
| | 256 | |
|
| | 257 | | public void RemoveLoadingObjectInmediate(string entityId) |
| | 258 | | { |
| 6 | 259 | | if (!loadingGameObjects.ContainsKey(entityId)) |
| 5 | 260 | | return; |
| 1 | 261 | | BIWLoadingPlaceHolder loadingPlaceHolder = loadingGameObjects[entityId]; |
| 1 | 262 | | loadingGameObjects.Remove(entityId); |
| 1 | 263 | | loadingPlaceHolder.Dispose(); |
| 1 | 264 | | } |
| | 265 | |
|
| | 266 | | #endregion |
| | 267 | |
|
| | 268 | | #region Add Components |
| | 269 | |
|
| | 270 | | private void AddSmartItemComponent(BIWEntity entity) |
| | 271 | | { |
| | 272 | | //Note (Adrian): This will disable the smart item component until it is implemented in kernel |
| | 273 | | //TODO: After the implementation in kernel of smart items, we should eliminate this return |
| 0 | 274 | | return; |
| | 275 | | SmartItemComponent.Model model = new SmartItemComponent.Model(); |
| | 276 | | model.values = new Dictionary<object, object>(); |
| | 277 | |
|
| | 278 | | sceneToEdit.EntityComponentCreateOrUpdateWithModel(entity.rootEntity.entityId, CLASS_ID_COMPONENT.SMART_ITEM |
| | 279 | |
|
| | 280 | | //Note (Adrian): We can't wait to set the component 1 frame, so we set it |
| | 281 | | if (entity.rootEntity.TryGetBaseComponent(CLASS_ID_COMPONENT.SMART_ITEM, out IEntityComponent component)) |
| | 282 | | ((SmartItemComponent) component).UpdateFromModel(model); |
| | 283 | | } |
| | 284 | |
|
| | 285 | | private void AddEntityNameComponent(CatalogItem catalogItem, BIWEntity entity) |
| | 286 | | { |
| 15 | 287 | | entityHandler.SetEntityName(entity, catalogItem.name, false); |
| 15 | 288 | | } |
| | 289 | |
|
| | 290 | | private void AddLockedComponent(BIWEntity entity) |
| | 291 | | { |
| 15 | 292 | | DCLLockedOnEdit.Model model = new DCLLockedOnEdit.Model(); |
| 15 | 293 | | model.isLocked = entity.isFloor; |
| | 294 | |
|
| 15 | 295 | | EntityComponentsUtils.AddLockedOnEditComponent(sceneToEdit, entity.rootEntity, model, Guid.NewGuid().ToStrin |
| 15 | 296 | | } |
| | 297 | |
|
| | 298 | | private void AddShape(CatalogItem catalogItem, BIWEntity entity) |
| | 299 | | { |
| 15 | 300 | | if (catalogItem.IsNFT()) |
| | 301 | | { |
| 0 | 302 | | NFTShape.Model model = new NFTShape.Model(); |
| 0 | 303 | | model.color = new Color(0.6404918f, 0.611472f, 0.8584906f); |
| 0 | 304 | | model.src = catalogItem.model; |
| 0 | 305 | | model.assetId = catalogItem.id; |
| | 306 | |
|
| 0 | 307 | | NFTShape nftShape = EntityComponentsUtils.AddNFTShapeComponent(sceneToEdit, entity.rootEntity, model, ca |
| 0 | 308 | | nftShape.CallWhenReady(entity.ShapeLoadFinish); |
| 0 | 309 | | } |
| | 310 | | else |
| | 311 | | { |
| 15 | 312 | | LoadableShape.Model model = new LoadableShape.Model(); |
| 15 | 313 | | model.src = catalogItem.model; |
| 15 | 314 | | model.assetId = catalogItem.id; |
| | 315 | |
|
| 15 | 316 | | GLTFShape gltfComponent = EntityComponentsUtils.AddGLTFComponent(sceneToEdit, entity.rootEntity, model, |
| 15 | 317 | | gltfComponent.CallWhenReady(entity.ShapeLoadFinish); |
| | 318 | | } |
| 15 | 319 | | } |
| | 320 | |
|
| | 321 | | #endregion |
| | 322 | |
|
| | 323 | | private void AddSceneMappings(CatalogItem catalogItem) |
| | 324 | | { |
| 0 | 325 | | LoadParcelScenesMessage.UnityParcelScene data = sceneToEdit.sceneData; |
| 0 | 326 | | data.baseUrl = BIWUrlUtils.GetUrlSceneObjectContent(); |
| 0 | 327 | | if (data.contents == null) |
| 0 | 328 | | data.contents = new List<ContentServerUtils.MappingPair>(); |
| 0 | 329 | | foreach (KeyValuePair<string, string> content in catalogItem.contents) |
| | 330 | | { |
| 0 | 331 | | ContentServerUtils.MappingPair mappingPair = new ContentServerUtils.MappingPair(); |
| 0 | 332 | | mappingPair.file = content.Key; |
| 0 | 333 | | mappingPair.hash = content.Value; |
| 0 | 334 | | bool found = false; |
| 0 | 335 | | foreach (ContentServerUtils.MappingPair mappingPairToCheck in data.contents) |
| | 336 | | { |
| 0 | 337 | | if (mappingPairToCheck.file == mappingPair.file) |
| | 338 | | { |
| 0 | 339 | | found = true; |
| 0 | 340 | | break; |
| | 341 | | } |
| | 342 | | } |
| | 343 | |
|
| 0 | 344 | | if (!found) |
| 0 | 345 | | data.contents.Add(mappingPair); |
| | 346 | | } |
| | 347 | |
|
| 0 | 348 | | DCL.Environment.i.world.sceneController.UpdateParcelScenesExecute(data); |
| 0 | 349 | | } |
| | 350 | |
|
| | 351 | | public void CreateLastCatalogItem() |
| | 352 | | { |
| 1 | 353 | | if (lastCatalogItemCreated != null) |
| | 354 | | { |
| 1 | 355 | | if (entityHandler.IsAnyEntitySelected()) |
| 1 | 356 | | entityHandler.DeselectEntities(); |
| 1 | 357 | | OnCatalogItemSelected(lastCatalogItemCreated); |
| 1 | 358 | | OnInputDone?.Invoke(); |
| | 359 | | } |
| 0 | 360 | | } |
| | 361 | |
|
| | 362 | | private void OnCatalogItemSelected(CatalogItem catalogItem) |
| | 363 | | { |
| 1 | 364 | | if (floorHandler.IsCatalogItemFloor(catalogItem)) |
| | 365 | | { |
| 0 | 366 | | floorHandler.ChangeFloor(catalogItem); |
| 0 | 367 | | } |
| | 368 | | else |
| | 369 | | { |
| 1 | 370 | | CreateCatalogItem(catalogItem); |
| | 371 | | } |
| | 372 | |
|
| 1 | 373 | | string catalogSection = ""; |
| 1 | 374 | | if ( context.editorContext.editorHUD != null) |
| 1 | 375 | | catalogSection = context.editorContext.editorHUD.GetCatalogSectionSelected().ToString(); |
| | 376 | |
|
| 1 | 377 | | itemsToSendAnalytics.Add(new KeyValuePair<CatalogItem, string>(catalogItem, catalogSection)); |
| 1 | 378 | | } |
| | 379 | |
|
| | 380 | | private void OnCatalogItemDropped(CatalogItem catalogItem) |
| | 381 | | { |
| 0 | 382 | | OnCatalogItemSelected(catalogItem); |
| 0 | 383 | | entityHandler.DeselectEntities(); |
| 0 | 384 | | } |
| | 385 | | } |
| | 386 | | } |