| | 1 | | using DCL; |
| | 2 | | using UnityEngine; |
| | 3 | | using DCL.Helpers; |
| | 4 | |
|
| | 5 | | public class ControlsHUDController : IHUD |
| | 6 | | { |
| | 7 | | internal ControlsHUDView view; |
| | 8 | |
|
| | 9 | | private bool prevMouseLockState = false; |
| | 10 | |
|
| | 11 | | public event System.Action OnControlsOpened; |
| | 12 | | public event System.Action OnControlsClosed; |
| | 13 | |
|
| 11 | 14 | | public BaseVariable<bool> controlsVisible => DataStore.i.HUDs.controlsVisible; |
| | 15 | |
|
| 3 | 16 | | public ControlsHUDController() |
| | 17 | | { |
| 3 | 18 | | view = UnityEngine.Object.Instantiate(Resources.Load<GameObject>("ControlsHUD")).GetComponent<ControlsHUDView>() |
| 3 | 19 | | view.name = "_ControlsHUD"; |
| 3 | 20 | | view.gameObject.SetActive(false); |
| | 21 | |
|
| 3 | 22 | | controlsVisible.OnChange += OnControlsVisibleChanged; |
| 3 | 23 | | OnControlsVisibleChanged(controlsVisible.Get(), false); |
| | 24 | |
|
| 3 | 25 | | view.onCloseActionTriggered += Hide; |
| | 26 | |
|
| 3 | 27 | | if (!DCL.Configuration.EnvironmentSettings.RUNNING_TESTS) |
| | 28 | | { |
| 0 | 29 | | KernelConfig.i.EnsureConfigInitialized().Then(config => OnKernelConfigChanged(config, null)); |
| 0 | 30 | | KernelConfig.i.OnChange += OnKernelConfigChanged; |
| | 31 | | } |
| 3 | 32 | | } |
| | 33 | |
|
| 4 | 34 | | public void SetVisibility(bool visible) { controlsVisible.Set(visible); } |
| | 35 | |
|
| 10 | 36 | | private void OnControlsVisibleChanged(bool current, bool previous) { SetVisibility_Internal(current); } |
| | 37 | |
|
| | 38 | | private void SetVisibility_Internal(bool visible) |
| | 39 | | { |
| 5 | 40 | | if (!view) |
| 0 | 41 | | return; |
| | 42 | |
|
| 5 | 43 | | if (IsVisible() && !visible) |
| | 44 | | { |
| 0 | 45 | | if (prevMouseLockState) |
| | 46 | | { |
| 0 | 47 | | Utils.LockCursor(); |
| | 48 | | } |
| | 49 | |
|
| 0 | 50 | | view.showHideAnimator.Hide(); |
| 0 | 51 | | OnControlsClosed?.Invoke(); |
| | 52 | |
|
| 0 | 53 | | AudioScriptableObjects.fadeOut.Play(true); |
| 0 | 54 | | } |
| 5 | 55 | | else if (!IsVisible() && visible) |
| | 56 | | { |
| 2 | 57 | | prevMouseLockState = Utils.IsCursorLocked; |
| 2 | 58 | | Utils.UnlockCursor(); |
| 2 | 59 | | view.gameObject.SetActive(true); |
| 2 | 60 | | view.showHideAnimator.Show(); |
| 2 | 61 | | OnControlsOpened?.Invoke(); |
| 2 | 62 | | AudioScriptableObjects.fadeIn.Play(true); |
| | 63 | | } |
| 5 | 64 | | } |
| | 65 | |
|
| | 66 | | public void Dispose() |
| | 67 | | { |
| 3 | 68 | | controlsVisible.OnChange -= OnControlsVisibleChanged; |
| 3 | 69 | | if (view) |
| | 70 | | { |
| 3 | 71 | | Object.Destroy(view.gameObject); |
| | 72 | | } |
| | 73 | |
|
| 3 | 74 | | if (!DCL.Configuration.EnvironmentSettings.RUNNING_TESTS) |
| | 75 | | { |
| 0 | 76 | | KernelConfig.i.OnChange -= OnKernelConfigChanged; |
| | 77 | | } |
| 3 | 78 | | } |
| | 79 | |
|
| | 80 | | public bool IsVisible() |
| | 81 | | { |
| 10 | 82 | | if (!view) |
| 0 | 83 | | return false; |
| | 84 | |
|
| 10 | 85 | | return view.showHideAnimator.isVisible; |
| | 86 | | } |
| | 87 | |
|
| | 88 | | public void Hide(bool restorePointerLockStatus) |
| | 89 | | { |
| 0 | 90 | | if (!restorePointerLockStatus) |
| 0 | 91 | | prevMouseLockState = false; |
| 0 | 92 | | controlsVisible.Set(false); |
| 0 | 93 | | } |
| | 94 | |
|
| | 95 | | private void OnKernelConfigChanged(KernelConfigModel current, KernelConfigModel previous) |
| | 96 | | { |
| 0 | 97 | | view?.voiceChatButton.SetActive(current.comms.voiceChatEnabled); |
| 0 | 98 | | view?.builderInWorldButton.SetActive(current.features.enableBuilderInWorld); |
| 0 | 99 | | } |
| | 100 | | } |