< Summary

Class:HUDController
Assembly:HUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/HUDController.cs
Covered lines:174
Uncovered lines:59
Coverable lines:233
Total lines:510
Line coverage:74.6% (174 of 233)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
HUDController(...)0%110100%
Initialize()0%220100%
ShowSettings()0%6200%
ShowControls()0%6200%
ToggleUIVisibility_OnTriggered(...)0%42600%
AllUIHiddenOnOnChange(...)0%440100%
ConfigureHUDElement(...)0%46.6544088.89%
HandlePublicChatChannelBacked()0%6200%
OpenPublicChannelWindow(...)0%6200%
OpenPrivateChatWindow(...)0%6200%
PrivateChatWindowHud_OnPressBack()0%12300%
TaskbarHud_onAnyTaskbarButtonClicked()0%6200%
CreateHudElement(...)0%4.074083.33%
UpdateHudElement(...)0%12300%
Cleanup()0%880100%
HandlePublicChatChannelClosed()0%2100%
GetHUDElement(...)0%220100%
IsHUDElementDeprecated(...)0%110100%
TriggerFakePlayerInfoCard()0%2100%
Dispose()0%110100%

File(s)

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

#LineLine coverage
 1using DCL;
 2using DCL.HelpAndSupportHUD;
 3using DCL.Huds.QuestsPanel;
 4using DCL.Huds.QuestsTracker;
 5using DCL.QuestsController;
 6using DCL.SettingsPanelHUD;
 7using SignupHUD;
 8using SocialFeaturesAnalytics;
 9using System;
 10using System.Collections.Generic;
 11using DCL.Chat;
 12using UnityEngine;
 13using UnityEngine.EventSystems;
 14
 15public class HUDController : IHUDController
 16{
 17    private const string TOGGLE_UI_VISIBILITY_ASSET_NAME = "ToggleUIVisibility";
 18
 19    static bool VERBOSE = false;
 020    public static HUDController i { get; private set; }
 21
 22    public IHUDFactory hudFactory = null;
 23
 24    private InputAction_Trigger toggleUIVisibilityTrigger;
 25    private ISocialAnalytics socialAnalytics;
 26
 66727    private readonly DCL.NotificationModel.Model hiddenUINotification = new DCL.NotificationModel.Model()
 28    {
 29        timer = 3,
 30        type = DCL.NotificationModel.Type.UI_HIDDEN,
 31        groupID = "UIHiddenNotification"
 32    };
 33
 66734    public HUDController(IHUDFactory hudFactory = null)
 35    {
 66736        this.hudFactory = hudFactory;
 66737        socialAnalytics = new SocialAnalytics(
 38            DCL.Environment.i.platform.serviceProviders.analytics,
 39            new UserProfileWebInterfaceBridge());
 66740    }
 41
 42    public void Initialize()
 43    {
 66744        i = this;
 45
 66746        if (this.hudFactory == null)
 66547            this.hudFactory = DCL.Environment.i.hud.factory;
 48
 66749        toggleUIVisibilityTrigger = Resources.Load<InputAction_Trigger>(TOGGLE_UI_VISIBILITY_ASSET_NAME);
 66750        toggleUIVisibilityTrigger.OnTriggered += ToggleUIVisibility_OnTriggered;
 51
 66752        CommonScriptableObjects.allUIHidden.OnChange += AllUIHiddenOnOnChange;
 66753        UserContextMenu.OnOpenPrivateChatRequest += OpenPrivateChatWindow;
 66754    }
 55
 56
 57    public event Action OnTaskbarCreation;
 58
 059    public ProfileHUDController profileHud => GetHUDElement(HUDElementID.PROFILE_HUD) as ProfileHUDController;
 60
 61    public NotificationHUDController notificationHud =>
 162        GetHUDElement(HUDElementID.NOTIFICATION) as NotificationHUDController;
 63
 364    public MinimapHUDController minimapHud => GetHUDElement(HUDElementID.MINIMAP) as MinimapHUDController;
 65
 66    public AvatarEditorHUDController avatarEditorHud =>
 267        GetHUDElement(HUDElementID.AVATAR_EDITOR) as AvatarEditorHUDController;
 68
 69    public SettingsPanelHUDController settingsPanelHud =>
 370        GetHUDElement(HUDElementID.SETTINGS_PANEL) as SettingsPanelHUDController;
 71
 72    public PlayerInfoCardHUDController playerInfoCardHud =>
 073        GetHUDElement(HUDElementID.PLAYER_INFO_CARD) as PlayerInfoCardHUDController;
 74
 75    public AirdroppingHUDController airdroppingHud =>
 076        GetHUDElement(HUDElementID.AIRDROPPING) as AirdroppingHUDController;
 77
 78    public TermsOfServiceHUDController termsOfServiceHud =>
 079        GetHUDElement(HUDElementID.TERMS_OF_SERVICE) as TermsOfServiceHUDController;
 80
 68081    public TaskbarHUDController taskbarHud => GetHUDElement(HUDElementID.TASKBAR) as TaskbarHUDController;
 82
 283    public LoadingHUDController loadingHud => GetHUDElement(HUDElementID.LOADING) as LoadingHUDController;
 84
 85    public WorldChatWindowController worldChatWindowHud =>
 67886        GetHUDElement(HUDElementID.WORLD_CHAT_WINDOW) as WorldChatWindowController;
 87
 88    public PrivateChatWindowController PrivateChatWindow =>
 67589        GetHUDElement(HUDElementID.PRIVATE_CHAT_WINDOW) as PrivateChatWindowController;
 90
 91    public PublicChatChannelController PublicChatChannelHud =>
 67792        GetHUDElement(HUDElementID.PUBLIC_CHAT_CHANNEL) as PublicChatChannelController;
 93
 67594    public FriendsHUDController friendsHud => GetHUDElement(HUDElementID.FRIENDS) as FriendsHUDController;
 95
 96    public TeleportPromptHUDController teleportHud =>
 097        GetHUDElement(HUDElementID.TELEPORT_DIALOG) as TeleportPromptHUDController;
 98
 099    public ControlsHUDController controlsHud => GetHUDElement(HUDElementID.CONTROLS_HUD) as ControlsHUDController;
 100
 101    public HelpAndSupportHUDController helpAndSupportHud =>
 1102        GetHUDElement(HUDElementID.HELP_AND_SUPPORT_HUD) as HelpAndSupportHUDController;
 103
 0104    public MinimapHUDController minimapHUD => GetHUDElement(HUDElementID.MINIMAP) as MinimapHUDController;
 105
 106    public VoiceChatWindowController voiceChatHud =>
 2107        GetHUDElement(HUDElementID.USERS_AROUND_LIST_HUD) as VoiceChatWindowController;
 108
 109    public QuestsPanelHUDController questsPanelHUD =>
 1110        GetHUDElement(HUDElementID.QUESTS_PANEL) as QuestsPanelHUDController;
 111
 112    public QuestsTrackerHUDController questsTrackerHUD =>
 1113        GetHUDElement(HUDElementID.QUESTS_TRACKER) as QuestsTrackerHUDController;
 114
 1115    public SignupHUDController signupHUD => GetHUDElement(HUDElementID.SIGNUP) as SignupHUDController;
 1116    public LoadingHUDController loadingController => GetHUDElement(HUDElementID.LOADING) as LoadingHUDController;
 117
 667118    public Dictionary<HUDElementID, IHUD> hudElements { get; private set; } = new Dictionary<HUDElementID, IHUD>();
 119
 1120    private UserProfile ownUserProfile => UserProfile.GetOwnUserProfile();
 1121    private BaseDictionary<string, WearableItem> wearableCatalog => CatalogController.wearableCatalog;
 122
 123    private void ShowSettings()
 124    {
 0125        settingsPanelHud?.SetVisibility(true);
 0126    }
 127
 128    private void ShowControls()
 129    {
 0130        controlsHud?.SetVisibility(true);
 0131    }
 132
 133    private void ToggleUIVisibility_OnTriggered(DCLAction_Trigger action)
 134    {
 0135        bool anyInputFieldIsSelected = EventSystem.current != null &&
 136                                       EventSystem.current.currentSelectedGameObject != null &&
 137                                       EventSystem.current.currentSelectedGameObject
 138                                           .GetComponent<TMPro.TMP_InputField>() != null;
 139
 0140        if (anyInputFieldIsSelected ||
 141            DataStore.i.exploreV2.isOpen.Get() ||
 142            CommonScriptableObjects.tutorialActive)
 0143            return;
 144
 0145        CommonScriptableObjects.allUIHidden.Set(!CommonScriptableObjects.allUIHidden.Get());
 0146    }
 147
 148    private void AllUIHiddenOnOnChange(bool current, bool previous)
 149    {
 3150        if (current)
 151        {
 2152            NotificationsController.i?.ShowNotification(hiddenUINotification);
 2153        }
 154        else
 155        {
 1156            NotificationsController.i?.DismissAllNotifications(hiddenUINotification.groupID);
 157        }
 1158    }
 159
 160    public void ConfigureHUDElement(HUDElementID hudElementId, HUDConfiguration configuration,
 161        string extraPayload = null)
 162    {
 163        //TODO(Brian): For now, the factory code is using this switch approach.
 164        //             In order to avoid the factory upkeep we can transform the IHUD elements
 165        //             To ScriptableObjects. In this scenario, we can make each element handle its own
 166        //             specific initialization details.
 167        //
 168        //             This will allow us to unify the serialized factory objects design,
 169        //             like we already do with ECS components.
 170
 171        switch (hudElementId)
 172        {
 173            case HUDElementID.NONE:
 174                break;
 175            case HUDElementID.MINIMAP:
 1176                if (minimapHud == null)
 177                {
 1178                    CreateHudElement(configuration, hudElementId);
 179
 1180                    if (minimapHud != null)
 181                    {
 1182                        minimapHud.Initialize();
 183                    }
 184                }
 185
 1186                break;
 187            case HUDElementID.PROFILE_HUD:
 1188                CreateHudElement(configuration, hudElementId);
 1189                break;
 190            case HUDElementID.NOTIFICATION:
 1191                CreateHudElement(configuration, hudElementId);
 1192                NotificationsController.i?.Initialize(notificationHud);
 1193                break;
 194            case HUDElementID.AVATAR_EDITOR:
 1195                CreateHudElement(configuration, hudElementId);
 1196                avatarEditorHud?.Initialize(ownUserProfile, wearableCatalog);
 1197                break;
 198            case HUDElementID.SETTINGS_PANEL:
 1199                CreateHudElement(configuration, hudElementId);
 1200                if (settingsPanelHud != null)
 1201                    settingsPanelHud.Initialize();
 1202                break;
 203            case HUDElementID.PLAYER_INFO_CARD:
 1204                CreateHudElement(configuration, hudElementId);
 1205                break;
 206            case HUDElementID.AIRDROPPING:
 1207                CreateHudElement(configuration, hudElementId);
 1208                break;
 209            case HUDElementID.TERMS_OF_SERVICE:
 1210                CreateHudElement(configuration, hudElementId);
 1211                break;
 212            case HUDElementID.WORLD_CHAT_WINDOW:
 1213                if (worldChatWindowHud == null)
 214                {
 1215                    CreateHudElement(configuration, hudElementId);
 216
 1217                    if (worldChatWindowHud != null)
 218                    {
 1219                        worldChatWindowHud.Initialize(WorldChatWindowComponentView.Create());
 1220                        worldChatWindowHud.SetVisibility(false);
 1221                        worldChatWindowHud.OnOpenPrivateChat -= OpenPrivateChatWindow;
 1222                        worldChatWindowHud.OnOpenPrivateChat += OpenPrivateChatWindow;
 1223                        worldChatWindowHud.OnOpenPublicChannel -= OpenPublicChannelWindow;
 1224                        worldChatWindowHud.OnOpenPublicChannel += OpenPublicChannelWindow;
 225
 1226                        taskbarHud?.AddWorldChatWindow(worldChatWindowHud);
 227                    }
 0228                }
 229                else
 0230                    UpdateHudElement(configuration, hudElementId);
 231
 1232                if (PublicChatChannelHud == null)
 233                {
 1234                    CreateHudElement(configuration, HUDElementID.PUBLIC_CHAT_CHANNEL);
 1235                    PublicChatChannelHud.Initialize();
 1236                    PublicChatChannelHud.OnBack -= HandlePublicChatChannelBacked;
 1237                    PublicChatChannelHud.OnBack += HandlePublicChatChannelBacked;
 1238                    PublicChatChannelHud.OnClosed -= HandlePublicChatChannelClosed;
 1239                    PublicChatChannelHud.OnClosed += HandlePublicChatChannelClosed;
 1240                    taskbarHud?.AddPublicChatChannel(PublicChatChannelHud);
 241                    // TODO: this call should be removed when chat notifications are implemented
 1242                    taskbarHud?.OpenPublicChatChannel(ChatUtils.NEARBY_CHANNEL_ID, false);
 1243                    PublicChatChannelHud.ActivatePreviewModeInstantly();
 1244                }
 245                else
 0246                    UpdateHudElement(configuration, HUDElementID.PUBLIC_CHAT_CHANNEL);
 247
 1248                if (PrivateChatWindow == null)
 249                {
 1250                    CreateHudElement(configuration, HUDElementID.PRIVATE_CHAT_WINDOW);
 251
 1252                    if (PrivateChatWindow != null)
 253                    {
 1254                        PrivateChatWindow.Initialize();
 1255                        PrivateChatWindow.SetVisibility(false);
 1256                        PrivateChatWindow.OnPressBack -= PrivateChatWindowHud_OnPressBack;
 1257                        PrivateChatWindow.OnPressBack += PrivateChatWindowHud_OnPressBack;
 258
 1259                        taskbarHud?.AddPrivateChatWindow(PrivateChatWindow);
 260                    }
 0261                }
 262                else
 0263                    UpdateHudElement(configuration, HUDElementID.PRIVATE_CHAT_WINDOW);
 264
 0265                break;
 266            case HUDElementID.FRIENDS:
 1267                if (friendsHud == null)
 268                {
 1269                    CreateHudElement(configuration, hudElementId);
 270
 1271                    if (friendsHud != null)
 272                    {
 1273                        friendsHud.Initialize();
 1274                        friendsHud.OnPressWhisper -= OpenPrivateChatWindow;
 1275                        friendsHud.OnPressWhisper += OpenPrivateChatWindow;
 276
 1277                        taskbarHud?.AddFriendsWindow(friendsHud);
 278                    }
 1279                }
 280                else
 281                {
 0282                    UpdateHudElement(configuration, hudElementId);
 283
 0284                    if (!configuration.active)
 0285                        taskbarHud?.DisableFriendsWindow();
 286                }
 287
 0288                break;
 289            case HUDElementID.TASKBAR:
 1290                if (taskbarHud == null)
 291                {
 1292                    CreateHudElement(configuration, hudElementId);
 293
 1294                    if (taskbarHud != null)
 295                    {
 1296                        taskbarHud.Initialize(SceneReferences.i.mouseCatcher);
 1297                        taskbarHud.OnAnyTaskbarButtonClicked -= TaskbarHud_onAnyTaskbarButtonClicked;
 1298                        taskbarHud.OnAnyTaskbarButtonClicked += TaskbarHud_onAnyTaskbarButtonClicked;
 299
 1300                        OnTaskbarCreation?.Invoke();
 301                    }
 0302                }
 303                else
 304                {
 0305                    UpdateHudElement(configuration, hudElementId);
 306                }
 307
 0308                break;
 309            case HUDElementID.OPEN_EXTERNAL_URL_PROMPT:
 1310                CreateHudElement(configuration, hudElementId);
 1311                break;
 312            case HUDElementID.NFT_INFO_DIALOG:
 1313                CreateHudElement(configuration, hudElementId);
 1314                break;
 315            case HUDElementID.TELEPORT_DIALOG:
 1316                CreateHudElement(configuration, hudElementId);
 1317                break;
 318            case HUDElementID.CONTROLS_HUD:
 1319                CreateHudElement(configuration, hudElementId);
 1320                break;
 321            case HUDElementID.HELP_AND_SUPPORT_HUD:
 1322                CreateHudElement(configuration, hudElementId);
 1323                settingsPanelHud?.AddHelpAndSupportWindow(helpAndSupportHud);
 1324                break;
 325            case HUDElementID.USERS_AROUND_LIST_HUD:
 1326                CreateHudElement(configuration, hudElementId);
 1327                if (voiceChatHud != null)
 1328                    taskbarHud?.AddVoiceChatWindow(voiceChatHud);
 329
 1330                break;
 331            case HUDElementID.GRAPHIC_CARD_WARNING:
 1332                CreateHudElement(configuration, hudElementId);
 1333                break;
 334            case HUDElementID.QUESTS_PANEL:
 1335                CreateHudElement(configuration, hudElementId);
 1336                if (configuration.active)
 1337                    questsPanelHUD.Initialize(QuestsController.i);
 1338                break;
 339            case HUDElementID.QUESTS_TRACKER:
 1340                CreateHudElement(configuration, hudElementId);
 1341                if (configuration.active)
 1342                    questsTrackerHUD.Initialize(QuestsController.i);
 1343                break;
 344            case HUDElementID.SIGNUP:
 1345                CreateHudElement(configuration, hudElementId);
 1346                if (configuration.active)
 347                {
 348                    //Same race condition risks as with the ProfileHUD
 349                    //TODO Refactor the way AvatarEditor sets its visibility to match our data driven pattern
 350                    //Then this reference can be removed so we just work with a BaseVariable<bool>.
 351                    //This refactor applies to the ProfileHUD and the way kernel asks the HUDController during signup
 1352                    signupHUD.Initialize(avatarEditorHud);
 353                }
 354
 1355                break;
 356            case HUDElementID.LOADING:
 1357                if (loadingHud == null)
 358                {
 1359                    CreateHudElement(configuration, hudElementId);
 1360                    if (loadingHud != null && configuration.active)
 1361                        loadingController.Initialize();
 362                }
 363
 364                break;
 365            case HUDElementID.AVATAR_NAMES:
 366                // TODO Remove the HUDElementId once kernel stops sending the Configure HUD message
 367                break;
 368        }
 369
 33370        var hudElement = GetHUDElement(hudElementId);
 371
 33372        if (hudElement != null)
 24373            hudElement.SetVisibility(configuration.active && configuration.visible);
 33374    }
 375
 376    private void HandlePublicChatChannelBacked()
 377    {
 0378        PublicChatChannelHud.SetVisibility(false);
 0379        taskbarHud?.GoBackFromChat();
 0380    }
 381
 382    private void OpenPublicChannelWindow(string channelId)
 383    {
 0384        taskbarHud?.OpenPublicChatChannel(channelId, true);
 0385    }
 386
 387    private void OpenPrivateChatWindow(string targetUserId)
 388    {
 0389        taskbarHud?.OpenPrivateChat(targetUserId);
 0390    }
 391
 392    private void PrivateChatWindowHud_OnPressBack()
 393    {
 0394        PrivateChatWindow?.SetVisibility(false);
 0395        taskbarHud?.GoBackFromChat();
 0396    }
 397
 398    private void TaskbarHud_onAnyTaskbarButtonClicked()
 399    {
 0400        playerInfoCardHud?.CloseCard();
 0401    }
 402
 403    public void CreateHudElement(HUDConfiguration config, HUDElementID id)
 404    {
 24405        bool controllerCreated = hudElements.ContainsKey(id);
 406
 24407        if (config.active && !controllerCreated)
 408        {
 24409            hudElements.Add(id, hudFactory.CreateHUD(id));
 410
 24411            if (VERBOSE)
 0412                Debug.Log($"Adding {id} .. type {hudElements[id].GetType().Name}");
 413        }
 24414    }
 415
 416    public void UpdateHudElement(HUDConfiguration config, HUDElementID id)
 417    {
 0418        if (!hudElements.ContainsKey(id))
 0419            return;
 420
 0421        if (VERBOSE)
 0422            Debug.Log(
 423                $"Updating {id}, type {hudElements[id].GetType().Name}, active: {config.active} visible: {config.visible
 424
 0425        hudElements[id].SetVisibility(config.visible);
 0426    }
 427
 428    public void Cleanup()
 429    {
 668430        toggleUIVisibilityTrigger.OnTriggered -= ToggleUIVisibility_OnTriggered;
 668431        CommonScriptableObjects.allUIHidden.OnChange -= AllUIHiddenOnOnChange;
 432
 668433        if (worldChatWindowHud != null)
 434        {
 1435            worldChatWindowHud.OnOpenPrivateChat -= OpenPrivateChatWindow;
 1436            worldChatWindowHud.OnOpenPublicChannel -= OpenPublicChannelWindow;
 437        }
 438
 668439        if (PrivateChatWindow != null)
 1440            PrivateChatWindow.OnPressBack -= PrivateChatWindowHud_OnPressBack;
 441
 668442        if (PublicChatChannelHud != null)
 443        {
 1444            PublicChatChannelHud.OnClosed -= HandlePublicChatChannelClosed;
 1445            PublicChatChannelHud.OnBack -= HandlePublicChatChannelBacked;
 446        }
 447
 448
 668449        if (friendsHud != null)
 1450            friendsHud.OnPressWhisper -= OpenPrivateChatWindow;
 451
 668452        if (taskbarHud != null)
 1453            taskbarHud.OnAnyTaskbarButtonClicked -= TaskbarHud_onAnyTaskbarButtonClicked;
 454
 668455        UserContextMenu.OnOpenPrivateChatRequest -= OpenPrivateChatWindow;
 456
 1384457        foreach (var kvp in hudElements)
 458        {
 24459            kvp.Value?.Dispose();
 460        }
 461
 668462        hudElements.Clear();
 668463    }
 464
 465    private void HandlePublicChatChannelClosed()
 466    {
 0467        PublicChatChannelHud.SetVisibility(false);
 0468    }
 469
 470    public IHUD GetHUDElement(HUDElementID id)
 471    {
 3527472        if (!hudElements.ContainsKey(id))
 3422473            return null;
 474
 105475        return hudElements[id];
 476    }
 477
 478    public static bool IsHUDElementDeprecated(HUDElementID element)
 479    {
 33480        Type enumType = typeof(HUDElementID);
 33481        var enumName = enumType.GetEnumName(element);
 33482        var fieldInfo = enumType.GetField(enumName);
 33483        return Attribute.IsDefined(fieldInfo, typeof(ObsoleteAttribute));
 484    }
 485
 486#if UNITY_EDITOR
 487    [ContextMenu("Trigger fake PlayerInfoCard")]
 488    public void TriggerFakePlayerInfoCard()
 489    {
 0490        var newModel = ownUserProfile.CloneModel();
 0491        newModel.name = "FakePassport";
 0492        newModel.description = "Fake Description for Testing";
 0493        newModel.userId = "test-id";
 494
 0495        UserProfileController.i.AddUserProfileToCatalog(newModel);
 0496        UserProfileController.GetProfileByUserId(newModel.userId).SetInventory(new[]
 497        {
 498            "dcl://halloween_2019/machete_headband_top_head",
 499            "dcl://halloween_2019/bee_suit_upper_body",
 500            "dcl://halloween_2019/bride_of_frankie_upper_body",
 501            "dcl://halloween_2019/creepy_nurse_upper_body",
 502        });
 0503        Resources.Load<StringVariable>("CurrentPlayerInfoCardId").Set(newModel.userId);
 0504    }
 505#endif
 506    public void Dispose()
 507    {
 667508        Cleanup();
 667509    }
 510}