< Summary

Class:HUDController
Assembly:HUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/HUDController.cs
Covered lines:166
Uncovered lines:46
Coverable lines:212
Total lines:463
Line coverage:78.3% (166 of 212)
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%52.2949088.89%
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
 80425    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    {
 80434        i = this;
 80435        this.hudFactory = hudFactory;
 36
 80437        toggleUIVisibilityTrigger = Resources.Load<InputAction_Trigger>(TOGGLE_UI_VISIBILITY_ASSET_NAME);
 80438        toggleUIVisibilityTrigger.OnTriggered += ToggleUIVisibility_OnTriggered;
 39
 80440        CommonScriptableObjects.allUIHidden.OnChange += AllUIHiddenOnOnChange;
 80441        UserContextMenu.OnOpenPrivateChatRequest += OpenPrivateChatWindow;
 80442    }
 43
 44    public event Action OnTaskbarCreation;
 45
 2546    public ProfileHUDController profileHud => GetHUDElement(HUDElementID.PROFILE_HUD) as ProfileHUDController;
 47
 48    public NotificationHUDController notificationHud =>
 149        GetHUDElement(HUDElementID.NOTIFICATION) as NotificationHUDController;
 50
 951    public MinimapHUDController minimapHud => GetHUDElement(HUDElementID.MINIMAP) as MinimapHUDController;
 52
 53    public AvatarEditorHUDController avatarEditorHud =>
 454        GetHUDElement(HUDElementID.AVATAR_EDITOR) as AvatarEditorHUDController;
 55
 356    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
 108273    public TaskbarHUDController taskbarHud => GetHUDElement(HUDElementID.TASKBAR) as TaskbarHUDController;
 74
 75    public WorldChatWindowHUDController worldChatWindowHud =>
 84576        GetHUDElement(HUDElementID.WORLD_CHAT_WINDOW) as WorldChatWindowHUDController;
 77
 78    public PrivateChatWindowHUDController privateChatWindowHud =>
 83779        GetHUDElement(HUDElementID.PRIVATE_CHAT_WINDOW) as PrivateChatWindowHUDController;
 80
 84381    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
 587    public ExploreHUDController exploreHud => GetHUDElement(HUDElementID.EXPLORE_HUD) as ExploreHUDController;
 88
 189    public HelpAndSupportHUDController helpAndSupportHud => GetHUDElement(HUDElementID.HELP_AND_SUPPORT_HUD) as HelpAndS
 90
 491    public UsersAroundListHUDController usersAroundListHud => GetHUDElement(HUDElementID.USERS_AROUND_LIST_HUD) as Users
 192    public QuestsPanelHUDController questsPanelHUD => GetHUDElement(HUDElementID.QUESTS_PANEL) as QuestsPanelHUDControll
 193    public QuestsTrackerHUDController questsTrackerHUD => GetHUDElement(HUDElementID.QUESTS_TRACKER) as QuestsTrackerHUD
 194    public SignupHUDController signupHUD => GetHUDElement(HUDElementID.SIGNUP) as SignupHUDController;
 195    public LoadingHUDController loadingController => GetHUDElement(HUDElementID.LOADING) as LoadingHUDController;
 96
 80497    public Dictionary<HUDElementID, IHUD> hudElements { get; private set; } = new Dictionary<HUDElementID, IHUD>();
 98
 199    private UserProfile ownUserProfile => UserProfile.GetOwnUserProfile();
 0100    private BaseDictionary<string, WearableItem> wearableCatalog => CatalogController.wearableCatalog;
 101
 0102    private void ShowSettings() { settingsPanelHud?.SetVisibility(true); }
 103
 0104    private void ShowControls() { controlsHud?.SetVisibility(true); }
 105
 106    private void ToggleUIVisibility_OnTriggered(DCLAction_Trigger action)
 107    {
 0108        bool anyInputFieldIsSelected = EventSystem.current != null &&
 109                                       EventSystem.current.currentSelectedGameObject != null &&
 110                                       EventSystem.current.currentSelectedGameObject.GetComponent<TMPro.TMP_InputField>(
 111                                       (!worldChatWindowHud.view.chatHudView.inputField.isFocused || !worldChatWindowHud
 112
 0113        if (anyInputFieldIsSelected ||
 114            settingsPanelHud.view.isOpen ||
 115            avatarEditorHud.view.isOpen ||
 116            DataStore.i.HUDs.navmapVisible.Get() ||
 117            CommonScriptableObjects.tutorialActive)
 0118            return;
 119
 0120        CommonScriptableObjects.allUIHidden.Set(!CommonScriptableObjects.allUIHidden.Get());
 0121    }
 122
 123    private void AllUIHiddenOnOnChange(bool current, bool previous)
 124    {
 36125        if (current)
 126        {
 18127            NotificationsController.i?.ShowNotification(hiddenUINotification);
 18128        }
 129        else
 130        {
 18131            NotificationsController.i?.DismissAllNotifications(hiddenUINotification.groupID);
 132        }
 18133    }
 134
 135    public void ConfigureHUDElement(HUDElementID hudElementId, HUDConfiguration configuration, string extraPayload = nul
 136    {
 137        //TODO(Brian): For now, the factory code is using this switch approach.
 138        //             In order to avoid the factory upkeep we can transform the IHUD elements
 139        //             To ScriptableObjects. In this scenario, we can make each element handle its own
 140        //             specific initialization details.
 141        //
 142        //             This will allow us to unify the serialized factory objects design,
 143        //             like we already do with ECS components.
 144
 145        switch (hudElementId)
 146        {
 147            case HUDElementID.NONE:
 148                break;
 149            case HUDElementID.MINIMAP:
 1150                CreateHudElement(configuration, hudElementId);
 1151                break;
 152            case HUDElementID.PROFILE_HUD:
 1153                CreateHudElement(configuration, hudElementId);
 1154                if (profileHud != null)
 155                {
 156                    //TODO This coupling might introduce a race condition if kernel configures this HUD before AvatarEdi
 1157                    profileHud?.AddBackpackWindow(avatarEditorHud);
 158                }
 159
 1160                break;
 161            case HUDElementID.NOTIFICATION:
 1162                CreateHudElement(configuration, hudElementId);
 1163                NotificationsController.i?.Initialize(notificationHud);
 1164                break;
 165            case HUDElementID.AVATAR_EDITOR:
 1166                CreateHudElement(configuration, hudElementId);
 1167                if (avatarEditorHud != null)
 168                {
 1169                    avatarEditorHud.Initialize(ownUserProfile, wearableCatalog);
 170                }
 171
 1172                break;
 173            case HUDElementID.SETTINGS_PANEL:
 1174                CreateHudElement(configuration, hudElementId);
 1175                if (settingsPanelHud != null)
 1176                    settingsPanelHud.Initialize();
 1177                break;
 178            case HUDElementID.EXPRESSIONS:
 179            case HUDElementID.EMOTES:
 2180                CreateHudElement(configuration, hudElementId);
 2181                break;
 182            case HUDElementID.PLAYER_INFO_CARD:
 1183                CreateHudElement(configuration, hudElementId);
 1184                break;
 185            case HUDElementID.AIRDROPPING:
 1186                CreateHudElement(configuration, hudElementId);
 1187                break;
 188            case HUDElementID.TERMS_OF_SERVICE:
 1189                CreateHudElement(configuration, hudElementId);
 1190                break;
 191            case HUDElementID.WORLD_CHAT_WINDOW:
 1192                if (worldChatWindowHud == null)
 193                {
 1194                    CreateHudElement(configuration, hudElementId);
 195
 1196                    if (worldChatWindowHud != null)
 197                    {
 1198                        worldChatWindowHud.Initialize(ChatController.i, SceneReferences.i.mouseCatcher);
 1199                        worldChatWindowHud.OnPressPrivateMessage -= OpenPrivateChatWindow;
 1200                        worldChatWindowHud.OnPressPrivateMessage += OpenPrivateChatWindow;
 1201                        worldChatWindowHud.view.OnDeactivatePreview -= View_OnDeactivatePreview;
 1202                        worldChatWindowHud.view.OnDeactivatePreview += View_OnDeactivatePreview;
 203
 1204                        taskbarHud?.AddWorldChatWindow(worldChatWindowHud);
 205                    }
 0206                }
 207                else
 208                {
 0209                    UpdateHudElement(configuration, hudElementId);
 210                }
 211
 0212                break;
 213            case HUDElementID.FRIENDS:
 1214                if (friendsHud == null)
 215                {
 1216                    CreateHudElement(configuration, hudElementId);
 217
 1218                    if (friendsHud != null)
 219                    {
 1220                        friendsHud.Initialize(FriendsController.i, UserProfile.GetOwnUserProfile());
 1221                        friendsHud.OnPressWhisper -= OpenPrivateChatWindow;
 1222                        friendsHud.OnPressWhisper += OpenPrivateChatWindow;
 223
 1224                        taskbarHud?.AddFriendsWindow(friendsHud);
 225                    }
 1226                }
 227                else
 228                {
 0229                    UpdateHudElement(configuration, hudElementId);
 230
 0231                    if (!configuration.active)
 0232                        taskbarHud?.DisableFriendsWindow();
 233                }
 234
 1235                if (privateChatWindowHud == null)
 236                {
 1237                    CreateHudElement(configuration, HUDElementID.PRIVATE_CHAT_WINDOW);
 238
 1239                    if (privateChatWindowHud != null)
 240                    {
 1241                        privateChatWindowHud.Initialize(ChatController.i);
 1242                        privateChatWindowHud.OnPressBack -= PrivateChatWindowHud_OnPressBack;
 1243                        privateChatWindowHud.OnPressBack += PrivateChatWindowHud_OnPressBack;
 244
 1245                        taskbarHud?.AddPrivateChatWindow(privateChatWindowHud);
 246                    }
 247                }
 248
 1249                break;
 250            case HUDElementID.TASKBAR:
 1251                if (taskbarHud == null)
 252                {
 1253                    CreateHudElement(configuration, hudElementId);
 254
 1255                    if (taskbarHud != null)
 256                    {
 1257                        taskbarHud.Initialize(
 258                            SceneReferences.i.mouseCatcher,
 259                            ChatController.i,
 260                            FriendsController.i,
 261                            DCL.Environment.i.world.sceneController,
 262                            DCL.Environment.i.world.state);
 1263                        taskbarHud.OnAnyTaskbarButtonClicked -= TaskbarHud_onAnyTaskbarButtonClicked;
 1264                        taskbarHud.OnAnyTaskbarButtonClicked += TaskbarHud_onAnyTaskbarButtonClicked;
 265
 1266                        if (!string.IsNullOrEmpty(extraPayload))
 267                        {
 0268                            var config = JsonUtility.FromJson<TaskbarHUDController.Configuration>(extraPayload);
 0269                            if (config.enableVoiceChat)
 270                            {
 0271                                taskbarHud.OnAddVoiceChat();
 272                            }
 273
 0274                            taskbarHud.SetQuestsPanelStatus(config.enableQuestPanel);
 275                        }
 276
 1277                        taskbarHud.AddSettingsWindow(settingsPanelHud);
 1278                        OnTaskbarCreation?.Invoke();
 279                    }
 0280                }
 281                else
 282                {
 0283                    UpdateHudElement(configuration, hudElementId);
 284                }
 285
 0286                break;
 287            case HUDElementID.MESSAGE_OF_THE_DAY:
 1288                CreateHudElement(configuration, hudElementId);
 1289                messageOfTheDayHud?.Initialize(JsonUtility.FromJson<MessageOfTheDayConfig>(extraPayload));
 1290                break;
 291            case HUDElementID.OPEN_EXTERNAL_URL_PROMPT:
 1292                CreateHudElement(configuration, hudElementId);
 1293                break;
 294            case HUDElementID.NFT_INFO_DIALOG:
 1295                CreateHudElement(configuration, hudElementId);
 1296                break;
 297            case HUDElementID.TELEPORT_DIALOG:
 1298                CreateHudElement(configuration, hudElementId);
 1299                break;
 300            case HUDElementID.CONTROLS_HUD:
 1301                CreateHudElement(configuration, hudElementId);
 1302                taskbarHud?.AddControlsMoreOption();
 1303                break;
 304            case HUDElementID.EXPLORE_HUD:
 1305                CreateHudElement(configuration, hudElementId);
 1306                if (exploreHud != null)
 307                {
 1308                    exploreHud.Initialize(FriendsController.i);
 1309                    taskbarHud?.AddExploreWindow(exploreHud);
 310                }
 311
 1312                break;
 313            case HUDElementID.HELP_AND_SUPPORT_HUD:
 1314                CreateHudElement(configuration, hudElementId);
 1315                taskbarHud?.AddHelpAndSupportWindow(helpAndSupportHud);
 1316                break;
 317            case HUDElementID.USERS_AROUND_LIST_HUD:
 1318                CreateHudElement(configuration, hudElementId);
 1319                if (usersAroundListHud != null)
 320                {
 1321                    minimapHud?.AddUsersAroundIndicator(usersAroundListHud);
 322                }
 323
 1324                break;
 325            case HUDElementID.GRAPHIC_CARD_WARNING:
 1326                CreateHudElement(configuration, hudElementId);
 1327                break;
 328            case HUDElementID.QUESTS_PANEL:
 1329                CreateHudElement(configuration, hudElementId);
 1330                if (configuration.active)
 1331                    questsPanelHUD.Initialize(QuestsController.i);
 1332                break;
 333            case HUDElementID.QUESTS_TRACKER:
 1334                CreateHudElement(configuration, hudElementId);
 1335                if (configuration.active)
 1336                    questsTrackerHUD.Initialize(QuestsController.i);
 1337                break;
 338            case HUDElementID.SIGNUP:
 1339                CreateHudElement(configuration, hudElementId);
 1340                if (configuration.active)
 341                {
 342                    //Same race condition risks as with the ProfileHUD
 343                    //TODO Refactor the way AvatarEditor sets its visibility to match our data driven pattern
 344                    //Then this reference can be removed so we just work with a BaseVariable<bool>.
 345                    //This refactor applies to the ProfileHUD and the way kernel asks the HUDController during signup
 1346                    signupHUD.Initialize(avatarEditorHud);
 347                }
 1348                break;
 349            case HUDElementID.LOADING:
 1350                CreateHudElement(configuration, hudElementId);
 1351                if (configuration.active)
 1352                    loadingController.Initialize();
 353                break;
 354            case HUDElementID.AVATAR_NAMES:
 355                // TODO Remove the HUDElementId once kernel stops sending the Configure HUD message
 356                break;
 357        }
 358
 32359        var hudElement = GetHUDElement(hudElementId);
 360
 32361        if (hudElement != null)
 27362            hudElement.SetVisibility(configuration.active && configuration.visible);
 32363    }
 364
 0365    private void OpenPrivateChatWindow(string targetUserId) { taskbarHud?.OpenPrivateChatTo(targetUserId); }
 366
 0367    private void View_OnDeactivatePreview() { playerInfoCardHud?.CloseCard(); }
 368
 0369    private void PrivateChatWindowHud_OnPressBack() { taskbarHud?.OpenFriendsWindow(); }
 370
 0371    private void TaskbarHud_onAnyTaskbarButtonClicked() { playerInfoCardHud?.CloseCard(); }
 372
 373    public void CreateHudElement(HUDConfiguration config, HUDElementID id)
 374    {
 27375        bool controllerCreated = hudElements.ContainsKey(id);
 376
 27377        if (config.active && !controllerCreated)
 378        {
 27379            hudElements.Add(id, hudFactory.CreateHUD(id));
 380
 27381            if (VERBOSE)
 0382                Debug.Log($"Adding {id} .. type {hudElements[id].GetType().Name}");
 383        }
 27384    }
 385
 386    public void UpdateHudElement(HUDConfiguration config, HUDElementID id)
 387    {
 0388        if (!hudElements.ContainsKey(id))
 0389            return;
 390
 0391        if (VERBOSE)
 0392            Debug.Log($"Updating {id}, type {hudElements[id].GetType().Name}, active: {config.active} visible: {config.v
 393
 0394        hudElements[id].SetVisibility(config.visible);
 0395    }
 396    public void Cleanup()
 397    {
 830398        toggleUIVisibilityTrigger.OnTriggered -= ToggleUIVisibility_OnTriggered;
 830399        CommonScriptableObjects.allUIHidden.OnChange -= AllUIHiddenOnOnChange;
 400
 830401        if (worldChatWindowHud != null)
 402        {
 1403            worldChatWindowHud.OnPressPrivateMessage -= OpenPrivateChatWindow;
 1404            worldChatWindowHud.view.OnDeactivatePreview -= View_OnDeactivatePreview;
 405        }
 406
 830407        if (privateChatWindowHud != null)
 1408            privateChatWindowHud.OnPressBack -= PrivateChatWindowHud_OnPressBack;
 409
 830410        if (friendsHud != null)
 1411            friendsHud.OnPressWhisper -= OpenPrivateChatWindow;
 412
 830413        if (taskbarHud != null)
 1414            taskbarHud.OnAnyTaskbarButtonClicked -= TaskbarHud_onAnyTaskbarButtonClicked;
 415
 830416        UserContextMenu.OnOpenPrivateChatRequest -= OpenPrivateChatWindow;
 417
 1714418        foreach (var kvp in hudElements)
 419        {
 27420            kvp.Value?.Dispose();
 421        }
 422
 830423        hudElements.Clear();
 830424    }
 425
 426    public IHUD GetHUDElement(HUDElementID id)
 427    {
 3789428        if (!hudElements.ContainsKey(id))
 3680429            return null;
 430
 109431        return hudElements[id];
 432    }
 433
 434    public static bool IsHUDElementDeprecated(HUDElementID element)
 435    {
 32436        Type enumType = typeof(HUDElementID);
 32437        var enumName = enumType.GetEnumName(element);
 32438        var fieldInfo = enumType.GetField(enumName);
 32439        return Attribute.IsDefined(fieldInfo, typeof(ObsoleteAttribute));
 440    }
 441
 442#if UNITY_EDITOR
 443    [ContextMenu("Trigger fake PlayerInfoCard")]
 444    public void TriggerFakePlayerInfoCard()
 445    {
 0446        var newModel = ownUserProfile.CloneModel();
 0447        newModel.name = "FakePassport";
 0448        newModel.description = "Fake Description for Testing";
 0449        newModel.userId = "test-id";
 0450        newModel.inventory = new[]
 451        {
 452            "dcl://halloween_2019/machete_headband_top_head",
 453            "dcl://halloween_2019/bee_suit_upper_body",
 454            "dcl://halloween_2019/bride_of_frankie_upper_body",
 455            "dcl://halloween_2019/creepy_nurse_upper_body",
 456        };
 457
 0458        UserProfileController.i.AddUserProfileToCatalog(newModel);
 0459        Resources.Load<StringVariable>("CurrentPlayerInfoCardId").Set(newModel.userId);
 0460    }
 461#endif
 1650462    public void Dispose() { Cleanup(); }
 463}