You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
2.3 KiB
78 lines
2.3 KiB
using Godot;
|
|
|
|
namespace Babushka.scripts.CSharp.Common.Inventory;
|
|
|
|
public partial class InventoryUi : Control
|
|
{
|
|
private GridContainer _slots;
|
|
private InventoryInstance _playerInventory;
|
|
|
|
private int? _slotOnMouse;
|
|
|
|
public override void _Ready()
|
|
{
|
|
GD.Print("Ready inventory ui");
|
|
_slots = GetNode<GridContainer>("Slots");
|
|
_playerInventory = InventoryManager.Instance.playerInventory;
|
|
PopulateSlots();
|
|
SetSlotContent();
|
|
}
|
|
|
|
public override void _ExitTree()
|
|
{
|
|
UnsubscribeSlots();
|
|
}
|
|
|
|
private void SetSlotContent()
|
|
{
|
|
for (var i = 0; i < _playerInventory.Slots.Count; i++)
|
|
{
|
|
var inventorySlot = _playerInventory.Slots[i];
|
|
var uiSlot = _slots.GetChild(i) as SlotUi;
|
|
|
|
uiSlot!.nameLabel.Text = inventorySlot.itemInstance?.blueprint.name ?? "";
|
|
uiSlot!.nameLabel.LabelSettings = uiSlot!.nameLabel.LabelSettings.Duplicate() as LabelSettings;
|
|
uiSlot!.nameLabel.LabelSettings!.FontColor = inventorySlot.itemInstance?.blueprint.color ?? Colors.White;
|
|
}
|
|
}
|
|
|
|
private void PopulateSlots()
|
|
{
|
|
var slotPackedScene = GD.Load<PackedScene>("res://prefabs/UI/Inventory/Slot.tscn");
|
|
for (var index = 0; index < _playerInventory.Slots.Count; index++)
|
|
{
|
|
var slotInstance = slotPackedScene.Instantiate<SlotUi>();
|
|
slotInstance.index = index;
|
|
slotInstance.Clicked += SlotClicked;
|
|
_slots.AddChild(slotInstance);
|
|
}
|
|
}
|
|
|
|
private void UnsubscribeSlots()
|
|
{
|
|
for (var index = 0; index < _playerInventory.Slots.Count; index++)
|
|
{
|
|
var slotInstance = _slots.GetChild(index) as SlotUi;
|
|
slotInstance!.Clicked -= SlotClicked;
|
|
}
|
|
}
|
|
|
|
private void SlotClicked(int index)
|
|
{
|
|
GD.Print($"Clicked slot {index}");
|
|
if (_slotOnMouse == null)
|
|
{
|
|
_slotOnMouse = index;
|
|
GD.Print($"Slot on mouse: {_slotOnMouse}");
|
|
}
|
|
else
|
|
{
|
|
var sourceSlot = _slotOnMouse.Value;
|
|
var destinationSlot = index;
|
|
InventoryManager.Instance.MoveItem(_playerInventory, sourceSlot, _playerInventory, destinationSlot);
|
|
_slotOnMouse = null;
|
|
SetSlotContent();
|
|
}
|
|
}
|
|
}
|