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