< Summary

Class:HUDController
Assembly:HUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/HUDController.cs
Covered lines:182
Uncovered lines:46
Coverable lines:228
Total lines:485
Line coverage:79.8% (182 of 228)
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%60.2557090%
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 AvatarNamesHUD;
 10using LoadingHUD;
 11using SignupHUD;
 12using UnityEngine;
 13using UnityEngine.EventSystems;
 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
 75826    private readonly Notification.Model hiddenUINotification = new Notification.Model()
 27    {
 28        timer = 3,
 29        type = NotificationFactory.Type.UI_HIDDEN,
 30        groupID = "UIHiddenNotification"
 31    };
 32
 33    public void Initialize(IHUDFactory hudFactory)
 34    {
 75835        i = this;
 75836        this.hudFactory = hudFactory;
 37
 75838        toggleUIVisibilityTrigger = Resources.Load<InputAction_Trigger>(TOGGLE_UI_VISIBILITY_ASSET_NAME);
 75839        toggleUIVisibilityTrigger.OnTriggered += ToggleUIVisibility_OnTriggered;
 40
 75841        CommonScriptableObjects.allUIHidden.OnChange += AllUIHiddenOnOnChange;
 75842        UserContextMenu.OnOpenPrivateChatRequest += OpenPrivateChatWindow;
 75843    }
 44
 45    public event Action OnBuilderProjectPanelCreation;
 46
 2547    public ProfileHUDController profileHud => GetHUDElement(HUDElementID.PROFILE_HUD) as ProfileHUDController;
 48
 49    public NotificationHUDController notificationHud =>
 350        GetHUDElement(HUDElementID.NOTIFICATION) as NotificationHUDController;
 51
 952    public MinimapHUDController minimapHud => GetHUDElement(HUDElementID.MINIMAP) as MinimapHUDController;
 53
 54    public AvatarEditorHUDController avatarEditorHud =>
 455        GetHUDElement(HUDElementID.AVATAR_EDITOR) as AvatarEditorHUDController;
 56
 357    public SettingsPanelHUDController settingsPanelHud => GetHUDElement(HUDElementID.SETTINGS_PANEL) as SettingsPanelHUD
 58
 59    public ExpressionsHUDController expressionsHud =>
 060        GetHUDElement(HUDElementID.EXPRESSIONS) as ExpressionsHUDController;
 61
 62    public PlayerInfoCardHUDController playerInfoCardHud =>
 063        GetHUDElement(HUDElementID.PLAYER_INFO_CARD) as PlayerInfoCardHUDController;
 64
 65    public WelcomeHUDController messageOfTheDayHud =>
 166        GetHUDElement(HUDElementID.MESSAGE_OF_THE_DAY) as WelcomeHUDController;
 67
 68    public AirdroppingHUDController airdroppingHud =>
 069        GetHUDElement(HUDElementID.AIRDROPPING) as AirdroppingHUDController;
 70
 71    public TermsOfServiceHUDController termsOfServiceHud =>
 072        GetHUDElement(HUDElementID.TERMS_OF_SERVICE) as TermsOfServiceHUDController;
 73
 133974    public TaskbarHUDController taskbarHud => GetHUDElement(HUDElementID.TASKBAR) as TaskbarHUDController;
 75
 76    public WorldChatWindowHUDController worldChatWindowHud =>
 79877        GetHUDElement(HUDElementID.WORLD_CHAT_WINDOW) as WorldChatWindowHUDController;
 78
 79    public PrivateChatWindowHUDController privateChatWindowHud =>
 79080        GetHUDElement(HUDElementID.PRIVATE_CHAT_WINDOW) as PrivateChatWindowHUDController;
 81
 79682    public FriendsHUDController friendsHud => GetHUDElement(HUDElementID.FRIENDS) as FriendsHUDController;
 83
 084    public TeleportPromptHUDController teleportHud => GetHUDElement(HUDElementID.TELEPORT_DIALOG) as TeleportPromptHUDCo
 85
 286    public ControlsHUDController controlsHud => GetHUDElement(HUDElementID.CONTROLS_HUD) as ControlsHUDController;
 87
 588    public ExploreHUDController exploreHud => GetHUDElement(HUDElementID.EXPLORE_HUD) as ExploreHUDController;
 89
 190    public HelpAndSupportHUDController helpAndSupportHud => GetHUDElement(HUDElementID.HELP_AND_SUPPORT_HUD) as HelpAndS
 91
 492    public UsersAroundListHUDController usersAroundListHud => GetHUDElement(HUDElementID.USERS_AROUND_LIST_HUD) as Users
 93
 247394    public BuildModeHUDController builderInWorldMainHud => GetHUDElement(HUDElementID.BUILDER_IN_WORLD_MAIN) as BuildMod
 95
 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;
 2799    public BuilderProjectsPanelController builderProjectsPanelController => GetHUDElement(HUDElementID.BUILDER_PROJECTS_
 1100    public LoadingHUDController loadingController => GetHUDElement(HUDElementID.LOADING) as LoadingHUDController;
 1101    public AvatarNamesHUDController avatarNamesController => GetHUDElement(HUDElementID.AVATAR_NAMES) as AvatarNamesHUDC
 102
 758103    public Dictionary<HUDElementID, IHUD> hudElements { get; private set; } = new Dictionary<HUDElementID, IHUD>();
 104
 1105    private UserProfile ownUserProfile => UserProfile.GetOwnUserProfile();
 0106    private BaseDictionary<string, WearableItem> wearableCatalog => CatalogController.wearableCatalog;
 107
 0108    private void ShowSettings() { settingsPanelHud?.SetVisibility(true); }
 109
 0110    private void ShowControls() { controlsHud?.SetVisibility(true); }
 111
 112    private void ToggleUIVisibility_OnTriggered(DCLAction_Trigger action)
 113    {
 0114        bool anyInputFieldIsSelected = EventSystem.current != null &&
 115                                       EventSystem.current.currentSelectedGameObject != null &&
 116                                       EventSystem.current.currentSelectedGameObject.GetComponent<TMPro.TMP_InputField>(
 117                                       (!worldChatWindowHud.view.chatHudView.inputField.isFocused || !worldChatWindowHud
 118
 0119        if (anyInputFieldIsSelected ||
 120            settingsPanelHud.view.isOpen ||
 121            avatarEditorHud.view.isOpen ||
 122            DCL.NavmapView.isOpen ||
 123            CommonScriptableObjects.tutorialActive)
 0124            return;
 125
 0126        CommonScriptableObjects.allUIHidden.Set(!CommonScriptableObjects.allUIHidden.Get());
 0127    }
 128
 129    private void AllUIHiddenOnOnChange(bool current, bool previous)
 130    {
 8131        if (current)
 132        {
 4133            NotificationsController.i?.ShowNotification(hiddenUINotification);
 4134        }
 135        else
 136        {
 4137            NotificationsController.i?.DismissAllNotifications(hiddenUINotification.groupID);
 138        }
 4139    }
 140
 141    public void ConfigureHUDElement(HUDElementID hudElementId, HUDConfiguration configuration, string extraPayload = nul
 142    {
 143        //TODO(Brian): For now, the factory code is using this switch approach.
 144        //             In order to avoid the factory upkeep we can transform the IHUD elements
 145        //             To ScriptableObjects. In this scenario, we can make each element handle its own
 146        //             specific initialization details.
 147        //
 148        //             This will allow us to unify the serialized factory objects design,
 149        //             like we already do with ECS components.
 150
 151        switch (hudElementId)
 152        {
 153            case HUDElementID.NONE:
 154                break;
 155            case HUDElementID.MINIMAP:
 1156                CreateHudElement(configuration, hudElementId);
 1157                break;
 158            case HUDElementID.PROFILE_HUD:
 1159                CreateHudElement(configuration, hudElementId);
 1160                if (profileHud != null)
 161                {
 162                    //TODO This coupling might introduce a race condition if kernel configures this HUD before AvatarEdi
 1163                    profileHud?.AddBackpackWindow(avatarEditorHud);
 164                }
 165
 1166                break;
 167            case HUDElementID.NOTIFICATION:
 1168                CreateHudElement(configuration, hudElementId);
 1169                NotificationsController.i?.Initialize(notificationHud);
 1170                break;
 171            case HUDElementID.AVATAR_EDITOR:
 1172                CreateHudElement(configuration, hudElementId);
 1173                if (avatarEditorHud != null)
 174                {
 1175                    avatarEditorHud.Initialize(ownUserProfile, wearableCatalog);
 176                }
 177
 1178                break;
 179            case HUDElementID.SETTINGS_PANEL:
 1180                CreateHudElement(configuration, hudElementId);
 1181                if (settingsPanelHud != null)
 1182                    settingsPanelHud.Initialize();
 1183                break;
 184            case HUDElementID.EXPRESSIONS:
 1185                CreateHudElement(configuration, hudElementId);
 1186                break;
 187            case HUDElementID.PLAYER_INFO_CARD:
 1188                CreateHudElement(configuration, hudElementId);
 1189                break;
 190            case HUDElementID.AIRDROPPING:
 1191                CreateHudElement(configuration, hudElementId);
 1192                break;
 193            case HUDElementID.TERMS_OF_SERVICE:
 1194                CreateHudElement(configuration, hudElementId);
 1195                break;
 196            case HUDElementID.WORLD_CHAT_WINDOW:
 1197                if (worldChatWindowHud == null)
 198                {
 1199                    CreateHudElement(configuration, hudElementId);
 200
 1201                    if (worldChatWindowHud != null)
 202                    {
 1203                        worldChatWindowHud.Initialize(ChatController.i, DCL.InitialSceneReferences.i?.mouseCatcher);
 1204                        worldChatWindowHud.OnPressPrivateMessage -= OpenPrivateChatWindow;
 1205                        worldChatWindowHud.OnPressPrivateMessage += OpenPrivateChatWindow;
 1206                        worldChatWindowHud.view.OnDeactivatePreview -= View_OnDeactivatePreview;
 1207                        worldChatWindowHud.view.OnDeactivatePreview += View_OnDeactivatePreview;
 208
 1209                        taskbarHud?.AddWorldChatWindow(worldChatWindowHud);
 210                    }
 0211                }
 212                else
 213                {
 0214                    UpdateHudElement(configuration, hudElementId);
 215                }
 216
 0217                break;
 218            case HUDElementID.FRIENDS:
 1219                if (friendsHud == null)
 220                {
 1221                    CreateHudElement(configuration, hudElementId);
 222
 1223                    if (friendsHud != null)
 224                    {
 1225                        friendsHud.Initialize(FriendsController.i, UserProfile.GetOwnUserProfile());
 1226                        friendsHud.OnPressWhisper -= OpenPrivateChatWindow;
 1227                        friendsHud.OnPressWhisper += OpenPrivateChatWindow;
 228
 1229                        taskbarHud?.AddFriendsWindow(friendsHud);
 230                    }
 1231                }
 232                else
 233                {
 0234                    UpdateHudElement(configuration, hudElementId);
 235
 0236                    if (!configuration.active)
 0237                        taskbarHud?.DisableFriendsWindow();
 238                }
 239
 1240                if (privateChatWindowHud == null)
 241                {
 1242                    CreateHudElement(configuration, HUDElementID.PRIVATE_CHAT_WINDOW);
 243
 1244                    if (privateChatWindowHud != null)
 245                    {
 1246                        privateChatWindowHud.Initialize(ChatController.i);
 1247                        privateChatWindowHud.OnPressBack -= PrivateChatWindowHud_OnPressBack;
 1248                        privateChatWindowHud.OnPressBack += PrivateChatWindowHud_OnPressBack;
 249
 1250                        taskbarHud?.AddPrivateChatWindow(privateChatWindowHud);
 251                    }
 252                }
 253
 1254                break;
 255            case HUDElementID.TASKBAR:
 1256                if (taskbarHud == null)
 257                {
 1258                    CreateHudElement(configuration, hudElementId);
 259
 1260                    if (taskbarHud != null)
 261                    {
 1262                        taskbarHud.Initialize(
 263                            InitialSceneReferences.i?.mouseCatcher,
 264                            ChatController.i,
 265                            FriendsController.i,
 266                            DCL.Environment.i.world.sceneController,
 267                            DCL.Environment.i.world.state);
 1268                        taskbarHud.OnAnyTaskbarButtonClicked -= TaskbarHud_onAnyTaskbarButtonClicked;
 1269                        taskbarHud.OnAnyTaskbarButtonClicked += TaskbarHud_onAnyTaskbarButtonClicked;
 270
 1271                        if (!string.IsNullOrEmpty(extraPayload))
 272                        {
 0273                            var config = JsonUtility.FromJson<TaskbarHUDController.Configuration>(extraPayload);
 0274                            if (config.enableVoiceChat)
 275                            {
 0276                                taskbarHud.OnAddVoiceChat();
 277                            }
 278
 0279                            taskbarHud.SetQuestsPanelStatus(config.enableQuestPanel);
 280                        }
 281
 1282                        taskbarHud.AddSettingsWindow(settingsPanelHud);
 283                    }
 1284                }
 285                else
 286                {
 0287                    UpdateHudElement(configuration, hudElementId);
 288                }
 289
 0290                break;
 291            case HUDElementID.MESSAGE_OF_THE_DAY:
 1292                CreateHudElement(configuration, hudElementId);
 1293                messageOfTheDayHud?.Initialize(JsonUtility.FromJson<MessageOfTheDayConfig>(extraPayload));
 1294                break;
 295            case HUDElementID.OPEN_EXTERNAL_URL_PROMPT:
 1296                CreateHudElement(configuration, hudElementId);
 1297                break;
 298            case HUDElementID.NFT_INFO_DIALOG:
 1299                CreateHudElement(configuration, hudElementId);
 1300                break;
 301            case HUDElementID.TELEPORT_DIALOG:
 1302                CreateHudElement(configuration, hudElementId);
 1303                break;
 304            case HUDElementID.CONTROLS_HUD:
 1305                CreateHudElement(configuration, hudElementId);
 1306                taskbarHud?.AddControlsMoreOption();
 1307                break;
 308            case HUDElementID.EXPLORE_HUD:
 1309                CreateHudElement(configuration, hudElementId);
 1310                if (exploreHud != null)
 311                {
 1312                    exploreHud.Initialize(FriendsController.i);
 1313                    taskbarHud?.AddExploreWindow(exploreHud);
 314                }
 315
 1316                break;
 317            case HUDElementID.HELP_AND_SUPPORT_HUD:
 1318                CreateHudElement(configuration, hudElementId);
 1319                taskbarHud?.AddHelpAndSupportWindow(helpAndSupportHud);
 1320                break;
 321            case HUDElementID.USERS_AROUND_LIST_HUD:
 1322                CreateHudElement(configuration, hudElementId);
 1323                if (usersAroundListHud != null)
 324                {
 1325                    minimapHud?.AddUsersAroundIndicator(usersAroundListHud);
 326                }
 327
 1328                break;
 329            case HUDElementID.GRAPHIC_CARD_WARNING:
 1330                CreateHudElement(configuration, hudElementId);
 1331                break;
 332            case HUDElementID.BUILDER_IN_WORLD_MAIN:
 1333                CreateHudElement(configuration, hudElementId);
 1334                if (configuration.active)
 1335                    builderInWorldMainHud.Initialize();
 1336                break;
 337            case HUDElementID.QUESTS_PANEL:
 1338                CreateHudElement(configuration, hudElementId);
 1339                if (configuration.active)
 1340                    questsPanelHUD.Initialize(QuestsController.i);
 1341                break;
 342            case HUDElementID.QUESTS_TRACKER:
 1343                CreateHudElement(configuration, hudElementId);
 1344                if (configuration.active)
 1345                    questsTrackerHUD.Initialize(QuestsController.i);
 1346                break;
 347            case HUDElementID.SIGNUP:
 1348                CreateHudElement(configuration, hudElementId);
 1349                if (configuration.active)
 350                {
 351                    //Same race condition risks as with the ProfileHUD
 352                    //TODO Refactor the way AvatarEditor sets its visibility to match our data driven pattern
 353                    //Then this reference can be removed so we just work with a BaseVariable<bool>.
 354                    //This refactor applies to the ProfileHUD and the way kernel asks the HUDController during signup
 1355                    signupHUD.Initialize(avatarEditorHud);
 356                }
 1357                break;
 358            case HUDElementID.BUILDER_PROJECTS_PANEL:
 1359                CreateHudElement(configuration, hudElementId);
 1360                if (configuration.active)
 361                {
 1362                    builderProjectsPanelController.Initialize();
 1363                    taskbarHud.SetBuilderInWorldStatus(true);
 364                }
 1365                OnBuilderProjectPanelCreation?.Invoke();
 0366                break;
 367            case HUDElementID.LOADING:
 1368                CreateHudElement(configuration, hudElementId);
 1369                if (configuration.active)
 1370                    loadingController.Initialize();
 1371                break;
 372            case HUDElementID.AVATAR_NAMES:
 1373                CreateHudElement(configuration, hudElementId);
 1374                if (configuration.active)
 375                {
 1376                    avatarNamesController.Initialize();
 377                }
 378                break;
 379        }
 380
 31381        var hudElement = GetHUDElement(hudElementId);
 382
 31383        if (hudElement != null)
 29384            hudElement.SetVisibility(configuration.active && configuration.visible);
 31385    }
 386
 0387    private void OpenPrivateChatWindow(string targetUserId) { taskbarHud?.OpenPrivateChatTo(targetUserId); }
 388
 0389    private void View_OnDeactivatePreview() { playerInfoCardHud?.CloseCard(); }
 390
 0391    private void PrivateChatWindowHud_OnPressBack() { taskbarHud?.OpenFriendsWindow(); }
 392
 0393    private void TaskbarHud_onAnyTaskbarButtonClicked() { playerInfoCardHud?.CloseCard(); }
 394
 395    public void CreateHudElement(HUDConfiguration config, HUDElementID id)
 396    {
 55397        bool controllerCreated = hudElements.ContainsKey(id);
 398
 55399        if (config.active && !controllerCreated)
 400        {
 55401            hudElements.Add(id, hudFactory.CreateHUD(id));
 402
 55403            if (VERBOSE)
 0404                Debug.Log($"Adding {id} .. type {hudElements[id].GetType().Name}");
 405        }
 55406    }
 407
 408    public void UpdateHudElement(HUDConfiguration config, HUDElementID id)
 409    {
 0410        if (!hudElements.ContainsKey(id))
 0411            return;
 412
 0413        if (VERBOSE)
 0414            Debug.Log($"Updating {id}, type {hudElements[id].GetType().Name}, active: {config.active} visible: {config.v
 415
 0416        hudElements[id].SetVisibility(config.visible);
 0417    }
 418    public void Cleanup()
 419    {
 783420        toggleUIVisibilityTrigger.OnTriggered -= ToggleUIVisibility_OnTriggered;
 783421        CommonScriptableObjects.allUIHidden.OnChange -= AllUIHiddenOnOnChange;
 422
 783423        if (worldChatWindowHud != null)
 424        {
 1425            worldChatWindowHud.OnPressPrivateMessage -= OpenPrivateChatWindow;
 1426            worldChatWindowHud.view.OnDeactivatePreview -= View_OnDeactivatePreview;
 427        }
 428
 783429        if (privateChatWindowHud != null)
 1430            privateChatWindowHud.OnPressBack -= PrivateChatWindowHud_OnPressBack;
 431
 783432        if (friendsHud != null)
 1433            friendsHud.OnPressWhisper -= OpenPrivateChatWindow;
 434
 783435        if (taskbarHud != null)
 1436            taskbarHud.OnAnyTaskbarButtonClicked -= TaskbarHud_onAnyTaskbarButtonClicked;
 437
 783438        UserContextMenu.OnOpenPrivateChatRequest -= OpenPrivateChatWindow;
 439
 1676440        foreach (var kvp in hudElements)
 441        {
 55442            kvp.Value?.Dispose();
 443        }
 444
 783445        hudElements.Clear();
 783446    }
 447
 448    public IHUD GetHUDElement(HUDElementID id)
 449    {
 6408450        if (!hudElements.ContainsKey(id))
 4377451            return null;
 452
 2031453        return hudElements[id];
 454    }
 455
 456    public static bool IsHUDElementDeprecated(HUDElementID element)
 457    {
 31458        Type enumType = typeof(HUDElementID);
 31459        var enumName = enumType.GetEnumName(element);
 31460        var fieldInfo = enumType.GetField(enumName);
 31461        return Attribute.IsDefined(fieldInfo, typeof(ObsoleteAttribute));
 462    }
 463
 464#if UNITY_EDITOR
 465    [ContextMenu("Trigger fake PlayerInfoCard")]
 466    public void TriggerFakePlayerInfoCard()
 467    {
 0468        var newModel = ownUserProfile.CloneModel();
 0469        newModel.name = "FakePassport";
 0470        newModel.description = "Fake Description for Testing";
 0471        newModel.userId = "test-id";
 0472        newModel.inventory = new[]
 473        {
 474            "dcl://halloween_2019/machete_headband_top_head",
 475            "dcl://halloween_2019/bee_suit_upper_body",
 476            "dcl://halloween_2019/bride_of_frankie_upper_body",
 477            "dcl://halloween_2019/creepy_nurse_upper_body",
 478        };
 479
 0480        UserProfileController.i.AddUserProfileToCatalog(newModel);
 0481        Resources.Load<StringVariable>("CurrentPlayerInfoCardId").Set(newModel.userId);
 0482    }
 483#endif
 1556484    public void Dispose() { Cleanup(); }
 485}