< Summary

Class:HUDController
Assembly:HUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/HUDController.cs
Covered lines:172
Uncovered lines:46
Coverable lines:218
Total lines:469
Line coverage:78.8% (172 of 218)
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%57.8855090.16%
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 SignupHUD;
 10using UnityEngine;
 11using UnityEngine.EventSystems;
 12
 13public class HUDController : IHUDController
 14{
 15    private const string TOGGLE_UI_VISIBILITY_ASSET_NAME = "ToggleUIVisibility";
 16
 17    static bool VERBOSE = false;
 018    public static HUDController i { get; private set; }
 19
 20    public IHUDFactory hudFactory = null;
 21
 22    private InputAction_Trigger toggleUIVisibilityTrigger;
 23
 54824    private readonly Notification.Model hiddenUINotification = new Notification.Model()
 25    {
 26        timer = 3,
 27        type = NotificationFactory.Type.UI_HIDDEN,
 28        groupID = "UIHiddenNotification"
 29    };
 30
 31    public void Initialize(IHUDFactory hudFactory)
 32    {
 54833        i = this;
 54834        this.hudFactory = hudFactory;
 35
 54836        toggleUIVisibilityTrigger = Resources.Load<InputAction_Trigger>(TOGGLE_UI_VISIBILITY_ASSET_NAME);
 54837        toggleUIVisibilityTrigger.OnTriggered += ToggleUIVisibility_OnTriggered;
 38
 54839        CommonScriptableObjects.allUIHidden.OnChange += AllUIHiddenOnOnChange;
 54840        UserContextMenu.OnOpenPrivateChatRequest += OpenPrivateChatWindow;
 54841    }
 42
 43    public event Action OnBuilderProjectPanelCreation;
 44
 645    public ProfileHUDController profileHud => GetHUDElement(HUDElementID.PROFILE_HUD) as ProfileHUDController;
 46
 47    public NotificationHUDController notificationHud =>
 148        GetHUDElement(HUDElementID.NOTIFICATION) as NotificationHUDController;
 49
 150    public MinimapHUDController minimapHud => GetHUDElement(HUDElementID.MINIMAP) as MinimapHUDController;
 51
 52    public AvatarEditorHUDController avatarEditorHud =>
 453        GetHUDElement(HUDElementID.AVATAR_EDITOR) as AvatarEditorHUDController;
 54
 355    public SettingsPanelHUDController settingsPanelHud => GetHUDElement(HUDElementID.SETTINGS_PANEL) as SettingsPanelHUD
 56
 57    public ExpressionsHUDController expressionsHud =>
 058        GetHUDElement(HUDElementID.EXPRESSIONS) as ExpressionsHUDController;
 59
 60    public PlayerInfoCardHUDController playerInfoCardHud =>
 061        GetHUDElement(HUDElementID.PLAYER_INFO_CARD) as PlayerInfoCardHUDController;
 62
 63    public WelcomeHUDController messageOfTheDayHud =>
 164        GetHUDElement(HUDElementID.MESSAGE_OF_THE_DAY) as WelcomeHUDController;
 65
 66    public AirdroppingHUDController airdroppingHud =>
 067        GetHUDElement(HUDElementID.AIRDROPPING) as AirdroppingHUDController;
 68
 69    public TermsOfServiceHUDController termsOfServiceHud =>
 070        GetHUDElement(HUDElementID.TERMS_OF_SERVICE) as TermsOfServiceHUDController;
 71
 60972    public TaskbarHUDController taskbarHud => GetHUDElement(HUDElementID.TASKBAR) as TaskbarHUDController;
 73
 74    public WorldChatWindowHUDController worldChatWindowHud =>
 56875        GetHUDElement(HUDElementID.WORLD_CHAT_WINDOW) as WorldChatWindowHUDController;
 76
 77    public PrivateChatWindowHUDController privateChatWindowHud =>
 56678        GetHUDElement(HUDElementID.PRIVATE_CHAT_WINDOW) as PrivateChatWindowHUDController;
 79
 56680    public FriendsHUDController friendsHud => GetHUDElement(HUDElementID.FRIENDS) as FriendsHUDController;
 81
 082    public TeleportPromptHUDController teleportHud => GetHUDElement(HUDElementID.TELEPORT_DIALOG) as TeleportPromptHUDCo
 83
 084    public ControlsHUDController controlsHud => GetHUDElement(HUDElementID.CONTROLS_HUD) as ControlsHUDController;
 85
 386    public ExploreHUDController exploreHud => GetHUDElement(HUDElementID.EXPLORE_HUD) as ExploreHUDController;
 87
 188    public HelpAndSupportHUDController helpAndSupportHud => GetHUDElement(HUDElementID.HELP_AND_SUPPORT_HUD) as HelpAndS
 89
 290    public UsersAroundListHUDController usersAroundListHud => GetHUDElement(HUDElementID.USERS_AROUND_LIST_HUD) as Users
 91
 27992    public BuildModeHUDController builderInWorldMainHud => GetHUDElement(HUDElementID.BUILDER_IN_WORLD_MAIN) as BuildMod
 93
 194    public QuestsPanelHUDController questsPanelHUD => GetHUDElement(HUDElementID.QUESTS_PANEL) as QuestsPanelHUDControll
 195    public QuestsTrackerHUDController questsTrackerHUD => GetHUDElement(HUDElementID.QUESTS_TRACKER) as QuestsTrackerHUD
 196    public SignupHUDController signupHUD => GetHUDElement(HUDElementID.SIGNUP) as SignupHUDController;
 197    public BuilderProjectsPanelController builderProjectsPanelController => GetHUDElement(HUDElementID.BUILDER_PROJECTS_
 98
 54899    public Dictionary<HUDElementID, IHUD> hudElements { get; private set; } = new Dictionary<HUDElementID, IHUD>();
 100
 1101    private UserProfile ownUserProfile => UserProfile.GetOwnUserProfile();
 0102    private BaseDictionary<string, WearableItem> wearableCatalog => CatalogController.wearableCatalog;
 103
 0104    private void ShowSettings() { settingsPanelHud?.SetVisibility(true); }
 105
 0106    private void ShowControls() { controlsHud?.SetVisibility(true); }
 107
 108    private void ToggleUIVisibility_OnTriggered(DCLAction_Trigger action)
 109    {
 0110        bool anyInputFieldIsSelected = EventSystem.current != null &&
 111                                       EventSystem.current.currentSelectedGameObject != null &&
 112                                       EventSystem.current.currentSelectedGameObject.GetComponent<TMPro.TMP_InputField>(
 113                                       (!worldChatWindowHud.view.chatHudView.inputField.isFocused || !worldChatWindowHud
 114
 0115        if (anyInputFieldIsSelected ||
 116            settingsPanelHud.view.isOpen ||
 117            avatarEditorHud.view.isOpen ||
 118            DCL.NavmapView.isOpen ||
 119            CommonScriptableObjects.tutorialActive)
 0120            return;
 121
 0122        CommonScriptableObjects.allUIHidden.Set(!CommonScriptableObjects.allUIHidden.Get());
 0123    }
 124
 125    private void AllUIHiddenOnOnChange(bool current, bool previous)
 126    {
 2127        if (current)
 128        {
 1129            NotificationsController.i?.ShowNotification(hiddenUINotification);
 1130        }
 131        else
 132        {
 1133            NotificationsController.i?.DismissAllNotifications(hiddenUINotification.groupID);
 134        }
 1135    }
 136
 137    public void ConfigureHUDElement(HUDElementID hudElementId, HUDConfiguration configuration, string extraPayload = nul
 138    {
 139        //TODO(Brian): For now, the factory code is using this switch approach.
 140        //             In order to avoid the factory upkeep we can transform the IHUD elements
 141        //             To ScriptableObjects. In this scenario, we can make each element handle its own
 142        //             specific initialization details.
 143        //
 144        //             This will allow us to unify the serialized factory objects design,
 145        //             like we already do with ECS components.
 146
 147        switch (hudElementId)
 148        {
 149            case HUDElementID.NONE:
 150                break;
 151            case HUDElementID.MINIMAP:
 1152                CreateHudElement(configuration, hudElementId);
 1153                break;
 154            case HUDElementID.PROFILE_HUD:
 1155                CreateHudElement(configuration, hudElementId);
 1156                if (profileHud != null)
 157                {
 158                    //TODO This coupling might introduce a race condition if kernel configures this HUD before AvatarEdi
 1159                    profileHud?.AddBackpackWindow(avatarEditorHud);
 160                }
 161
 1162                break;
 163            case HUDElementID.NOTIFICATION:
 1164                CreateHudElement(configuration, hudElementId);
 1165                NotificationsController.i?.Initialize(notificationHud);
 1166                break;
 167            case HUDElementID.AVATAR_EDITOR:
 1168                CreateHudElement(configuration, hudElementId);
 1169                if (avatarEditorHud != null)
 170                {
 1171                    avatarEditorHud.Initialize(ownUserProfile, wearableCatalog);
 172                }
 173
 1174                break;
 175            case HUDElementID.SETTINGS_PANEL:
 1176                CreateHudElement(configuration, hudElementId);
 1177                if (settingsPanelHud != null)
 1178                    settingsPanelHud.Initialize();
 1179                break;
 180            case HUDElementID.EXPRESSIONS:
 1181                CreateHudElement(configuration, hudElementId);
 1182                break;
 183            case HUDElementID.PLAYER_INFO_CARD:
 1184                CreateHudElement(configuration, hudElementId);
 1185                break;
 186            case HUDElementID.AIRDROPPING:
 1187                CreateHudElement(configuration, hudElementId);
 1188                break;
 189            case HUDElementID.TERMS_OF_SERVICE:
 1190                CreateHudElement(configuration, hudElementId);
 1191                break;
 192            case HUDElementID.WORLD_CHAT_WINDOW:
 1193                if (worldChatWindowHud == null)
 194                {
 1195                    CreateHudElement(configuration, hudElementId);
 196
 1197                    if (worldChatWindowHud != null)
 198                    {
 1199                        worldChatWindowHud.Initialize(ChatController.i, DCL.InitialSceneReferences.i?.mouseCatcher);
 1200                        worldChatWindowHud.OnPressPrivateMessage -= OpenPrivateChatWindow;
 1201                        worldChatWindowHud.OnPressPrivateMessage += OpenPrivateChatWindow;
 1202                        worldChatWindowHud.view.OnDeactivatePreview -= View_OnDeactivatePreview;
 1203                        worldChatWindowHud.view.OnDeactivatePreview += View_OnDeactivatePreview;
 204
 1205                        taskbarHud?.AddWorldChatWindow(worldChatWindowHud);
 206                    }
 0207                }
 208                else
 209                {
 0210                    UpdateHudElement(configuration, hudElementId);
 211                }
 212
 0213                break;
 214            case HUDElementID.FRIENDS:
 1215                if (friendsHud == null)
 216                {
 1217                    CreateHudElement(configuration, hudElementId);
 218
 1219                    if (friendsHud != null)
 220                    {
 1221                        friendsHud.Initialize(FriendsController.i, UserProfile.GetOwnUserProfile());
 1222                        friendsHud.OnPressWhisper -= OpenPrivateChatWindow;
 1223                        friendsHud.OnPressWhisper += OpenPrivateChatWindow;
 224
 1225                        taskbarHud?.AddFriendsWindow(friendsHud);
 226                    }
 1227                }
 228                else
 229                {
 0230                    UpdateHudElement(configuration, hudElementId);
 231
 0232                    if (!configuration.active)
 0233                        taskbarHud?.DisableFriendsWindow();
 234                }
 235
 1236                if (privateChatWindowHud == null)
 237                {
 1238                    CreateHudElement(configuration, HUDElementID.PRIVATE_CHAT_WINDOW);
 239
 1240                    if (privateChatWindowHud != null)
 241                    {
 1242                        privateChatWindowHud.Initialize(ChatController.i);
 1243                        privateChatWindowHud.OnPressBack -= PrivateChatWindowHud_OnPressBack;
 1244                        privateChatWindowHud.OnPressBack += PrivateChatWindowHud_OnPressBack;
 245
 1246                        taskbarHud?.AddPrivateChatWindow(privateChatWindowHud);
 247                    }
 248                }
 249
 1250                break;
 251            case HUDElementID.TASKBAR:
 1252                if (taskbarHud == null)
 253                {
 1254                    CreateHudElement(configuration, hudElementId);
 255
 1256                    if (taskbarHud != null)
 257                    {
 1258                        taskbarHud.Initialize(
 259                            InitialSceneReferences.i?.mouseCatcher,
 260                            ChatController.i,
 261                            FriendsController.i,
 262                            DCL.Environment.i.world.sceneController,
 263                            DCL.Environment.i.world.state);
 1264                        taskbarHud.OnAnyTaskbarButtonClicked -= TaskbarHud_onAnyTaskbarButtonClicked;
 1265                        taskbarHud.OnAnyTaskbarButtonClicked += TaskbarHud_onAnyTaskbarButtonClicked;
 266
 1267                        if (!string.IsNullOrEmpty(extraPayload))
 268                        {
 0269                            var config = JsonUtility.FromJson<TaskbarHUDController.Configuration>(extraPayload);
 0270                            if (config.enableVoiceChat)
 271                            {
 0272                                taskbarHud.OnAddVoiceChat();
 273                            }
 274
 0275                            taskbarHud.SetQuestsPanelStatus(config.enableQuestPanel);
 276                        }
 277
 1278                        taskbarHud.AddSettingsWindow(settingsPanelHud);
 279                    }
 1280                }
 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.BUILDER_IN_WORLD_MAIN:
 1329                CreateHudElement(configuration, hudElementId);
 1330                if (configuration.active)
 1331                    builderInWorldMainHud.Initialize();
 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                }
 1353                break;
 354            case HUDElementID.BUILDER_PROJECTS_PANEL:
 1355                CreateHudElement(configuration, hudElementId);
 1356                if (configuration.active)
 357                {
 1358                    builderProjectsPanelController.Initialize();
 1359                    taskbarHud.SetBuilderInWorldStatus(true);
 360                }
 1361                OnBuilderProjectPanelCreation?.Invoke();
 362                break;
 363        }
 364
 29365        var hudElement = GetHUDElement(hudElementId);
 366
 29367        if (hudElement != null)
 27368            hudElement.SetVisibility(configuration.active && configuration.visible);
 29369    }
 370
 0371    private void OpenPrivateChatWindow(string targetUserId) { taskbarHud?.OpenPrivateChatTo(targetUserId); }
 372
 0373    private void View_OnDeactivatePreview() { playerInfoCardHud?.CloseCard(); }
 374
 0375    private void PrivateChatWindowHud_OnPressBack() { taskbarHud?.OpenFriendsWindow(); }
 376
 0377    private void TaskbarHud_onAnyTaskbarButtonClicked() { playerInfoCardHud?.CloseCard(); }
 378
 379    public void CreateHudElement(HUDConfiguration config, HUDElementID id)
 380    {
 27381        bool controllerCreated = hudElements.ContainsKey(id);
 382
 27383        if (config.active && !controllerCreated)
 384        {
 27385            hudElements.Add(id, hudFactory.CreateHUD(id));
 386
 27387            if (VERBOSE)
 0388                Debug.Log($"Adding {id} .. type {hudElements[id].GetType().Name}");
 389        }
 27390    }
 391
 392    public void UpdateHudElement(HUDConfiguration config, HUDElementID id)
 393    {
 0394        if (!hudElements.ContainsKey(id))
 0395            return;
 396
 0397        if (VERBOSE)
 0398            Debug.Log($"Updating {id}, type {hudElements[id].GetType().Name}, active: {config.active} visible: {config.v
 399
 0400        hudElements[id].SetVisibility(config.visible);
 0401    }
 402    public void Cleanup()
 403    {
 559404        toggleUIVisibilityTrigger.OnTriggered -= ToggleUIVisibility_OnTriggered;
 559405        CommonScriptableObjects.allUIHidden.OnChange -= AllUIHiddenOnOnChange;
 406
 559407        if (worldChatWindowHud != null)
 408        {
 1409            worldChatWindowHud.OnPressPrivateMessage -= OpenPrivateChatWindow;
 1410            worldChatWindowHud.view.OnDeactivatePreview -= View_OnDeactivatePreview;
 411        }
 412
 559413        if (privateChatWindowHud != null)
 1414            privateChatWindowHud.OnPressBack -= PrivateChatWindowHud_OnPressBack;
 415
 559416        if (friendsHud != null)
 1417            friendsHud.OnPressWhisper -= OpenPrivateChatWindow;
 418
 559419        if (taskbarHud != null)
 1420            taskbarHud.OnAnyTaskbarButtonClicked -= TaskbarHud_onAnyTaskbarButtonClicked;
 421
 559422        UserContextMenu.OnOpenPrivateChatRequest -= OpenPrivateChatWindow;
 423
 1172424        foreach (var kvp in hudElements)
 425        {
 27426            kvp.Value?.Dispose();
 427        }
 428
 559429        hudElements.Clear();
 559430    }
 431
 432    public IHUD GetHUDElement(HUDElementID id)
 433    {
 2729434        if (!hudElements.ContainsKey(id))
 2617435            return null;
 436
 112437        return hudElements[id];
 438    }
 439
 440    public static bool IsHUDElementDeprecated(HUDElementID element)
 441    {
 29442        Type enumType = typeof(HUDElementID);
 29443        var enumName = enumType.GetEnumName(element);
 29444        var fieldInfo = enumType.GetField(enumName);
 29445        return Attribute.IsDefined(fieldInfo, typeof(ObsoleteAttribute));
 446    }
 447
 448#if UNITY_EDITOR
 449    [ContextMenu("Trigger fake PlayerInfoCard")]
 450    public void TriggerFakePlayerInfoCard()
 451    {
 0452        var newModel = ownUserProfile.CloneModel();
 0453        newModel.name = "FakePassport";
 0454        newModel.description = "Fake Description for Testing";
 0455        newModel.userId = "test-id";
 0456        newModel.inventory = new[]
 457        {
 458            "dcl://halloween_2019/machete_headband_top_head",
 459            "dcl://halloween_2019/bee_suit_upper_body",
 460            "dcl://halloween_2019/bride_of_frankie_upper_body",
 461            "dcl://halloween_2019/creepy_nurse_upper_body",
 462        };
 463
 0464        UserProfileController.i.AddUserProfileToCatalog(newModel);
 0465        Resources.Load<StringVariable>("CurrentPlayerInfoCardId").Set(newModel.userId);
 0466    }
 467#endif
 1108468    public void Dispose() { Cleanup(); }
 469}