< Summary

Class:HUDController
Assembly:HUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/HUDController.cs
Covered lines:156
Uncovered lines:44
Coverable lines:200
Total lines:442
Line coverage:78% (156 of 200)
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%1321100%
AllUIHiddenOnOnChange(...)0%440100%
ConfigureHUDElement(...)0%47.0644088.35%
OpenPrivateChatWindow(...)0%6200%
View_OnDeactivatePreview()0%6200%
PrivateChatWindowHud_OnPressBack()0%6200%
TaskbarHud_onAnyTaskbarButtonClicked()0%6200%
CreateHudElement(...)0%4.074083.33%
UpdateHudElement(...)0%12300%
Cleanup()0%770100%
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 System;
 2using DCL;
 3using DCL.HelpAndSupportHUD;
 4using DCL.Huds.QuestsPanel;
 5using DCL.Huds.QuestsTracker;
 6using DCL.QuestsController;
 7using DCL.SettingsPanelHUD;
 8using System.Collections.Generic;
 9using LoadingHUD;
 10using SignupHUD;
 11using UnityEngine;
 12using UnityEngine.EventSystems;
 13using Environment = System.Environment;
 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
 63026    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
 63033    public HUDController (IHUDFactory hudFactory = null)
 34    {
 63035        this.hudFactory = hudFactory;
 63036    }
 37
 38    public void Initialize()
 39    {
 63040        i = this;
 41
 63042        if ( this.hudFactory == null )
 63043            this.hudFactory = DCL.Environment.i.hud.factory;
 44
 63045        toggleUIVisibilityTrigger = Resources.Load<InputAction_Trigger>(TOGGLE_UI_VISIBILITY_ASSET_NAME);
 63046        toggleUIVisibilityTrigger.OnTriggered += ToggleUIVisibility_OnTriggered;
 47
 63048        CommonScriptableObjects.allUIHidden.OnChange += AllUIHiddenOnOnChange;
 63049        UserContextMenu.OnOpenPrivateChatRequest += OpenPrivateChatWindow;
 63050    }
 51
 52
 53    public event Action OnTaskbarCreation;
 54
 1055    public ProfileHUDController profileHud => GetHUDElement(HUDElementID.PROFILE_HUD) as ProfileHUDController;
 56
 57    public NotificationHUDController notificationHud =>
 158        GetHUDElement(HUDElementID.NOTIFICATION) as NotificationHUDController;
 59
 760    public MinimapHUDController minimapHud => GetHUDElement(HUDElementID.MINIMAP) as MinimapHUDController;
 61
 62    public AvatarEditorHUDController avatarEditorHud =>
 263        GetHUDElement(HUDElementID.AVATAR_EDITOR) as AvatarEditorHUDController;
 64
 10865    public SettingsPanelHUDController settingsPanelHud => GetHUDElement(HUDElementID.SETTINGS_PANEL) as SettingsPanelHUD
 66
 67    public EmotesHUDController emotesHUD =>
 068        GetHUDElement(HUDElementID.EMOTES) as EmotesHUDController;
 69
 70    public PlayerInfoCardHUDController playerInfoCardHud =>
 071        GetHUDElement(HUDElementID.PLAYER_INFO_CARD) as PlayerInfoCardHUDController;
 72
 73    public AirdroppingHUDController airdroppingHud =>
 074        GetHUDElement(HUDElementID.AIRDROPPING) as AirdroppingHUDController;
 75
 76    public TermsOfServiceHUDController termsOfServiceHud =>
 077        GetHUDElement(HUDElementID.TERMS_OF_SERVICE) as TermsOfServiceHUDController;
 78
 65979    public TaskbarHUDController taskbarHud => GetHUDElement(HUDElementID.TASKBAR) as TaskbarHUDController;
 80
 81    public WorldChatWindowHUDController worldChatWindowHud =>
 65082        GetHUDElement(HUDElementID.WORLD_CHAT_WINDOW) as WorldChatWindowHUDController;
 83
 84    public PrivateChatWindowHUDController privateChatWindowHud =>
 64285        GetHUDElement(HUDElementID.PRIVATE_CHAT_WINDOW) as PrivateChatWindowHUDController;
 86
 64887    public FriendsHUDController friendsHud => GetHUDElement(HUDElementID.FRIENDS) as FriendsHUDController;
 88
 089    public TeleportPromptHUDController teleportHud => GetHUDElement(HUDElementID.TELEPORT_DIALOG) as TeleportPromptHUDCo
 90
 291    public ControlsHUDController controlsHud => GetHUDElement(HUDElementID.CONTROLS_HUD) as ControlsHUDController;
 92
 193    public HelpAndSupportHUDController helpAndSupportHud => GetHUDElement(HUDElementID.HELP_AND_SUPPORT_HUD) as HelpAndS
 94
 295    public UsersAroundListHUDController usersAroundListHud => GetHUDElement(HUDElementID.USERS_AROUND_LIST_HUD) as Users
 196    public QuestsPanelHUDController questsPanelHUD => GetHUDElement(HUDElementID.QUESTS_PANEL) as QuestsPanelHUDControll
 197    public QuestsTrackerHUDController questsTrackerHUD => GetHUDElement(HUDElementID.QUESTS_TRACKER) as QuestsTrackerHUD
 198    public SignupHUDController signupHUD => GetHUDElement(HUDElementID.SIGNUP) as SignupHUDController;
 199    public LoadingHUDController loadingController => GetHUDElement(HUDElementID.LOADING) as LoadingHUDController;
 100
 630101    public Dictionary<HUDElementID, IHUD> hudElements { get; private set; } = new Dictionary<HUDElementID, IHUD>();
 102
 1103    private UserProfile ownUserProfile => UserProfile.GetOwnUserProfile();
 1104    private BaseDictionary<string, WearableItem> wearableCatalog => CatalogController.wearableCatalog;
 105
 0106    private void ShowSettings() { settingsPanelHud?.SetVisibility(true); }
 107
 0108    private void ShowControls() { controlsHud?.SetVisibility(true); }
 109
 110    private void ToggleUIVisibility_OnTriggered(DCLAction_Trigger action)
 111    {
 0112        bool anyInputFieldIsSelected = EventSystem.current != null &&
 113                                       EventSystem.current.currentSelectedGameObject != null &&
 114                                       EventSystem.current.currentSelectedGameObject.GetComponent<TMPro.TMP_InputField>(
 115                                       (!worldChatWindowHud.view.chatHudView.inputField.isFocused || !worldChatWindowHud
 116
 0117        if (anyInputFieldIsSelected ||
 118            settingsPanelHud.view.isOpen ||
 119            avatarEditorHud.view.isOpen ||
 120            DataStore.i.HUDs.navmapVisible.Get() ||
 121            CommonScriptableObjects.tutorialActive)
 0122            return;
 123
 0124        CommonScriptableObjects.allUIHidden.Set(!CommonScriptableObjects.allUIHidden.Get());
 0125    }
 126
 127    private void AllUIHiddenOnOnChange(bool current, bool previous)
 128    {
 4129        if (current)
 130        {
 2131            NotificationsController.i?.ShowNotification(hiddenUINotification);
 2132        }
 133        else
 134        {
 2135            NotificationsController.i?.DismissAllNotifications(hiddenUINotification.groupID);
 136        }
 2137    }
 138
 139    public void ConfigureHUDElement(HUDElementID hudElementId, HUDConfiguration configuration, string extraPayload = nul
 140    {
 141        //TODO(Brian): For now, the factory code is using this switch approach.
 142        //             In order to avoid the factory upkeep we can transform the IHUD elements
 143        //             To ScriptableObjects. In this scenario, we can make each element handle its own
 144        //             specific initialization details.
 145        //
 146        //             This will allow us to unify the serialized factory objects design,
 147        //             like we already do with ECS components.
 148
 149        switch (hudElementId)
 150        {
 151            case HUDElementID.NONE:
 152                break;
 153            case HUDElementID.MINIMAP:
 1154                CreateHudElement(configuration, hudElementId);
 1155                break;
 156            case HUDElementID.PROFILE_HUD:
 1157                CreateHudElement(configuration, hudElementId);
 1158                break;
 159            case HUDElementID.NOTIFICATION:
 1160                CreateHudElement(configuration, hudElementId);
 1161                NotificationsController.i?.Initialize(notificationHud);
 1162                break;
 163            case HUDElementID.AVATAR_EDITOR:
 1164                CreateHudElement(configuration, hudElementId);
 1165                avatarEditorHud?.Initialize(ownUserProfile, wearableCatalog);
 1166                break;
 167            case HUDElementID.SETTINGS_PANEL:
 1168                CreateHudElement(configuration, hudElementId);
 1169                if (settingsPanelHud != null)
 1170                    settingsPanelHud.Initialize();
 1171                break;
 172            case HUDElementID.EXPRESSIONS:
 173            case HUDElementID.EMOTES:
 2174                CreateHudElement(configuration, hudElementId);
 2175                break;
 176            case HUDElementID.PLAYER_INFO_CARD:
 1177                CreateHudElement(configuration, hudElementId);
 1178                break;
 179            case HUDElementID.AIRDROPPING:
 1180                CreateHudElement(configuration, hudElementId);
 1181                break;
 182            case HUDElementID.TERMS_OF_SERVICE:
 1183                CreateHudElement(configuration, hudElementId);
 1184                break;
 185            case HUDElementID.WORLD_CHAT_WINDOW:
 1186                if (worldChatWindowHud == null)
 187                {
 1188                    CreateHudElement(configuration, hudElementId);
 189
 1190                    if (worldChatWindowHud != null)
 191                    {
 1192                        worldChatWindowHud.Initialize(ChatController.i, SceneReferences.i.mouseCatcher);
 1193                        worldChatWindowHud.OnPressPrivateMessage -= OpenPrivateChatWindow;
 1194                        worldChatWindowHud.OnPressPrivateMessage += OpenPrivateChatWindow;
 1195                        worldChatWindowHud.view.OnDeactivatePreview -= View_OnDeactivatePreview;
 1196                        worldChatWindowHud.view.OnDeactivatePreview += View_OnDeactivatePreview;
 197
 1198                        taskbarHud?.AddWorldChatWindow(worldChatWindowHud);
 199                    }
 0200                }
 201                else
 202                {
 0203                    UpdateHudElement(configuration, hudElementId);
 204                }
 205
 0206                break;
 207            case HUDElementID.FRIENDS:
 1208                if (friendsHud == null)
 209                {
 1210                    CreateHudElement(configuration, hudElementId);
 211
 1212                    if (friendsHud != null)
 213                    {
 1214                        friendsHud.Initialize(FriendsController.i, UserProfile.GetOwnUserProfile());
 1215                        friendsHud.OnPressWhisper -= OpenPrivateChatWindow;
 1216                        friendsHud.OnPressWhisper += OpenPrivateChatWindow;
 217
 1218                        taskbarHud?.AddFriendsWindow(friendsHud);
 219                    }
 1220                }
 221                else
 222                {
 0223                    UpdateHudElement(configuration, hudElementId);
 224
 0225                    if (!configuration.active)
 0226                        taskbarHud?.DisableFriendsWindow();
 227                }
 228
 1229                if (privateChatWindowHud == null)
 230                {
 1231                    CreateHudElement(configuration, HUDElementID.PRIVATE_CHAT_WINDOW);
 232
 1233                    if (privateChatWindowHud != null)
 234                    {
 1235                        privateChatWindowHud.Initialize(ChatController.i);
 1236                        privateChatWindowHud.OnPressBack -= PrivateChatWindowHud_OnPressBack;
 1237                        privateChatWindowHud.OnPressBack += PrivateChatWindowHud_OnPressBack;
 238
 1239                        taskbarHud?.AddPrivateChatWindow(privateChatWindowHud);
 240                    }
 241                }
 242
 1243                break;
 244            case HUDElementID.TASKBAR:
 1245                if (taskbarHud == null)
 246                {
 1247                    CreateHudElement(configuration, hudElementId);
 248
 1249                    if (taskbarHud != null)
 250                    {
 1251                        taskbarHud.Initialize(
 252                            SceneReferences.i.mouseCatcher,
 253                            ChatController.i,
 254                            FriendsController.i,
 255                            DCL.Environment.i.world.sceneController,
 256                            DCL.Environment.i.world.state);
 1257                        taskbarHud.OnAnyTaskbarButtonClicked -= TaskbarHud_onAnyTaskbarButtonClicked;
 1258                        taskbarHud.OnAnyTaskbarButtonClicked += TaskbarHud_onAnyTaskbarButtonClicked;
 259
 1260                        if (!string.IsNullOrEmpty(extraPayload))
 261                        {
 0262                            var config = JsonUtility.FromJson<TaskbarHUDController.Configuration>(extraPayload);
 0263                            if (config.enableVoiceChat)
 264                            {
 0265                                taskbarHud.OnAddVoiceChat();
 266                            }
 267                        }
 268
 1269                        OnTaskbarCreation?.Invoke();
 270                    }
 0271                }
 272                else
 273                {
 0274                    UpdateHudElement(configuration, hudElementId);
 275                }
 276
 0277                break;
 278            case HUDElementID.OPEN_EXTERNAL_URL_PROMPT:
 1279                CreateHudElement(configuration, hudElementId);
 1280                break;
 281            case HUDElementID.NFT_INFO_DIALOG:
 1282                CreateHudElement(configuration, hudElementId);
 1283                break;
 284            case HUDElementID.TELEPORT_DIALOG:
 1285                CreateHudElement(configuration, hudElementId);
 1286                break;
 287            case HUDElementID.CONTROLS_HUD:
 1288                CreateHudElement(configuration, hudElementId);
 1289                break;
 290            case HUDElementID.HELP_AND_SUPPORT_HUD:
 1291                CreateHudElement(configuration, hudElementId);
 1292                settingsPanelHud?.AddHelpAndSupportWindow(helpAndSupportHud);
 1293                break;
 294            case HUDElementID.USERS_AROUND_LIST_HUD:
 1295                CreateHudElement(configuration, hudElementId);
 1296                if (usersAroundListHud != null)
 297                {
 1298                    minimapHud?.AddUsersAroundIndicator(usersAroundListHud);
 299                }
 300
 1301                break;
 302            case HUDElementID.GRAPHIC_CARD_WARNING:
 1303                CreateHudElement(configuration, hudElementId);
 1304                break;
 305            case HUDElementID.QUESTS_PANEL:
 1306                CreateHudElement(configuration, hudElementId);
 1307                if (configuration.active)
 1308                    questsPanelHUD.Initialize(QuestsController.i);
 1309                break;
 310            case HUDElementID.QUESTS_TRACKER:
 1311                CreateHudElement(configuration, hudElementId);
 1312                if (configuration.active)
 1313                    questsTrackerHUD.Initialize(QuestsController.i);
 1314                break;
 315            case HUDElementID.SIGNUP:
 1316                CreateHudElement(configuration, hudElementId);
 1317                if (configuration.active)
 318                {
 319                    //Same race condition risks as with the ProfileHUD
 320                    //TODO Refactor the way AvatarEditor sets its visibility to match our data driven pattern
 321                    //Then this reference can be removed so we just work with a BaseVariable<bool>.
 322                    //This refactor applies to the ProfileHUD and the way kernel asks the HUDController during signup
 1323                    signupHUD.Initialize(avatarEditorHud);
 324                }
 325
 1326                break;
 327            case HUDElementID.LOADING:
 1328                CreateHudElement(configuration, hudElementId);
 1329                if (configuration.active)
 1330                    loadingController.Initialize();
 331                break;
 332            case HUDElementID.AVATAR_NAMES:
 333                // TODO Remove the HUDElementId once kernel stops sending the Configure HUD message
 334                break;
 335        }
 336
 32337        var hudElement = GetHUDElement(hudElementId);
 338
 32339        if (hudElement != null)
 25340            hudElement.SetVisibility(configuration.active && configuration.visible);
 32341    }
 342
 0343    private void OpenPrivateChatWindow(string targetUserId) { taskbarHud?.OpenPrivateChatTo(targetUserId); }
 344
 0345    private void View_OnDeactivatePreview() { playerInfoCardHud?.CloseCard(); }
 346
 0347    private void PrivateChatWindowHud_OnPressBack() { taskbarHud?.OpenFriendsWindow(); }
 348
 0349    private void TaskbarHud_onAnyTaskbarButtonClicked() { playerInfoCardHud?.CloseCard(); }
 350
 351    public void CreateHudElement(HUDConfiguration config, HUDElementID id)
 352    {
 25353        bool controllerCreated = hudElements.ContainsKey(id);
 354
 25355        if (config.active && !controllerCreated)
 356        {
 25357            hudElements.Add(id, hudFactory.CreateHUD(id));
 358
 25359            if (VERBOSE)
 0360                Debug.Log($"Adding {id} .. type {hudElements[id].GetType().Name}");
 361        }
 25362    }
 363
 364    public void UpdateHudElement(HUDConfiguration config, HUDElementID id)
 365    {
 0366        if (!hudElements.ContainsKey(id))
 0367            return;
 368
 0369        if (VERBOSE)
 0370            Debug.Log($"Updating {id}, type {hudElements[id].GetType().Name}, active: {config.active} visible: {config.v
 371
 0372        hudElements[id].SetVisibility(config.visible);
 0373    }
 374
 375    public void Cleanup()
 376    {
 635377        toggleUIVisibilityTrigger.OnTriggered -= ToggleUIVisibility_OnTriggered;
 635378        CommonScriptableObjects.allUIHidden.OnChange -= AllUIHiddenOnOnChange;
 379
 635380        if (worldChatWindowHud != null)
 381        {
 1382            worldChatWindowHud.OnPressPrivateMessage -= OpenPrivateChatWindow;
 1383            worldChatWindowHud.view.OnDeactivatePreview -= View_OnDeactivatePreview;
 384        }
 385
 635386        if (privateChatWindowHud != null)
 1387            privateChatWindowHud.OnPressBack -= PrivateChatWindowHud_OnPressBack;
 388
 635389        if (friendsHud != null)
 1390            friendsHud.OnPressWhisper -= OpenPrivateChatWindow;
 391
 635392        if (taskbarHud != null)
 1393            taskbarHud.OnAnyTaskbarButtonClicked -= TaskbarHud_onAnyTaskbarButtonClicked;
 394
 635395        UserContextMenu.OnOpenPrivateChatRequest -= OpenPrivateChatWindow;
 396
 1320397        foreach (var kvp in hudElements)
 398        {
 25399            kvp.Value?.Dispose();
 400        }
 401
 635402        hudElements.Clear();
 635403    }
 404
 405    public IHUD GetHUDElement(HUDElementID id)
 406    {
 2857407        if (!hudElements.ContainsKey(id))
 2763408            return null;
 409
 94410        return hudElements[id];
 411    }
 412
 413    public static bool IsHUDElementDeprecated(HUDElementID element)
 414    {
 32415        Type enumType = typeof(HUDElementID);
 32416        var enumName = enumType.GetEnumName(element);
 32417        var fieldInfo = enumType.GetField(enumName);
 32418        return Attribute.IsDefined(fieldInfo, typeof(ObsoleteAttribute));
 419    }
 420
 421#if UNITY_EDITOR
 422    [ContextMenu("Trigger fake PlayerInfoCard")]
 423    public void TriggerFakePlayerInfoCard()
 424    {
 0425        var newModel = ownUserProfile.CloneModel();
 0426        newModel.name = "FakePassport";
 0427        newModel.description = "Fake Description for Testing";
 0428        newModel.userId = "test-id";
 0429        newModel.inventory = new[]
 430        {
 431            "dcl://halloween_2019/machete_headband_top_head",
 432            "dcl://halloween_2019/bee_suit_upper_body",
 433            "dcl://halloween_2019/bride_of_frankie_upper_body",
 434            "dcl://halloween_2019/creepy_nurse_upper_body",
 435        };
 436
 0437        UserProfileController.i.AddUserProfileToCatalog(newModel);
 0438        Resources.Load<StringVariable>("CurrentPlayerInfoCardId").Set(newModel.userId);
 0439    }
 440#endif
 1260441    public void Dispose() { Cleanup(); }
 442}