< Summary

Class:TaskbarHUDController
Assembly:TaskbarHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/TaskbarHUD/TaskbarHUDController.cs
Covered lines:128
Uncovered lines:95
Coverable lines:223
Total lines:448
Line coverage:57.3% (128 of 223)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
CreateView()0%110100%
Initialize(...)0%330100%
ChatHeadsGroup_OnHeadClose(...)0%110100%
View_OnFriendsToggleOn()0%3.333066.67%
View_OnFriendsToggleOff()0%220100%
View_OnExperiencesToggleOn()0%6200%
View_OnExperiencesToggleOff()0%110100%
ToggleFriendsTrigger_OnTriggered(...)0%6200%
ToggleWorldChatTrigger_OnTriggered(...)0%2100%
CloseWindowTrigger_OnTriggered(...)0%2100%
View_OnChatToggleOn()0%2.022083.33%
View_OnChatToggleOff()0%2.152066.67%
ChatHeadsGroup_OnHeadOpen(...)0%2.032080%
MouseCatcher_OnMouseUnlock()0%2100%
MouseCatcher_OnMouseLock()0%6200%
AddWorldChatWindow(...)0%5.315076.92%
OpenFriendsWindow()0%2100%
OpenPrivateChatTo(...)0%2100%
AddPrivateChatWindow(...)0%5.515072.73%
AddFriendsWindow(...)0%5.395075%
InitializeExperiencesViewer(...)0%3.462028.57%
IsExperiencesViewerOpenChanged(...)0%6200%
NumOfLoadedExperiencesChanged(...)0%220100%
OnAddVoiceChat()0%2100%
DisableFriendsWindow()0%2100%
OpenPrivateChatWindow(...)0%2.032080%
Dispose()0%770100%
SetVisibility(...)0%2100%
SetVisibility(...)0%110100%
OnWorldChatToggleInputPress()0%42600%
OnCloseWindowToggleInputPress()0%6200%
SetVoiceChatRecording(...)0%6200%
SetVoiceChatEnabledByScene(...)0%6200%
OnFriendsToggleInputPress()0%56700%
OnAddMessage(...)0%3.333066.67%
AnyWindowsDifferentThanChatIsOpen()0%440100%
MarkWorldChatAsReadIfOtherWindowIsOpen()0%6200%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/TaskbarHUD/TaskbarHUDController.cs

