| | 1 | | using DCL; |
| | 2 | | using DCL.Configuration; |
| | 3 | | using DCL.Controllers; |
| | 4 | | using DCL.Tutorial; |
| | 5 | | using Newtonsoft.Json; |
| | 6 | | using System.Collections; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.Linq; |
| | 9 | | using DCL.Builder; |
| | 10 | | using UnityEngine; |
| | 11 | | using Environment = DCL.Environment; |
| | 12 | |
|
| | 13 | | public class BuilderInWorldEditor : IBIWEditor |
| | 14 | | { |
| | 15 | | internal const string AVATAR_HIDE_CONSTRAINT = "BuilderInWorld_Avatar_Invisible"; |
| | 16 | |
|
| | 17 | | private GameObject cursorGO; |
| | 18 | | private GameObject[] groundVisualsGO; |
| | 19 | |
|
| 9 | 20 | | internal IBIWOutlinerController outlinerController => context.editorContext.outlinerController; |
| 8 | 21 | | internal IBIWInputHandler inputHandler => context.editorContext.inputHandler; |
| 8 | 22 | | internal IBIWPublishController publishController => context.editorContext.publishController; |
| 16 | 23 | | internal IBIWCreatorController creatorController => context.editorContext.creatorController; |
| 8 | 24 | | internal IBIWModeController modeController => context.editorContext.modeController; |
| 38 | 25 | | internal IBIWFloorHandler floorHandler => context.editorContext.floorHandler; |
| 8 | 26 | | internal IBIWEntityHandler entityHandler => context.editorContext.entityHandler; |
| 8 | 27 | | internal IBIWActionController actionController => context.editorContext.actionController; |
| 8 | 28 | | internal IBIWSaveController saveController => context.editorContext.saveController; |
| 8 | 29 | | internal IBIWInputWrapper inputWrapper => context.editorContext.inputWrapper; |
| 8 | 30 | | internal IBIWRaycastController raycastController => context.editorContext.raycastController; |
| 8 | 31 | | internal IBIWGizmosController gizmosController => context.editorContext.gizmosController; |
| | 32 | |
|
| | 33 | | private BuilderInWorldBridge builderInWorldBridge; |
| | 34 | | private BuilderInWorldAudioHandler biwAudioHandler; |
| | 35 | | private DCL.Camera.CameraController mainCameraController; |
| | 36 | |
|
| | 37 | | internal IContext context; |
| | 38 | |
|
| 8 | 39 | | private readonly List<IBIWController> controllers = new List<IBIWController>(); |
| | 40 | | private Material skyBoxMaterial; |
| | 41 | |
|
| 0 | 42 | | public bool isBuilderInWorldActivated { get; internal set; } = false; |
| | 43 | |
|
| | 44 | | private bool isInit = false; |
| | 45 | | private Material previousSkyBoxMaterial; |
| | 46 | | private PlayerAvatarController avatarRenderer; |
| | 47 | |
|
| | 48 | | private float startEditorTimeStamp = 0; |
| | 49 | | internal IBuilderScene sceneToEdit; |
| | 50 | |
|
| | 51 | | public void Initialize(IContext context) |
| | 52 | | { |
| 8 | 53 | | if (isInit) |
| 0 | 54 | | return; |
| | 55 | |
|
| 8 | 56 | | isInit = true; |
| | 57 | |
|
| 8 | 58 | | this.context = context; |
| | 59 | |
|
| 8 | 60 | | InitReferences(SceneReferences.i); |
| | 61 | |
|
| 8 | 62 | | BIWNFTController.i.OnNFTUsageChange += OnNFTUsageChange; |
| | 63 | |
|
| 8 | 64 | | InitHUD(context); |
| | 65 | |
|
| 8 | 66 | | InitControllers(); |
| | 67 | |
|
| 8 | 68 | | CommonScriptableObjects.builderInWorldNotNecessaryUIVisibilityStatus.Set(true); |
| | 69 | |
|
| 8 | 70 | | mainCameraController = context.sceneReferences.cameraController.GetComponent<DCL.Camera.CameraController>(); |
| 8 | 71 | | biwAudioHandler = UnityEngine.Object.Instantiate(context.projectReferencesAsset.audioPrefab, Vector3.zero, Quate |
| 8 | 72 | | biwAudioHandler.Initialize(context); |
| 8 | 73 | | biwAudioHandler.gameObject.SetActive(false); |
| | 74 | |
|
| 8 | 75 | | floorHandler.OnAllParcelsFloorLoaded += NewSceneFloorLoaded; |
| | 76 | |
|
| 8 | 77 | | avatarRenderer = context.sceneReferences.playerAvatarController.GetComponent<PlayerAvatarController>(); |
| 8 | 78 | | } |
| | 79 | |
|
| | 80 | | public void InitReferences(SceneReferences sceneReferences) |
| | 81 | | { |
| 8 | 82 | | builderInWorldBridge = sceneReferences.biwBridgeGameObject.GetComponent<BuilderInWorldBridge>(); |
| 8 | 83 | | cursorGO = sceneReferences.cursorCanvas; |
| | 84 | |
|
| 8 | 85 | | List<GameObject> grounds = new List<GameObject>(); |
| | 86 | |
|
| 8 | 87 | | if (sceneReferences.groundVisual != null) |
| | 88 | | { |
| 0 | 89 | | for (int i = 0; i < sceneReferences.groundVisual.transform.transform.childCount; i++) |
| | 90 | | { |
| 0 | 91 | | grounds.Add(sceneReferences.groundVisual.transform.transform.GetChild(i).gameObject); |
| | 92 | | } |
| | 93 | | } |
| | 94 | |
|
| 8 | 95 | | groundVisualsGO = grounds.ToArray(); |
| 8 | 96 | | skyBoxMaterial = context.projectReferencesAsset.skyBoxMaterial; |
| 8 | 97 | | } |
| | 98 | |
|
| | 99 | | private void InitHUD(IContext context) |
| | 100 | | { |
| 8 | 101 | | context.editorContext.editorHUD.Initialize(context); |
| 8 | 102 | | context.editorContext.editorHUD.OnTutorialAction += StartTutorial; |
| 8 | 103 | | context.editorContext.editorHUD.OnProjectNameAndDescriptionChanged += ChangeProjectNameAndDescription; |
| 8 | 104 | | } |
| | 105 | |
|
| | 106 | | public void Dispose() |
| | 107 | | { |
| 8 | 108 | | if (context.editorContext.editorHUD != null) |
| | 109 | | { |
| 8 | 110 | | context.editorContext.editorHUD.OnProjectNameAndDescriptionChanged -= ChangeProjectNameAndDescription; |
| 8 | 111 | | context.editorContext.editorHUD.OnTutorialAction -= StartTutorial; |
| | 112 | | } |
| | 113 | |
|
| 8 | 114 | | floorHandler.OnAllParcelsFloorLoaded -= NewSceneFloorLoaded; |
| 8 | 115 | | BIWNFTController.i.OnNFTUsageChange -= OnNFTUsageChange; |
| 8 | 116 | | BIWNFTController.i.Dispose(); |
| | 117 | |
|
| 8 | 118 | | CleanItems(); |
| | 119 | |
|
| 8 | 120 | | if (biwAudioHandler.gameObject != null) |
| | 121 | | { |
| 8 | 122 | | biwAudioHandler.Dispose(); |
| 8 | 123 | | UnityEngine.Object.Destroy(biwAudioHandler.gameObject); |
| | 124 | | } |
| 8 | 125 | | } |
| | 126 | |
|
| | 127 | | public void OnGUI() |
| | 128 | | { |
| 1 | 129 | | if (!isBuilderInWorldActivated) |
| 0 | 130 | | return; |
| | 131 | |
|
| 28 | 132 | | foreach (var controller in controllers) |
| | 133 | | { |
| 13 | 134 | | controller.OnGUI(); |
| | 135 | | } |
| 1 | 136 | | } |
| | 137 | |
|
| | 138 | | public void Update() |
| | 139 | | { |
| 2 | 140 | | if (!isBuilderInWorldActivated) |
| 1 | 141 | | return; |
| | 142 | |
|
| 28 | 143 | | foreach (var controller in controllers) |
| | 144 | | { |
| 13 | 145 | | controller.Update(); |
| | 146 | | } |
| 1 | 147 | | } |
| | 148 | |
|
| | 149 | | public void LateUpdate() |
| | 150 | | { |
| 1 | 151 | | if (!isBuilderInWorldActivated) |
| 0 | 152 | | return; |
| | 153 | |
|
| 28 | 154 | | foreach (var controller in controllers) |
| | 155 | | { |
| 13 | 156 | | controller.LateUpdate(); |
| | 157 | | } |
| 1 | 158 | | } |
| | 159 | |
|
| | 160 | | private void OnNFTUsageChange() |
| | 161 | | { |
| 0 | 162 | | context.editorContext.editorHUD.RefreshCatalogAssetPack(); |
| 0 | 163 | | context.editorContext.editorHUD.RefreshCatalogContent(); |
| 0 | 164 | | } |
| | 165 | |
|
| | 166 | | private void InitControllers() |
| | 167 | | { |
| 8 | 168 | | InitController(entityHandler); |
| 8 | 169 | | InitController(modeController); |
| 8 | 170 | | InitController(publishController); |
| 8 | 171 | | InitController(creatorController); |
| 8 | 172 | | InitController(outlinerController); |
| 8 | 173 | | InitController(floorHandler); |
| 8 | 174 | | InitController(inputHandler); |
| 8 | 175 | | InitController(actionController); |
| 8 | 176 | | InitController(inputWrapper); |
| 8 | 177 | | InitController(raycastController); |
| 8 | 178 | | InitController(gizmosController); |
| 8 | 179 | | InitController(saveController); |
| 8 | 180 | | } |
| | 181 | |
|
| | 182 | | public void InitController(IBIWController controller) |
| | 183 | | { |
| 103 | 184 | | controller.Initialize(context); |
| 103 | 185 | | controllers.Add(controller); |
| 103 | 186 | | } |
| | 187 | |
|
| 0 | 188 | | private void StartTutorial() { TutorialController.i.SetBuilderInWorldTutorialEnabled(); } |
| | 189 | |
|
| | 190 | | public void CleanItems() |
| | 191 | | { |
| 8 | 192 | | if ( context.editorContext.editorHUD != null) |
| 8 | 193 | | context.editorContext.editorHUD.Dispose(); |
| | 194 | |
|
| 8 | 195 | | Camera camera = Camera.main; |
| | 196 | |
|
| 8 | 197 | | if (camera != null) |
| | 198 | | { |
| 8 | 199 | | BIWOutline outliner = camera.GetComponent<BIWOutline>(); |
| 8 | 200 | | UnityEngine.Object.Destroy(outliner); |
| | 201 | | } |
| | 202 | |
|
| 8 | 203 | | floorHandler?.CleanUp(); |
| 8 | 204 | | creatorController?.CleanUp(); |
| 8 | 205 | | } |
| | 206 | |
|
| | 207 | | private void ChangeProjectNameAndDescription(string name, string description) |
| | 208 | | { |
| 0 | 209 | | sceneToEdit.manifest.project.title = name; |
| 0 | 210 | | sceneToEdit.manifest.project.description = description; |
| | 211 | |
|
| 0 | 212 | | saveController.ForceSave(); |
| 0 | 213 | | } |
| | 214 | |
|
| | 215 | | public void EnterEditMode(IBuilderScene builderScene) |
| | 216 | | { |
| 5 | 217 | | sceneToEdit = builderScene; |
| | 218 | |
|
| 5 | 219 | | BIWNFTController.i.StartEditMode(); |
| 5 | 220 | | ParcelSettings.VISUAL_LOADING_ENABLED = false; |
| | 221 | |
|
| 5 | 222 | | if (biwAudioHandler != null && biwAudioHandler.gameObject != null) |
| 5 | 223 | | biwAudioHandler.gameObject.SetActive(true); |
| | 224 | |
|
| 5 | 225 | | cursorGO.SetActive(false); |
| | 226 | |
|
| 5 | 227 | | if ( context.editorContext.editorHUD != null) |
| | 228 | | { |
| 5 | 229 | | context.editorContext.editorHUD.SetParcelScene(sceneToEdit.scene); |
| 5 | 230 | | context.editorContext.editorHUD.RefreshCatalogContent(); |
| 5 | 231 | | context.editorContext.editorHUD.RefreshCatalogAssetPack(); |
| 5 | 232 | | context.editorContext.editorHUD.SetVisibilityOfCatalog(true); |
| 5 | 233 | | context.editorContext.editorHUD.SetVisibilityOfInspector(true); |
| 5 | 234 | | if (sceneToEdit.HasBeenCreatedThisSession() && sceneToEdit.sceneType == IBuilderScene.SceneType.LAND) |
| 0 | 235 | | context.editorContext.editorHUD.NewSceneForLand(sceneToEdit); |
| | 236 | | } |
| | 237 | |
|
| 5 | 238 | | var culling = mainCameraController.GetCulling(); |
| 5 | 239 | | mainCameraController.SetCulling( BIWUtils.GetBIWCulling(culling)); |
| | 240 | |
|
| 5 | 241 | | CommonScriptableObjects.builderInWorldNotNecessaryUIVisibilityStatus.Set(false); |
| 5 | 242 | | DataStore.i.builderInWorld.showTaskBar.Set(true); |
| | 243 | |
|
| 5 | 244 | | EnterBiwControllers(); |
| 5 | 245 | | Environment.i.world.sceneController.ActivateBuilderInWorldEditScene(); |
| | 246 | |
|
| 5 | 247 | | if (IsNewScene()) |
| 5 | 248 | | SetupNewScene(); |
| | 249 | |
|
| 5 | 250 | | isBuilderInWorldActivated = true; |
| | 251 | |
|
| 5 | 252 | | previousSkyBoxMaterial = RenderSettings.skybox; |
| 5 | 253 | | RenderSettings.skybox = skyBoxMaterial; |
| | 254 | |
|
| 5 | 255 | | DeactivateAvatars(); |
| | 256 | |
|
| 10 | 257 | | foreach (var groundVisual in groundVisualsGO) |
| | 258 | | { |
| 0 | 259 | | groundVisual.SetActive(false); |
| | 260 | | } |
| | 261 | |
|
| 5 | 262 | | DataStore.i.player.otherPlayers.OnAdded += DeactivatePlayerAvatar; |
| | 263 | |
|
| 5 | 264 | | startEditorTimeStamp = Time.realtimeSinceStartup; |
| | 265 | |
|
| 5 | 266 | | BIWAnalytics.AddSceneInfo(sceneToEdit.scene.sceneData.basePosition, BIWUtils.GetLandOwnershipType(DataStore.i.bu |
| 5 | 267 | | } |
| | 268 | |
|
| | 269 | | public void ExitEditMode() |
| | 270 | | { |
| 1 | 271 | | Environment.i.platform.cullingController.Start(); |
| 1 | 272 | | BIWNFTController.i.ExitEditMode(); |
| | 273 | |
|
| 1 | 274 | | CommonScriptableObjects.builderInWorldNotNecessaryUIVisibilityStatus.Set(true); |
| 1 | 275 | | DataStore.i.builderInWorld.showTaskBar.Set(true); |
| | 276 | |
|
| 1 | 277 | | ParcelSettings.VISUAL_LOADING_ENABLED = true; |
| | 278 | |
|
| 1 | 279 | | var culling = mainCameraController.GetCulling(); |
| 1 | 280 | | culling -= BIWSettings.FX_LAYER; |
| 1 | 281 | | mainCameraController.SetCulling(culling); |
| | 282 | |
|
| 1 | 283 | | outlinerController.CancelAllOutlines(); |
| | 284 | |
|
| 1 | 285 | | cursorGO.SetActive(true); |
| | 286 | |
|
| 1 | 287 | | if ( context.editorContext.editorHUD != null) |
| | 288 | | { |
| 1 | 289 | | context.editorContext.editorHUD.ClearEntityList(); |
| 1 | 290 | | context.editorContext.editorHUD.SetVisibility(false); |
| | 291 | | } |
| | 292 | |
|
| 1 | 293 | | Environment.i.world.sceneController.DeactivateBuilderInWorldEditScene(); |
| 1 | 294 | | Environment.i.world.blockersController.SetEnabled(true); |
| | 295 | |
|
| 1 | 296 | | ExitBiwControllers(); |
| | 297 | |
|
| 2 | 298 | | foreach (var groundVisual in groundVisualsGO) |
| | 299 | | { |
| 0 | 300 | | groundVisual.SetActive(true); |
| | 301 | | } |
| | 302 | |
|
| 1 | 303 | | isBuilderInWorldActivated = false; |
| 1 | 304 | | RenderSettings.skybox = previousSkyBoxMaterial; |
| | 305 | |
|
| 1 | 306 | | if (biwAudioHandler.gameObject != null) |
| 1 | 307 | | biwAudioHandler.gameObject.SetActive(false); |
| 1 | 308 | | DataStore.i.common.appMode.Set(AppMode.DEFAULT); |
| 1 | 309 | | DataStore.i.virtualAudioMixer.sceneSFXVolume.Set(1f); |
| | 310 | |
|
| 1 | 311 | | ActivateAvatars(); |
| | 312 | |
|
| 1 | 313 | | BIWAnalytics.ExitEditor(Time.realtimeSinceStartup - startEditorTimeStamp); |
| 1 | 314 | | } |
| | 315 | |
|
| | 316 | | internal void ActivateAvatars() |
| | 317 | | { |
| 1 | 318 | | avatarRenderer.SetAvatarVisibility(true); |
| | 319 | |
|
| 2 | 320 | | foreach (Player player in DataStore.i.player.otherPlayers.GetValues()) |
| | 321 | | { |
| 0 | 322 | | ActivatePlayerAvatar(null,player); |
| | 323 | | } |
| 1 | 324 | | } |
| | 325 | |
|
| | 326 | | internal void ActivatePlayerAvatar(string id,Player player) |
| | 327 | | { |
| 0 | 328 | | player.avatar.RemoveVisibilityConstrain(AVATAR_HIDE_CONSTRAINT); |
| 0 | 329 | | player.playerName.RemoveVisibilityConstaint(AVATAR_HIDE_CONSTRAINT); |
| 0 | 330 | | } |
| | 331 | |
|
| | 332 | | internal void DeactivatePlayerAvatar(string id,Player player) |
| | 333 | | { |
| 0 | 334 | | player.avatar.AddVisibilityConstraint(AVATAR_HIDE_CONSTRAINT); |
| 0 | 335 | | player.playerName.AddVisibilityConstaint(AVATAR_HIDE_CONSTRAINT); |
| 0 | 336 | | } |
| | 337 | |
|
| | 338 | | internal void DeactivateAvatars() |
| | 339 | | { |
| 5 | 340 | | avatarRenderer.SetAvatarVisibility(false); |
| | 341 | |
|
| 10 | 342 | | foreach (Player player in DataStore.i.player.otherPlayers.GetValues()) |
| | 343 | | { |
| 0 | 344 | | DeactivatePlayerAvatar(null, player); |
| | 345 | | } |
| 5 | 346 | | } |
| | 347 | |
|
| | 348 | | public void EnterBiwControllers() |
| | 349 | | { |
| 166 | 350 | | foreach (var controller in controllers) |
| | 351 | | { |
| 77 | 352 | | controller.EnterEditMode(sceneToEdit); |
| | 353 | | } |
| | 354 | |
|
| | 355 | | //Note: This audio should inside the controllers, it is here because it is still a monobehaviour |
| 6 | 356 | | biwAudioHandler.EnterEditMode(sceneToEdit.scene); |
| 6 | 357 | | } |
| | 358 | |
|
| | 359 | | public void ExitBiwControllers() |
| | 360 | | { |
| 28 | 361 | | foreach (var controller in controllers) |
| | 362 | | { |
| 13 | 363 | | controller.ExitEditMode(); |
| | 364 | | } |
| | 365 | |
|
| 1 | 366 | | if (biwAudioHandler.gameObject != null) |
| 1 | 367 | | biwAudioHandler.ExitEditMode(); |
| 1 | 368 | | } |
| | 369 | |
|
| 5 | 370 | | public bool IsNewScene() { return sceneToEdit.scene.entities.Count <= 0; } |
| | 371 | |
|
| 12 | 372 | | public void SetupNewScene() { floorHandler.CreateDefaultFloor(); } |
| | 373 | |
|
| | 374 | | private void NewSceneFloorLoaded() |
| | 375 | | { |
| 0 | 376 | | if (!sceneToEdit.HasBeenCreatedThisSession()) |
| 0 | 377 | | return; |
| | 378 | |
|
| 0 | 379 | | context.cameraController.TakeSceneScreenshotFromResetPosition(snapshot => |
| | 380 | | { |
| 0 | 381 | | context.builderAPIController.SetThumbnail(sceneToEdit.manifest.project.id, snapshot); |
| 0 | 382 | | } |
| | 383 | | ); |
| 0 | 384 | | } |
| | 385 | | } |