< Summary

Class:HUDController
Assembly:HUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/HUDController.cs
Covered lines:161
Uncovered lines:45
Coverable lines:206
Total lines:456
Line coverage:78.1% (161 of 206)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
HUDController(...)0%110100%
Initialize()0%220100%
ShowSettings()0%6200%
ShowControls()0%6200%
ToggleUIVisibility_OnTriggered(...)0%1321100%
AllUIHiddenOnOnChange(...)0%440100%
ConfigureHUDElement(...)0%46.7344088.79%
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;
 13using Environment = System.Environment;
 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
 62926    private readonly DCL.NotificationModel.Model hiddenUINotification = new DCL.NotificationModel.Model()
 27    {
 28        timer = 3,
 29        type = DCL.NotificationModel.Type.UI_HIDDEN,
 30        groupID = "UIHiddenNotification"
 31    };
 32
 62933    public HUDController (IHUDFactory hudFactory = null)
 34    {
 62935        this.hudFactory = hudFactory;
 62936    }
 37
 38    public void Initialize()
 39    {
 62940        i = this;
 41
 62942        if ( this.hudFactory == null )
 62943            this.hudFactory = DCL.Environment.i.hud.factory;
 44
 62945        toggleUIVisibilityTrigger = Resources.Load<InputAction_Trigger>(TOGGLE_UI_VISIBILITY_ASSET_NAME);
 62946        toggleUIVisibilityTrigger.OnTriggered += ToggleUIVisibility_OnTriggered;
 47
 62948        CommonScriptableObjects.allUIHidden.OnChange += AllUIHiddenOnOnChange;
 62949        UserContextMenu.OnOpenPrivateChatRequest += OpenPrivateChatWindow;
 62950    }
 51
 52
 53    public event Action OnTaskbarCreation;
 54
 1055    public ProfileHUDController profileHud => GetHUDElement(HUDElementID.PROFILE_HUD) as ProfileHUDController;
 56
 57    public NotificationHUDController notificationHud =>
 158        GetHUDElement(HUDElementID.NOTIFICATION) as NotificationHUDController;
 59
 1060    public MinimapHUDController minimapHud => GetHUDElement(HUDElementID.MINIMAP) as MinimapHUDController;
 61
 62    public AvatarEditorHUDController avatarEditorHud =>
 263        GetHUDElement(HUDElementID.AVATAR_EDITOR) as AvatarEditorHUDController;
 64
 10865    public SettingsPanelHUDController settingsPanelHud => GetHUDElement(HUDElementID.SETTINGS_PANEL) as SettingsPanelHUD
 66
 67    public EmotesHUDController emotesHUD =>
 068        GetHUDElement(HUDElementID.EMOTES) as EmotesHUDController;
 69
 70    public PlayerInfoCardHUDController playerInfoCardHud =>
 071        GetHUDElement(HUDElementID.PLAYER_INFO_CARD) as PlayerInfoCardHUDController;
 72
 73    public AirdroppingHUDController airdroppingHud =>
 074        GetHUDElement(HUDElementID.AIRDROPPING) as AirdroppingHUDController;
 75
 76    public TermsOfServiceHUDController termsOfServiceHud =>
 077        GetHUDElement(HUDElementID.TERMS_OF_SERVICE) as TermsOfServiceHUDController;
 78
 65879    public TaskbarHUDController taskbarHud => GetHUDElement(HUDElementID.TASKBAR) as TaskbarHUDController;
 80
 281    public LoadingHUDController loadingHud => GetHUDElement(HUDElementID.LOADING) as LoadingHUDController;
 82
 83    public WorldChatWindowHUDController worldChatWindowHud =>
 64984        GetHUDElement(HUDElementID.WORLD_CHAT_WINDOW) as WorldChatWindowHUDController;
 85
 86    public PrivateChatWindowHUDController privateChatWindowHud =>
 64187        GetHUDElement(HUDElementID.PRIVATE_CHAT_WINDOW) as PrivateChatWindowHUDController;
 88
 64789    public FriendsHUDController friendsHud => GetHUDElement(HUDElementID.FRIENDS) as FriendsHUDController;
 90
 091    public TeleportPromptHUDController teleportHud => GetHUDElement(HUDElementID.TELEPORT_DIALOG) as TeleportPromptHUDCo
 92
 293    public ControlsHUDController controlsHud => GetHUDElement(HUDElementID.CONTROLS_HUD) as ControlsHUDController;
 94
 195    public HelpAndSupportHUDController helpAndSupportHud => GetHUDElement(HUDElementID.HELP_AND_SUPPORT_HUD) as HelpAndS
 96
 097    public MinimapHUDController minimapHUD => GetHUDElement(HUDElementID.MINIMAP) as MinimapHUDController;
 98
 299    public UsersAroundListHUDController usersAroundListHud => GetHUDElement(HUDElementID.USERS_AROUND_LIST_HUD) as Users
 1100    public QuestsPanelHUDController questsPanelHUD => GetHUDElement(HUDElementID.QUESTS_PANEL) as QuestsPanelHUDControll
 1101    public QuestsTrackerHUDController questsTrackerHUD => GetHUDElement(HUDElementID.QUESTS_TRACKER) as QuestsTrackerHUD
 1102    public SignupHUDController signupHUD => GetHUDElement(HUDElementID.SIGNUP) as SignupHUDController;
 1103    public LoadingHUDController loadingController => GetHUDElement(HUDElementID.LOADING) as LoadingHUDController;
 104
 629105    public Dictionary<HUDElementID, IHUD> hudElements { get; private set; } = new Dictionary<HUDElementID, IHUD>();
 106
 1107    private UserProfile ownUserProfile => UserProfile.GetOwnUserProfile();
 1108    private BaseDictionary<string, WearableItem> wearableCatalog => CatalogController.wearableCatalog;
 109
 0110    private void ShowSettings() { settingsPanelHud?.SetVisibility(true); }
 111
 0112    private void ShowControls() { controlsHud?.SetVisibility(true); }
 113
 114    private void ToggleUIVisibility_OnTriggered(DCLAction_Trigger action)
 115    {
 0116        bool anyInputFieldIsSelected = EventSystem.current != null &&
 117                                       EventSystem.current.currentSelectedGameObject != null &&
 118                                       EventSystem.current.currentSelectedGameObject.GetComponent<TMPro.TMP_InputField>(
 119                                       (!worldChatWindowHud.view.chatHudView.inputField.isFocused || !worldChatWindowHud
 120
 0121        if (anyInputFieldIsSelected ||
 122            settingsPanelHud.view.isOpen ||
 123            avatarEditorHud.view.isOpen ||
 124            DataStore.i.HUDs.navmapVisible.Get() ||
 125            CommonScriptableObjects.tutorialActive)
 0126            return;
 127
 0128        CommonScriptableObjects.allUIHidden.Set(!CommonScriptableObjects.allUIHidden.Get());
 0129    }
 130
 131    private void AllUIHiddenOnOnChange(bool current, bool previous)
 132    {
 4133        if (current)
 134        {
 2135            NotificationsController.i?.ShowNotification(hiddenUINotification);
 2136        }
 137        else
 138        {
 2139            NotificationsController.i?.DismissAllNotifications(hiddenUINotification.groupID);
 140        }
 2141    }
 142
 143    public void ConfigureHUDElement(HUDElementID hudElementId, HUDConfiguration configuration, string extraPayload = nul
 144    {
 145        //TODO(Brian): For now, the factory code is using this switch approach.
 146        //             In order to avoid the factory upkeep we can transform the IHUD elements
 147        //             To ScriptableObjects. In this scenario, we can make each element handle its own
 148        //             specific initialization details.
 149        //
 150        //             This will allow us to unify the serialized factory objects design,
 151        //             like we already do with ECS components.
 152
 153        switch (hudElementId)
 154        {
 155            case HUDElementID.NONE:
 156                break;
 157            case HUDElementID.MINIMAP:
 1158                if (minimapHud == null)
 159                {
 1160                    CreateHudElement(configuration, hudElementId);
 161
 1162                    if (minimapHud != null)
 163                    {
 1164                        minimapHud.Initialize();
 165                    }
 166                }
 1167                break;
 168            case HUDElementID.PROFILE_HUD:
 1169                CreateHudElement(configuration, hudElementId);
 1170                break;
 171            case HUDElementID.NOTIFICATION:
 1172                CreateHudElement(configuration, hudElementId);
 1173                NotificationsController.i?.Initialize(notificationHud);
 1174                break;
 175            case HUDElementID.AVATAR_EDITOR:
 1176                CreateHudElement(configuration, hudElementId);
 1177                avatarEditorHud?.Initialize(ownUserProfile, wearableCatalog);
 1178                break;
 179            case HUDElementID.SETTINGS_PANEL:
 1180                CreateHudElement(configuration, hudElementId);
 1181                if (settingsPanelHud != null)
 1182                    settingsPanelHud.Initialize();
 1183                break;
 184            case HUDElementID.EXPRESSIONS:
 185            case HUDElementID.EMOTES:
 2186                CreateHudElement(configuration, hudElementId);
 2187                break;
 188            case HUDElementID.PLAYER_INFO_CARD:
 1189                CreateHudElement(configuration, hudElementId);
 1190                break;
 191            case HUDElementID.AIRDROPPING:
 1192                CreateHudElement(configuration, hudElementId);
 1193                break;
 194            case HUDElementID.TERMS_OF_SERVICE:
 1195                CreateHudElement(configuration, hudElementId);
 1196                break;
 197            case HUDElementID.WORLD_CHAT_WINDOW:
 1198                if (worldChatWindowHud == null)
 199                {
 1200                    CreateHudElement(configuration, hudElementId);
 201
 1202                    if (worldChatWindowHud != null)
 203                    {
 1204                        worldChatWindowHud.Initialize(ChatController.i, SceneReferences.i.mouseCatcher);
 1205                        worldChatWindowHud.OnPressPrivateMessage -= OpenPrivateChatWindow;
 1206                        worldChatWindowHud.OnPressPrivateMessage += OpenPrivateChatWindow;
 1207                        worldChatWindowHud.view.OnDeactivatePreview -= View_OnDeactivatePreview;
 1208                        worldChatWindowHud.view.OnDeactivatePreview += View_OnDeactivatePreview;
 209
 1210                        taskbarHud?.AddWorldChatWindow(worldChatWindowHud);
 211                    }
 0212                }
 213                else
 214                {
 0215                    UpdateHudElement(configuration, hudElementId);
 216                }
 217
 0218                break;
 219            case HUDElementID.FRIENDS:
 1220                if (friendsHud == null)
 221                {
 1222                    CreateHudElement(configuration, hudElementId);
 223
 1224                    if (friendsHud != null)
 225                    {
 1226                        friendsHud.Initialize(FriendsController.i, UserProfile.GetOwnUserProfile());
 1227                        friendsHud.OnPressWhisper -= OpenPrivateChatWindow;
 1228                        friendsHud.OnPressWhisper += OpenPrivateChatWindow;
 229
 1230                        taskbarHud?.AddFriendsWindow(friendsHud);
 231                    }
 1232                }
 233                else
 234                {
 0235                    UpdateHudElement(configuration, hudElementId);
 236
 0237                    if (!configuration.active)
 0238                        taskbarHud?.DisableFriendsWindow();
 239                }
 240
 1241                if (privateChatWindowHud == null)
 242                {
 1243                    CreateHudElement(configuration, HUDElementID.PRIVATE_CHAT_WINDOW);
 244
 1245                    if (privateChatWindowHud != null)
 246                    {
 1247                        privateChatWindowHud.Initialize(ChatController.i);
 1248                        privateChatWindowHud.OnPressBack -= PrivateChatWindowHud_OnPressBack;
 1249                        privateChatWindowHud.OnPressBack += PrivateChatWindowHud_OnPressBack;
 250
 1251                        taskbarHud?.AddPrivateChatWindow(privateChatWindowHud);
 252                    }
 253                }
 254
 1255                break;
 256            case HUDElementID.TASKBAR:
 1257                if (taskbarHud == null)
 258                {
 1259                    CreateHudElement(configuration, hudElementId);
 260
 1261                    if (taskbarHud != null)
 262                    {
 1263                        taskbarHud.Initialize(
 264                            SceneReferences.i.mouseCatcher,
 265                            ChatController.i,
 266                            FriendsController.i);
 1267                        taskbarHud.OnAnyTaskbarButtonClicked -= TaskbarHud_onAnyTaskbarButtonClicked;
 1268                        taskbarHud.OnAnyTaskbarButtonClicked += TaskbarHud_onAnyTaskbarButtonClicked;
 269
 1270                        if (!string.IsNullOrEmpty(extraPayload))
 271                        {
 0272                            var config = JsonUtility.FromJson<TaskbarHUDController.Configuration>(extraPayload);
 0273                            if (config.enableVoiceChat)
 274                            {
 0275                                taskbarHud.OnAddVoiceChat();
 276                            }
 277                        }
 278
 1279                        OnTaskbarCreation?.Invoke();
 280                    }
 0281                }
 282                else
 283                {
 0284                    UpdateHudElement(configuration, hudElementId);
 285                }
 286
 0287                break;
 288            case HUDElementID.OPEN_EXTERNAL_URL_PROMPT:
 1289                CreateHudElement(configuration, hudElementId);
 1290                break;
 291            case HUDElementID.NFT_INFO_DIALOG:
 1292                CreateHudElement(configuration, hudElementId);
 1293                break;
 294            case HUDElementID.TELEPORT_DIALOG:
 1295                CreateHudElement(configuration, hudElementId);
 1296                break;
 297            case HUDElementID.CONTROLS_HUD:
 1298                CreateHudElement(configuration, hudElementId);
 1299                break;
 300            case HUDElementID.HELP_AND_SUPPORT_HUD:
 1301                CreateHudElement(configuration, hudElementId);
 1302                settingsPanelHud?.AddHelpAndSupportWindow(helpAndSupportHud);
 1303                break;
 304            case HUDElementID.USERS_AROUND_LIST_HUD:
 1305                CreateHudElement(configuration, hudElementId);
 1306                if (usersAroundListHud != null)
 307                {
 1308                    minimapHud?.AddUsersAroundIndicator(usersAroundListHud);
 309                }
 310
 1311                break;
 312            case HUDElementID.GRAPHIC_CARD_WARNING:
 1313                CreateHudElement(configuration, hudElementId);
 1314                break;
 315            case HUDElementID.QUESTS_PANEL:
 1316                CreateHudElement(configuration, hudElementId);
 1317                if (configuration.active)
 1318                    questsPanelHUD.Initialize(QuestsController.i);
 1319                break;
 320            case HUDElementID.QUESTS_TRACKER:
 1321                CreateHudElement(configuration, hudElementId);
 1322                if (configuration.active)
 1323                    questsTrackerHUD.Initialize(QuestsController.i);
 1324                break;
 325            case HUDElementID.SIGNUP:
 1326                CreateHudElement(configuration, hudElementId);
 1327                if (configuration.active)
 328                {
 329                    //Same race condition risks as with the ProfileHUD
 330                    //TODO Refactor the way AvatarEditor sets its visibility to match our data driven pattern
 331                    //Then this reference can be removed so we just work with a BaseVariable<bool>.
 332                    //This refactor applies to the ProfileHUD and the way kernel asks the HUDController during signup
 1333                    signupHUD.Initialize(avatarEditorHud);
 334                }
 335
 1336                break;
 337            case HUDElementID.LOADING:
 1338                if (loadingHud == null)
 339                {
 1340                    CreateHudElement(configuration, hudElementId);
 1341                    if (loadingHud != null && configuration.active)
 1342                        loadingController.Initialize();
 343
 344                }
 345                break;
 346            case HUDElementID.AVATAR_NAMES:
 347                // TODO Remove the HUDElementId once kernel stops sending the Configure HUD message
 348                break;
 349        }
 350
 32351        var hudElement = GetHUDElement(hudElementId);
 352
 32353        if (hudElement != null)
 25354            hudElement.SetVisibility(configuration.active && configuration.visible);
 32355    }
 356
 0357    private void OpenPrivateChatWindow(string targetUserId) { taskbarHud?.OpenPrivateChatTo(targetUserId); }
 358
 0359    private void View_OnDeactivatePreview() { playerInfoCardHud?.CloseCard(); }
 360
 0361    private void PrivateChatWindowHud_OnPressBack() { taskbarHud?.OpenFriendsWindow(); }
 362
 0363    private void TaskbarHud_onAnyTaskbarButtonClicked() { playerInfoCardHud?.CloseCard(); }
 364
 365    public void CreateHudElement(HUDConfiguration config, HUDElementID id)
 366    {
 25367        bool controllerCreated = hudElements.ContainsKey(id);
 368
 25369        if (config.active && !controllerCreated)
 370        {
 25371            hudElements.Add(id, hudFactory.CreateHUD(id));
 372
 25373            if (VERBOSE)
 0374                Debug.Log($"Adding {id} .. type {hudElements[id].GetType().Name}");
 375        }
 25376    }
 377
 378    public void UpdateHudElement(HUDConfiguration config, HUDElementID id)
 379    {
 0380        if (!hudElements.ContainsKey(id))
 0381            return;
 382
 0383        if (VERBOSE)
 0384            Debug.Log($"Updating {id}, type {hudElements[id].GetType().Name}, active: {config.active} visible: {config.v
 385
 0386        hudElements[id].SetVisibility(config.visible);
 0387    }
 388
 389    public void Cleanup()
 390    {
 634391        toggleUIVisibilityTrigger.OnTriggered -= ToggleUIVisibility_OnTriggered;
 634392        CommonScriptableObjects.allUIHidden.OnChange -= AllUIHiddenOnOnChange;
 393
 634394        if (worldChatWindowHud != null)
 395        {
 1396            worldChatWindowHud.OnPressPrivateMessage -= OpenPrivateChatWindow;
 1397            worldChatWindowHud.view.OnDeactivatePreview -= View_OnDeactivatePreview;
 398        }
 399
 634400        if (privateChatWindowHud != null)
 1401            privateChatWindowHud.OnPressBack -= PrivateChatWindowHud_OnPressBack;
 402
 634403        if (friendsHud != null)
 1404            friendsHud.OnPressWhisper -= OpenPrivateChatWindow;
 405
 634406        if (taskbarHud != null)
 1407            taskbarHud.OnAnyTaskbarButtonClicked -= TaskbarHud_onAnyTaskbarButtonClicked;
 408
 634409        UserContextMenu.OnOpenPrivateChatRequest -= OpenPrivateChatWindow;
 410
 1318411        foreach (var kvp in hudElements)
 412        {
 25413            kvp.Value?.Dispose();
 414        }
 415
 634416        hudElements.Clear();
 634417    }
 418
 419    public IHUD GetHUDElement(HUDElementID id)
 420    {
 2858421        if (!hudElements.ContainsKey(id))
 2761422            return null;
 423
 97424        return hudElements[id];
 425    }
 426
 427    public static bool IsHUDElementDeprecated(HUDElementID element)
 428    {
 32429        Type enumType = typeof(HUDElementID);
 32430        var enumName = enumType.GetEnumName(element);
 32431        var fieldInfo = enumType.GetField(enumName);
 32432        return Attribute.IsDefined(fieldInfo, typeof(ObsoleteAttribute));
 433    }
 434
 435#if UNITY_EDITOR
 436    [ContextMenu("Trigger fake PlayerInfoCard")]
 437    public void TriggerFakePlayerInfoCard()
 438    {
 0439        var newModel = ownUserProfile.CloneModel();
 0440        newModel.name = "FakePassport";
 0441        newModel.description = "Fake Description for Testing";
 0442        newModel.userId = "test-id";
 0443        newModel.inventory = new[]
 444        {
 445            "dcl://halloween_2019/machete_headband_top_head",
 446            "dcl://halloween_2019/bee_suit_upper_body",
 447            "dcl://halloween_2019/bride_of_frankie_upper_body",
 448            "dcl://halloween_2019/creepy_nurse_upper_body",
 449        };
 450
 0451        UserProfileController.i.AddUserProfileToCatalog(newModel);
 0452        Resources.Load<StringVariable>("CurrentPlayerInfoCardId").Set(newModel.userId);
 0453    }
 454#endif
 1258455    public void Dispose() { Cleanup(); }
 456}