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