using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; namespace OTGIntegrated.Crafting { public sealed class CraftingUI : MonoBehaviour { public GameObject panelRoot; public Transform recipeListRoot; public Text ingredientsText; public Text resultText; public Text descriptionText; public Text messageText; public Button craftButton; public Button closeButton; public Font uiFont; private CraftingSystem craftingSystem; private OTGIntegrated.Inventory.Inventory inventory; private RecipeData selectedRecipe; private readonly List spawnedButtons = new List(); public void Initialize(CraftingSystem system, OTGIntegrated.Inventory.Inventory playerInventory) { craftingSystem = system; inventory = playerInventory; if (craftingSystem != null) { craftingSystem.OnCraftingChanged -= Refresh; craftingSystem.OnCraftingChanged += Refresh; } if (inventory != null) { inventory.OnInventoryChanged -= Refresh; inventory.OnInventoryChanged += Refresh; } if (craftButton != null) { craftButton.onClick.RemoveAllListeners(); craftButton.onClick.AddListener(() => { if (craftingSystem != null && selectedRecipe != null) { craftingSystem.Craft(selectedRecipe); } }); } if (closeButton != null) { closeButton.onClick.RemoveAllListeners(); closeButton.onClick.AddListener(() => FindObjectOfType()?.CloseCurrentWindow()); } Refresh(); } private void OnEnable() { Refresh(); } private void OnDestroy() { if (craftingSystem != null) { craftingSystem.OnCraftingChanged -= Refresh; } if (inventory != null) { inventory.OnInventoryChanged -= Refresh; } } public void Refresh() { if (recipeListRoot == null || craftingSystem == null) { return; } ClearButtons(); if (selectedRecipe == null && craftingSystem.availableRecipes.Count > 0) { selectedRecipe = craftingSystem.availableRecipes[0]; } for (int i = 0; i < craftingSystem.availableRecipes.Count; i++) { RecipeData recipe = craftingSystem.availableRecipes[i]; CreateRecipeButton(recipe); } UpdateDetails(); } private void CreateRecipeButton(RecipeData recipe) { GameObject root = new GameObject(recipe != null ? recipe.displayName : "Recipe", typeof(RectTransform), typeof(Image), typeof(Button)); root.transform.SetParent(recipeListRoot, false); spawnedButtons.Add(root); RectTransform rect = root.GetComponent(); rect.sizeDelta = new Vector2(300f, 46f); bool canCraft = craftingSystem != null && craftingSystem.CanCraft(recipe); Image image = root.GetComponent(); image.color = recipe == selectedRecipe ? new Color(0.82f, 0.58f, 0.18f, 0.95f) : canCraft ? new Color(0.14f, 0.28f, 0.22f, 0.92f) : new Color(0.12f, 0.13f, 0.16f, 0.88f); Text label = CreateText(root.transform, "Text", recipe != null ? recipe.displayName : "Recipe", 17, TextAnchor.MiddleLeft); label.rectTransform.anchorMin = new Vector2(0f, 0f); label.rectTransform.anchorMax = new Vector2(1f, 1f); label.rectTransform.offsetMin = new Vector2(14f, 0f); label.rectTransform.offsetMax = new Vector2(-8f, 0f); Button button = root.GetComponent