InventoryScreen
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
internal interface IInventoryHolder
|
||||
public interface IInventoryHolder
|
||||
{
|
||||
public Inventory Inventory { get; }
|
||||
public string Name { get; }
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ public class Player : MonoBehaviour, IInventoryHolder, IDataPersistence<GameData
|
||||
|
||||
public Inventory Inventory => inventory;
|
||||
|
||||
public string Name => gameObject.name;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
// Add("Лимонад", 5);
|
||||
@@ -22,7 +24,6 @@ public class Player : MonoBehaviour, IInventoryHolder, IDataPersistence<GameData
|
||||
// Debug.Log($"{key}: брошено {dropped} из {value - added}");
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
public void LoadData(GameData data)
|
||||
{
|
||||
|
||||
@@ -6,6 +6,8 @@ public class Shelf : MonoBehaviour, IInventoryHolder, IDataPersistence<ShelfData
|
||||
|
||||
public Inventory Inventory => inventory;
|
||||
|
||||
public string Name => type;
|
||||
|
||||
public string type = "shelf";
|
||||
|
||||
void Awake()
|
||||
|
||||
@@ -4,12 +4,15 @@ using UnityEngine.UIElements;
|
||||
|
||||
public class InfoBox : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
|
||||
{
|
||||
// public Vector3 worldOffset = new Vector3(0, 1f, 0);
|
||||
public Vector3 worldOffset = new Vector3(0, 0, 0);
|
||||
[SerializeField]
|
||||
private VisualTreeAsset uiAsset;
|
||||
|
||||
[SerializeField]
|
||||
private PanelSettings uiPanelSettings;
|
||||
|
||||
public UIDocument uiDocument;
|
||||
public bool AlwaysVisible;
|
||||
|
||||
private UIDocument uiDocument;
|
||||
private Camera mainCamera;
|
||||
private IInventoryHolder inventoryHolder;
|
||||
private Label infoBox;
|
||||
@@ -17,36 +20,31 @@ public class InfoBox : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
|
||||
void Start()
|
||||
{
|
||||
mainCamera = Camera.main;
|
||||
inventoryHolder = GetComponent<IInventoryHolder>();
|
||||
|
||||
if (uiDocument != null)
|
||||
{
|
||||
infoBox = uiDocument.rootVisualElement.Q<Label>("InfoBox");
|
||||
inventoryHolder = GetComponent<IInventoryHolder>();
|
||||
}
|
||||
uiDocument = gameObject.AddComponent<UIDocument>();
|
||||
uiDocument.panelSettings = uiPanelSettings;
|
||||
uiDocument.visualTreeAsset = uiAsset;
|
||||
|
||||
infoBox = uiDocument.rootVisualElement.Q<Label>("InfoBox");
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
// if (infoBox != null)
|
||||
{
|
||||
// Convert world position to screen space
|
||||
Vector3 screenPos = mainCamera.WorldToScreenPoint(transform.position + worldOffset);
|
||||
// Convert world position to screen space
|
||||
Vector3 screenPos = mainCamera.WorldToScreenPoint(transform.position);
|
||||
|
||||
// Update panel position (UI Toolkit uses top-left origin)
|
||||
infoBox.style.left = screenPos.x;
|
||||
infoBox.style.top = Screen.height - screenPos.y;
|
||||
infoBox.style.visibility = AlwaysVisible ? Visibility.Visible : Visibility.Hidden;
|
||||
// Update panel position (UI Toolkit uses top-left origin)
|
||||
infoBox.style.left = screenPos.x;
|
||||
infoBox.style.top = Screen.height - screenPos.y;
|
||||
infoBox.style.visibility = AlwaysVisible ? Visibility.Visible : Visibility.Hidden;
|
||||
|
||||
UpdateText();
|
||||
}
|
||||
UpdateText();
|
||||
}
|
||||
|
||||
void UpdateText()
|
||||
{
|
||||
// if (infoBox != null)
|
||||
{
|
||||
infoBox.text = inventoryHolder.Inventory.ToString();
|
||||
}
|
||||
infoBox.text = inventoryHolder.Inventory.ToString();
|
||||
}
|
||||
|
||||
public void OnPointerEnter(PointerEventData eventData)
|
||||
@@ -59,4 +57,4 @@ public class InfoBox : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
|
||||
if (!AlwaysVisible)
|
||||
infoBox.style.visibility = Visibility.Hidden;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
public class InventoryScreen : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
private VisualTreeAsset uiItemAsset;
|
||||
|
||||
private UIDocument uiDocument;
|
||||
|
||||
private VisualElement root;
|
||||
private VisualElement itemList;
|
||||
private Label headerLabel;
|
||||
private Label footerLabel;
|
||||
|
||||
private IInventoryHolder inventoryHolder;
|
||||
private IInventoryHolder targetInventoryHolder;
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
uiDocument = GetComponent<UIDocument>();
|
||||
|
||||
root = uiDocument.rootVisualElement;
|
||||
var inventory = root.Q("Inventory");
|
||||
|
||||
root.style.display = DisplayStyle.None;
|
||||
|
||||
itemList = inventory.Q("Inventory").Q("ContainerScroll");
|
||||
headerLabel = inventory.Q<Label>("Header");
|
||||
footerLabel = inventory.Q<Label>("Footer");
|
||||
|
||||
root.RegisterCallback<ClickEvent>(OnRootClicked);
|
||||
inventory.RegisterCallback<ClickEvent>(evt => evt.StopPropagation());
|
||||
}
|
||||
|
||||
private void OnRootClicked(ClickEvent evt)
|
||||
{
|
||||
Debug.Log("Root Clicked");
|
||||
Hide();
|
||||
}
|
||||
|
||||
private void Show()
|
||||
{
|
||||
root.style.display = DisplayStyle.Flex;
|
||||
}
|
||||
|
||||
private void Hide()
|
||||
{
|
||||
root.style.display = DisplayStyle.None;
|
||||
}
|
||||
|
||||
public void Display(IInventoryHolder inventory, IInventoryHolder targetInventory = null)
|
||||
{
|
||||
this.inventoryHolder = inventory;
|
||||
this.targetInventoryHolder = inventory;
|
||||
|
||||
itemList.Clear();
|
||||
|
||||
foreach (var item in inventory.Inventory.Items)
|
||||
{
|
||||
var child = uiItemAsset.Instantiate();
|
||||
// child.AddToClassList(".item");
|
||||
child.Q<Label>("Name").text = item.Key;
|
||||
child.Q<Label>("Quantity").text = item.Value.ToString();
|
||||
itemList.Add(child);
|
||||
}
|
||||
|
||||
headerLabel.text = inventory.Name;
|
||||
footerLabel.text = $"Свободно: {inventory.Inventory.Free}";
|
||||
|
||||
Show();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 631c59a16a4091a4caa7b4ff24839552
|
||||
@@ -0,0 +1,31 @@
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
public class InventoryScreenInvoker : MonoBehaviour, IPointerClickHandler
|
||||
{
|
||||
public void OnPointerClick(PointerEventData eventData)
|
||||
{
|
||||
Show();
|
||||
}
|
||||
|
||||
private void Show()
|
||||
{
|
||||
var inventoryScreen = FindObjectsByType<InventoryScreen>(FindObjectsSortMode.None).First();
|
||||
if (inventoryScreen == null)
|
||||
{
|
||||
Debug.Log("No inventory screen defined on current scene");
|
||||
return;
|
||||
}
|
||||
|
||||
var inventory = GetComponent<IInventoryHolder>();
|
||||
if (inventory == null)
|
||||
{
|
||||
Debug.Log("Cannot show inventory screen on an object without an inventory");
|
||||
return;
|
||||
}
|
||||
|
||||
inventoryScreen.Display(inventory);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 38451aa38ba98df4f939ff6b7b8214c2
|
||||
Reference in New Issue
Block a user