< 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:509
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 UnityEngine;
 12using UnityEngine.EventSystems;
 13
 14public class HUDController : IHUDController
 15{
 16    private const string TOGGLE_UI_VISIBILITY_ASSET_NAME = "ToggleUIVisibility";
 17
 18    static bool VERBOSE = false;
 019    public static HUDController i { get; private set; }
 20
 21    public IHUDFactory hudFactory = null;
 22
 23    private InputAction_Trigger toggleUIVisibilityTrigger;
 24    private ISocialAnalytics socialAnalytics;
 25
 66326    private readonly DCL.NotificationModel.Model hiddenUINotification = new DCL.NotificationModel.Model()
 27    {
 28        timer = 3,
 29        type = DCL.NotificationModel.Type.UI_HIDDEN,
 30        groupID = "UIHiddenNotification"
 31    };
 32
 66333    public HUDController(IHUDFactory hudFactory = null)
 34    {
 66335        this.hudFactory = hudFactory;
 66336        socialAnalytics = new SocialAnalytics(
 37            DCL.Environment.i.platform.serviceProviders.analytics,
 38            new UserProfileWebInterfaceBridge());
 66339    }
 40
 41    public void Initialize()
 42    {
 66343        i = this;
 44
 66345        if (this.hudFactory == null)
 66146            this.hudFactory = DCL.Environment.i.hud.factory;
 47
 66348        toggleUIVisibilityTrigger = Resources.Load<InputAction_Trigger>(TOGGLE_UI_VISIBILITY_ASSET_NAME);
 66349        toggleUIVisibilityTrigger.OnTriggered += ToggleUIVisibility_OnTriggered;
 50
 66351        CommonScriptableObjects.allUIHidden.OnChange += AllUIHiddenOnOnChange;
 66352        UserContextMenu.OnOpenPrivateChatRequest += OpenPrivateChatWindow;
 66353    }
 54
 55
 56    public event Action OnTaskbarCreation;
 57
 058    public ProfileHUDController profileHud => GetHUDElement(HUDElementID.PROFILE_HUD) as ProfileHUDController;
 59
 60    public NotificationHUDController notificationHud =>
 161        GetHUDElement(HUDElementID.NOTIFICATION) as NotificationHUDController;
 62
 363    public MinimapHUDController minimapHud => GetHUDElement(HUDElementID.MINIMAP) as MinimapHUDController;
 64
 65    public AvatarEditorHUDController avatarEditorHud =>
 266        GetHUDElement(HUDElementID.AVATAR_EDITOR) as AvatarEditorHUDController;
 67
 68    public SettingsPanelHUDController settingsPanelHud =>
 369        GetHUDElement(HUDElementID.SETTINGS_PANEL) as SettingsPanelHUDController;
 70
 71    public PlayerInfoCardHUDController playerInfoCardHud =>
 072        GetHUDElement(HUDElementID.PLAYER_INFO_CARD) as PlayerInfoCardHUDController;
 73
 74    public AirdroppingHUDController airdroppingHud =>
 075        GetHUDElement(HUDElementID.AIRDROPPING) as AirdroppingHUDController;
 76
 77    public TermsOfServiceHUDController termsOfServiceHud =>
 078        GetHUDElement(HUDElementID.TERMS_OF_SERVICE) as TermsOfServiceHUDController;
 79
 67680    public TaskbarHUDController taskbarHud => GetHUDElement(HUDElementID.TASKBAR) as TaskbarHUDController;
 81
 282    public LoadingHUDController loadingHud => GetHUDElement(HUDElementID.LOADING) as LoadingHUDController;
 83
 84    public WorldChatWindowController worldChatWindowHud =>
 67485        GetHUDElement(HUDElementID.WORLD_CHAT_WINDOW) as WorldChatWindowController;
 86
 87    public PrivateChatWindowController PrivateChatWindow =>
 67188        GetHUDElement(HUDElementID.PRIVATE_CHAT_WINDOW) as PrivateChatWindowController;
 89
 90    public PublicChatChannelController PublicChatChannelHud =>
 67391        GetHUDElement(HUDElementID.PUBLIC_CHAT_CHANNEL) as PublicChatChannelController;
 92
 67193    public FriendsHUDController friendsHud => GetHUDElement(HUDElementID.FRIENDS) as FriendsHUDController;
 94
 95    public TeleportPromptHUDController teleportHud =>
 096        GetHUDElement(HUDElementID.TELEPORT_DIALOG) as TeleportPromptHUDController;
 97
 098    public ControlsHUDController controlsHud => GetHUDElement(HUDElementID.CONTROLS_HUD) as ControlsHUDController;
 99
 100    public HelpAndSupportHUDController helpAndSupportHud =>
 1101        GetHUDElement(HUDElementID.HELP_AND_SUPPORT_HUD) as HelpAndSupportHUDController;
 102
 0103    public MinimapHUDController minimapHUD => GetHUDElement(HUDElementID.MINIMAP) as MinimapHUDController;
 104
 105    public VoiceChatWindowController voiceChatHud =>
 2106        GetHUDElement(HUDElementID.USERS_AROUND_LIST_HUD) as VoiceChatWindowController;
 107
 108    public QuestsPanelHUDController questsPanelHUD =>
 1109        GetHUDElement(HUDElementID.QUESTS_PANEL) as QuestsPanelHUDController;
 110
 111    public QuestsTrackerHUDController questsTrackerHUD =>
 1112        GetHUDElement(HUDElementID.QUESTS_TRACKER) as QuestsTrackerHUDController;
 113
 1114    public SignupHUDController signupHUD => GetHUDElement(HUDElementID.SIGNUP) as SignupHUDController;
 1115    public LoadingHUDController loadingController => GetHUDElement(HUDElementID.LOADING) as LoadingHUDController;
 116
 663117    public Dictionary<HUDElementID, IHUD> hudElements { get; private set; } = new Dictionary<HUDElementID, IHUD>();
 118
 1119    private UserProfile ownUserProfile => UserProfile.GetOwnUserProfile();
 1120    private BaseDictionary<string, WearableItem> wearableCatalog => CatalogController.wearableCatalog;
 121
 122    private void ShowSettings()
 123    {
 0124        settingsPanelHud?.SetVisibility(true);
 0125    }
 126
 127    private void ShowControls()
 128    {
 0129        controlsHud?.SetVisibility(true);
 0130    }
 131
 132    private void ToggleUIVisibility_OnTriggered(DCLAction_Trigger action)
 133    {
 0134        bool anyInputFieldIsSelected = EventSystem.current != null &&
 135                                       EventSystem.current.currentSelectedGameObject != null &&
 136                                       EventSystem.current.currentSelectedGameObject
 137                                           .GetComponent<TMPro.TMP_InputField>() != null;
 138
 0139        if (anyInputFieldIsSelected ||
 140            DataStore.i.exploreV2.isOpen.Get() ||
 141            CommonScriptableObjects.tutorialActive)
 0142            return;
 143
 0144        CommonScriptableObjects.allUIHidden.Set(!CommonScriptableObjects.allUIHidden.Get());
 0145    }
 146
 147    private void AllUIHiddenOnOnChange(bool current, bool previous)
 148    {
 3149        if (current)
 150        {
 2151            NotificationsController.i?.ShowNotification(hiddenUINotification);
 2152        }
 153        else
 154        {
 1155            NotificationsController.i?.DismissAllNotifications(hiddenUINotification.groupID);
 156        }
 1157    }
 158
 159    public void ConfigureHUDElement(HUDElementID hudElementId, HUDConfiguration configuration,
 160        string extraPayload = null)
 161    {
 162        //TODO(Brian): For now, the factory code is using this switch approach.
 163        //             In order to avoid the factory upkeep we can transform the IHUD elements
 164        //             To ScriptableObjects. In this scenario, we can make each element handle its own
 165        //             specific initialization details.
 166        //
 167        //             This will allow us to unify the serialized factory objects design,
 168        //             like we already do with ECS components.
 169
 170        switch (hudElementId)
 171        {
 172            case HUDElementID.NONE:
 173                break;
 174            case HUDElementID.MINIMAP:
 1175                if (minimapHud == null)
 176                {
 1177                    CreateHudElement(configuration, hudElementId);
 178
 1179                    if (minimapHud != null)
 180                    {
 1181                        minimapHud.Initialize();
 182                    }
 183                }
 184
 1185                break;
 186            case HUDElementID.PROFILE_HUD:
 1187                CreateHudElement(configuration, hudElementId);
 1188                break;
 189            case HUDElementID.NOTIFICATION:
 1190                CreateHudElement(configuration, hudElementId);
 1191                NotificationsController.i?.Initialize(notificationHud);
 1192                break;
 193            case HUDElementID.AVATAR_EDITOR:
 1194                CreateHudElement(configuration, hudElementId);
 1195                avatarEditorHud?.Initialize(ownUserProfile, wearableCatalog);
 1196                break;
 197            case HUDElementID.SETTINGS_PANEL:
 1198                CreateHudElement(configuration, hudElementId);
 1199                if (settingsPanelHud != null)
 1200                    settingsPanelHud.Initialize();
 1201                break;
 202            case HUDElementID.PLAYER_INFO_CARD:
 1203                CreateHudElement(configuration, hudElementId);
 1204                break;
 205            case HUDElementID.AIRDROPPING:
 1206                CreateHudElement(configuration, hudElementId);
 1207                break;
 208            case HUDElementID.TERMS_OF_SERVICE:
 1209                CreateHudElement(configuration, hudElementId);
 1210                break;
 211            case HUDElementID.WORLD_CHAT_WINDOW:
 1212                if (worldChatWindowHud == null)
 213                {
 1214                    CreateHudElement(configuration, hudElementId);
 215
 1216                    if (worldChatWindowHud != null)
 217                    {
 1218                        worldChatWindowHud.Initialize(WorldChatWindowComponentView.Create());
 1219                        worldChatWindowHud.SetVisibility(false);
 1220                        worldChatWindowHud.OnOpenPrivateChat -= OpenPrivateChatWindow;
 1221                        worldChatWindowHud.OnOpenPrivateChat += OpenPrivateChatWindow;
 1222                        worldChatWindowHud.OnOpenPublicChannel -= OpenPublicChannelWindow;
 1223                        worldChatWindowHud.OnOpenPublicChannel += OpenPublicChannelWindow;
 224
 1225                        taskbarHud?.AddWorldChatWindow(worldChatWindowHud);
 226                    }
 0227                }
 228                else
 0229                    UpdateHudElement(configuration, hudElementId);
 230
 1231                if (PublicChatChannelHud == null)
 232                {
 1233                    CreateHudElement(configuration, HUDElementID.PUBLIC_CHAT_CHANNEL);
 1234                    PublicChatChannelHud.Initialize();
 1235                    PublicChatChannelHud.OnBack -= HandlePublicChatChannelBacked;
 1236                    PublicChatChannelHud.OnBack += HandlePublicChatChannelBacked;
 1237                    PublicChatChannelHud.OnClosed -= HandlePublicChatChannelClosed;
 1238                    PublicChatChannelHud.OnClosed += HandlePublicChatChannelClosed;
 1239                    taskbarHud?.AddPublicChatChannel(PublicChatChannelHud);
 240                    // TODO: this call should be removed when chat notifications are implemented
 1241                    taskbarHud?.OpenPublicChatChannel("general", false);
 1242                    PublicChatChannelHud.ActivatePreviewModeInstantly();
 1243                }
 244                else
 0245                    UpdateHudElement(configuration, HUDElementID.PUBLIC_CHAT_CHANNEL);
 246
 1247                if (PrivateChatWindow == null)
 248                {
 1249                    CreateHudElement(configuration, HUDElementID.PRIVATE_CHAT_WINDOW);
 250
 1251                    if (PrivateChatWindow != null)
 252                    {
 1253                        PrivateChatWindow.Initialize();
 1254                        PrivateChatWindow.SetVisibility(false);
 1255                        PrivateChatWindow.OnPressBack -= PrivateChatWindowHud_OnPressBack;
 1256                        PrivateChatWindow.OnPressBack += PrivateChatWindowHud_OnPressBack;
 257
 1258                        taskbarHud?.AddPrivateChatWindow(PrivateChatWindow);
 259                    }
 0260                }
 261                else
 0262                    UpdateHudElement(configuration, HUDElementID.PRIVATE_CHAT_WINDOW);
 263
 0264                break;
 265            case HUDElementID.FRIENDS:
 1266                if (friendsHud == null)
 267                {
 1268                    CreateHudElement(configuration, hudElementId);
 269
 1270                    if (friendsHud != null)
 271                    {
 1272                        friendsHud.Initialize();
 1273                        friendsHud.OnPressWhisper -= OpenPrivateChatWindow;
 1274                        friendsHud.OnPressWhisper += OpenPrivateChatWindow;
 275
 1276                        taskbarHud?.AddFriendsWindow(friendsHud);
 277                    }
 1278                }
 279                else
 280                {
 0281                    UpdateHudElement(configuration, hudElementId);
 282
 0283                    if (!configuration.active)
 0284                        taskbarHud?.DisableFriendsWindow();
 285                }
 286
 0287                break;
 288            case HUDElementID.TASKBAR:
 1289                if (taskbarHud == null)
 290                {
 1291                    CreateHudElement(configuration, hudElementId);
 292
 1293                    if (taskbarHud != null)
 294                    {
 1295                        taskbarHud.Initialize(SceneReferences.i.mouseCatcher);
 1296                        taskbarHud.OnAnyTaskbarButtonClicked -= TaskbarHud_onAnyTaskbarButtonClicked;
 1297                        taskbarHud.OnAnyTaskbarButtonClicked += TaskbarHud_onAnyTaskbarButtonClicked;
 298
 1299                        OnTaskbarCreation?.Invoke();
 300                    }
 0301                }
 302                else
 303                {
 0304                    UpdateHudElement(configuration, hudElementId);
 305                }
 306
 0307                break;
 308            case HUDElementID.OPEN_EXTERNAL_URL_PROMPT:
 1309                CreateHudElement(configuration, hudElementId);
 1310                break;
 311            case HUDElementID.NFT_INFO_DIALOG:
 1312                CreateHudElement(configuration, hudElementId);
 1313                break;
 314            case HUDElementID.TELEPORT_DIALOG:
 1315                CreateHudElement(configuration, hudElementId);
 1316                break;
 317            case HUDElementID.CONTROLS_HUD:
 1318                CreateHudElement(configuration, hudElementId);
 1319                break;
 320            case HUDElementID.HELP_AND_SUPPORT_HUD:
 1321                CreateHudElement(configuration, hudElementId);
 1322                settingsPanelHud?.AddHelpAndSupportWindow(helpAndSupportHud);
 1323                break;
 324            case HUDElementID.USERS_AROUND_LIST_HUD:
 1325                CreateHudElement(configuration, hudElementId);
 1326                if (voiceChatHud != null)
 1327                    taskbarHud?.AddVoiceChatWindow(voiceChatHud);
 328
 1329                break;
 330            case HUDElementID.GRAPHIC_CARD_WARNING:
 1331                CreateHudElement(configuration, hudElementId);
 1332                break;
 333            case HUDElementID.QUESTS_PANEL:
 1334                CreateHudElement(configuration, hudElementId);
 1335                if (configuration.active)
 1336                    questsPanelHUD.Initialize(QuestsController.i);
 1337                break;
 338            case HUDElementID.QUESTS_TRACKER:
 1339                CreateHudElement(configuration, hudElementId);
 1340                if (configuration.active)
 1341                    questsTrackerHUD.Initialize(QuestsController.i);
 1342                break;
 343            case HUDElementID.SIGNUP:
 1344                CreateHudElement(configuration, hudElementId);
 1345                if (configuration.active)
 346                {
 347                    //Same race condition risks as with the ProfileHUD
 348                    //TODO Refactor the way AvatarEditor sets its visibility to match our data driven pattern
 349                    //Then this reference can be removed so we just work with a BaseVariable<bool>.
 350                    //This refactor applies to the ProfileHUD and the way kernel asks the HUDController during signup
 1351                    signupHUD.Initialize(avatarEditorHud);
 352                }
 353
 1354                break;
 355            case HUDElementID.LOADING:
 1356                if (loadingHud == null)
 357                {
 1358                    CreateHudElement(configuration, hudElementId);
 1359                    if (loadingHud != null && configuration.active)
 1360                        loadingController.Initialize();
 361                }
 362
 363                break;
 364            case HUDElementID.AVATAR_NAMES:
 365                // TODO Remove the HUDElementId once kernel stops sending the Configure HUD message
 366                break;
 367        }
 368
 33369        var hudElement = GetHUDElement(hudElementId);
 370
 33371        if (hudElement != null)
 24372            hudElement.SetVisibility(configuration.active && configuration.visible);
 33373    }
 374
 375    private void HandlePublicChatChannelBacked()
 376    {
 0377        PublicChatChannelHud.SetVisibility(false);
 0378        taskbarHud?.GoBackFromChat();
 0379    }
 380
 381    private void OpenPublicChannelWindow(string channelId)
 382    {
 0383        taskbarHud?.OpenPublicChatChannel(channelId, true);
 0384    }
 385
 386    private void OpenPrivateChatWindow(string targetUserId)
 387    {
 0388        taskbarHud?.OpenPrivateChat(targetUserId);
 0389    }
 390
 391    private void PrivateChatWindowHud_OnPressBack()
 392    {
 0393        PrivateChatWindow?.SetVisibility(false);
 0394        taskbarHud?.GoBackFromChat();
 0395    }
 396
 397    private void TaskbarHud_onAnyTaskbarButtonClicked()
 398    {
 0399        playerInfoCardHud?.CloseCard();
 0400    }
 401
 402    public void CreateHudElement(HUDConfiguration config, HUDElementID id)
 403    {
 24404        bool controllerCreated = hudElements.ContainsKey(id);
 405
 24406        if (config.active && !controllerCreated)
 407        {
 24408            hudElements.Add(id, hudFactory.CreateHUD(id));
 409
 24410            if (VERBOSE)
 0411                Debug.Log($"Adding {id} .. type {hudElements[id].GetType().Name}");
 412        }
 24413    }
 414
 415    public void UpdateHudElement(HUDConfiguration config, HUDElementID id)
 416    {
 0417        if (!hudElements.ContainsKey(id))
 0418            return;
 419
 0420        if (VERBOSE)
 0421            Debug.Log(
 422                $"Updating {id}, type {hudElements[id].GetType().Name}, active: {config.active} visible: {config.visible
 423
 0424        hudElements[id].SetVisibility(config.visible);
 0425    }
 426
 427    public void Cleanup()
 428    {
 664429        toggleUIVisibilityTrigger.OnTriggered -= ToggleUIVisibility_OnTriggered;
 664430        CommonScriptableObjects.allUIHidden.OnChange -= AllUIHiddenOnOnChange;
 431
 664432        if (worldChatWindowHud != null)
 433        {
 1434            worldChatWindowHud.OnOpenPrivateChat -= OpenPrivateChatWindow;
 1435            worldChatWindowHud.OnOpenPublicChannel -= OpenPublicChannelWindow;
 436        }
 437
 664438        if (PrivateChatWindow != null)
 1439            PrivateChatWindow.OnPressBack -= PrivateChatWindowHud_OnPressBack;
 440
 664441        if (PublicChatChannelHud != null)
 442        {
 1443            PublicChatChannelHud.OnClosed -= HandlePublicChatChannelClosed;
 1444            PublicChatChannelHud.OnBack -= HandlePublicChatChannelBacked;
 445        }
 446
 447
 664448        if (friendsHud != null)
 1449            friendsHud.OnPressWhisper -= OpenPrivateChatWindow;
 450
 664451        if (taskbarHud != null)
 1452            taskbarHud.OnAnyTaskbarButtonClicked -= TaskbarHud_onAnyTaskbarButtonClicked;
 453
 664454        UserContextMenu.OnOpenPrivateChatRequest -= OpenPrivateChatWindow;
 455
 1376456        foreach (var kvp in hudElements)
 457        {
 24458            kvp.Value?.Dispose();
 459        }
 460
 664461        hudElements.Clear();
 664462    }
 463
 464    private void HandlePublicChatChannelClosed()
 465    {
 0466        PublicChatChannelHud.SetVisibility(false);
 0467    }
 468
 469    public IHUD GetHUDElement(HUDElementID id)
 470    {
 3507471        if (!hudElements.ContainsKey(id))
 3402472            return null;
 473
 105474        return hudElements[id];
 475    }
 476
 477    public static bool IsHUDElementDeprecated(HUDElementID element)
 478    {
 33479        Type enumType = typeof(HUDElementID);
 33480        var enumName = enumType.GetEnumName(element);
 33481        var fieldInfo = enumType.GetField(enumName);
 33482        return Attribute.IsDefined(fieldInfo, typeof(ObsoleteAttribute));
 483    }
 484
 485#if UNITY_EDITOR
 486    [ContextMenu("Trigger fake PlayerInfoCard")]
 487    public void TriggerFakePlayerInfoCard()
 488    {
 0489        var newModel = ownUserProfile.CloneModel();
 0490        newModel.name = "FakePassport";
 0491        newModel.description = "Fake Description for Testing";
 0492        newModel.userId = "test-id";
 493
 0494        UserProfileController.i.AddUserProfileToCatalog(newModel);
 0495        UserProfileController.GetProfileByUserId(newModel.userId).SetInventory(new[]
 496        {
 497            "dcl://halloween_2019/machete_headband_top_head",
 498            "dcl://halloween_2019/bee_suit_upper_body",
 499            "dcl://halloween_2019/bride_of_frankie_upper_body",
 500            "dcl://halloween_2019/creepy_nurse_upper_body",
 501        });
 0502        Resources.Load<StringVariable>("CurrentPlayerInfoCardId").Set(newModel.userId);
 0503    }
 504#endif
 505    public void Dispose()
 506    {
 663507        Cleanup();
 663508    }
 509}