< Summary

Class:HUDController
Assembly:HUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/HUDController.cs
Covered lines:179
Uncovered lines:44
Coverable lines:223
Total lines:479
Line coverage:80.2% (179 of 223)
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%59.4556089.68%
OpenPrivateChatWindow(...)0%6200%
View_OnDeactivatePreview()0%6200%
PrivateChatWindowHud_OnPressBack()0%6200%
TaskbarHud_onAnyTaskbarButtonClicked()0%220100%
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
 76525    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    {
 76534        i = this;
 76535        this.hudFactory = hudFactory;
 36
 76537        toggleUIVisibilityTrigger = Resources.Load<InputAction_Trigger>(TOGGLE_UI_VISIBILITY_ASSET_NAME);
 76538        toggleUIVisibilityTrigger.OnTriggered += ToggleUIVisibility_OnTriggered;
 39
 76540        CommonScriptableObjects.allUIHidden.OnChange += AllUIHiddenOnOnChange;
 76541        UserContextMenu.OnOpenPrivateChatRequest += OpenPrivateChatWindow;
 76542    }
 43
 44    public event Action OnBuilderProjectPanelCreation;
 45
 2546    public ProfileHUDController profileHud => GetHUDElement(HUDElementID.PROFILE_HUD) as ProfileHUDController;
 47
 48    public NotificationHUDController notificationHud =>
 349        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 ExpressionsHUDController expressionsHud =>
 059        GetHUDElement(HUDElementID.EXPRESSIONS) as ExpressionsHUDController;
 60
 61    public PlayerInfoCardHUDController playerInfoCardHud =>
 162        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
 99373    public TaskbarHUDController taskbarHud => GetHUDElement(HUDElementID.TASKBAR) as TaskbarHUDController;
 74
 75    public WorldChatWindowHUDController worldChatWindowHud =>
 80676        GetHUDElement(HUDElementID.WORLD_CHAT_WINDOW) as WorldChatWindowHUDController;
 77
 78    public PrivateChatWindowHUDController privateChatWindowHud =>
 79879        GetHUDElement(HUDElementID.PRIVATE_CHAT_WINDOW) as PrivateChatWindowHUDController;
 80
 80481    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
 92
 238993    public BuildModeHUDController builderInWorldMainHud => GetHUDElement(HUDElementID.BUILDER_IN_WORLD_MAIN) as BuildMod
 94
 195    public QuestsPanelHUDController questsPanelHUD => GetHUDElement(HUDElementID.QUESTS_PANEL) as QuestsPanelHUDControll
 196    public QuestsTrackerHUDController questsTrackerHUD => GetHUDElement(HUDElementID.QUESTS_TRACKER) as QuestsTrackerHUD
 197    public SignupHUDController signupHUD => GetHUDElement(HUDElementID.SIGNUP) as SignupHUDController;
 2698    public BuilderProjectsPanelController builderProjectsPanelController => GetHUDElement(HUDElementID.BUILDER_PROJECTS_
 199    public LoadingHUDController loadingController => GetHUDElement(HUDElementID.LOADING) as LoadingHUDController;
 100
 765101    public Dictionary<HUDElementID, IHUD> hudElements { get; private set; } = new Dictionary<HUDElementID, IHUD>();
 102
 1103    private UserProfile ownUserProfile => UserProfile.GetOwnUserProfile();
 0104    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            DCL.NavmapView.isOpen ||
 121            CommonScriptableObjects.tutorialActive)
 0122            return;
 123
 0124        CommonScriptableObjects.allUIHidden.Set(!CommonScriptableObjects.allUIHidden.Get());
 0125    }
 126
 127    private void AllUIHiddenOnOnChange(bool current, bool previous)
 128    {
 44129        if (current)
 130        {
 22131            NotificationsController.i?.ShowNotification(hiddenUINotification);
 22132        }
 133        else
 134        {
 22135            NotificationsController.i?.DismissAllNotifications(hiddenUINotification.groupID);
 136        }
 22137    }
 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                if (profileHud != null)
 159                {
 160                    //TODO This coupling might introduce a race condition if kernel configures this HUD before AvatarEdi
 1161                    profileHud?.AddBackpackWindow(avatarEditorHud);
 162                }
 163
 1164                break;
 165            case HUDElementID.NOTIFICATION:
 1166                CreateHudElement(configuration, hudElementId);
 1167                NotificationsController.i?.Initialize(notificationHud);
 1168                break;
 169            case HUDElementID.AVATAR_EDITOR:
 1170                CreateHudElement(configuration, hudElementId);
 1171                if (avatarEditorHud != null)
 172                {
 1173                    avatarEditorHud.Initialize(ownUserProfile, wearableCatalog);
 174                }
 175
 1176                break;
 177            case HUDElementID.SETTINGS_PANEL:
 1178                CreateHudElement(configuration, hudElementId);
 1179                if (settingsPanelHud != null)
 1180                    settingsPanelHud.Initialize();
 1181                break;
 182            case HUDElementID.EXPRESSIONS:
 1183                CreateHudElement(configuration, hudElementId);
 1184                break;
 185            case HUDElementID.PLAYER_INFO_CARD:
 1186                CreateHudElement(configuration, hudElementId);
 1187                break;
 188            case HUDElementID.AIRDROPPING:
 1189                CreateHudElement(configuration, hudElementId);
 1190                break;
 191            case HUDElementID.TERMS_OF_SERVICE:
 1192                CreateHudElement(configuration, hudElementId);
 1193                break;
 194            case HUDElementID.WORLD_CHAT_WINDOW:
 1195                if (worldChatWindowHud == null)
 196                {
 1197                    CreateHudElement(configuration, hudElementId);
 198
 1199                    if (worldChatWindowHud != null)
 200                    {
 1201                        worldChatWindowHud.Initialize(ChatController.i, DCL.InitialSceneReferences.i?.mouseCatcher);
 1202                        worldChatWindowHud.OnPressPrivateMessage -= OpenPrivateChatWindow;
 1203                        worldChatWindowHud.OnPressPrivateMessage += OpenPrivateChatWindow;
 1204                        worldChatWindowHud.view.OnDeactivatePreview -= View_OnDeactivatePreview;
 1205                        worldChatWindowHud.view.OnDeactivatePreview += View_OnDeactivatePreview;
 206
 1207                        taskbarHud?.AddWorldChatWindow(worldChatWindowHud);
 208                    }
 0209                }
 210                else
 211                {
 0212                    UpdateHudElement(configuration, hudElementId);
 213                }
 214
 0215                break;
 216            case HUDElementID.FRIENDS:
 1217                if (friendsHud == null)
 218                {
 1219                    CreateHudElement(configuration, hudElementId);
 220
 1221                    if (friendsHud != null)
 222                    {
 1223                        friendsHud.Initialize(FriendsController.i, UserProfile.GetOwnUserProfile());
 1224                        friendsHud.OnPressWhisper -= OpenPrivateChatWindow;
 1225                        friendsHud.OnPressWhisper += OpenPrivateChatWindow;
 226
 1227                        taskbarHud?.AddFriendsWindow(friendsHud);
 228                    }
 1229                }
 230                else
 231                {
 0232                    UpdateHudElement(configuration, hudElementId);
 233
 0234                    if (!configuration.active)
 0235                        taskbarHud?.DisableFriendsWindow();
 236                }
 237
 1238                if (privateChatWindowHud == null)
 239                {
 1240                    CreateHudElement(configuration, HUDElementID.PRIVATE_CHAT_WINDOW);
 241
 1242                    if (privateChatWindowHud != null)
 243                    {
 1244                        privateChatWindowHud.Initialize(ChatController.i);
 1245                        privateChatWindowHud.OnPressBack -= PrivateChatWindowHud_OnPressBack;
 1246                        privateChatWindowHud.OnPressBack += PrivateChatWindowHud_OnPressBack;
 247
 1248                        taskbarHud?.AddPrivateChatWindow(privateChatWindowHud);
 249                    }
 250                }
 251
 1252                break;
 253            case HUDElementID.TASKBAR:
 1254                if (taskbarHud == null)
 255                {
 1256                    CreateHudElement(configuration, hudElementId);
 257
 1258                    if (taskbarHud != null)
 259                    {
 1260                        taskbarHud.Initialize(
 261                            InitialSceneReferences.i?.mouseCatcher,
 262                            ChatController.i,
 263                            FriendsController.i,
 264                            DCL.Environment.i.world.sceneController,
 265                            DCL.Environment.i.world.state);
 1266                        taskbarHud.OnAnyTaskbarButtonClicked -= TaskbarHud_onAnyTaskbarButtonClicked;
 1267                        taskbarHud.OnAnyTaskbarButtonClicked += TaskbarHud_onAnyTaskbarButtonClicked;
 268
 1269                        if (!string.IsNullOrEmpty(extraPayload))
 270                        {
 0271                            var config = JsonUtility.FromJson<TaskbarHUDController.Configuration>(extraPayload);
 0272                            if (config.enableVoiceChat)
 273                            {
 0274                                taskbarHud.OnAddVoiceChat();
 275                            }
 276
 0277                            taskbarHud.SetQuestsPanelStatus(config.enableQuestPanel);
 278                        }
 279
 1280                        taskbarHud.AddSettingsWindow(settingsPanelHud);
 281                    }
 1282                }
 283                else
 284                {
 0285                    UpdateHudElement(configuration, hudElementId);
 286                }
 287
 0288                break;
 289            case HUDElementID.MESSAGE_OF_THE_DAY:
 1290                CreateHudElement(configuration, hudElementId);
 1291                messageOfTheDayHud?.Initialize(JsonUtility.FromJson<MessageOfTheDayConfig>(extraPayload));
 1292                break;
 293            case HUDElementID.OPEN_EXTERNAL_URL_PROMPT:
 1294                CreateHudElement(configuration, hudElementId);
 1295                break;
 296            case HUDElementID.NFT_INFO_DIALOG:
 1297                CreateHudElement(configuration, hudElementId);
 1298                break;
 299            case HUDElementID.TELEPORT_DIALOG:
 1300                CreateHudElement(configuration, hudElementId);
 1301                break;
 302            case HUDElementID.CONTROLS_HUD:
 1303                CreateHudElement(configuration, hudElementId);
 1304                taskbarHud?.AddControlsMoreOption();
 1305                break;
 306            case HUDElementID.EXPLORE_HUD:
 1307                CreateHudElement(configuration, hudElementId);
 1308                if (exploreHud != null)
 309                {
 1310                    exploreHud.Initialize(FriendsController.i);
 1311                    taskbarHud?.AddExploreWindow(exploreHud);
 312                }
 313
 1314                break;
 315            case HUDElementID.HELP_AND_SUPPORT_HUD:
 1316                CreateHudElement(configuration, hudElementId);
 1317                taskbarHud?.AddHelpAndSupportWindow(helpAndSupportHud);
 1318                break;
 319            case HUDElementID.USERS_AROUND_LIST_HUD:
 1320                CreateHudElement(configuration, hudElementId);
 1321                if (usersAroundListHud != null)
 322                {
 1323                    minimapHud?.AddUsersAroundIndicator(usersAroundListHud);
 324                }
 325
 1326                break;
 327            case HUDElementID.GRAPHIC_CARD_WARNING:
 1328                CreateHudElement(configuration, hudElementId);
 1329                break;
 330            case HUDElementID.BUILDER_IN_WORLD_MAIN:
 1331                CreateHudElement(configuration, hudElementId);
 1332                if (configuration.active)
 1333                    builderInWorldMainHud.Initialize();
 1334                break;
 335            case HUDElementID.QUESTS_PANEL:
 1336                CreateHudElement(configuration, hudElementId);
 1337                if (configuration.active)
 1338                    questsPanelHUD.Initialize(QuestsController.i);
 1339                break;
 340            case HUDElementID.QUESTS_TRACKER:
 1341                CreateHudElement(configuration, hudElementId);
 1342                if (configuration.active)
 1343                    questsTrackerHUD.Initialize(QuestsController.i);
 1344                break;
 345            case HUDElementID.SIGNUP:
 1346                CreateHudElement(configuration, hudElementId);
 1347                if (configuration.active)
 348                {
 349                    //Same race condition risks as with the ProfileHUD
 350                    //TODO Refactor the way AvatarEditor sets its visibility to match our data driven pattern
 351                    //Then this reference can be removed so we just work with a BaseVariable<bool>.
 352                    //This refactor applies to the ProfileHUD and the way kernel asks the HUDController during signup
 1353                    signupHUD.Initialize(avatarEditorHud);
 354                }
 1355                break;
 356            case HUDElementID.BUILDER_PROJECTS_PANEL:
 1357                CreateHudElement(configuration, hudElementId);
 1358                if (configuration.active)
 359                {
 1360                    builderProjectsPanelController.Initialize();
 1361                    taskbarHud.SetBuilderInWorldStatus(true);
 362                }
 1363                OnBuilderProjectPanelCreation?.Invoke();
 0364                break;
 365            case HUDElementID.LOADING:
 1366                CreateHudElement(configuration, hudElementId);
 1367                if (configuration.active)
 1368                    loadingController.Initialize();
 369                break;
 370            case HUDElementID.AVATAR_NAMES:
 371                // TODO Remove the HUDElementId once kernel stops sending the Configure HUD message
 372                break;
 373        }
 374
 31375        var hudElement = GetHUDElement(hudElementId);
 376
 31377        if (hudElement != null)
 28378            hudElement.SetVisibility(configuration.active && configuration.visible);
 31379    }
 380
 0381    private void OpenPrivateChatWindow(string targetUserId) { taskbarHud?.OpenPrivateChatTo(targetUserId); }
 382
 0383    private void View_OnDeactivatePreview() { playerInfoCardHud?.CloseCard(); }
 384
 0385    private void PrivateChatWindowHud_OnPressBack() { taskbarHud?.OpenFriendsWindow(); }
 386
 2387    private void TaskbarHud_onAnyTaskbarButtonClicked() { playerInfoCardHud?.CloseCard(); }
 388
 389    public void CreateHudElement(HUDConfiguration config, HUDElementID id)
 390    {
 53391        bool controllerCreated = hudElements.ContainsKey(id);
 392
 53393        if (config.active && !controllerCreated)
 394        {
 53395            hudElements.Add(id, hudFactory.CreateHUD(id));
 396
 53397            if (VERBOSE)
 0398                Debug.Log($"Adding {id} .. type {hudElements[id].GetType().Name}");
 399        }
 53400    }
 401
 402    public void UpdateHudElement(HUDConfiguration config, HUDElementID id)
 403    {
 0404        if (!hudElements.ContainsKey(id))
 0405            return;
 406
 0407        if (VERBOSE)
 0408            Debug.Log($"Updating {id}, type {hudElements[id].GetType().Name}, active: {config.active} visible: {config.v
 409
 0410        hudElements[id].SetVisibility(config.visible);
 0411    }
 412    public void Cleanup()
 413    {
 791414        toggleUIVisibilityTrigger.OnTriggered -= ToggleUIVisibility_OnTriggered;
 791415        CommonScriptableObjects.allUIHidden.OnChange -= AllUIHiddenOnOnChange;
 416
 791417        if (worldChatWindowHud != null)
 418        {
 1419            worldChatWindowHud.OnPressPrivateMessage -= OpenPrivateChatWindow;
 1420            worldChatWindowHud.view.OnDeactivatePreview -= View_OnDeactivatePreview;
 421        }
 422
 791423        if (privateChatWindowHud != null)
 1424            privateChatWindowHud.OnPressBack -= PrivateChatWindowHud_OnPressBack;
 425
 791426        if (friendsHud != null)
 1427            friendsHud.OnPressWhisper -= OpenPrivateChatWindow;
 428
 791429        if (taskbarHud != null)
 1430            taskbarHud.OnAnyTaskbarButtonClicked -= TaskbarHud_onAnyTaskbarButtonClicked;
 431
 791432        UserContextMenu.OnOpenPrivateChatRequest -= OpenPrivateChatWindow;
 433
 1688434        foreach (var kvp in hudElements)
 435        {
 53436            kvp.Value?.Dispose();
 437        }
 438
 791439        hudElements.Clear();
 791440    }
 441
 442    public IHUD GetHUDElement(HUDElementID id)
 443    {
 6000444        if (!hudElements.ContainsKey(id))
 4065445            return null;
 446
 1935447        return hudElements[id];
 448    }
 449
 450    public static bool IsHUDElementDeprecated(HUDElementID element)
 451    {
 31452        Type enumType = typeof(HUDElementID);
 31453        var enumName = enumType.GetEnumName(element);
 31454        var fieldInfo = enumType.GetField(enumName);
 31455        return Attribute.IsDefined(fieldInfo, typeof(ObsoleteAttribute));
 456    }
 457
 458#if UNITY_EDITOR
 459    [ContextMenu("Trigger fake PlayerInfoCard")]
 460    public void TriggerFakePlayerInfoCard()
 461    {
 0462        var newModel = ownUserProfile.CloneModel();
 0463        newModel.name = "FakePassport";
 0464        newModel.description = "Fake Description for Testing";
 0465        newModel.userId = "test-id";
 0466        newModel.inventory = new[]
 467        {
 468            "dcl://halloween_2019/machete_headband_top_head",
 469            "dcl://halloween_2019/bee_suit_upper_body",
 470            "dcl://halloween_2019/bride_of_frankie_upper_body",
 471            "dcl://halloween_2019/creepy_nurse_upper_body",
 472        };
 473
 0474        UserProfileController.i.AddUserProfileToCatalog(newModel);
 0475        Resources.Load<StringVariable>("CurrentPlayerInfoCardId").Set(newModel.userId);
 0476    }
 477#endif
 1572478    public void Dispose() { Cleanup(); }
 479}