| | 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 | | private GameObject cursorGO; |
| | 16 | | private GameObject[] groundVisualsGO; |
| | 17 | |
|
| 9 | 18 | | internal IBIWOutlinerController outlinerController => context.editorContext.outlinerController; |
| 8 | 19 | | internal IBIWInputHandler inputHandler => context.editorContext.inputHandler; |
| 8 | 20 | | internal IBIWPublishController publishController => context.editorContext.publishController; |
| 16 | 21 | | internal IBIWCreatorController creatorController => context.editorContext.creatorController; |
| 8 | 22 | | internal IBIWModeController modeController => context.editorContext.modeController; |
| 22 | 23 | | internal IBIWFloorHandler floorHandler => context.editorContext.floorHandler; |
| 8 | 24 | | internal IBIWEntityHandler entityHandler => context.editorContext.entityHandler; |
| 8 | 25 | | internal IBIWActionController actionController => context.editorContext.actionController; |
| 8 | 26 | | internal IBIWSaveController saveController => context.editorContext.saveController; |
| 8 | 27 | | internal IBIWInputWrapper inputWrapper => context.editorContext.inputWrapper; |
| 8 | 28 | | internal IBIWRaycastController raycastController => context.editorContext.raycastController; |
| 8 | 29 | | internal IBIWGizmosController gizmosController => context.editorContext.gizmosController; |
| | 30 | |
|
| | 31 | | private BuilderInWorldBridge builderInWorldBridge; |
| | 32 | | private BuilderInWorldAudioHandler biwAudioHandler; |
| | 33 | | internal IContext context; |
| | 34 | |
|
| 8 | 35 | | private readonly List<IBIWController> controllers = new List<IBIWController>(); |
| | 36 | | private Material skyBoxMaterial; |
| | 37 | |
|
| 0 | 38 | | public bool isBuilderInWorldActivated { get; internal set; } = false; |
| | 39 | |
|
| | 40 | | private bool isInit = false; |
| | 41 | | private Material previousSkyBoxMaterial; |
| | 42 | |
|
| | 43 | | private float startEditorTimeStamp = 0; |
| | 44 | | internal IParcelScene sceneToEdit; |
| | 45 | |
|
| | 46 | | public void Initialize(IContext context) |
| | 47 | | { |
| 8 | 48 | | if (isInit) |
| 0 | 49 | | return; |
| | 50 | |
|
| 8 | 51 | | isInit = true; |
| | 52 | |
|
| 8 | 53 | | this.context = context; |
| | 54 | |
|
| 8 | 55 | | InitReferences(SceneReferences.i); |
| | 56 | |
|
| 8 | 57 | | if (builderInWorldBridge != null) |
| 8 | 58 | | builderInWorldBridge.OnBuilderProjectInfo += BuilderProjectPanelInfo; |
| | 59 | |
|
| 8 | 60 | | BIWNFTController.i.OnNFTUsageChange += OnNFTUsageChange; |
| | 61 | |
|
| 8 | 62 | | InitHUD(context); |
| | 63 | |
|
| 8 | 64 | | InitControllers(); |
| | 65 | |
|
| 8 | 66 | | CommonScriptableObjects.builderInWorldNotNecessaryUIVisibilityStatus.Set(true); |
| | 67 | |
|
| 8 | 68 | | biwAudioHandler = UnityEngine.Object.Instantiate(context.projectReferencesAsset.audioPrefab, Vector3.zero, Quate |
| 8 | 69 | | biwAudioHandler.Initialize(context); |
| 8 | 70 | | biwAudioHandler.gameObject.SetActive(false); |
| 8 | 71 | | } |
| | 72 | |
|
| | 73 | | public void InitReferences(SceneReferences sceneReferences) |
| | 74 | | { |
| 8 | 75 | | builderInWorldBridge = sceneReferences.biwBridgeGameObject.GetComponent<BuilderInWorldBridge>(); |
| 8 | 76 | | cursorGO = sceneReferences.cursorCanvas; |
| | 77 | |
|
| 8 | 78 | | List<GameObject> grounds = new List<GameObject>(); |
| | 79 | |
|
| 80 | 80 | | for (int i = 0; i < sceneReferences.groundVisual.transform.transform.childCount; i++) |
| | 81 | | { |
| 32 | 82 | | grounds.Add(sceneReferences.groundVisual.transform.transform.GetChild(i).gameObject); |
| | 83 | | } |
| | 84 | |
|
| 8 | 85 | | groundVisualsGO = grounds.ToArray(); |
| 8 | 86 | | skyBoxMaterial = context.projectReferencesAsset.skyBoxMaterial; |
| 8 | 87 | | } |
| | 88 | |
|
| | 89 | | private void InitHUD(IContext context) |
| | 90 | | { |
| 8 | 91 | | context.editorContext.editorHUD.Initialize(context); |
| 8 | 92 | | context.editorContext.editorHUD.OnTutorialAction += StartTutorial; |
| 8 | 93 | | } |
| | 94 | |
|
| | 95 | | public void Dispose() |
| | 96 | | { |
| 8 | 97 | | if (context.editorContext.editorHUD != null) |
| 8 | 98 | | context.editorContext.editorHUD.OnTutorialAction -= StartTutorial; |
| | 99 | |
|
| | 100 | |
|
| 8 | 101 | | BIWNFTController.i.OnNFTUsageChange -= OnNFTUsageChange; |
| | 102 | |
|
| 8 | 103 | | BIWNFTController.i.Dispose(); |
| 8 | 104 | | builderInWorldBridge.OnBuilderProjectInfo -= BuilderProjectPanelInfo; |
| | 105 | |
|
| 8 | 106 | | CleanItems(); |
| | 107 | |
|
| 8 | 108 | | if (biwAudioHandler.gameObject != null) |
| | 109 | | { |
| 8 | 110 | | biwAudioHandler.Dispose(); |
| 8 | 111 | | UnityEngine.Object.Destroy(biwAudioHandler.gameObject); |
| | 112 | | } |
| 8 | 113 | | } |
| | 114 | |
|
| | 115 | | public void OnGUI() |
| | 116 | | { |
| 1 | 117 | | if (!isBuilderInWorldActivated) |
| 0 | 118 | | return; |
| | 119 | |
|
| 28 | 120 | | foreach (var controller in controllers) |
| | 121 | | { |
| 13 | 122 | | controller.OnGUI(); |
| | 123 | | } |
| 1 | 124 | | } |
| | 125 | |
|
| | 126 | | public void Update() |
| | 127 | | { |
| 2 | 128 | | if (!isBuilderInWorldActivated) |
| 1 | 129 | | return; |
| | 130 | |
|
| 28 | 131 | | foreach (var controller in controllers) |
| | 132 | | { |
| 13 | 133 | | controller.Update(); |
| | 134 | | } |
| 1 | 135 | | } |
| | 136 | |
|
| | 137 | | public void LateUpdate() |
| | 138 | | { |
| 1 | 139 | | if (!isBuilderInWorldActivated) |
| 0 | 140 | | return; |
| | 141 | |
|
| 28 | 142 | | foreach (var controller in controllers) |
| | 143 | | { |
| 13 | 144 | | controller.LateUpdate(); |
| | 145 | | } |
| 1 | 146 | | } |
| | 147 | |
|
| | 148 | | private void OnNFTUsageChange() |
| | 149 | | { |
| 0 | 150 | | context.editorContext.editorHUD.RefreshCatalogAssetPack(); |
| 0 | 151 | | context.editorContext.editorHUD.RefreshCatalogContent(); |
| 0 | 152 | | } |
| | 153 | |
|
| 0 | 154 | | private void BuilderProjectPanelInfo(string title, string description) { context.editorContext.editorHUD.SetBuilder |
| | 155 | |
|
| | 156 | | private void InitControllers() |
| | 157 | | { |
| 8 | 158 | | InitController(entityHandler); |
| 8 | 159 | | InitController(modeController); |
| 8 | 160 | | InitController(publishController); |
| 8 | 161 | | InitController(creatorController); |
| 8 | 162 | | InitController(outlinerController); |
| 8 | 163 | | InitController(floorHandler); |
| 8 | 164 | | InitController(inputHandler); |
| 8 | 165 | | InitController(saveController); |
| 8 | 166 | | InitController(actionController); |
| 8 | 167 | | InitController(inputWrapper); |
| 8 | 168 | | InitController(raycastController); |
| 8 | 169 | | InitController(gizmosController); |
| 8 | 170 | | } |
| | 171 | |
|
| | 172 | | public void InitController(IBIWController controller) |
| | 173 | | { |
| 103 | 174 | | controller.Initialize(context); |
| 103 | 175 | | controllers.Add(controller); |
| 103 | 176 | | } |
| | 177 | |
|
| 0 | 178 | | private void StartTutorial() { TutorialController.i.SetBuilderInWorldTutorialEnabled(); } |
| | 179 | |
|
| | 180 | | public void CleanItems() |
| | 181 | | { |
| 8 | 182 | | if ( context.editorContext.editorHUD != null) |
| 8 | 183 | | context.editorContext.editorHUD.Dispose(); |
| | 184 | |
|
| 8 | 185 | | Camera camera = Camera.main; |
| | 186 | |
|
| 8 | 187 | | if (camera != null) |
| | 188 | | { |
| 8 | 189 | | BIWOutline outliner = camera.GetComponent<BIWOutline>(); |
| 8 | 190 | | UnityEngine.Object.Destroy(outliner); |
| | 191 | | } |
| | 192 | |
|
| 8 | 193 | | floorHandler?.CleanUp(); |
| 8 | 194 | | creatorController?.CleanUp(); |
| 8 | 195 | | } |
| | 196 | |
|
| | 197 | | public void EnterEditMode(IParcelScene sceneToEdit) |
| | 198 | | { |
| 5 | 199 | | this.sceneToEdit = sceneToEdit; |
| | 200 | |
|
| 5 | 201 | | BIWNFTController.i.StartEditMode(); |
| 5 | 202 | | if (biwAudioHandler != null && biwAudioHandler.gameObject != null) |
| 5 | 203 | | biwAudioHandler.gameObject.SetActive(true); |
| | 204 | |
|
| 5 | 205 | | ParcelSettings.VISUAL_LOADING_ENABLED = false; |
| 5 | 206 | | cursorGO.SetActive(false); |
| | 207 | |
|
| 5 | 208 | | if ( context.editorContext.editorHUD != null) |
| | 209 | | { |
| 5 | 210 | | context.editorContext.editorHUD.SetParcelScene(sceneToEdit); |
| 5 | 211 | | context.editorContext.editorHUD.RefreshCatalogContent(); |
| 5 | 212 | | context.editorContext.editorHUD.RefreshCatalogAssetPack(); |
| 5 | 213 | | context.editorContext.editorHUD.SetVisibilityOfCatalog(true); |
| 5 | 214 | | context.editorContext.editorHUD.SetVisibilityOfInspector(true); |
| | 215 | | } |
| | 216 | |
|
| 5 | 217 | | CommonScriptableObjects.builderInWorldNotNecessaryUIVisibilityStatus.Set(false); |
| 5 | 218 | | DataStore.i.builderInWorld.showTaskBar.Set(true); |
| | 219 | |
|
| 5 | 220 | | EnterBiwControllers(); |
| 5 | 221 | | Environment.i.world.sceneController.ActivateBuilderInWorldEditScene(); |
| | 222 | |
|
| 5 | 223 | | if (IsNewScene()) |
| 5 | 224 | | SetupNewScene(); |
| | 225 | |
|
| 5 | 226 | | isBuilderInWorldActivated = true; |
| | 227 | |
|
| 5 | 228 | | previousSkyBoxMaterial = RenderSettings.skybox; |
| 5 | 229 | | RenderSettings.skybox = skyBoxMaterial; |
| | 230 | |
|
| 50 | 231 | | foreach (var groundVisual in groundVisualsGO) |
| | 232 | | { |
| 20 | 233 | | groundVisual.SetActive(false); |
| | 234 | | } |
| | 235 | |
|
| 5 | 236 | | startEditorTimeStamp = Time.realtimeSinceStartup; |
| | 237 | |
|
| 5 | 238 | | BIWAnalytics.AddSceneInfo(sceneToEdit.sceneData.basePosition, BIWUtils.GetLandOwnershipType(DataStore.i.builderI |
| 5 | 239 | | } |
| | 240 | |
|
| | 241 | | public void ExitEditMode() |
| | 242 | | { |
| 1 | 243 | | Environment.i.platform.cullingController.Start(); |
| 1 | 244 | | BIWNFTController.i.ExitEditMode(); |
| | 245 | |
|
| 1 | 246 | | CommonScriptableObjects.builderInWorldNotNecessaryUIVisibilityStatus.Set(true); |
| 1 | 247 | | DataStore.i.builderInWorld.showTaskBar.Set(true); |
| | 248 | |
|
| 1 | 249 | | ParcelSettings.VISUAL_LOADING_ENABLED = true; |
| | 250 | |
|
| 1 | 251 | | outlinerController.CancelAllOutlines(); |
| | 252 | |
|
| 1 | 253 | | cursorGO.SetActive(true); |
| | 254 | |
|
| 1 | 255 | | InmediateExit(); |
| | 256 | |
|
| 1 | 257 | | if ( context.editorContext.editorHUD != null) |
| | 258 | | { |
| 1 | 259 | | context.editorContext.editorHUD.ClearEntityList(); |
| 1 | 260 | | context.editorContext.editorHUD.SetVisibility(false); |
| | 261 | | } |
| | 262 | |
|
| 1 | 263 | | Environment.i.world.sceneController.DeactivateBuilderInWorldEditScene(); |
| 1 | 264 | | Environment.i.world.blockersController.SetEnabled(true); |
| | 265 | |
|
| 1 | 266 | | ExitBiwControllers(); |
| | 267 | |
|
| 10 | 268 | | foreach (var groundVisual in groundVisualsGO) |
| | 269 | | { |
| 4 | 270 | | groundVisual.SetActive(true); |
| | 271 | | } |
| | 272 | |
|
| 1 | 273 | | isBuilderInWorldActivated = false; |
| 1 | 274 | | RenderSettings.skybox = previousSkyBoxMaterial; |
| | 275 | |
|
| 1 | 276 | | if (biwAudioHandler.gameObject != null) |
| 1 | 277 | | biwAudioHandler.gameObject.SetActive(false); |
| 1 | 278 | | DataStore.i.appMode.Set(AppMode.DEFAULT); |
| 1 | 279 | | DataStore.i.virtualAudioMixer.sceneSFXVolume.Set(1f); |
| 1 | 280 | | BIWAnalytics.ExitEditor(Time.realtimeSinceStartup - startEditorTimeStamp); |
| 1 | 281 | | } |
| | 282 | |
|
| 2 | 283 | | public void InmediateExit() { builderInWorldBridge.ExitKernelEditMode(sceneToEdit); } |
| | 284 | |
|
| | 285 | | public void EnterBiwControllers() |
| | 286 | | { |
| 166 | 287 | | foreach (var controller in controllers) |
| | 288 | | { |
| 77 | 289 | | controller.EnterEditMode(sceneToEdit); |
| | 290 | | } |
| | 291 | |
|
| | 292 | | //Note: This audio should inside the controllers, it is here because it is still a monobehaviour |
| 6 | 293 | | biwAudioHandler.EnterEditMode(sceneToEdit); |
| 6 | 294 | | } |
| | 295 | |
|
| | 296 | | public void ExitBiwControllers() |
| | 297 | | { |
| 28 | 298 | | foreach (var controller in controllers) |
| | 299 | | { |
| 13 | 300 | | controller.ExitEditMode(); |
| | 301 | | } |
| | 302 | |
|
| 1 | 303 | | if (biwAudioHandler.gameObject != null) |
| 1 | 304 | | biwAudioHandler.ExitEditMode(); |
| 1 | 305 | | } |
| | 306 | |
|
| 5 | 307 | | public bool IsNewScene() { return sceneToEdit.entities.Count <= 0; } |
| | 308 | |
|
| 12 | 309 | | public void SetupNewScene() { floorHandler.CreateDefaultFloor(); } |
| | 310 | | } |