Files

194 lines
6.4 KiB
C#
Raw Permalink Normal View History

2025-04-07 02:10:59 +02:00
using Godot;
namespace Babushka.scripts.CSharp.Common.Inventory;
public partial class InventoryUi : Control
{
[Export] private Control _slotsParent;
[Export] private Control _slotsMover;
[Export] private Control[] _headerSlots;
[Export] private Control _slotSelect;
2025-04-07 02:10:59 +02:00
private InventoryInstance _playerInventory;
2025-04-07 02:10:59 +02:00
private int? _slotOnMouse;
2025-04-07 02:52:57 +02:00
private bool _inventoryExtended = false;
private Tween? _inventoryExtensionTween;
2025-05-19 17:17:01 +02:00
private float _inventoryClosedOffset = 0f;
private float _inventoryOpenedOffset = 200f;
2025-04-07 02:10:59 +02:00
public override void _Ready()
{
_playerInventory = InventoryManager.Instance.playerInventory;
2025-05-19 21:35:55 +02:00
//PopulateSlots();
SubscribeSlots();
2025-04-07 02:10:59 +02:00
SetSlotContent();
2025-04-07 18:43:56 +02:00
SetSlotSelectPosition();
2025-04-14 16:49:29 +02:00
InventoryManager.Instance.playerInventory.InventoryContentsChanged += SetSlotContent;
2025-04-07 02:10:59 +02:00
}
public override void _ExitTree()
{
InventoryManager.Instance.playerInventory.InventoryContentsChanged -= SetSlotContent;
2025-04-07 02:10:59 +02:00
UnsubscribeSlots();
}
private void SetSlotContent()
{
for (var i = 0; i < _playerInventory.Slots.Count; i++)
{
var inventorySlot = _playerInventory.Slots[i];
var uiSlot = _slotsParent.GetChild(i) as SlotUi;
2025-04-07 02:10:59 +02:00
2025-05-20 19:48:49 +02:00
if (inventorySlot.itemInstance != null)
{
var blueprint = inventorySlot.itemInstance.blueprint;
var amount = inventorySlot.itemInstance.amount;
if (blueprint.icon != null)
{
// show icon
uiSlot!.nameLabel.Text = "";
uiSlot.icon.Texture = blueprint.icon;
}
else
{
// show name label
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;
uiSlot.icon.Texture = null;
}
// amount
uiSlot!.amountLabel.Text = amount != 1 ? amount.ToString() : "";
}
else
{
uiSlot!.nameLabel.Text = "";
uiSlot!.icon.Texture = null;
uiSlot!.amountLabel.Text = "";
}
2025-04-07 02:10:59 +02:00
}
}
2025-04-07 18:43:56 +02:00
private void SetSlotSelectPosition()
{
_slotSelect.GlobalPosition = _headerSlots[InventoryManager.Instance.CurrentSelectedSlotIndex].GlobalPosition;
2025-04-07 18:43:56 +02:00
}
2025-04-07 02:10:59 +02:00
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;
_slotsParent.AddChild(slotInstance);
2025-04-07 02:10:59 +02:00
}
}
2025-05-20 19:48:49 +02:00
2025-05-19 21:35:55 +02:00
private void SubscribeSlots()
{
for (var index = 0; index < _playerInventory.Slots.Count; index++)
{
var slotInstance = _slotsParent.GetChild<SlotUi>(index);
2025-05-19 21:35:55 +02:00
slotInstance.index = index;
slotInstance.Clicked += SlotClicked;
}
}
2025-04-07 02:10:59 +02:00
private void UnsubscribeSlots()
{
for (var index = 0; index < _playerInventory.Slots.Count; index++)
{
var slotInstance = _slotsParent.GetChild(index) as SlotUi;
2025-04-07 02:10:59 +02:00
slotInstance!.Clicked -= SlotClicked;
}
}
private void SlotClicked(int index)
{
2025-04-07 02:52:57 +02:00
if (!_inventoryExtended) return;
2025-04-07 18:43:56 +02:00
2025-04-07 02:10:59 +02:00
GD.Print($"Clicked slot {index}");
if (_slotOnMouse == null)
{
2025-04-07 02:52:57 +02:00
if (_playerInventory.Slots[index].IsEmpty()) return;
2025-04-07 02:10:59 +02:00
_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;
2025-04-14 16:49:29 +02:00
//SetSlotContent();
2025-04-07 02:10:59 +02:00
}
}
2025-04-07 02:52:57 +02:00
public override void _Process(double delta)
{
if (Input.IsActionJustPressed("ui_inventory_open_close"))
{
// We adjust the offset value in accordance with the screen height to make sure that the inventory is always in the middle of the screen.
_inventoryOpenedOffset = (GetViewportRect().Size.Y / 2 + 400) * (-1);
2025-04-07 18:43:56 +02:00
InputInventoryOpenClose();
}
2025-07-04 23:15:45 +02:00
if (Input.IsActionJustPressed("ui_inventory_close"))
{
if(_inventoryExtended)
InputInventoryOpenClose();
}
2025-04-17 16:10:12 +02:00
2025-04-07 18:43:56 +02:00
if (Input.IsActionJustPressed("ui_inventory_disadvance"))
{
InventoryManager.Instance.CurrentSelectedSlotIndex++;
if (InventoryManager.Instance.CurrentSelectedSlotIndex > 8)
InventoryManager.Instance.CurrentSelectedSlotIndex = 0;
2025-04-07 18:43:56 +02:00
SetSlotSelectPosition();
}
2025-04-17 16:10:12 +02:00
if (Input.IsActionJustPressed("ui_inventory_advance"))
2025-04-07 18:43:56 +02:00
{
InventoryManager.Instance.CurrentSelectedSlotIndex--;
if (InventoryManager.Instance.CurrentSelectedSlotIndex < 0)
InventoryManager.Instance.CurrentSelectedSlotIndex = 8;
2025-04-07 18:43:56 +02:00
SetSlotSelectPosition();
}
}
private void InputInventoryOpenClose()
{
_inventoryExtensionTween?.Kill();
_inventoryExtended = !_inventoryExtended;
if (_inventoryExtended)
{
2025-05-20 18:27:50 +02:00
GD.Print("Open inventory");
2025-04-07 18:43:56 +02:00
_inventoryExtensionTween = GetTree().CreateTween();
_slotSelect.Hide();
_inventoryExtensionTween
2025-05-19 17:17:01 +02:00
.TweenProperty(_slotsMover, "offset_bottom", _inventoryOpenedOffset, 0.4)
2025-04-07 18:43:56 +02:00
.SetTrans(Tween.TransitionType.Quad)
.SetEase(Tween.EaseType.Out);
}
else
{
2025-05-20 18:27:50 +02:00
GD.Print("Close inventory");
2025-04-07 18:43:56 +02:00
_inventoryExtensionTween = GetTree().CreateTween();
_slotSelect.Show();
_inventoryExtensionTween
2025-05-19 17:17:01 +02:00
.TweenProperty(_slotsMover, "offset_bottom", _inventoryClosedOffset, 0.4)
2025-04-07 18:43:56 +02:00
.SetTrans(Tween.TransitionType.Quad)
.SetEase(Tween.EaseType.Out);
_slotOnMouse = null;
2025-04-07 02:52:57 +02:00
}
}
2025-05-20 19:48:49 +02:00
}