< Summary

Class:SceneCatalogController
Assembly:BuildModeHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/HUD/BuildModeHUD/Scripts/Common/SceneCatalogController.cs
Covered lines:129
Uncovered lines:46
Coverable lines:175
Total lines:387
Line coverage:73.7% (129 of 175)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SceneCatalogController()0%110100%
Initialize(...)0%660100%
AdapterStartDrag(...)0%2.52050%
Dispose()0%12.9110069.23%
GetCurrentSection()0%2100%
AssetsFiltered(...)0%2.062075%
FilterRemoved()0%2100%
AssetsPackFilter(...)0%6200%
CategoryFilter(...)0%6200%
FavoritesFilter(...)0%6200%
ToggleCatalogExpanse()0%110100%
QuickBarInput(...)0%110100%
ShowFavorites()0%220100%
GenerateFavorites()0%110100%
CatalogItemSelected(...)0%220100%
ResumeInput()0%220100%
StopInput()0%220100%
OnPointerEnter(...)0%6200%
OnPointerExit(...)0%6200%
HideCatalogClicked()0%220100%
OnCatalogItemPackSelected(...)0%110100%
SetCatalogAssetPackInListView(...)0%2.062075%
GenerateContentListForCatalogItemPack(...)0%330100%
GetAssetsListByCategory(...)0%330100%
SceneCatalogBack()0%3.013088.89%
IsCatalogOpen()0%110100%
IsCatalogExpanded()0%110100%
ShowCategories()0%3.333066.67%
ShowAssetsPacks()0%3.143075%
ShowCatalogContent()0%3.333066.67%
OpenCatalog()0%110100%
CloseCatalog()0%110100%
RefreshAssetPack()0%6200%
RefreshCatalog()0%2.152066.67%
ShowLastSelectedSection()0%6.984042.86%
SetActive(...)0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/HUD/BuildModeHUD/Scripts/Common/SceneCatalogController.cs

