Modify Hotbar and InventorySlot organization

Made InventorySlot a bit more modular.
pull/37/head
John Montagu, the 4th Earl of Sandvich 2024-05-30 14:16:22 -07:00
parent 71f14c5121
commit 78daa89ba6
Signed by: sandvich
GPG Key ID: 9A39BE37E602B22D
7 changed files with 95 additions and 52 deletions

View File

@ -5,7 +5,7 @@ namespace SupaLidlGame.UI.Inventory;
public partial class Hotbar : GridContainer public partial class Hotbar : GridContainer
{ {
[Export] [Export]
private Godot.Collections.Array<HotbarSlot> _slots; private Godot.Collections.Array<InventorySlot> _slots;
public override void _Ready() public override void _Ready()
{ {

View File

@ -1,7 +1,7 @@
[gd_scene load_steps=3 format=3 uid="uid://sfs8dpfitpdu"] [gd_scene load_steps=3 format=3 uid="uid://sfs8dpfitpdu"]
[ext_resource type="Script" path="res://UI/Inventory/Hotbar.cs" id="1_2sak2"] [ext_resource type="Script" path="res://UI/Inventory/Hotbar.cs" id="1_2sak2"]
[ext_resource type="PackedScene" uid="uid://ctad0dkoyw8ad" path="res://UI/Inventory/InventorySlot.tscn" id="2_upggg"] [ext_resource type="PackedScene" uid="uid://dmvu2hjyrwc1y" path="res://UI/Inventory/HotbarSlot.tscn" id="2_3axfe"]
[node name="Hotbar" type="GridContainer" node_paths=PackedStringArray("_slots")] [node name="Hotbar" type="GridContainer" node_paths=PackedStringArray("_slots")]
anchors_preset = 1 anchors_preset = 1
@ -15,11 +15,11 @@ columns = 3
script = ExtResource("1_2sak2") script = ExtResource("1_2sak2")
_slots = [NodePath("InventorySlot"), NodePath("InventorySlot2"), NodePath("InventorySlot3")] _slots = [NodePath("InventorySlot"), NodePath("InventorySlot2"), NodePath("InventorySlot3")]
[node name="InventorySlot" parent="." instance=ExtResource("2_upggg")] [node name="InventorySlot" parent="." instance=ExtResource("2_3axfe")]
layout_mode = 2 layout_mode = 2
[node name="InventorySlot2" parent="." instance=ExtResource("2_upggg")] [node name="InventorySlot2" parent="." instance=ExtResource("2_3axfe")]
layout_mode = 2 layout_mode = 2
[node name="InventorySlot3" parent="." instance=ExtResource("2_upggg")] [node name="InventorySlot3" parent="." instance=ExtResource("2_3axfe")]
layout_mode = 2 layout_mode = 2

View File

@ -0,0 +1,11 @@
[gd_scene load_steps=3 format=3 uid="uid://dmvu2hjyrwc1y"]
[ext_resource type="PackedScene" uid="uid://ctad0dkoyw8ad" path="res://UI/Inventory/InventorySlot.tscn" id="1_fb62b"]
[ext_resource type="Texture2D" uid="uid://dc1gcsbhkchvg" path="res://Assets/Sprites/UI/hotbar-active.png" id="2_bcv71"]
[node name="InventorySlot" instance=ExtResource("1_fb62b")]
[node name="SelectedFrame" type="NinePatchRect" parent="." index="2"]
visible = false
layout_mode = 2
texture = ExtResource("2_bcv71")

View File

@ -1,4 +1,5 @@
using Godot; using Godot;
using GodotUtilities;
using SupaLidlGame.Items; using SupaLidlGame.Items;
namespace SupaLidlGame.UI.Inventory; namespace SupaLidlGame.UI.Inventory;
@ -7,22 +8,52 @@ public partial class InventoryGrid : GridContainer
{ {
private SupaLidlGame.Items.Inventory _inventorySource; private SupaLidlGame.Items.Inventory _inventorySource;
[Export]
private PackedScene _slotScene;
public SupaLidlGame.Items.Inventory InventorySource public SupaLidlGame.Items.Inventory InventorySource
{ {
set set
{ {
_inventorySource = value; _inventorySource = value;
Redraw();
} }
get => _inventorySource; get => _inventorySource;
} }
public override void _Ready()
{
}
public void Redraw() public void Redraw()
{ {
if (_inventorySource is null)
{
this.QueueFreeChildren();
}
var children = GetChildren();
for (int i = children.Count; i < _inventorySource.InventoryCapacity; i++)
{
AddChild(_slotScene.Instantiate<InventorySlot>());
}
for (int i = children.Count - 1; i >= _inventorySource.InventoryCapacity; i--)
{
children[i].QueueFree();
}
children = GetChildren();
for (int i = 0; i < children.Count; i++)
{
InventorySlot slot = children[i] as InventorySlot;
if (i >= _inventorySource.Items.Count)
{
slot.Item = null;
}
else if (slot.Item != _inventorySource.Items[i])
{
slot.Item = _inventorySource.Items[i];
}
}
} }
} }

View File

@ -1,4 +1,7 @@
[gd_scene format=3 uid="uid://chmokkxsy5vas"] [gd_scene load_steps=3 format=3 uid="uid://chmokkxsy5vas"]
[ext_resource type="Script" path="res://UI/Inventory/InventoryGrid.cs" id="1_7128g"]
[ext_resource type="PackedScene" uid="uid://ctad0dkoyw8ad" path="res://UI/Inventory/InventorySlot.tscn" id="2_b6vp8"]
[node name="InventoryGrid" type="GridContainer"] [node name="InventoryGrid" type="GridContainer"]
anchors_preset = 15 anchors_preset = 15
@ -6,3 +9,6 @@ anchor_right = 1.0
anchor_bottom = 1.0 anchor_bottom = 1.0
grow_horizontal = 2 grow_horizontal = 2
grow_vertical = 2 grow_vertical = 2
columns = 5
script = ExtResource("1_7128g")
_slotScene = ExtResource("2_b6vp8")

View File

@ -4,16 +4,31 @@ using GodotUtilities.SourceGenerators;
namespace SupaLidlGame.UI.Inventory; namespace SupaLidlGame.UI.Inventory;
[Scene] public partial class InventorySlot : Container
public partial class InventorySlot : ColorRect
{ {
[Node("TextureRect")] private bool _isSelected = false;
protected TextureRect _textureRect;
[Node("Selected")] public bool IsSelected
protected NinePatchRect _frame; {
get => _isSelected;
set
{
_isSelected = value;
if (_selectedFrame is not null)
{
_selectedFrame.Visible = _isSelected;
_frame.Visible = !_isSelected;
}
}
}
protected static Texture2D _placeholderTexture; private TextureRect _textureRect;
private NinePatchRect _frame;
private NinePatchRect _selectedFrame;
private static Texture2D _placeholderTexture;
private Items.ItemMetadata _item; private Items.ItemMetadata _item;
@ -41,12 +56,10 @@ public partial class InventorySlot : ColorRect
"res://Assets/Sprites/UI/hotbar-inactive.png"); "res://Assets/Sprites/UI/hotbar-inactive.png");
} }
public override void _Notification(int what) public override void _Ready()
{ {
if (what == NotificationSceneInstantiated) _textureRect = GetNode<TextureRect>("TextureRect");
{ _frame = GetNode<NinePatchRect>("Frame");
WireNodes(); _selectedFrame = GetNode<NinePatchRect>("SelectedFrame");
}
base._Notification(what);
} }
} }

