Added slot select

This commit is contained in:
cblech
2025-04-07 18:43:56 +02:00
parent 77591425cb
commit daa53e4aef
10 changed files with 133 additions and 26 deletions
+54 -23
View File
@@ -7,19 +7,24 @@ 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>("Slots");
_slots = GetNode<GridContainer>("SlotsContainer/Slots");
_playerInventory = InventoryManager.Instance.playerInventory;
_slotSelect = GetNode<Control>("SlotsContainer/SlotSelectContainer/Selector");
PopulateSlots();
SetSlotContent();
SetSlotSelectPosition();
}
public override void _ExitTree()
@@ -40,6 +45,11 @@ public partial class InventoryUi : Control
}
}
private void SetSlotSelectPosition()
{
_slotSelect.Position = new Vector2(_selectedSlot * 100, 0);
}
private void PopulateSlots()
{
var slotPackedScene = GD.Load<PackedScene>("res://prefabs/UI/Inventory/Slot.tscn");
@@ -64,7 +74,7 @@ public partial class InventoryUi : Control
private void SlotClicked(int index)
{
if (!_inventoryExtended) return;
GD.Print($"Clicked slot {index}");
if (_slotOnMouse == null)
{
@@ -86,29 +96,50 @@ public partial class InventoryUi : Control
{
if (Input.IsActionJustPressed("ui_inventory_open_close"))
{
_inventoryExtensionTween?.Kill();
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();
}
}
_inventoryExtended = !_inventoryExtended;
if (_inventoryExtended)
{
//GD.Print("Open inventory");
_inventoryExtensionTween = GetTree().CreateTween();
_inventoryExtensionTween
.TweenProperty(_slots, "offset_bottom", -100, 0.4)
.SetTrans(Tween.TransitionType.Quad)
.SetEase(Tween.EaseType.Out);
}
else
{
//GD.Print("Close inventory");
_inventoryExtensionTween = GetTree().CreateTween();
_inventoryExtensionTween
.TweenProperty(_slots, "offset_bottom", 200, 0.4)
.SetTrans(Tween.TransitionType.Quad)
.SetEase(Tween.EaseType.Out);
private void InputInventoryOpenClose()
{
_inventoryExtensionTween?.Kill();
_slotOnMouse = null;
}
_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;
}
}
}