| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using TMPro; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.EventSystems; |
| | 6 | |
|
| | 7 | | public class CatalogAssetGroupAdapter : MonoBehaviour |
| | 8 | | { |
| | 9 | | public TextMeshProUGUI categoryTxt; |
| | 10 | | public GameObject categoryContentGO; |
| | 11 | | public System.Action<CatalogItem> OnCatalogItemClicked; |
| | 12 | | public System.Action<CatalogItem, CatalogItemAdapter> OnCatalogItemFavorite; |
| | 13 | | public System.Action<CatalogItem, CatalogItemAdapter, BaseEventData> OnAdapterStartDragging; |
| | 14 | | public System.Action<PointerEventData, CatalogItemAdapter> OnPointerEnterInAdapter; |
| | 15 | | public System.Action<PointerEventData, CatalogItemAdapter> OnPointerExitInAdapter; |
| | 16 | |
|
| | 17 | | [Header("Prefab References")] |
| | 18 | | public GameObject catalogItemAdapterPrefab; |
| | 19 | |
|
| | 20 | | public void SetContent(string category, List<CatalogItem> catalogItemList) |
| | 21 | | { |
| 0 | 22 | | categoryTxt.text = category.ToUpper(); |
| 0 | 23 | | RemoveAdapters(); |
| 0 | 24 | | foreach (CatalogItem catalogItem in catalogItemList) |
| | 25 | | { |
| 0 | 26 | | if (catalogItem.IsSmartItem()) |
| | 27 | | continue; |
| | 28 | |
|
| 0 | 29 | | CatalogItemAdapter adapter = Instantiate(catalogItemAdapterPrefab, categoryContentGO.transform).GetComponent |
| 0 | 30 | | adapter.SetContent(catalogItem); |
| 0 | 31 | | SubscribeToEvents(adapter); |
| | 32 | | } |
| 0 | 33 | | } |
| | 34 | |
|
| | 35 | | public void SubscribeToEvents(CatalogItemAdapter adapter) |
| | 36 | | { |
| 1 | 37 | | adapter.OnCatalogItemClicked += CatalogItemClicked; |
| 1 | 38 | | adapter.OnCatalogItemFavorite += CatalogItemFavorite; |
| 1 | 39 | | adapter.OnAdapterStartDrag += AdapterStartDragging; |
| 1 | 40 | | adapter.OnPointerEnterInAdapter += OnPointerEnter; |
| 1 | 41 | | adapter.OnPointerExitInAdapter += OnPointerExit; |
| 1 | 42 | | } |
| | 43 | |
|
| | 44 | | public void UnsubscribeToEvents(CatalogItemAdapter adapter) |
| | 45 | | { |
| 0 | 46 | | adapter.OnCatalogItemClicked -= CatalogItemClicked; |
| 0 | 47 | | adapter.OnCatalogItemFavorite -= CatalogItemFavorite; |
| 0 | 48 | | adapter.OnAdapterStartDrag -= AdapterStartDragging; |
| 0 | 49 | | adapter.OnPointerEnterInAdapter -= OnPointerEnter; |
| 0 | 50 | | adapter.OnPointerExitInAdapter -= OnPointerExit; |
| 0 | 51 | | } |
| | 52 | |
|
| | 53 | | public void RemoveAdapters() |
| | 54 | | { |
| 0 | 55 | | for (int i = 0; i < categoryContentGO.transform.childCount; i++) |
| | 56 | | { |
| 0 | 57 | | CatalogItemAdapter toRemove = categoryContentGO.transform.GetChild(i).GetComponent<CatalogItemAdapter>(); |
| 0 | 58 | | if (toRemove != null) |
| | 59 | | { |
| 0 | 60 | | UnsubscribeToEvents(toRemove); |
| 0 | 61 | | Destroy(toRemove.gameObject); |
| | 62 | | } |
| | 63 | | } |
| 0 | 64 | | } |
| | 65 | |
|
| 0 | 66 | | private void CatalogItemClicked(CatalogItem catalogItemClicked) { OnCatalogItemClicked?.Invoke(catalogItemClicked); |
| | 67 | |
|
| 0 | 68 | | private void CatalogItemFavorite(CatalogItem catalogItemClicked, CatalogItemAdapter adapter) { OnCatalogItemFavorite |
| | 69 | |
|
| 18 | 70 | | private void AdapterStartDragging(CatalogItem catalogItemClicked, CatalogItemAdapter adapter, BaseEventData data) { |
| | 71 | |
|
| 0 | 72 | | private void OnPointerEnter(PointerEventData eventData, CatalogItemAdapter adapter) { OnPointerEnterInAdapter?.Invok |
| | 73 | |
|
| 0 | 74 | | private void OnPointerExit(PointerEventData eventData, CatalogItemAdapter adapter) { OnPointerExitInAdapter?.Invoke( |
| | 75 | | } |