| | 1 | | using DCL; |
| | 2 | | using DCL.Configuration; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCL.Interface; |
| | 5 | | using System; |
| | 6 | | using UnityEngine; |
| | 7 | |
|
| | 8 | | public class RenderingController : MonoBehaviour |
| | 9 | | { |
| 0 | 10 | | public static float firstActivationTime { get; private set; } |
| | 11 | | private bool firstActivationTimeHasBeenSet; |
| | 12 | | private readonly bool VERBOSE = false; |
| | 13 | |
|
| 0 | 14 | | public CompositeLock renderingActivatedAckLock = new (); |
| | 15 | |
|
| 0 | 16 | | private bool activatedRenderingBefore { get; set; } |
| 0 | 17 | | private bool isDecoupledLoadingScreenEnabled => true; |
| 0 | 18 | | private bool isSignUpFlow => DataStore.i.common.isSignUpFlow.Get(); |
| | 19 | |
|
| | 20 | | private DataStoreRef<DataStore_LoadingScreen> dataStore_LoadingScreenRef; |
| | 21 | |
|
| | 22 | | private void Awake() |
| | 23 | | { |
| 0 | 24 | | CommonScriptableObjects.rendererState.OnLockAdded += AddLock; |
| 0 | 25 | | CommonScriptableObjects.rendererState.OnLockRemoved += RemoveLock; |
| 0 | 26 | | CommonScriptableObjects.rendererState.Set(false); |
| | 27 | |
|
| 0 | 28 | | dataStore_LoadingScreenRef.Ref.decoupledLoadingHUD.visible.OnChange += DecoupleLoadingScreenVisibilityChange; |
| 0 | 29 | | DecoupleLoadingScreenVisibilityChange(true, true); |
| 0 | 30 | | } |
| | 31 | |
|
| | 32 | | private void DecoupleLoadingScreenVisibilityChange(bool visible, bool _) |
| | 33 | | { |
| 0 | 34 | | if (visible) |
| 0 | 35 | | DeactivateRendering_Internal(); |
| | 36 | | else |
| | 37 | | //Coming-from-kernel condition. If we are on signup flow, then we must force the ActivateRendering |
| 0 | 38 | | ActivateRendering_Internal(isSignUpFlow); |
| 0 | 39 | | } |
| | 40 | |
|
| | 41 | | private void OnDestroy() |
| | 42 | | { |
| 0 | 43 | | CommonScriptableObjects.rendererState.OnLockAdded -= AddLock; |
| 0 | 44 | | CommonScriptableObjects.rendererState.OnLockRemoved -= RemoveLock; |
| 0 | 45 | | } |
| | 46 | |
|
| | 47 | | [ContextMenu("Disable Rendering")] |
| | 48 | | public void DeactivateRendering() |
| | 49 | | { |
| 0 | 50 | | if (isDecoupledLoadingScreenEnabled) return; |
| | 51 | |
|
| 0 | 52 | | DeactivateRendering_Internal(); |
| 0 | 53 | | } |
| | 54 | |
|
| | 55 | | private void DeactivateRendering_Internal() |
| | 56 | | { |
| 0 | 57 | | if (!CommonScriptableObjects.rendererState.Get()) return; |
| | 58 | |
|
| 0 | 59 | | CommonScriptableObjects.rendererState.Set(false); |
| | 60 | |
|
| 0 | 61 | | if (!isDecoupledLoadingScreenEnabled) |
| 0 | 62 | | WebInterface.ReportControlEvent(new WebInterface.DeactivateRenderingACK()); |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | [ContextMenu("Enable Rendering")] |
| | 66 | | public void ActivateRendering() |
| | 67 | | { |
| 0 | 68 | | if (isDecoupledLoadingScreenEnabled) return; |
| | 69 | |
|
| 0 | 70 | | ActivateRendering_Internal(forceActivate: false); |
| 0 | 71 | | } |
| | 72 | |
|
| | 73 | | public void ForceActivateRendering() |
| | 74 | | { |
| 0 | 75 | | if (isDecoupledLoadingScreenEnabled) return; |
| | 76 | |
|
| 0 | 77 | | ActivateRendering_Internal(forceActivate: true); |
| 0 | 78 | | } |
| | 79 | |
|
| | 80 | | public void ActivateRendering_Internal(bool forceActivate) |
| | 81 | | { |
| 0 | 82 | | if (CommonScriptableObjects.rendererState.Get()) return; |
| | 83 | |
|
| 0 | 84 | | if (!firstActivationTimeHasBeenSet) |
| | 85 | | { |
| 0 | 86 | | firstActivationTime = Time.realtimeSinceStartup; |
| 0 | 87 | | firstActivationTimeHasBeenSet = true; |
| | 88 | | } |
| | 89 | |
|
| 0 | 90 | | if (!forceActivate && !renderingActivatedAckLock.isUnlocked) |
| | 91 | | { |
| 0 | 92 | | renderingActivatedAckLock.OnAllLocksRemoved -= ActivateRendering_Internal; |
| 0 | 93 | | renderingActivatedAckLock.OnAllLocksRemoved += ActivateRendering_Internal; |
| 0 | 94 | | return; |
| | 95 | | } |
| | 96 | |
|
| 0 | 97 | | ActivateRendering_Internal(); |
| 0 | 98 | | } |
| | 99 | |
|
| | 100 | | private void ActivateRendering_Internal() |
| | 101 | | { |
| 0 | 102 | | renderingActivatedAckLock.OnAllLocksRemoved -= ActivateRendering_Internal; |
| | 103 | |
|
| 0 | 104 | | if (!activatedRenderingBefore) |
| | 105 | | { |
| 0 | 106 | | Utils.UnlockCursor(); |
| 0 | 107 | | activatedRenderingBefore = true; |
| | 108 | | } |
| | 109 | |
|
| 0 | 110 | | CommonScriptableObjects.rendererState.Set(true); |
| | 111 | |
|
| 0 | 112 | | if (!isDecoupledLoadingScreenEnabled) |
| 0 | 113 | | WebInterface.ReportControlEvent(new WebInterface.ActivateRenderingACK()); |
| 0 | 114 | | } |
| | 115 | |
|
| | 116 | | private void AddLock(object id) |
| | 117 | | { |
| 0 | 118 | | if (CommonScriptableObjects.rendererState.Get()) |
| 0 | 119 | | return; |
| | 120 | |
|
| 0 | 121 | | if (VERBOSE) |
| 0 | 122 | | Debug.Log("Add lock: " + id); |
| | 123 | |
|
| 0 | 124 | | renderingActivatedAckLock.AddLock(id); |
| 0 | 125 | | } |
| | 126 | |
|
| | 127 | | private void RemoveLock(object id) |
| | 128 | | { |
| 0 | 129 | | if (VERBOSE) |
| 0 | 130 | | Debug.Log("remove lock: " + id); |
| | 131 | |
|
| 0 | 132 | | renderingActivatedAckLock.RemoveLock(id); |
| 0 | 133 | | } |
| | 134 | | } |