// Filename : NPCShopUIManager.cs // Creator : Converted from C++ EC_Shop // Date : 2024 using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro; using PerfectWorld.Scripts.Shop; using BrewMonster; using BrewMonster.UI; using BrewMonster.Network; using static CSNetwork.Common.ExpTypes; using ModelRenderer.Scripts.Common; public class NPCShopUIManager : AUIDialog { [Header("UI Panels")] public GameObject npcShopMainPanel; public GameObject npcShopDetailPanel; [Header("Tabs")] public Transform tabButtonContainer; public GameObject tabButtonPrefab; public string tabButtonTextComponentName = "Text"; [Header("Item Display")] public Transform itemContainer; public GameObject itemPanelPrefab; [Header("Main Panel Components")] public Button closeShopButton; private List currentItemPanels = new List(); private List currentTabButtons = new List(); private List tabPageMapping = new List(); // Maps tab index to page index private int currentTabIndex = 0; private uint currentNPCID = 0; private NPC_SELL_SERVICE? cachedSellService = null; /// Current NPC id for this shop session. Send SEVNPC_HELLO with this before buy. public uint CurrentNPCID => currentNPCID; private NPCShopDetailPanel detailPanelScript; public override void OnEnable() { base.OnEnable(); closeShopButton.onClick.AddListener(CloseShop); } public override void OnDisable() { base.OnDisable(); closeShopButton.onClick.RemoveListener(CloseShop); } void Start() { // Initialize detail panel reference if (npcShopDetailPanel != null) { detailPanelScript = npcShopDetailPanel.GetComponent(); if (detailPanelScript == null) detailPanelScript = npcShopDetailPanel.GetComponentInChildren(); // Initially hide the detail panel if (npcShopDetailPanel.activeSelf) npcShopDetailPanel.SetActive(false); } } public void OpenNPCShop(uint npcID) { if (npcShopMainPanel == null) { Debug.LogError("NPCShopUIManager: npcShopMainPanel is null!"); return; } // Get NPC ID from CECUIManager uint actualNPCID = npcID; CECUIManager uiManager = CECUIManager.Instance; if (uiManager != null) { int managerNPCID = uiManager.GetCurrentTargetNPCID(); if (managerNPCID > 0) { actualNPCID = (uint)managerNPCID; } } currentNPCID = actualNPCID; // Load sell service data if (!LoadSellService(actualNPCID)) { Debug.LogError($"NPCShopUIManager: Failed to load sell service for NPC {actualNPCID}"); return; } // // Show panel // npcShopMainPanel.SetActive(true); // Create tabs CreateTabs(); // Select first tab if (tabPageMapping.Count > 0) { OnTabSelected(0); } RefreshShopDisplay(); } bool LoadSellService(uint npcID) { try { var elementDataMan = ElementDataManProvider.GetElementDataMan(); if (elementDataMan == null) return false; // Get NPC_ESSENCE DATA_TYPE dataType = DATA_TYPE.DT_INVALID; object npcData = elementDataMan.get_data_ptr(npcID, ID_SPACE.ID_SPACE_ESSENCE, ref dataType); if (dataType != DATA_TYPE.DT_NPC_ESSENCE || npcData == null) return false; NPC_ESSENCE npcEssence = (NPC_ESSENCE)npcData; uint sellServiceID = npcEssence.id_sell_service; if (sellServiceID == 0) return false; // Get NPC_SELL_SERVICE DATA_TYPE serviceDataType = DATA_TYPE.DT_INVALID; object serviceData = elementDataMan.get_data_ptr(sellServiceID, ID_SPACE.ID_SPACE_ESSENCE, ref serviceDataType); if (serviceDataType != DATA_TYPE.DT_NPC_SELL_SERVICE || serviceData == null) return false; cachedSellService = (NPC_SELL_SERVICE)serviceData; return true; } catch (System.Exception ex) { Debug.LogError($"NPCShopUIManager: Exception loading sell service: {ex.Message}"); return false; } } void CreateTabs() { ClearTabs(); if (currentNPCID == 0 || tabButtonContainer == null || tabButtonPrefab == null) return; if (!cachedSellService.HasValue || cachedSellService.Value.pages == null) return; var sellService = cachedSellService.Value; tabPageMapping.Clear(); // Create tabs for each page that has items for (int pageIndex = 0; pageIndex < sellService.pages.Length; pageIndex++) { var page = sellService.pages[pageIndex]; // Check if page has any items bool hasItems = false; if (page.goods != null) { foreach (var good in page.goods) { if (good.id != 0) { hasItems = true; break; } } } if (!hasItems) continue; tabPageMapping.Add(pageIndex); // Get page title string pageTitle = ByteToStringUtils.UshortArrayToUnicodeString(page.page_title); if (string.IsNullOrEmpty(pageTitle)) { pageTitle = $"Page {pageIndex + 1}"; } // Create tab button GameObject tabButtonObj = Instantiate(tabButtonPrefab, tabButtonContainer); tabButtonObj.name = $"Tab_Page_{pageIndex}"; tabButtonObj.SetActive(true); // Get button component Button tabButton = tabButtonObj.GetComponent