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