< 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:438
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%110100%
ShowSettings()0%6200%
ShowControls()0%6200%
ToggleUIVisibility_OnTriggered(...)0%1321100%
AllUIHiddenOnOnChange(...)0%440100%
ConfigureHUDElement(...)0%49.0746088.68%
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;
 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
 79825    private readonly DCL.NotificationModel.Model hiddenUINotification = new DCL.NotificationModel.Model()
 26    {
 27        timer = 3,
 28        type = DCL.NotificationModel.Type.UI_HIDDEN,
 29        groupID = "UIHiddenNotification"
 30    };
 31
 32    public void Initialize(IHUDFactory hudFactory)
 33    {
 79834        i = this;
 79835        this.hudFactory = hudFactory;
 36
 79837        toggleUIVisibilityTrigger = Resources.Load<InputAction_Trigger>(TOGGLE_UI_VISIBILITY_ASSET_NAME);
 79838        toggleUIVisibilityTrigger.OnTriggered += ToggleUIVisibility_OnTriggered;
 39
 79840        CommonScriptableObjects.allUIHidden.OnChange += AllUIHiddenOnOnChange;
 79841        UserContextMenu.OnOpenPrivateChatRequest += OpenPrivateChatWindow;
 79842    }
 43
 44    public event Action OnTaskbarCreation;
 45
 1046    public ProfileHUDController profileHud => GetHUDElement(HUDElementID.PROFILE_HUD) as ProfileHUDController;
 47
 48    public NotificationHUDController notificationHud =>
 149        GetHUDElement(HUDElementID.NOTIFICATION) as NotificationHUDController;
 50
 751    public MinimapHUDController minimapHud => GetHUDElement(HUDElementID.MINIMAP) as MinimapHUDController;
 52
 53    public AvatarEditorHUDController avatarEditorHud =>
 254        GetHUDElement(HUDElementID.AVATAR_EDITOR) as AvatarEditorHUDController;
 55
 10856    public SettingsPanelHUDController settingsPanelHud => GetHUDElement(HUDElementID.SETTINGS_PANEL) as SettingsPanelHUD
 57
 58    public EmotesHUDController emotesHUD =>
 059        GetHUDElement(HUDElementID.EMOTES) as EmotesHUDController;
 60
 61    public PlayerInfoCardHUDController playerInfoCardHud =>
 062        GetHUDElement(HUDElementID.PLAYER_INFO_CARD) as PlayerInfoCardHUDController;
 63
 64    public WelcomeHUDController messageOfTheDayHud =>
 165        GetHUDElement(HUDElementID.MESSAGE_OF_THE_DAY) as WelcomeHUDController;
 66
 67    public AirdroppingHUDController airdroppingHud =>
 068        GetHUDElement(HUDElementID.AIRDROPPING) as AirdroppingHUDController;
 69
 70    public TermsOfServiceHUDController termsOfServiceHud =>
 071        GetHUDElement(HUDElementID.TERMS_OF_SERVICE) as TermsOfServiceHUDController;
 72
 82773    public TaskbarHUDController taskbarHud => GetHUDElement(HUDElementID.TASKBAR) as TaskbarHUDController;
 74
 75    public WorldChatWindowHUDController worldChatWindowHud =>
 81876        GetHUDElement(HUDElementID.WORLD_CHAT_WINDOW) as WorldChatWindowHUDController;
 77
 78    public PrivateChatWindowHUDController privateChatWindowHud =>
 81079        GetHUDElement(HUDElementID.PRIVATE_CHAT_WINDOW) as PrivateChatWindowHUDController;
 80
 81681    public FriendsHUDController friendsHud => GetHUDElement(HUDElementID.FRIENDS) as FriendsHUDController;
 82
 083    public TeleportPromptHUDController teleportHud => GetHUDElement(HUDElementID.TELEPORT_DIALOG) as TeleportPromptHUDCo
 84
 285    public ControlsHUDController controlsHud => GetHUDElement(HUDElementID.CONTROLS_HUD) as ControlsHUDController;
 86
 187    public HelpAndSupportHUDController helpAndSupportHud => GetHUDElement(HUDElementID.HELP_AND_SUPPORT_HUD) as HelpAndS
 88
 289    public UsersAroundListHUDController usersAroundListHud => GetHUDElement(HUDElementID.USERS_AROUND_LIST_HUD) as Users
 190    public QuestsPanelHUDController questsPanelHUD => GetHUDElement(HUDElementID.QUESTS_PANEL) as QuestsPanelHUDControll
 191    public QuestsTrackerHUDController questsTrackerHUD => GetHUDElement(HUDElementID.QUESTS_TRACKER) as QuestsTrackerHUD
 192    public SignupHUDController signupHUD => GetHUDElement(HUDElementID.SIGNUP) as SignupHUDController;
 193    public LoadingHUDController loadingController => GetHUDElement(HUDElementID.LOADING) as LoadingHUDController;
 94
 79895    public Dictionary<HUDElementID, IHUD> hudElements { get; private set; } = new Dictionary<HUDElementID, IHUD>();
 96
 197    private UserProfile ownUserProfile => UserProfile.GetOwnUserProfile();
 198    private BaseDictionary<string, WearableItem> wearableCatalog => CatalogController.wearableCatalog;
 99
 0100    private void ShowSettings() { settingsPanelHud?.SetVisibility(true); }
 101
 0102    private void ShowControls() { controlsHud?.SetVisibility(true); }
 103
 104    private void ToggleUIVisibility_OnTriggered(DCLAction_Trigger action)
 105    {
 0106        bool anyInputFieldIsSelected = EventSystem.current != null &&
 107                                       EventSystem.current.currentSelectedGameObject != null &&
 108                                       EventSystem.current.currentSelectedGameObject.GetComponent<TMPro.TMP_InputField>(
 109                                       (!worldChatWindowHud.view.chatHudView.inputField.isFocused || !worldChatWindowHud
 110
 0111        if (anyInputFieldIsSelected ||
 112            settingsPanelHud.view.isOpen ||
 113            avatarEditorHud.view.isOpen ||
 114            DataStore.i.HUDs.navmapVisible.Get() ||
 115            CommonScriptableObjects.tutorialActive)
 0116            return;
 117
 0118        CommonScriptableObjects.allUIHidden.Set(!CommonScriptableObjects.allUIHidden.Get());
 0119    }
 120
 121    private void AllUIHiddenOnOnChange(bool current, bool previous)
 122    {
 35123        if (current)
 124        {
 17125            NotificationsController.i?.ShowNotification(hiddenUINotification);
 17126        }
 127        else
 128        {
 18129            NotificationsController.i?.DismissAllNotifications(hiddenUINotification.groupID);
 130        }
 18131    }
 132
 133    public void ConfigureHUDElement(HUDElementID hudElementId, HUDConfiguration configuration, string extraPayload = nul
 134    {
 135        //TODO(Brian): For now, the factory code is using this switch approach.
 136        //             In order to avoid the factory upkeep we can transform the IHUD elements
 137        //             To ScriptableObjects. In this scenario, we can make each element handle its own
 138        //             specific initialization details.
 139        //
 140        //             This will allow us to unify the serialized factory objects design,
 141        //             like we already do with ECS components.
 142
 143        switch (hudElementId)
 144        {
 145            case HUDElementID.NONE:
 146                break;
 147            case HUDElementID.MINIMAP:
 1148                CreateHudElement(configuration, hudElementId);
 1149                break;
 150            case HUDElementID.PROFILE_HUD:
 1151                CreateHudElement(configuration, hudElementId);
 1152                break;
 153            case HUDElementID.NOTIFICATION:
 1154                CreateHudElement(configuration, hudElementId);
 1155                NotificationsController.i?.Initialize(notificationHud);
 1156                break;
 157            case HUDElementID.AVATAR_EDITOR:
 1158                CreateHudElement(configuration, hudElementId);
 1159                avatarEditorHud?.Initialize(ownUserProfile, wearableCatalog);
 1160                break;
 161            case HUDElementID.SETTINGS_PANEL:
 1162                CreateHudElement(configuration, hudElementId);
 1163                if (settingsPanelHud != null)
 1164                    settingsPanelHud.Initialize();
 1165                break;
 166            case HUDElementID.EXPRESSIONS:
 167            case HUDElementID.EMOTES:
 2168                CreateHudElement(configuration, hudElementId);
 2169                break;
 170            case HUDElementID.PLAYER_INFO_CARD:
 1171                CreateHudElement(configuration, hudElementId);
 1172                break;
 173            case HUDElementID.AIRDROPPING:
 1174                CreateHudElement(configuration, hudElementId);
 1175                break;
 176            case HUDElementID.TERMS_OF_SERVICE:
 1177                CreateHudElement(configuration, hudElementId);
 1178                break;
 179            case HUDElementID.WORLD_CHAT_WINDOW:
 1180                if (worldChatWindowHud == null)
 181                {
 1182                    CreateHudElement(configuration, hudElementId);
 183
 1184                    if (worldChatWindowHud != null)
 185                    {
 1186                        worldChatWindowHud.Initialize(ChatController.i, SceneReferences.i.mouseCatcher);
 1187                        worldChatWindowHud.OnPressPrivateMessage -= OpenPrivateChatWindow;
 1188                        worldChatWindowHud.OnPressPrivateMessage += OpenPrivateChatWindow;
 1189                        worldChatWindowHud.view.OnDeactivatePreview -= View_OnDeactivatePreview;
 1190                        worldChatWindowHud.view.OnDeactivatePreview += View_OnDeactivatePreview;
 191
 1192                        taskbarHud?.AddWorldChatWindow(worldChatWindowHud);
 193                    }
 0194                }
 195                else
 196                {
 0197                    UpdateHudElement(configuration, hudElementId);
 198                }
 199
 0200                break;
 201            case HUDElementID.FRIENDS:
 1202                if (friendsHud == null)
 203                {
 1204                    CreateHudElement(configuration, hudElementId);
 205
 1206                    if (friendsHud != null)
 207                    {
 1208                        friendsHud.Initialize(FriendsController.i, UserProfile.GetOwnUserProfile());
 1209                        friendsHud.OnPressWhisper -= OpenPrivateChatWindow;
 1210                        friendsHud.OnPressWhisper += OpenPrivateChatWindow;
 211
 1212                        taskbarHud?.AddFriendsWindow(friendsHud);
 213                    }
 1214                }
 215                else
 216                {
 0217                    UpdateHudElement(configuration, hudElementId);
 218
 0219                    if (!configuration.active)
 0220                        taskbarHud?.DisableFriendsWindow();
 221                }
 222
 1223                if (privateChatWindowHud == null)
 224                {
 1225                    CreateHudElement(configuration, HUDElementID.PRIVATE_CHAT_WINDOW);
 226
 1227                    if (privateChatWindowHud != null)
 228                    {
 1229                        privateChatWindowHud.Initialize(ChatController.i);
 1230                        privateChatWindowHud.OnPressBack -= PrivateChatWindowHud_OnPressBack;
 1231                        privateChatWindowHud.OnPressBack += PrivateChatWindowHud_OnPressBack;
 232
 1233                        taskbarHud?.AddPrivateChatWindow(privateChatWindowHud);
 234                    }
 235                }
 236
 1237                break;
 238            case HUDElementID.TASKBAR:
 1239                if (taskbarHud == null)
 240                {
 1241                    CreateHudElement(configuration, hudElementId);
 242
 1243                    if (taskbarHud != null)
 244                    {
 1245                        taskbarHud.Initialize(
 246                            SceneReferences.i.mouseCatcher,
 247                            ChatController.i,
 248                            FriendsController.i,
 249                            DCL.Environment.i.world.sceneController,
 250                            DCL.Environment.i.world.state);
 1251                        taskbarHud.OnAnyTaskbarButtonClicked -= TaskbarHud_onAnyTaskbarButtonClicked;
 1252                        taskbarHud.OnAnyTaskbarButtonClicked += TaskbarHud_onAnyTaskbarButtonClicked;
 253
 1254                        if (!string.IsNullOrEmpty(extraPayload))
 255                        {
 0256                            var config = JsonUtility.FromJson<TaskbarHUDController.Configuration>(extraPayload);
 0257                            if (config.enableVoiceChat)
 258                            {
 0259                                taskbarHud.OnAddVoiceChat();
 260                            }
 261                        }
 262
 1263                        OnTaskbarCreation?.Invoke();
 264                    }
 0265                }
 266                else
 267                {
 0268                    UpdateHudElement(configuration, hudElementId);
 269                }
 270
 0271                break;
 272            case HUDElementID.MESSAGE_OF_THE_DAY:
 1273                CreateHudElement(configuration, hudElementId);
 1274                messageOfTheDayHud?.Initialize(JsonUtility.FromJson<MessageOfTheDayConfig>(extraPayload));
 1275                break;
 276            case HUDElementID.OPEN_EXTERNAL_URL_PROMPT:
 1277                CreateHudElement(configuration, hudElementId);
 1278                break;
 279            case HUDElementID.NFT_INFO_DIALOG:
 1280                CreateHudElement(configuration, hudElementId);
 1281                break;
 282            case HUDElementID.TELEPORT_DIALOG:
 1283                CreateHudElement(configuration, hudElementId);
 1284                break;
 285            case HUDElementID.CONTROLS_HUD:
 1286                CreateHudElement(configuration, hudElementId);
 1287                break;
 288            case HUDElementID.HELP_AND_SUPPORT_HUD:
 1289                CreateHudElement(configuration, hudElementId);
 1290                settingsPanelHud?.AddHelpAndSupportWindow(helpAndSupportHud);
 1291                break;
 292            case HUDElementID.USERS_AROUND_LIST_HUD:
 1293                CreateHudElement(configuration, hudElementId);
 1294                if (usersAroundListHud != null)
 295                {
 1296                    minimapHud?.AddUsersAroundIndicator(usersAroundListHud);
 297                }
 298
 1299                break;
 300            case HUDElementID.GRAPHIC_CARD_WARNING:
 1301                CreateHudElement(configuration, hudElementId);
 1302                break;
 303            case HUDElementID.QUESTS_PANEL:
 1304                CreateHudElement(configuration, hudElementId);
 1305                if (configuration.active)
 1306                    questsPanelHUD.Initialize(QuestsController.i);
 1307                break;
 308            case HUDElementID.QUESTS_TRACKER:
 1309                CreateHudElement(configuration, hudElementId);
 1310                if (configuration.active)
 1311                    questsTrackerHUD.Initialize(QuestsController.i);
 1312                break;
 313            case HUDElementID.SIGNUP:
 1314                CreateHudElement(configuration, hudElementId);
 1315                if (configuration.active)
 316                {
 317                    //Same race condition risks as with the ProfileHUD
 318                    //TODO Refactor the way AvatarEditor sets its visibility to match our data driven pattern
 319                    //Then this reference can be removed so we just work with a BaseVariable<bool>.
 320                    //This refactor applies to the ProfileHUD and the way kernel asks the HUDController during signup
 1321                    signupHUD.Initialize(avatarEditorHud);
 322                }
 1323                break;
 324            case HUDElementID.LOADING:
 1325                CreateHudElement(configuration, hudElementId);
 1326                if (configuration.active)
 1327                    loadingController.Initialize();
 328                break;
 329            case HUDElementID.AVATAR_NAMES:
 330                // TODO Remove the HUDElementId once kernel stops sending the Configure HUD message
 331                break;
 332        }
 333
 32334        var hudElement = GetHUDElement(hudElementId);
 335
 32336        if (hudElement != null)
 26337            hudElement.SetVisibility(configuration.active && configuration.visible);
 32338    }
 339
 0340    private void OpenPrivateChatWindow(string targetUserId) { taskbarHud?.OpenPrivateChatTo(targetUserId); }
 341
 0342    private void View_OnDeactivatePreview() { playerInfoCardHud?.CloseCard(); }
 343
 0344    private void PrivateChatWindowHud_OnPressBack() { taskbarHud?.OpenFriendsWindow(); }
 345
 0346    private void TaskbarHud_onAnyTaskbarButtonClicked() { playerInfoCardHud?.CloseCard(); }
 347
 348    public void CreateHudElement(HUDConfiguration config, HUDElementID id)
 349    {
 26350        bool controllerCreated = hudElements.ContainsKey(id);
 351
 26352        if (config.active && !controllerCreated)
 353        {
 26354            hudElements.Add(id, hudFactory.CreateHUD(id));
 355
 26356            if (VERBOSE)
 0357                Debug.Log($"Adding {id} .. type {hudElements[id].GetType().Name}");
 358        }
 26359    }
 360
 361    public void UpdateHudElement(HUDConfiguration config, HUDElementID id)
 362    {
 0363        if (!hudElements.ContainsKey(id))
 0364            return;
 365
 0366        if (VERBOSE)
 0367            Debug.Log($"Updating {id}, type {hudElements[id].GetType().Name}, active: {config.active} visible: {config.v
 368
 0369        hudElements[id].SetVisibility(config.visible);
 0370    }
 371    public void Cleanup()
 372    {
 803373        toggleUIVisibilityTrigger.OnTriggered -= ToggleUIVisibility_OnTriggered;
 803374        CommonScriptableObjects.allUIHidden.OnChange -= AllUIHiddenOnOnChange;
 375
 803376        if (worldChatWindowHud != null)
 377        {
 1378            worldChatWindowHud.OnPressPrivateMessage -= OpenPrivateChatWindow;
 1379            worldChatWindowHud.view.OnDeactivatePreview -= View_OnDeactivatePreview;
 380        }
 381
 803382        if (privateChatWindowHud != null)
 1383            privateChatWindowHud.OnPressBack -= PrivateChatWindowHud_OnPressBack;
 384
 803385        if (friendsHud != null)
 1386            friendsHud.OnPressWhisper -= OpenPrivateChatWindow;
 387
 803388        if (taskbarHud != null)
 1389            taskbarHud.OnAnyTaskbarButtonClicked -= TaskbarHud_onAnyTaskbarButtonClicked;
 390
 803391        UserContextMenu.OnOpenPrivateChatRequest -= OpenPrivateChatWindow;
 392
 1658393        foreach (var kvp in hudElements)
 394        {
 26395            kvp.Value?.Dispose();
 396        }
 397
 803398        hudElements.Clear();
 803399    }
 400
 401    public IHUD GetHUDElement(HUDElementID id)
 402    {
 3531403        if (!hudElements.ContainsKey(id))
 3434404            return null;
 405
 97406        return hudElements[id];
 407    }
 408
 409    public static bool IsHUDElementDeprecated(HUDElementID element)
 410    {
 32411        Type enumType = typeof(HUDElementID);
 32412        var enumName = enumType.GetEnumName(element);
 32413        var fieldInfo = enumType.GetField(enumName);
 32414        return Attribute.IsDefined(fieldInfo, typeof(ObsoleteAttribute));
 415    }
 416
 417#if UNITY_EDITOR
 418    [ContextMenu("Trigger fake PlayerInfoCard")]
 419    public void TriggerFakePlayerInfoCard()
 420    {
 0421        var newModel = ownUserProfile.CloneModel();
 0422        newModel.name = "FakePassport";
 0423        newModel.description = "Fake Description for Testing";
 0424        newModel.userId = "test-id";
 0425        newModel.inventory = new[]
 426        {
 427            "dcl://halloween_2019/machete_headband_top_head",
 428            "dcl://halloween_2019/bee_suit_upper_body",
 429            "dcl://halloween_2019/bride_of_frankie_upper_body",
 430            "dcl://halloween_2019/creepy_nurse_upper_body",
 431        };
 432
 0433        UserProfileController.i.AddUserProfileToCatalog(newModel);
 0434        Resources.Load<StringVariable>("CurrentPlayerInfoCardId").Set(newModel.userId);
 0435    }
 436#endif
 1596437    public void Dispose() { Cleanup(); }
 438}