2024-05-29 14:55:23 -07:00
|
|
|
using Godot;
|
2024-05-30 14:16:22 -07:00
|
|
|
using GodotUtilities;
|
2024-05-29 14:55:23 -07:00
|
|
|
using SupaLidlGame.Items;
|
|
|
|
|
|
|
|
namespace SupaLidlGame.UI.Inventory;
|
|
|
|
|
|
|
|
public partial class InventoryGrid : GridContainer
|
|
|
|
{
|
2024-08-21 16:20:35 -07:00
|
|
|
private Items.IItemCollection _source;
|
2024-05-29 14:55:23 -07:00
|
|
|
|
2024-05-30 14:16:22 -07:00
|
|
|
[Export]
|
|
|
|
private PackedScene _slotScene;
|
|
|
|
|
2024-06-01 14:21:34 -07:00
|
|
|
public ButtonGroup ButtonGroup { get; private set; }
|
|
|
|
|
2024-08-21 16:20:35 -07:00
|
|
|
public Items.IItemCollection Source
|
2024-05-29 14:55:23 -07:00
|
|
|
{
|
2024-06-01 14:21:34 -07:00
|
|
|
get => _source;
|
2024-05-29 14:55:23 -07:00
|
|
|
set
|
|
|
|
{
|
2024-06-01 14:21:34 -07:00
|
|
|
GD.Print("Set InventoryGrid source");
|
|
|
|
_source = value;
|
2024-05-30 14:16:22 -07:00
|
|
|
Redraw();
|
2024-05-29 14:55:23 -07:00
|
|
|
}
|
2024-06-01 14:21:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
[Signal]
|
|
|
|
public delegate void SlotFocusedEventHandler(InventorySlot slot);
|
|
|
|
|
|
|
|
[Signal]
|
|
|
|
public delegate void SlotUnfocusedEventHandler(InventorySlot slot);
|
|
|
|
|
|
|
|
[Signal]
|
|
|
|
public delegate void SlotSelectedEventHandler(InventorySlot slot);
|
|
|
|
|
|
|
|
public InventoryGrid()
|
|
|
|
{
|
|
|
|
ButtonGroup = new();
|
2024-05-29 14:55:23 -07:00
|
|
|
}
|
|
|
|
|
2024-05-30 14:16:22 -07:00
|
|
|
public void Redraw()
|
2024-05-29 14:55:23 -07:00
|
|
|
{
|
2024-06-01 14:21:34 -07:00
|
|
|
GD.Print("Redrawing inventory grid...");
|
|
|
|
|
|
|
|
if (_source is null)
|
2024-05-30 14:16:22 -07:00
|
|
|
{
|
|
|
|
this.QueueFreeChildren();
|
|
|
|
}
|
2024-05-29 14:55:23 -07:00
|
|
|
|
2024-05-30 14:16:22 -07:00
|
|
|
var children = GetChildren();
|
2024-05-29 14:55:23 -07:00
|
|
|
|
2024-06-01 14:21:34 -07:00
|
|
|
for (int i = children.Count; i < _source.Capacity; i++)
|
2024-05-30 14:16:22 -07:00
|
|
|
{
|
2024-06-01 14:21:34 -07:00
|
|
|
AddInventorySlot();
|
2024-05-30 14:16:22 -07:00
|
|
|
}
|
|
|
|
|
2024-06-01 14:21:34 -07:00
|
|
|
for (int i = children.Count - 1; i >= _source.Capacity; i--)
|
2024-05-30 14:16:22 -07:00
|
|
|
{
|
|
|
|
children[i].QueueFree();
|
|
|
|
}
|
2024-05-29 14:55:23 -07:00
|
|
|
|
2024-05-30 14:16:22 -07:00
|
|
|
children = GetChildren();
|
|
|
|
|
2024-06-01 14:21:34 -07:00
|
|
|
// iterate through items and update the grid
|
|
|
|
using (var items = _source.GetItems().GetEnumerator())
|
2024-05-30 14:16:22 -07:00
|
|
|
{
|
2024-06-01 14:21:34 -07:00
|
|
|
GD.Print("Updating items...");
|
|
|
|
int i;
|
|
|
|
for (i = 0; items.MoveNext(); i++)
|
|
|
|
{
|
|
|
|
InventorySlot slot = children[i] as InventorySlot;
|
2024-05-30 14:16:22 -07:00
|
|
|
|
2024-06-01 14:21:34 -07:00
|
|
|
ItemMetadata item = items.Current;
|
|
|
|
slot.Item = item;
|
2024-10-05 15:32:26 -07:00
|
|
|
slot.Index = i;
|
2024-06-01 14:21:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// make remaining slots display empty
|
|
|
|
for (; i < _source.Capacity; i++)
|
2024-05-30 14:16:22 -07:00
|
|
|
{
|
2024-06-01 14:21:34 -07:00
|
|
|
InventorySlot slot = children[i] as InventorySlot;
|
|
|
|
|
2024-05-30 14:16:22 -07:00
|
|
|
slot.Item = null;
|
2024-10-05 15:32:26 -07:00
|
|
|
slot.Index = i;
|
2024-05-30 14:16:22 -07:00
|
|
|
}
|
2024-06-01 14:21:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < children.Count; i++)
|
|
|
|
{
|
|
|
|
var child = children[i] as Control;
|
|
|
|
if (i > 0)
|
|
|
|
{
|
|
|
|
child.FocusPrevious = child.GetPathTo(children[i - 1]);
|
|
|
|
}
|
|
|
|
if (i < children.Count - 1)
|
2024-05-30 14:16:22 -07:00
|
|
|
{
|
2024-06-01 14:21:34 -07:00
|
|
|
child.FocusNext = child.GetPathTo(children[i + 1]);
|
2024-05-30 14:16:22 -07:00
|
|
|
}
|
|
|
|
}
|
2024-06-01 14:21:34 -07:00
|
|
|
|
|
|
|
if (children.Count > 0)
|
|
|
|
{
|
|
|
|
var button = children[0] as Button;
|
|
|
|
button.ButtonPressed = true;
|
|
|
|
button.GrabFocus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private InventorySlot AddInventorySlot()
|
|
|
|
{
|
|
|
|
var slot = _slotScene.Instantiate<InventorySlot>();
|
|
|
|
AddChild(slot);
|
|
|
|
slot.ButtonGroup = ButtonGroup;
|
|
|
|
|
|
|
|
void focusedHandler()
|
|
|
|
{
|
|
|
|
EmitSignal(SignalName.SlotFocused, slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
void unfocusedHandler()
|
|
|
|
{
|
|
|
|
EmitSignal(SignalName.SlotUnfocused, slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
void toggledHandler()
|
|
|
|
{
|
|
|
|
EmitSignal(SignalName.SlotSelected, slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
slot.Connect(
|
|
|
|
InventorySlot.SignalName.FocusEntered,
|
|
|
|
Callable.From(focusedHandler)
|
|
|
|
);
|
|
|
|
|
|
|
|
slot.Connect(
|
|
|
|
InventorySlot.SignalName.FocusExited,
|
|
|
|
Callable.From(unfocusedHandler)
|
|
|
|
);
|
|
|
|
|
|
|
|
slot.Connect(
|
|
|
|
InventorySlot.SignalName.MouseEntered,
|
|
|
|
Callable.From(focusedHandler)
|
|
|
|
);
|
|
|
|
|
|
|
|
slot.Connect(
|
|
|
|
InventorySlot.SignalName.MouseExited,
|
|
|
|
Callable.From(unfocusedHandler)
|
|
|
|
);
|
|
|
|
|
|
|
|
slot.Connect(
|
|
|
|
InventorySlot.SignalName.Pressed,
|
|
|
|
Callable.From(toggledHandler)
|
|
|
|
);
|
|
|
|
|
|
|
|
return slot;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void RemoveInventorySlot(InventorySlot slot)
|
|
|
|
{
|
|
|
|
RemoveChild(slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool GrabSlotFocus()
|
|
|
|
{
|
|
|
|
var children = GetChildren();
|
|
|
|
if (children.Count > 0)
|
|
|
|
{
|
|
|
|
var button = children[0] as Button;
|
|
|
|
button.GrabFocus();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2024-05-29 14:55:23 -07:00
|
|
|
}
|
|
|
|
}
|