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.
Babushka/scripts/CSharp/Common/Inventory/InventoryUi.cs

193 lines
6.3 KiB

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;
private InventoryInstance _playerInventory;
private int? _slotOnMouse;
private bool _inventoryExtended = false;
private Tween? _inventoryExtensionTween;
private float _inventoryClosedOffset = 0f;
private float _inventoryOpenedOffset = 200f;
public override void _Ready()
{
_playerInventory = InventoryManager.Instance.playerInventory;
//PopulateSlots();
SubscribeSlots();
SetSlotContent();
SetSlotSelectPosition();
InventoryManager.Instance.playerInventory.InventoryContentsChanged += 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 = _slotsParent.GetChild(i) as SlotUi;
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 = "";
}
}
}
private void SetSlotSelectPosition()
{
_slotSelect.GlobalPosition = _headerSlots[InventoryManager.Instance.CurrentSelectedSlotIndex].GlobalPosition;
}
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);
}
}
private void SubscribeSlots()
{
for (var index = 0; index < _playerInventory.Slots.Count; index++)
{
var slotInstance = _slotsParent.GetChild<SlotUi>(index);
slotInstance.index = index;
slotInstance.Clicked += SlotClicked;
}
}
private void UnsubscribeSlots()
{
for (var index = 0; index < _playerInventory.Slots.Count; index++)
{
var slotInstance = _slotsParent.GetChild(index) as SlotUi;
slotInstance!.Clicked -= SlotClicked;
}
}
private void SlotClicked(int index)
{
if (!_inventoryExtended) return;
GD.Print($"Clicked slot {index}");
if (_slotOnMouse == null)
{
if (_playerInventory.Slots[index].IsEmpty()) return;
_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();
}
}
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);
InputInventoryOpenClose();
}
if (Input.IsActionJustPressed("ui_inventory_close"))
{
if(_inventoryExtended)
InputInventoryOpenClose();
}
if (Input.IsActionJustPressed("ui_inventory_disadvance"))
{
InventoryManager.Instance.CurrentSelectedSlotIndex++;
if (InventoryManager.Instance.CurrentSelectedSlotIndex > 8)
InventoryManager.Instance.CurrentSelectedSlotIndex = 0;
SetSlotSelectPosition();
}
if (Input.IsActionJustPressed("ui_inventory_advance"))
{
InventoryManager.Instance.CurrentSelectedSlotIndex--;
if (InventoryManager.Instance.CurrentSelectedSlotIndex < 0)
InventoryManager.Instance.CurrentSelectedSlotIndex = 8;
SetSlotSelectPosition();
}
}
private void InputInventoryOpenClose()
{
_inventoryExtensionTween?.Kill();
_inventoryExtended = !_inventoryExtended;
if (_inventoryExtended)
{
GD.Print("Open inventory");
_inventoryExtensionTween = GetTree().CreateTween();
_slotSelect.Hide();
_inventoryExtensionTween
.TweenProperty(_slotsMover, "offset_bottom", _inventoryOpenedOffset, 0.4)
.SetTrans(Tween.TransitionType.Quad)
.SetEase(Tween.EaseType.Out);
}
else
{
GD.Print("Close inventory");
_inventoryExtensionTween = GetTree().CreateTween();
_slotSelect.Show();
_inventoryExtensionTween
.TweenProperty(_slotsMover, "offset_bottom", _inventoryClosedOffset, 0.4)
.SetTrans(Tween.TransitionType.Quad)
.SetEase(Tween.EaseType.Out);
_slotOnMouse = null;
}
}
}