View File

@ -1,38 +1,20 @@
[gd_scene load_steps=3 format=3 uid="uid://ctad0dkoyw8ad"] [gd_scene load_steps=4 format=3 uid="uid://ctad0dkoyw8ad"]
[ext_resource type="Script" path="res://UI/Inventory/InventorySlot.cs" id="1_tj1me"] [ext_resource type="Script" path="res://UI/Inventory/InventorySlot.cs" id="1_fju5i"]
[ext_resource type="Texture2D" uid="uid://dc1gcsbhkchvg" path="res://Assets/Sprites/UI/hotbar-active.png" id="2_m56j3"] [ext_resource type="Texture2D" uid="uid://dc1gcsbhkchvg" path="res://Assets/Sprites/UI/hotbar-active.png" id="2_m56j3"]
[node name="InventorySlot" type="ColorRect"] [sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_6jbma"]
[node name="InventorySlot" type="PanelContainer"]
custom_minimum_size = Vector2(32, 32) custom_minimum_size = Vector2(32, 32)
color = Color(1, 1, 1, 0) theme_override_styles/panel = SubResource("StyleBoxEmpty_6jbma")
script = ExtResource("1_tj1me") script = ExtResource("1_fju5i")
[node name="TextureRect" type="TextureRect" parent="."] [node name="TextureRect" type="TextureRect" parent="."]
layout_mode = 1 layout_mode = 2
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
stretch_mode = 3 stretch_mode = 3
[node name="Selected" type="NinePatchRect" parent="."] [node name="Frame" type="NinePatchRect" parent="."]
visible = false
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
texture = ExtResource("2_m56j3")
[node name="Unselected" type="NinePatchRect" parent="."]
self_modulate = Color(1, 1, 1, 0.5) self_modulate = Color(1, 1, 1, 0.5)
layout_mode = 1 layout_mode = 2
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
texture = ExtResource("2_m56j3") texture = ExtResource("2_m56j3")