#LineLine coverage
 1using DCL;
 2using DCL.Controllers;
 3using DCL.HelpAndSupportHUD;
 4using DCL.Helpers;
 5using DCL.Interface;
 6using System;
 7using System.Collections.Generic;
 8using System.Linq;
 9using UnityEngine;
 10using UnityEngine.EventSystems;
 11
 12public class TaskbarHUDController : IHUD
 13{
 14    [Serializable]
 15    public struct Configuration
 16    {
 17        public bool enableVoiceChat;
 18        public bool enableQuestPanel;
 19    }
 20
 21    public TaskbarHUDView view;
 22    public WorldChatWindowHUDController worldChatWindowHud;
 23    public PrivateChatWindowHUDController privateChatWindowHud;
 24    public FriendsHUDController friendsHud;
 25    public HelpAndSupportHUDController helpAndSupportHud;
 26
 27    IMouseCatcher mouseCatcher;
 28    protected IChatController chatController;
 29    protected IFriendsController friendsController;
 30
 31    private InputAction_Trigger toggleFriendsTrigger;
 32    private InputAction_Trigger closeWindowTrigger;
 33    private InputAction_Trigger toggleWorldChatTrigger;
 34    private Transform experiencesViewerTransform;
 35
 36    public event System.Action OnAnyTaskbarButtonClicked;
 37
 038    public RectTransform socialTooltipReference { get => view.socialTooltipReference; }
 39
 1240    internal BaseVariable<Transform> isExperiencesViewerInitialized => DataStore.i.experiencesViewer.isInitialized;
 1241    internal BaseVariable<bool> isExperiencesViewerOpen => DataStore.i.experiencesViewer.isOpen;
 1242    internal BaseVariable<int> numOfLoadedExperiences => DataStore.i.experiencesViewer.numOfLoadedExperiences;
 43
 444    protected internal virtual TaskbarHUDView CreateView() { return TaskbarHUDView.Create(this, chatController, friendsC
 45
 46    public void Initialize(
 47        IMouseCatcher mouseCatcher,
 48        IChatController chatController,
 49        IFriendsController friendsController)
 50    {
 451        this.friendsController = friendsController;
 452        this.mouseCatcher = mouseCatcher;
 453        this.chatController = chatController;
 54
 455        view = CreateView();
 56
 457        if (mouseCatcher != null)
 58        {
 159            mouseCatcher.OnMouseLock -= MouseCatcher_OnMouseLock;
 160            mouseCatcher.OnMouseUnlock -= MouseCatcher_OnMouseUnlock;
 161            mouseCatcher.OnMouseLock += MouseCatcher_OnMouseLock;
 162            mouseCatcher.OnMouseUnlock += MouseCatcher_OnMouseUnlock;
 63        }
 64
 465        view.chatHeadsGroup.OnHeadToggleOn += ChatHeadsGroup_OnHeadOpen;
 466        view.chatHeadsGroup.OnHeadToggleOff += ChatHeadsGroup_OnHeadClose;
 67
 468        view.leftWindowContainerLayout.enabled = false;
 69
 470        view.OnChatToggleOff += View_OnChatToggleOff;
 471        view.OnChatToggleOn += View_OnChatToggleOn;
 472        view.OnFriendsToggleOff += View_OnFriendsToggleOff;
 473        view.OnFriendsToggleOn += View_OnFriendsToggleOn;
 474        view.OnExperiencesToggleOff += View_OnExperiencesToggleOff;
 475        view.OnExperiencesToggleOn += View_OnExperiencesToggleOn;
 76
 477        toggleFriendsTrigger = Resources.Load<InputAction_Trigger>("ToggleFriends");
 478        toggleFriendsTrigger.OnTriggered -= ToggleFriendsTrigger_OnTriggered;
 479        toggleFriendsTrigger.OnTriggered += ToggleFriendsTrigger_OnTriggered;
 80
 481        closeWindowTrigger = Resources.Load<InputAction_Trigger>("CloseWindow");
 482        closeWindowTrigger.OnTriggered -= CloseWindowTrigger_OnTriggered;
 483        closeWindowTrigger.OnTriggered += CloseWindowTrigger_OnTriggered;
 84
 485        toggleWorldChatTrigger = Resources.Load<InputAction_Trigger>("ToggleWorldChat");
 486        toggleWorldChatTrigger.OnTriggered -= ToggleWorldChatTrigger_OnTriggered;
 487        toggleWorldChatTrigger.OnTriggered += ToggleWorldChatTrigger_OnTriggered;
 88
 489        isExperiencesViewerOpen.OnChange += IsExperiencesViewerOpenChanged;
 90
 491        if (chatController != null)
 92        {
 393            chatController.OnAddMessage -= OnAddMessage;
 394            chatController.OnAddMessage += OnAddMessage;
 95        }
 96
 497        view.leftWindowContainerAnimator.Show();
 98
 499        CommonScriptableObjects.isTaskbarHUDInitialized.Set(true);
 4100        DataStore.i.builderInWorld.showTaskBar.OnChange += SetVisibility;
 101
 4102        isExperiencesViewerInitialized.OnChange += InitializeExperiencesViewer;
 4103        InitializeExperiencesViewer(isExperiencesViewerInitialized.Get(), null);
 104
 4105        numOfLoadedExperiences.OnChange += NumOfLoadedExperiencesChanged;
 4106        NumOfLoadedExperiencesChanged(numOfLoadedExperiences.Get(), 0);
 4107    }
 108
 2109    private void ChatHeadsGroup_OnHeadClose(TaskbarButton obj) { privateChatWindowHud.SetVisibility(false); }
 110
 111    private void View_OnFriendsToggleOn()
 112    {
 2113        friendsHud?.SetVisibility(true);
 2114        OnAnyTaskbarButtonClicked?.Invoke();
 0115    }
 116
 4117    private void View_OnFriendsToggleOff() { friendsHud?.SetVisibility(false); }
 118
 119    private void View_OnExperiencesToggleOn()
 120    {
 0121        isExperiencesViewerOpen.Set(true);
 0122        OnAnyTaskbarButtonClicked?.Invoke();
 0123    }
 124
 8125    private void View_OnExperiencesToggleOff() { isExperiencesViewerOpen.Set(false); }
 126
 127    private void ToggleFriendsTrigger_OnTriggered(DCLAction_Trigger action)
 128    {
 0129        if (!view.friendsButton.transform.parent.gameObject.activeSelf)
 0130            return;
 131
 0132        OnFriendsToggleInputPress();
 0133    }
 134
 0135    private void ToggleWorldChatTrigger_OnTriggered(DCLAction_Trigger action) { OnWorldChatToggleInputPress(); }
 136
 0137    private void CloseWindowTrigger_OnTriggered(DCLAction_Trigger action) { OnCloseWindowToggleInputPress(); }
 138
 139    private void View_OnChatToggleOn()
 140    {
 3141        worldChatWindowHud.SetVisibility(true);
 3142        worldChatWindowHud.MarkWorldChatMessagesAsRead();
 3143        worldChatWindowHud.view.DeactivatePreview();
 3144        worldChatWindowHud.OnPressReturn();
 3145        OnAnyTaskbarButtonClicked?.Invoke();
 0146    }
 147
 148    private void View_OnChatToggleOff()
 149    {
 2150        if (view.AllButtonsToggledOff())
 151        {
 2152            worldChatWindowHud.SetVisibility(true);
 2153            worldChatWindowHud.view.ActivatePreview();
 2154        }
 155        else
 156        {
 0157            worldChatWindowHud.SetVisibility(false);
 158        }
 0159    }
 160
 161    private void ChatHeadsGroup_OnHeadOpen(TaskbarButton taskbarBtn)
 162    {
 1163        ChatHeadButton head = taskbarBtn as ChatHeadButton;
 164
 1165        if (taskbarBtn == null)
 0166            return;
 167
 1168        OpenPrivateChatWindow(head.profile.userId);
 1169    }
 170
 0171    private void MouseCatcher_OnMouseUnlock() { view.leftWindowContainerAnimator.Show(); }
 172
 173    private void MouseCatcher_OnMouseLock()
 174    {
 0175        view.leftWindowContainerAnimator.Hide();
 176
 0177        foreach (var btn in view.GetButtonList())
 178        {
 0179            btn.SetToggleState(false);
 180        }
 181
 0182        worldChatWindowHud.SetVisibility(true);
 0183        worldChatWindowHud.view.ActivatePreview();
 184
 0185        MarkWorldChatAsReadIfOtherWindowIsOpen();
 0186    }
 187
 188    public void AddWorldChatWindow(WorldChatWindowHUDController controller)
 189    {
 2190        if (controller == null || controller.view == null)
 191        {
 0192            Debug.LogWarning("AddChatWindow >>> World Chat Window doesn't exist yet!");
 0193            return;
 194        }
 195
 2196        if (controller.view.transform.parent == view.leftWindowContainer)
 0197            return;
 198
 2199        controller.view.transform.SetParent(view.leftWindowContainer, false);
 2200        experiencesViewerTransform?.SetAsLastSibling();
 201
 2202        worldChatWindowHud = controller;
 203
 2204        view.OnAddChatWindow();
 2205        worldChatWindowHud.view.OnClose += () => { view.friendsButton.SetToggleState(false, false); };
 206
 2207        view.chatButton.SetToggleState(true);
 2208        view.chatButton.SetToggleState(false);
 2209    }
 210
 0211    public void OpenFriendsWindow() { view.friendsButton.SetToggleState(true); }
 212
 213    public void OpenPrivateChatTo(string userId)
 214    {
 0215        var button = view.chatHeadsGroup.AddChatHead(userId, (ulong) System.DateTimeOffset.UtcNow.ToUnixTimeMilliseconds
 0216        button.toggleButton.onClick.Invoke();
 0217    }
 218
 219    public void AddPrivateChatWindow(PrivateChatWindowHUDController controller)
 220    {
 2221        if (controller == null || controller.view == null)
 222        {
 0223            Debug.LogWarning("AddPrivateChatWindow >>> Private Chat Window doesn't exist yet!");
 0224            return;
 225        }
 226
 2227        if (controller.view.transform.parent == view.leftWindowContainer)
 0228            return;
 229
 2230        controller.view.transform.SetParent(view.leftWindowContainer, false);
 2231        experiencesViewerTransform?.SetAsLastSibling();
 232
 2233        privateChatWindowHud = controller;
 234
 2235        privateChatWindowHud.view.OnMinimize += () =>
 236        {
 0237            ChatHeadButton btn = view.GetButtonList()
 238                                     .FirstOrDefault(
 0239                                         (x) => x is ChatHeadButton &&
 240                                                (x as ChatHeadButton).profile.userId == privateChatWindowHud.conversatio
 241                ChatHeadButton;
 242
 0243            if (btn != null)
 0244                btn.SetToggleState(false, false);
 245
 0246            MarkWorldChatAsReadIfOtherWindowIsOpen();
 0247        };
 248
 2249        privateChatWindowHud.view.OnClose += () =>
 250        {
 0251            ChatHeadButton btn = view.GetButtonList()
 252                                     .FirstOrDefault(
 0253                                         (x) => x is ChatHeadButton &&
 254                                                (x as ChatHeadButton).profile.userId == privateChatWindowHud.conversatio
 255                ChatHeadButton;
 256
 0257            if (btn != null)
 258            {
 0259                btn.SetToggleState(false, false);
 0260                view.chatHeadsGroup.RemoveChatHead(btn);
 261            }
 262
 0263            MarkWorldChatAsReadIfOtherWindowIsOpen();
 0264        };
 2265    }
 266
 267    public void AddFriendsWindow(FriendsHUDController controller)
 268    {
 3269        if (controller == null || controller.view == null)
 270        {
 0271            Debug.LogWarning("AddFriendsWindow >>> Friends window doesn't exist yet!");
 0272            return;
 273        }
 274
 3275        if (controller.view.transform.parent == view.leftWindowContainer)
 0276            return;
 277
 3278        controller.view.transform.SetParent(view.leftWindowContainer, false);
 3279        experiencesViewerTransform?.SetAsLastSibling();
 280
 3281        friendsHud = controller;
 3282        view.OnAddFriendsWindow();
 3283        friendsHud.view.OnClose += () =>
 284        {
 0285            view.friendsButton.SetToggleState(false, false);
 0286            MarkWorldChatAsReadIfOtherWindowIsOpen();
 0287        };
 288
 3289        friendsHud.view.friendsList.OnDeleteConfirmation += (userIdToRemove) => { view.chatHeadsGroup.RemoveChatHead(use
 3290    }
 291
 292    internal void InitializeExperiencesViewer(Transform currentViewTransform, Transform previousViewTransform)
 293    {
 4294        if (currentViewTransform == null)
 4295            return;
 296
 0297        experiencesViewerTransform = currentViewTransform;
 0298        experiencesViewerTransform.SetParent(view.leftWindowContainer, false);
 0299        experiencesViewerTransform.SetAsLastSibling();
 300
 0301        view.OnAddExperiencesWindow();
 0302    }
 303
 304    private void IsExperiencesViewerOpenChanged(bool current, bool previous)
 305    {
 0306        if (current)
 0307            return;
 308
 0309        view.experiencesButton.SetToggleState(false, false);
 0310        MarkWorldChatAsReadIfOtherWindowIsOpen();
 0311    }
 312
 313    private void NumOfLoadedExperiencesChanged(int current, int previous)
 314    {
 4315        view.SetExperiencesVisbility(current > 0);
 316
 4317        if (current == 0)
 4318            View_OnExperiencesToggleOff();
 4319    }
 320
 0321    public void OnAddVoiceChat() { view.OnAddVoiceChat(); }
 322
 323    public void DisableFriendsWindow()
 324    {
 0325        view.friendsButton.transform.parent.gameObject.SetActive(false);
 0326        view.chatHeadsGroup.ClearChatHeads();
 0327    }
 328
 329    private void OpenPrivateChatWindow(string userId)
 330    {
 1331        privateChatWindowHud.Configure(userId);
 1332        privateChatWindowHud.SetVisibility(true);
 1333        privateChatWindowHud.ForceFocus();
 1334        OnAnyTaskbarButtonClicked?.Invoke();
 0335    }
 336
 337    public void Dispose()
 338    {
 4339        if (view != null)
 340        {
 4341            view.chatHeadsGroup.OnHeadToggleOn -= ChatHeadsGroup_OnHeadOpen;
 4342            view.chatHeadsGroup.OnHeadToggleOff -= ChatHeadsGroup_OnHeadClose;
 343
 4344            view.OnChatToggleOff -= View_OnChatToggleOff;
 4345            view.OnChatToggleOn -= View_OnChatToggleOn;
 4346            view.OnFriendsToggleOff -= View_OnFriendsToggleOff;
 4347            view.OnFriendsToggleOn -= View_OnFriendsToggleOn;
 4348            view.OnExperiencesToggleOff -= View_OnExperiencesToggleOff;
 4349            view.OnExperiencesToggleOn -= View_OnExperiencesToggleOn;
 350
 4351            UnityEngine.Object.Destroy(view.gameObject);
 352        }
 353
 4354        if (mouseCatcher != null)
 355        {
 1356            mouseCatcher.OnMouseLock -= MouseCatcher_OnMouseLock;
 1357            mouseCatcher.OnMouseUnlock -= MouseCatcher_OnMouseUnlock;
 358        }
 359
 4360        if (toggleFriendsTrigger != null)
 4361            toggleFriendsTrigger.OnTriggered -= ToggleFriendsTrigger_OnTriggered;
 362
 4363        if (closeWindowTrigger != null)
 4364            closeWindowTrigger.OnTriggered -= CloseWindowTrigger_OnTriggered;
 365
 4366        if (toggleWorldChatTrigger != null)
 4367            toggleWorldChatTrigger.OnTriggered -= ToggleWorldChatTrigger_OnTriggered;
 368
 4369        if (chatController != null)
 3370            chatController.OnAddMessage -= OnAddMessage;
 371
 4372        DataStore.i.builderInWorld.showTaskBar.OnChange -= SetVisibility;
 4373        isExperiencesViewerOpen.OnChange -= IsExperiencesViewerOpenChanged;
 4374        isExperiencesViewerInitialized.OnChange -= InitializeExperiencesViewer;
 4375        numOfLoadedExperiences.OnChange -= NumOfLoadedExperiencesChanged;
 4376    }
 377
 0378    public void SetVisibility(bool visible, bool previus) { SetVisibility(visible); }
 379
 2380    public void SetVisibility(bool visible) { view.SetVisibility(visible); }
 381
 382    public void OnWorldChatToggleInputPress()
 383    {
 0384        bool anyInputFieldIsSelected = EventSystem.current != null &&
 385                                       EventSystem.current.currentSelectedGameObject != null &&
 386                                       EventSystem.current.currentSelectedGameObject.GetComponent<TMPro.TMP_InputField>(
 387
 0388        if (anyInputFieldIsSelected)
 0389            return;
 390
 0391        worldChatWindowHud.OnPressReturn();
 392
 0393        if (AnyWindowsDifferentThanChatIsOpen())
 394        {
 0395            foreach (var btn in view.GetButtonList())
 396            {
 0397                btn.SetToggleState(btn == view.chatButton);
 398            }
 399        }
 0400    }
 401
 402    public void OnCloseWindowToggleInputPress()
 403    {
 0404        if (mouseCatcher.isLocked)
 0405            return;
 406
 0407        view.chatButton.SetToggleState(false, false);
 0408        worldChatWindowHud.view.chatHudView.ResetInputField();
 0409        worldChatWindowHud.view.ActivatePreview();
 0410    }
 411
 0412    public void SetVoiceChatRecording(bool recording) { view?.voiceChatButton.SetOnRecording(recording); }
 413
 0414    public void SetVoiceChatEnabledByScene(bool enabled) { view?.voiceChatButton.SetEnabledByScene(enabled); }
 415
 416    private void OnFriendsToggleInputPress()
 417    {
 0418        bool anyInputFieldIsSelected = EventSystem.current != null &&
 419                                       EventSystem.current.currentSelectedGameObject != null &&
 420                                       EventSystem.current.currentSelectedGameObject.GetComponent<TMPro.TMP_InputField>(
 421                                       (!worldChatWindowHud.view.chatHudView.inputField.isFocused || !worldChatWindowHud
 422
 0423        if (anyInputFieldIsSelected)
 0424            return;
 425
 0426        Utils.UnlockCursor();
 0427        view.leftWindowContainerAnimator.Show();
 0428        view.friendsButton.SetToggleState(!view.friendsButton.toggledOn);
 0429    }
 430
 431    void OnAddMessage(ChatMessage message)
 432    {
 1433        if (!AnyWindowsDifferentThanChatIsOpen() && message.messageType == ChatMessage.Type.PUBLIC)
 0434            worldChatWindowHud.MarkWorldChatMessagesAsRead((long) message.timestamp);
 1435    }
 436
 437    private bool AnyWindowsDifferentThanChatIsOpen()
 438    {
 1439        return (friendsHud != null && friendsHud.view.gameObject.activeSelf) ||
 440               (privateChatWindowHud != null && privateChatWindowHud.view.gameObject.activeSelf);
 441    }
 442
 443    private void MarkWorldChatAsReadIfOtherWindowIsOpen()
 444    {
 0445        if (!AnyWindowsDifferentThanChatIsOpen())
 0446            worldChatWindowHud.MarkWorldChatMessagesAsRead();
 0447    }
 448}