| | 1 | | using Cinemachine; |
| | 2 | | using Cysharp.Threading.Tasks; |
| | 3 | | using DCL; |
| | 4 | | using DCL.Camera; |
| | 5 | | using DCL.CameraTool; |
| | 6 | | using DCL.Helpers; |
| | 7 | | using DCL.Skybox; |
| | 8 | | using DCL.Tasks; |
| | 9 | | using DCLFeatures.ScreencaptureCamera.UI; |
| | 10 | | using DCLServices.CameraReelService; |
| | 11 | | using System; |
| | 12 | | using System.Threading; |
| | 13 | | using UnityEngine; |
| | 14 | | using Environment = DCL.Environment; |
| | 15 | |
|
| | 16 | | namespace DCLFeatures.ScreencaptureCamera.CameraObject |
| | 17 | | { |
| | 18 | | public class ScreencaptureCameraBehaviour : MonoBehaviour |
| | 19 | | { |
| | 20 | | private const string UPLOADING_ERROR_MESSAGE = "There was an unexpected error when uploading the picture. Try ag |
| | 21 | | private const string STORAGE_LIMIT_REACHED_MESSAGE = "You can't take more pictures because you have reached the |
| | 22 | |
|
| | 23 | | private const float SPLASH_FX_DURATION = 0.5f; |
| | 24 | | private const float MIDDLE_PAUSE_FX_DURATION = 0.1f; |
| | 25 | | private const float IMAGE_TRANSITION_FX_DURATION = 0.5f; |
| | 26 | |
|
| | 27 | | private const float MIN_PLAYERNAME_HEIGHT = 1.14f; |
| | 28 | |
|
| | 29 | | [Header("MAIN COMPONENTS")] |
| | 30 | | [SerializeField] internal Camera cameraPrefab; |
| | 31 | | [SerializeField] internal ScreencaptureCameraHUDView screencaptureCameraHUDViewPrefab; |
| | 32 | | [SerializeField] private PlayerName playerNamePrefab; |
| | 33 | |
|
| | 34 | | [SerializeField] private CharacterController cameraTarget; |
| | 35 | | [SerializeField] private CinemachineVirtualCamera virtualCamera; |
| | 36 | |
|
| | 37 | | internal Camera mainCamera; |
| | 38 | | internal DCLCharacterController characterController; |
| | 39 | | internal CameraController cameraController; |
| | 40 | |
|
| | 41 | | internal Camera screenshotCamera; |
| | 42 | | internal BooleanVariable isScreencaptureCameraActive; |
| | 43 | |
|
| | 44 | | internal ScreenRecorder screenRecorderLazyValue; |
| | 45 | | internal IAvatarsLODController avatarsLODControllerLazyValue; |
| | 46 | |
|
| 0 | 47 | | internal ScreencaptureCameraFactory factory = new (); |
| | 48 | |
|
| | 49 | | private bool isInstantiated; |
| 0 | 50 | | private float lastScreenshotTime = -Mathf.Infinity; |
| | 51 | | private CameraReelStorageStatus storageStatus; |
| | 52 | | private string playerId; |
| | 53 | | private PlayerName playerName; |
| | 54 | | private CancellationTokenSource uploadPictureCancellationToken; |
| | 55 | |
|
| | 56 | | // UI |
| | 57 | | private Canvas enableCameraButton; |
| | 58 | | private ScreencaptureCameraHUDView screencaptureCameraHUDView; |
| | 59 | | private ScreencaptureCameraHUDController screencaptureCameraHUDController; |
| | 60 | |
|
| | 61 | | // Lazy Values |
| | 62 | | private CinemachineBrain characterCinemachineBrainLazyValue; |
| | 63 | | private ICameraReelStorageService cameraReelStorageServiceLazyValue; |
| | 64 | |
|
| | 65 | | // Cached states |
| | 66 | | private bool prevMouseLockState; |
| | 67 | | private bool prevMouseButtonCursorLockMode; |
| | 68 | | private Camera prevSkyboxCamera; |
| | 69 | |
|
| | 70 | | // DataStore Variables |
| | 71 | | private BooleanVariable allUIHidden; |
| | 72 | | private BooleanVariable cameraModeInputLocked; |
| | 73 | | private BaseVariable<bool> cameraLeftMouseButtonCursorLock; |
| | 74 | | private BooleanVariable cameraBlocked; |
| | 75 | | private BooleanVariable featureKeyTriggersBlocked; |
| | 76 | | private BooleanVariable userMovementKeysBlocked; |
| | 77 | |
|
| | 78 | | private bool isInTransition; |
| | 79 | |
|
| | 80 | | private InputAction_Trigger toggleScreenshotCameraAction; |
| | 81 | | private InputAction_Trigger toggleCameraReelAction; |
| | 82 | | private InputAction_Trigger exitScreenshotModeAction; |
| | 83 | |
|
| 0 | 84 | | public DataStore_Player Player { private get; set; } |
| 0 | 85 | | public bool HasStorageSpace => storageStatus.HasFreeSpace; |
| | 86 | |
|
| 0 | 87 | | private ICameraReelStorageService cameraReelStorageService => cameraReelStorageServiceLazyValue ??= Environment. |
| 0 | 88 | | private ICameraReelAnalyticsService analytics => Environment.i.serviceLocator.Get<ICameraReelAnalyticsService>() |
| | 89 | |
|
| 0 | 90 | | private Transform characterCameraTransform => mainCamera.transform; |
| 0 | 91 | | private CinemachineBrain characterCinemachineBrain => characterCinemachineBrainLazyValue ??= mainCamera.GetCompo |
| 0 | 92 | | private IAvatarsLODController avatarsLODController => avatarsLODControllerLazyValue ??= Environment.i.serviceLoc |
| | 93 | |
|
| | 94 | | private ScreenRecorder screenRecorderLazy |
| | 95 | | { |
| | 96 | | get |
| | 97 | | { |
| 0 | 98 | | if (isInstantiated) |
| 0 | 99 | | return screenRecorderLazyValue; |
| | 100 | |
|
| 0 | 101 | | InstantiateCameraObjects(); |
| | 102 | |
|
| 0 | 103 | | return screenRecorderLazyValue; |
| | 104 | | } |
| | 105 | | } |
| | 106 | |
|
| 0 | 107 | | private bool isOnCooldown => Time.time - lastScreenshotTime < SPLASH_FX_DURATION + IMAGE_TRANSITION_FX_DURATION |
| | 108 | |
|
| | 109 | | internal void Awake() |
| | 110 | | { |
| 0 | 111 | | toggleScreenshotCameraAction = Resources.Load<InputAction_Trigger>("ToggleScreenshotCamera"); |
| 0 | 112 | | toggleCameraReelAction = Resources.Load<InputAction_Trigger>("ToggleCameraReelSection"); |
| 0 | 113 | | exitScreenshotModeAction = Resources.Load<InputAction_Trigger>("CloseScreenshotCamera"); |
| | 114 | |
|
| 0 | 115 | | toggleScreenshotCameraAction.OnTriggered += ToggleScreenshotCamera; |
| 0 | 116 | | toggleCameraReelAction.OnTriggered += OpenCameraReelGallery; |
| | 117 | |
|
| 0 | 118 | | exitScreenshotModeAction.OnTriggered += CloseScreenshotCamera; |
| | 119 | |
|
| 0 | 120 | | uploadPictureCancellationToken = new CancellationTokenSource(); |
| 0 | 121 | | } |
| | 122 | |
|
| | 123 | | private void Start() |
| | 124 | | { |
| 0 | 125 | | playerId = Player.ownPlayer.Get().id; |
| 0 | 126 | | storageStatus = new CameraReelStorageStatus(0, 0); |
| | 127 | |
|
| 0 | 128 | | UpdateStorageInfo(); |
| | 129 | |
|
| 0 | 130 | | characterController = DCLCharacterController.i; |
| 0 | 131 | | cameraController = SceneReferences.i.cameraController; |
| 0 | 132 | | mainCamera = cameraController.GetCamera(); |
| 0 | 133 | | } |
| | 134 | |
|
| | 135 | | internal void OnDestroy() |
| | 136 | | { |
| 0 | 137 | | uploadPictureCancellationToken?.Cancel(); |
| 0 | 138 | | uploadPictureCancellationToken?.Dispose(); |
| | 139 | |
|
| 0 | 140 | | toggleScreenshotCameraAction.OnTriggered -= ToggleScreenshotCamera; |
| 0 | 141 | | toggleCameraReelAction.OnTriggered -= OpenCameraReelGallery; |
| 0 | 142 | | exitScreenshotModeAction.OnTriggered -= CloseScreenshotCamera; |
| 0 | 143 | | } |
| | 144 | |
|
| | 145 | | private void OpenCameraReelGallery(DCLAction_Trigger _) => |
| 0 | 146 | | OpenCameraReelGallery("Shortcut"); |
| | 147 | |
|
| | 148 | | private void OpenCameraReelGallery(string source) |
| | 149 | | { |
| 0 | 150 | | if (isScreencaptureCameraActive.Get()) |
| 0 | 151 | | ToggleScreenshotCamera(isEnabled: false); |
| | 152 | |
|
| 0 | 153 | | bool cameraReelWasOpen = DataStore.i.HUDs.cameraReelSectionVisible.Get(); |
| | 154 | |
|
| 0 | 155 | | if (!cameraReelWasOpen) |
| 0 | 156 | | DataStore.i.HUDs.cameraReelOpenSource.Set(source); |
| | 157 | |
|
| 0 | 158 | | DataStore.i.HUDs.cameraReelSectionVisible.Set(!cameraReelWasOpen); |
| 0 | 159 | | } |
| | 160 | |
|
| | 161 | | public void SetExternalDependencies(BooleanVariable allUIHidden, BooleanVariable cameraModeInputLocked, BaseVari |
| | 162 | | BooleanVariable cameraBlocked, BooleanVariable featureKeyTriggersBlocked, BooleanVariable userMovementKeysBl |
| | 163 | | { |
| 0 | 164 | | this.allUIHidden = allUIHidden; |
| 0 | 165 | | this.cameraModeInputLocked = cameraModeInputLocked; |
| 0 | 166 | | this.cameraLeftMouseButtonCursorLock = cameraLeftMouseButtonCursorLock; |
| 0 | 167 | | this.cameraBlocked = cameraBlocked; |
| 0 | 168 | | this.featureKeyTriggersBlocked = featureKeyTriggersBlocked; |
| 0 | 169 | | this.userMovementKeysBlocked = userMovementKeysBlocked; |
| 0 | 170 | | isScreencaptureCameraActive = isScreenshotCameraActive; |
| 0 | 171 | | } |
| | 172 | |
|
| | 173 | | public void CaptureScreenshot(string source) |
| | 174 | | { |
| 0 | 175 | | if (isOnCooldown || !storageStatus.HasFreeSpace || !isScreencaptureCameraActive.Get() || screenRecorderLazy. |
| | 176 | |
|
| 0 | 177 | | lastScreenshotTime = Time.time; |
| 0 | 178 | | screencaptureCameraHUDController.SetVisibility(false, storageStatus.HasFreeSpace); |
| | 179 | |
|
| 0 | 180 | | StartCoroutine(screenRecorderLazy.CaptureScreenshot(OnComplete)); |
| 0 | 181 | | return; |
| | 182 | |
|
| | 183 | | void OnComplete(Texture2D screenshot) |
| | 184 | | { |
| 0 | 185 | | screencaptureCameraHUDController.SetVisibility(true, storageStatus.HasFreeSpace); |
| 0 | 186 | | screencaptureCameraHUDController.PlayScreenshotFX(screenshot, SPLASH_FX_DURATION, MIDDLE_PAUSE_FX_DURATI |
| | 187 | |
|
| 0 | 188 | | var metadata = ScreenshotMetadata.Create(Player, avatarsLODController, screenshotCamera); |
| 0 | 189 | | UploadScreenshotAsync(screenshot, metadata, source, uploadPictureCancellationToken.Token).Forget(); |
| 0 | 190 | | } |
| | 191 | |
|
| | 192 | | async UniTaskVoid UploadScreenshotAsync(Texture2D screenshot, ScreenshotMetadata metadata, string source, Ca |
| | 193 | | { |
| | 194 | | try |
| | 195 | | { |
| 0 | 196 | | storageStatus = await cameraReelStorageService.UploadScreenshot(screenshot, metadata, cancellationTo |
| | 197 | |
|
| 0 | 198 | | analytics.TakePhoto(metadata.userAddress, |
| | 199 | | $"{metadata.scene.location.x},{metadata.scene.location.y}", |
| | 200 | | metadata.visiblePeople.Length, |
| | 201 | | source); |
| 0 | 202 | | } |
| 0 | 203 | | catch (OperationCanceledException) { } |
| 0 | 204 | | catch (ScreenshotLimitReachedException) { DataStore.i.notifications.DefaultErrorNotification.Set(STORAGE |
| | 205 | | catch (Exception e) |
| | 206 | | { |
| 0 | 207 | | DataStore.i.notifications.DefaultErrorNotification.Set(UPLOADING_ERROR_MESSAGE, true); |
| 0 | 208 | | Debug.LogException(e); |
| 0 | 209 | | } |
| 0 | 210 | | } |
| | 211 | | } |
| | 212 | |
|
| | 213 | | public void ToggleScreenshotCamera(string source = null, bool isEnabled = true) |
| | 214 | | { |
| 0 | 215 | | if (isInTransition || isEnabled == isScreencaptureCameraActive.Get()) return; |
| | 216 | |
|
| 0 | 217 | | if (isEnabled && !string.IsNullOrEmpty(source)) |
| 0 | 218 | | analytics.OpenCamera(source); |
| | 219 | |
|
| 0 | 220 | | UpdateStorageInfo(); |
| | 221 | |
|
| 0 | 222 | | bool activateScreenshotCamera = !(isInstantiated && screenshotCamera.gameObject.activeSelf); |
| 0 | 223 | | Utils.UnlockCursor(); |
| | 224 | |
|
| 0 | 225 | | if (activateScreenshotCamera) |
| 0 | 226 | | OpenCameraAsync(); |
| | 227 | | else |
| | 228 | | { |
| 0 | 229 | | ToggleExternalSystems(activateScreenshotCamera: false); |
| 0 | 230 | | ToggleCameraSystems(activateScreenshotCamera: false); |
| 0 | 231 | | isScreencaptureCameraActive.Set(false); |
| | 232 | | } |
| 0 | 233 | | } |
| | 234 | |
|
| | 235 | | private async void OpenCameraAsync() |
| | 236 | | { |
| 0 | 237 | | isInTransition = true; |
| | 238 | |
|
| 0 | 239 | | cameraModeInputLocked.Set(true); |
| 0 | 240 | | cameraController.SetCameraMode(CameraMode.ModeId.ThirdPerson); |
| | 241 | |
|
| 0 | 242 | | await UniTask.WaitWhile(() => cameraController.CameraIsBlending); |
| 0 | 243 | | isInTransition = false; |
| | 244 | |
|
| 0 | 245 | | ToggleExternalSystems(activateScreenshotCamera: true); |
| 0 | 246 | | ToggleCameraSystems(activateScreenshotCamera: true); |
| 0 | 247 | | isScreencaptureCameraActive.Set(true); |
| 0 | 248 | | } |
| | 249 | |
|
| | 250 | | private void CloseScreenshotCamera(DCLAction_Trigger _) => |
| 0 | 251 | | ToggleScreenshotCamera("Shortcut", isEnabled: false); |
| | 252 | |
|
| | 253 | | private void ToggleScreenshotCamera(DCLAction_Trigger _) => |
| 0 | 254 | | ToggleScreenshotCamera("Shortcut", isEnabled: !isScreencaptureCameraActive.Get()); |
| | 255 | |
|
| | 256 | | private void ToggleCameraSystems(bool activateScreenshotCamera) |
| | 257 | | { |
| 0 | 258 | | if (activateScreenshotCamera) |
| | 259 | | { |
| 0 | 260 | | if (!isInstantiated) |
| 0 | 261 | | InstantiateCameraObjects(); |
| | 262 | |
|
| 0 | 263 | | prevSkyboxCamera = SkyboxController.i.SkyboxCamera.CurrentCamera; |
| 0 | 264 | | SkyboxController.i.AssignMainOverlayCamera(screenshotCamera.transform); |
| 0 | 265 | | playerName.Show(); |
| | 266 | | } |
| | 267 | | else |
| | 268 | | { |
| 0 | 269 | | SkyboxController.i.AssignMainOverlayCamera(prevSkyboxCamera.transform); |
| 0 | 270 | | playerName.Hide(); |
| | 271 | | } |
| | 272 | |
|
| 0 | 273 | | SetPlayerCameraActive(isActive: !activateScreenshotCamera); |
| 0 | 274 | | SetScreenshotCameraActive(isActive: activateScreenshotCamera); |
| 0 | 275 | | avatarsLODController.SetCamera(activateScreenshotCamera ? screenshotCamera : mainCamera); |
| 0 | 276 | | } |
| | 277 | |
|
| | 278 | | private void SetScreenshotCameraActive(bool isActive) |
| | 279 | | { |
| 0 | 280 | | screenshotCamera.gameObject.SetActive(isActive); |
| 0 | 281 | | screencaptureCameraHUDController.SetVisibility(isActive, storageStatus.HasFreeSpace); |
| 0 | 282 | | } |
| | 283 | |
|
| | 284 | | private void SetPlayerCameraActive(bool isActive) |
| | 285 | | { |
| 0 | 286 | | cameraController.SetCameraEnabledState(isActive); |
| 0 | 287 | | characterController.SetMovementInputToZero(); |
| 0 | 288 | | characterCinemachineBrain.enabled = isActive; |
| 0 | 289 | | } |
| | 290 | |
|
| | 291 | | private void ToggleExternalSystems(bool activateScreenshotCamera) |
| | 292 | | { |
| 0 | 293 | | if (activateScreenshotCamera) |
| | 294 | | { |
| 0 | 295 | | allUIHidden.Set(true); |
| | 296 | |
|
| 0 | 297 | | cameraModeInputLocked.Set(true); |
| | 298 | |
|
| 0 | 299 | | prevMouseButtonCursorLockMode = cameraLeftMouseButtonCursorLock.Get(); |
| 0 | 300 | | cameraLeftMouseButtonCursorLock.Set(true); |
| | 301 | | } |
| | 302 | | else |
| | 303 | | { |
| 0 | 304 | | allUIHidden.Set(false); |
| 0 | 305 | | cameraModeInputLocked.Set(false); |
| 0 | 306 | | cameraLeftMouseButtonCursorLock.Set(prevMouseButtonCursorLockMode); |
| | 307 | | } |
| | 308 | |
|
| 0 | 309 | | cameraBlocked.Set(activateScreenshotCamera); |
| 0 | 310 | | featureKeyTriggersBlocked.Set(activateScreenshotCamera); |
| 0 | 311 | | userMovementKeysBlocked.Set(activateScreenshotCamera); |
| 0 | 312 | | } |
| | 313 | |
|
| | 314 | | private async void UpdateStorageInfo() => |
| 0 | 315 | | storageStatus = await cameraReelStorageService.GetUserGalleryStorageInfo(playerId); |
| | 316 | |
|
| | 317 | | internal void InstantiateCameraObjects() |
| | 318 | | { |
| 0 | 319 | | (screencaptureCameraHUDController, screencaptureCameraHUDView) = factory.CreateHUD(this, screencaptureCamera |
| 0 | 320 | | screenRecorderLazyValue = factory.CreateScreenRecorder(screencaptureCameraHUDView.RectTransform); |
| 0 | 321 | | screenshotCamera = factory.CreateScreencaptureCamera(cameraPrefab, characterCameraTransform, transform, char |
| 0 | 322 | | playerName = factory.CreatePlayerNameUI(playerNamePrefab, MIN_PLAYERNAME_HEIGHT, Player, playerAvatar: chara |
| | 323 | |
|
| 0 | 324 | | isInstantiated = true; |
| 0 | 325 | | } |
| | 326 | | } |
| | 327 | | } |