first commit
This commit is contained in:
@@ -0,0 +1,161 @@
|
||||
using System.Collections.Generic;
|
||||
using OTGIntegrated.Items;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace OTGIntegrated.Inventory
|
||||
{
|
||||
public sealed class InventoryUI : MonoBehaviour
|
||||
{
|
||||
public GameObject panelRoot;
|
||||
public Transform slotGrid;
|
||||
public Text titleText;
|
||||
public Text descriptionText;
|
||||
public Text hintText;
|
||||
public Button closeButton;
|
||||
public Font uiFont;
|
||||
|
||||
private Inventory inventory;
|
||||
private readonly List<GameObject> spawnedSlots = new List<GameObject>();
|
||||
|
||||
public void Initialize(Inventory inventory)
|
||||
{
|
||||
this.inventory = inventory;
|
||||
if (inventory != null)
|
||||
{
|
||||
inventory.OnInventoryChanged -= Refresh;
|
||||
inventory.OnInventoryChanged += Refresh;
|
||||
}
|
||||
|
||||
if (closeButton != null)
|
||||
{
|
||||
closeButton.onClick.RemoveAllListeners();
|
||||
closeButton.onClick.AddListener(() => FindObjectOfType<OTGIntegrated.UI.UIManager>()?.CloseCurrentWindow());
|
||||
}
|
||||
|
||||
Refresh();
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
Refresh();
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (inventory != null)
|
||||
{
|
||||
inventory.OnInventoryChanged -= Refresh;
|
||||
}
|
||||
}
|
||||
|
||||
public void Refresh()
|
||||
{
|
||||
if (slotGrid == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ClearSlots();
|
||||
IReadOnlyList<InventorySlot> slots = inventory != null ? inventory.GetAllSlots() : null;
|
||||
int maxSlots = inventory != null ? inventory.MaxSlots : 20;
|
||||
|
||||
for (int i = 0; i < maxSlots; i++)
|
||||
{
|
||||
InventorySlot slot = slots != null && i < slots.Count ? slots[i] : null;
|
||||
CreateSlot(i, slot);
|
||||
}
|
||||
|
||||
if (titleText != null)
|
||||
{
|
||||
titleText.text = "Инвентарь";
|
||||
}
|
||||
|
||||
if (hintText != null)
|
||||
{
|
||||
hintText.text = "I — закрыть F5 — сохранить F9 — загрузить";
|
||||
}
|
||||
|
||||
if (descriptionText != null && string.IsNullOrEmpty(descriptionText.text))
|
||||
{
|
||||
descriptionText.text = "Выберите предмет.";
|
||||
}
|
||||
}
|
||||
|
||||
private void CreateSlot(int index, InventorySlot slot)
|
||||
{
|
||||
GameObject root = new GameObject($"InventorySlot_{index}", typeof(RectTransform), typeof(Image), typeof(Button));
|
||||
root.transform.SetParent(slotGrid, false);
|
||||
spawnedSlots.Add(root);
|
||||
|
||||
RectTransform rect = root.GetComponent<RectTransform>();
|
||||
rect.sizeDelta = new Vector2(90f, 90f);
|
||||
|
||||
Image background = root.GetComponent<Image>();
|
||||
background.color = slot != null && !slot.IsEmpty()
|
||||
? Color.Lerp(new Color(0.09f, 0.1f, 0.12f, 0.95f), slot.item.itemColor, 0.35f)
|
||||
: new Color(0.07f, 0.08f, 0.1f, 0.82f);
|
||||
|
||||
Text label = CreateText(root.transform, "Label", slot != null && !slot.IsEmpty() ? slot.item.displayName : string.Empty, 14, TextAnchor.MiddleCenter);
|
||||
label.rectTransform.anchorMin = new Vector2(0.08f, 0.24f);
|
||||
label.rectTransform.anchorMax = new Vector2(0.92f, 0.78f);
|
||||
label.rectTransform.offsetMin = Vector2.zero;
|
||||
label.rectTransform.offsetMax = Vector2.zero;
|
||||
|
||||
Text count = CreateText(root.transform, "Count", slot != null && !slot.IsEmpty() ? slot.amount.ToString() : string.Empty, 14, TextAnchor.LowerRight);
|
||||
count.rectTransform.anchorMin = new Vector2(0.1f, 0.05f);
|
||||
count.rectTransform.anchorMax = new Vector2(0.95f, 0.35f);
|
||||
count.rectTransform.offsetMin = Vector2.zero;
|
||||
count.rectTransform.offsetMax = Vector2.zero;
|
||||
|
||||
Button button = root.GetComponent<Button>();
|
||||
button.onClick.AddListener(() => SelectSlot(slot));
|
||||
}
|
||||
|
||||
private void SelectSlot(InventorySlot slot)
|
||||
{
|
||||
if (descriptionText == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (slot == null || slot.IsEmpty())
|
||||
{
|
||||
descriptionText.text = "Пустой слот.";
|
||||
return;
|
||||
}
|
||||
|
||||
ItemData item = slot.item;
|
||||
descriptionText.text = $"{item.displayName}\n\n{item.description}\n\nТип: {item.itemType}\nКоличество: {slot.amount}";
|
||||
}
|
||||
|
||||
private Text CreateText(Transform parent, string name, string value, int fontSize, TextAnchor anchor)
|
||||
{
|
||||
GameObject textObject = new GameObject(name, typeof(RectTransform), typeof(Text));
|
||||
textObject.transform.SetParent(parent, false);
|
||||
Text text = textObject.GetComponent<Text>();
|
||||
text.text = value;
|
||||
text.font = uiFont != null ? uiFont : Resources.GetBuiltinResource<Font>("Arial.ttf");
|
||||
text.fontSize = fontSize;
|
||||
text.alignment = anchor;
|
||||
text.color = new Color(0.94f, 0.93f, 0.88f, 1f);
|
||||
text.resizeTextForBestFit = true;
|
||||
text.resizeTextMinSize = 10;
|
||||
text.resizeTextMaxSize = fontSize;
|
||||
return text;
|
||||
}
|
||||
|
||||
private void ClearSlots()
|
||||
{
|
||||
for (int i = 0; i < spawnedSlots.Count; i++)
|
||||
{
|
||||
if (spawnedSlots[i] != null)
|
||||
{
|
||||
Destroy(spawnedSlots[i]);
|
||||
}
|
||||
}
|
||||
|
||||
spawnedSlots.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user