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