#LineLine coverage
 1using DCL.Configuration;
 2using DCL.Helpers;
 3using System;
 4using System.Collections.Generic;
 5using UnityEngine;
 6using UnityEngine.EventSystems;
 7
 8public interface ISceneCatalogController
 9{
 10    event Action OnHideCatalogClicked;
 11    event Action<CatalogItem> OnCatalogItemSelected;
 12    event Action OnResumeInput;
 13    event Action OnStopInput;
 14    event Action<PointerEventData, CatalogItemAdapter> OnPointerEnterInCatalogItemAdapter;
 15    event Action<PointerEventData, CatalogItemAdapter> OnPointerExitInCatalogItemAdapter;
 16    event Action<CatalogItem, CatalogItemAdapter> OnCatalogItemStartDrag;
 17    void Initialize(ISceneCatalogView view, IQuickBarController quickBarController);
 18    void Dispose();
 19    void AssetsPackFilter(bool isOn);
 20    void CategoryFilter(bool isOn);
 21    void FavoritesFilter(bool isOn);
 22    void ToggleCatalogExpanse();
 23    void QuickBarInput(int quickBarSlot);
 24    void ShowFavorites();
 25    void CatalogItemSelected(CatalogItem catalogItem);
 26    void OnCatalogItemPackSelected(CatalogItemPack catalogItemPack);
 27    void SceneCatalogBack();
 28    bool IsCatalogOpen();
 29    bool IsCatalogExpanded();
 30    void ShowCategories();
 31    void ShowAssetsPacks();
 32    void ShowCatalogContent();
 33    void OpenCatalog();
 34    void CloseCatalog();
 35    void RefreshAssetPack();
 36    void RefreshCatalog();
 37    void SetActive(bool isActive);
 38    BuildModeCatalogSection GetCurrentSection();
 39}
 40
 41public class SceneCatalogController : ISceneCatalogController
 42{
 43    internal const string FAVORITE_NAME = "Favorites";
 44
 45    public event Action OnHideCatalogClicked;
 46    public event Action<CatalogItem> OnCatalogItemSelected;
 47    public event Action OnResumeInput;
 48    public event Action OnStopInput;
 49    public event Action<PointerEventData, CatalogItemAdapter> OnPointerEnterInCatalogItemAdapter;
 50    public event Action<PointerEventData, CatalogItemAdapter> OnPointerExitInCatalogItemAdapter;
 51    public event Action<CatalogItem, CatalogItemAdapter> OnCatalogItemStartDrag;
 52
 53    internal ISceneCatalogView sceneCatalogView;
 54    internal IQuickBarController quickBarController;
 55    internal FavoritesController favoritesController;
 56    internal BIWSearchBarController biwSearchBarController;
 57    internal bool isShowingAssetPacks = false;
 3258    internal bool isFilterByAssetPacks = true;
 3259    internal BuildModeCatalogSection currentSection = BuildModeCatalogSection.ASSET_PACKS;
 60
 61    public void Initialize(ISceneCatalogView sceneCatalogView, IQuickBarController quickBarController)
 62    {
 3163        this.sceneCatalogView = sceneCatalogView;
 3164        this.quickBarController = quickBarController;
 3165        favoritesController = new FavoritesController(sceneCatalogView.catalogGroupList);
 3166        biwSearchBarController = new BIWSearchBarController();
 3167        biwSearchBarController.Initialize(sceneCatalogView);
 68
 3169        sceneCatalogView.OnHideCatalogClicked += HideCatalogClicked;
 70
 3171        if (sceneCatalogView.catalogAssetPackList != null)
 172            sceneCatalogView.catalogAssetPackList.OnCatalogPackClick += OnCatalogItemPackSelected;
 73
 3174        if (sceneCatalogView.catalogGroupList != null)
 75        {
 176            sceneCatalogView.catalogGroupList.OnCatalogItemClicked += CatalogItemSelected;
 177            sceneCatalogView.catalogGroupList.OnCatalogItemStarDragging += AdapterStartDrag;
 178            sceneCatalogView.catalogGroupList.OnPointerEnterInAdapter += OnPointerEnter;
 179            sceneCatalogView.catalogGroupList.OnPointerExitInAdapter += OnPointerExit;
 80        }
 81
 3182        if (sceneCatalogView.category != null)
 183            sceneCatalogView.category.onValueChanged.AddListener(CategoryFilter);
 84
 3185        if (sceneCatalogView.favorites != null)
 186            sceneCatalogView.favorites.onValueChanged.AddListener(FavoritesFilter);
 87
 3188        if (sceneCatalogView.assetPack != null)
 189            sceneCatalogView.assetPack.onValueChanged.AddListener(AssetsPackFilter);
 90
 3191        sceneCatalogView.OnSceneCatalogBack += SceneCatalogBack;
 3192        quickBarController.OnQuickBarShortcutSelected += QuickBarInput;
 3193        quickBarController.OnCatalogItemSelected += CatalogItemSelected;
 94
 3195        biwSearchBarController.OnFilterChange += AssetsFiltered;
 3196        biwSearchBarController.OnFilterRemove += FilterRemoved;
 3197    }
 98
 999    private void AdapterStartDrag(CatalogItem item, CatalogItemAdapter adapter) { OnCatalogItemStartDrag?.Invoke(item, a
 100
 101    public void Dispose()
 102    {
 31103        if (sceneCatalogView != null)
 104        {
 30105            sceneCatalogView.OnHideCatalogClicked -= HideCatalogClicked;
 106
 30107            if (sceneCatalogView.catalogAssetPackList != null)
 0108                sceneCatalogView.catalogAssetPackList.OnCatalogPackClick -= OnCatalogItemPackSelected;
 109
 30110            if (sceneCatalogView.catalogGroupList != null)
 111            {
 0112                sceneCatalogView.catalogGroupList.OnCatalogItemClicked -= CatalogItemSelected;
 0113                sceneCatalogView.catalogGroupList.OnCatalogItemStarDragging -= AdapterStartDrag;
 0114                sceneCatalogView.catalogGroupList.OnPointerEnterInAdapter -= OnPointerEnter;
 0115                sceneCatalogView.catalogGroupList.OnPointerExitInAdapter -= OnPointerExit;
 116            }
 117
 30118            if (sceneCatalogView.category != null)
 0119                sceneCatalogView.category.onValueChanged.RemoveListener(CategoryFilter);
 120
 30121            if (sceneCatalogView.favorites != null)
 0122                sceneCatalogView.favorites.onValueChanged.RemoveListener(FavoritesFilter);
 123
 30124            if (sceneCatalogView.assetPack != null)
 0125                sceneCatalogView.assetPack.onValueChanged.RemoveListener(AssetsPackFilter);
 126
 30127            sceneCatalogView.OnSceneCatalogBack -= SceneCatalogBack;
 128        }
 129
 31130        if (quickBarController != null)
 131        {
 30132            quickBarController.OnQuickBarShortcutSelected -= QuickBarInput;
 30133            quickBarController.OnCatalogItemSelected -= CatalogItemSelected;
 134        }
 135
 31136        if (biwSearchBarController != null)
 137        {
 30138            biwSearchBarController.OnFilterChange -= AssetsFiltered;
 30139            biwSearchBarController.OnFilterRemove -= FilterRemoved;
 140
 30141            biwSearchBarController.Dispose();
 142        }
 143
 31144        if(favoritesController != null)
 30145            favoritesController.Dispose();
 31146    }
 147
 0148    public BuildModeCatalogSection GetCurrentSection() { return currentSection; }
 149
 150    public void AssetsFiltered(List<Dictionary<string, List<CatalogItem>>> filterObjects)
 151    {
 2152        ShowCatalogContent();
 2153        if (sceneCatalogView.catalogGroupList != null)
 0154            sceneCatalogView.catalogGroupList.SetContent(filterObjects);
 2155    }
 156
 0157    public void FilterRemoved() { ShowLastSelectedSection(); }
 158
 159    public void AssetsPackFilter(bool isOn)
 160    {
 0161        if (!isOn)
 0162            return;
 163
 0164        isFilterByAssetPacks = true;
 0165        ShowAssetsPacks();
 0166    }
 167
 168    public void CategoryFilter(bool isOn)
 169    {
 0170        if (!isOn)
 0171            return;
 172
 0173        isFilterByAssetPacks = false;
 0174        ShowCategories();
 0175    }
 176
 177    public void FavoritesFilter(bool isOn)
 178    {
 0179        if (!isOn)
 0180            return;
 181
 0182        ShowFavorites();
 0183    }
 184
 2185    public void ToggleCatalogExpanse() { sceneCatalogView.ToggleCatalogExpanse(); }
 186
 6187    public void QuickBarInput(int quickBarSlot) { quickBarController.QuickBarObjectSelected(quickBarSlot); }
 188
 189    public void ShowFavorites()
 190    {
 1191        currentSection = BuildModeCatalogSection.FAVOURITES;
 1192        biwSearchBarController.ReleaseFilters();
 193
 1194        sceneCatalogView.SetCatalogTitle(FAVORITE_NAME);
 1195        ShowCatalogContent();
 196
 1197        var favorites  = GenerateFavorites();
 198
 1199        sceneCatalogView.catalogGroupList?.SetContent(favorites);
 1200        sceneCatalogView.ShowBackButton(false);
 1201    }
 202
 203    internal List<Dictionary<string, List<CatalogItem>>>  GenerateFavorites()
 204    {
 2205        List<Dictionary<string, List<CatalogItem>>> favorites = new List<Dictionary<string, List<CatalogItem>>>();
 2206        Dictionary<string, List<CatalogItem>> groupedCategoryItems = new Dictionary<string, List<CatalogItem>>();
 2207        groupedCategoryItems.Add(FAVORITE_NAME, favoritesController.GetFavorites());
 2208        favorites.Add(groupedCategoryItems);
 2209        return favorites;
 210    }
 211
 11212    public void CatalogItemSelected(CatalogItem catalogItem) { OnCatalogItemSelected?.Invoke(catalogItem); }
 213
 2214    public void ResumeInput() { OnResumeInput?.Invoke(); }
 215
 2216    public void StopInput() { OnStopInput?.Invoke(); }
 217
 0218    private void OnPointerEnter(PointerEventData eventData, CatalogItemAdapter adapter) { OnPointerEnterInCatalogItemAda
 219
 0220    private void OnPointerExit(PointerEventData eventData, CatalogItemAdapter adapter) { OnPointerExitInCatalogItemAdapt
 221
 2222    public void HideCatalogClicked() { OnHideCatalogClicked?.Invoke(); }
 223
 224    public void OnCatalogItemPackSelected(CatalogItemPack catalogItemPack)
 225    {
 1226        ShowCatalogContent();
 1227        SetCatalogAssetPackInListView(catalogItemPack);
 1228        sceneCatalogView.ShowBackButton(true);
 1229    }
 230
 231    internal void SetCatalogAssetPackInListView(CatalogItemPack catalogItemPack)
 232    {
 1233        sceneCatalogView.SetCatalogTitle(catalogItemPack.title);
 234
 1235        var contentList = GenerateContentListForCatalogItemPack(catalogItemPack);
 236
 1237        sceneCatalogView.catalogGroupList?.SetContent(contentList);
 0238    }
 239
 240    internal  List<Dictionary<string, List<CatalogItem>>> GenerateContentListForCatalogItemPack(CatalogItemPack catalogI
 241    {
 1242        Dictionary<string, List<CatalogItem>> groupedCatalogItem = new Dictionary<string, List<CatalogItem>>();
 243
 6244        foreach (CatalogItem sceneObject in catalogItemPack.assets)
 245        {
 2246            string titleToUse = sceneObject.categoryName;
 247
 2248            if (!groupedCatalogItem.ContainsKey(titleToUse))
 249            {
 1250                groupedCatalogItem.Add(titleToUse, GetAssetsListByCategory(titleToUse, catalogItemPack));
 251            }
 252        }
 253
 1254        List<Dictionary<string, List<CatalogItem>>> contentList = new List<Dictionary<string, List<CatalogItem>>>
 255        {
 256            groupedCatalogItem
 257        };
 258
 1259        return contentList;
 260    }
 261
 262    internal List<CatalogItem> GetAssetsListByCategory(string category, CatalogItemPack sceneAssetPack)
 263    {
 2264        List<CatalogItem> catalogItemList = new List<CatalogItem>();
 265
 14266        foreach (CatalogItem catalogItem in sceneAssetPack.assets)
 267        {
 5268            if (category == catalogItem.categoryName)
 4269                catalogItemList.Add(catalogItem);
 270        }
 271
 2272        return catalogItemList;
 273    }
 274
 275    public void SceneCatalogBack()
 276    {
 2277        if (isShowingAssetPacks)
 278        {
 1279            sceneCatalogView.CloseCatalog();
 1280        }
 281        else
 282        {
 1283            if (isFilterByAssetPacks)
 1284                ShowAssetsPacks();
 285            else
 0286                ShowCategories();
 287
 1288            sceneCatalogView.ShowBackButton(false);
 1289            biwSearchBarController.ReleaseFilters();
 290        }
 1291    }
 292
 1293    public bool IsCatalogOpen() { return sceneCatalogView.IsCatalogOpen(); }
 294
 1295    public bool IsCatalogExpanded() { return sceneCatalogView.IsCatalogExpanded(); }
 296
 297    public void ShowCategories()
 298    {
 1299        currentSection = BuildModeCatalogSection.CATEGORIES;
 1300        biwSearchBarController.ReleaseFilters();
 301
 1302        if (sceneCatalogView.catalogAssetPackList != null)
 303        {
 0304            sceneCatalogView.catalogAssetPackList.SetCategoryStyle();
 0305            sceneCatalogView.catalogAssetPackList.SetContent(BIWCatalogManager.GetCatalogItemPacksFilteredByCategories()
 0306            sceneCatalogView.catalogAssetPackList.gameObject.SetActive(true);
 307        }
 308
 1309        isShowingAssetPacks = true;
 1310        sceneCatalogView.SetCatalogTitle(BIWSettings.CATALOG_ASSET_PACK_TITLE);
 311
 1312        if (sceneCatalogView.catalogGroupList != null)
 0313            sceneCatalogView.catalogGroupList.gameObject.SetActive(false);
 314
 1315        sceneCatalogView.ShowBackButton(false);
 1316    }
 317
 318    public void ShowAssetsPacks()
 319    {
 3320        currentSection = BuildModeCatalogSection.ASSET_PACKS;
 3321        biwSearchBarController.ReleaseFilters();
 322
 3323        if (sceneCatalogView.catalogAssetPackList != null)
 324        {
 0325            sceneCatalogView.catalogAssetPackList.SetAssetPackStyle();
 0326            sceneCatalogView.catalogAssetPackList.gameObject.SetActive(true);
 327        }
 328
 3329        isShowingAssetPacks = true;
 3330        sceneCatalogView.SetCatalogTitle(BIWSettings.CATALOG_ASSET_PACK_TITLE);
 3331        RefreshCatalog();
 332
 3333        if (sceneCatalogView.catalogGroupList != null)
 0334            sceneCatalogView.catalogGroupList.gameObject.SetActive(false);
 335
 3336        sceneCatalogView.ShowBackButton(false);
 3337    }
 338
 339    public void ShowCatalogContent()
 340    {
 4341        isShowingAssetPacks = false;
 4342        if (sceneCatalogView.catalogAssetPackList != null)
 0343            sceneCatalogView.catalogAssetPackList.gameObject.SetActive(false);
 344
 4345        if (sceneCatalogView.catalogGroupList != null)
 0346            sceneCatalogView.catalogGroupList.gameObject.SetActive(true);
 4347    }
 348
 349    public void OpenCatalog()
 350    {
 1351        ShowLastSelectedSection();
 1352        Utils.UnlockCursor();
 1353        sceneCatalogView.SetActive(true);
 1354    }
 355
 2356    public void CloseCatalog() { sceneCatalogView.CloseCatalog(); }
 357
 358    public void RefreshAssetPack()
 359    {
 0360        if (sceneCatalogView.catalogGroupList != null)
 0361            sceneCatalogView.catalogGroupList.RefreshDisplay();
 0362    }
 363
 364    public void RefreshCatalog()
 365    {
 3366        if (sceneCatalogView.catalogAssetPackList != null)
 0367            sceneCatalogView.catalogAssetPackList.SetContent(BIWCatalogManager.GetCatalogItemPackList());
 3368    }
 369
 370    private void ShowLastSelectedSection()
 371    {
 1372        switch (currentSection)
 373        {
 374            case BuildModeCatalogSection.CATEGORIES:
 0375                ShowCategories();
 0376                break;
 377            case BuildModeCatalogSection.ASSET_PACKS:
 1378                ShowAssetsPacks();
 1379                break;
 380            case BuildModeCatalogSection.FAVOURITES:
 0381                ShowFavorites();
 382                break;
 383        }
 0384    }
 385
 4386    public void SetActive(bool isActive) { sceneCatalogView.SetActive(isActive); }
 387}