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

146 lines
4.4 KiB

#nullable enable
using Godot;
namespace Babushka.scripts.CSharp.Common.Inventory;
public partial class InventoryUi : Control
{
private GridContainer _slots;
private InventoryInstance _playerInventory;
private Control _slotSelect;
private int? _slotOnMouse;
private int _selectedSlot = 0;
private bool _inventoryExtended = false;
private Tween? _inventoryExtensionTween;
public override void _Ready()
{
GD.Print("Ready inventory ui");
_slots = GetNode<GridContainer>("SlotsContainer/Slots");
_playerInventory = InventoryManager.Instance.playerInventory;
_slotSelect = GetNode<Control>("SlotsContainer/SlotSelectContainer/Selector");
PopulateSlots();
SetSlotContent();
SetSlotSelectPosition();
}
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 SetSlotSelectPosition()
{
_slotSelect.Position = new Vector2(_selectedSlot * 100, 0);
}
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)
{
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"))
{
InputInventoryOpenClose();
}
if (Input.IsActionJustPressed("ui_inventory_disadvance"))
{
_selectedSlot++;
if (_selectedSlot > 9)
_selectedSlot = 0;
SetSlotSelectPosition();
}
if(Input.IsActionJustPressed("ui_inventory_advance"))
{
_selectedSlot--;
if (_selectedSlot < 0)
_selectedSlot = 9;
SetSlotSelectPosition();
}
}
private void InputInventoryOpenClose()
{
_inventoryExtensionTween?.Kill();
_inventoryExtended = !_inventoryExtended;
if (_inventoryExtended)
{
//GD.Print("Open inventory");
_inventoryExtensionTween = GetTree().CreateTween();
_slotSelect.Hide();
_inventoryExtensionTween
.TweenProperty(_slots, "offset_bottom", -100, 0.4)
.SetTrans(Tween.TransitionType.Quad)
.SetEase(Tween.EaseType.Out);
}
else
{
//GD.Print("Close inventory");
_inventoryExtensionTween = GetTree().CreateTween();
_slotSelect.Show();
_inventoryExtensionTween
.TweenProperty(_slots, "offset_bottom", 200, 0.4)
.SetTrans(Tween.TransitionType.Quad)
.SetEase(Tween.EaseType.Out);
_slotOnMouse = null;
}
}
}