diff --git a/UI/Inventory/Hotbar.cs b/UI/Inventory/Hotbar.cs index 18e2db7..afbf0c4 100644 --- a/UI/Inventory/Hotbar.cs +++ b/UI/Inventory/Hotbar.cs @@ -5,7 +5,7 @@ namespace SupaLidlGame.UI.Inventory; public partial class Hotbar : GridContainer { [Export] - private Godot.Collections.Array _slots; + private Godot.Collections.Array _slots; public override void _Ready() { diff --git a/UI/Inventory/Hotbar.tscn b/UI/Inventory/Hotbar.tscn index 1ac1ab4..f97f418 100644 --- a/UI/Inventory/Hotbar.tscn +++ b/UI/Inventory/Hotbar.tscn @@ -1,7 +1,7 @@ [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="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")] anchors_preset = 1 @@ -15,11 +15,11 @@ columns = 3 script = ExtResource("1_2sak2") _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 -[node name="InventorySlot2" parent="." instance=ExtResource("2_upggg")] +[node name="InventorySlot2" parent="." instance=ExtResource("2_3axfe")] layout_mode = 2 -[node name="InventorySlot3" parent="." instance=ExtResource("2_upggg")] +[node name="InventorySlot3" parent="." instance=ExtResource("2_3axfe")] layout_mode = 2 diff --git a/UI/Inventory/HotbarSlot.tscn b/UI/Inventory/HotbarSlot.tscn new file mode 100644 index 0000000..de7ce71 --- /dev/null +++ b/UI/Inventory/HotbarSlot.tscn @@ -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") diff --git a/UI/Inventory/InventoryGrid.cs b/UI/Inventory/InventoryGrid.cs index e5364d1..f51d07c 100644 --- a/UI/Inventory/InventoryGrid.cs +++ b/UI/Inventory/InventoryGrid.cs @@ -1,4 +1,5 @@ using Godot; +using GodotUtilities; using SupaLidlGame.Items; namespace SupaLidlGame.UI.Inventory; @@ -7,22 +8,52 @@ public partial class InventoryGrid : GridContainer { private SupaLidlGame.Items.Inventory _inventorySource; + [Export] + private PackedScene _slotScene; + public SupaLidlGame.Items.Inventory InventorySource { set { _inventorySource = value; + Redraw(); } get => _inventorySource; } - public override void _Ready() - { - - } - public void Redraw() { + if (_inventorySource is null) + { + this.QueueFreeChildren(); + } + var children = GetChildren(); + + for (int i = children.Count; i < _inventorySource.InventoryCapacity; i++) + { + AddChild(_slotScene.Instantiate()); + } + + 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]; + } + } } } diff --git a/UI/Inventory/InventoryGrid.tscn b/UI/Inventory/InventoryGrid.tscn index 74f26d5..291e659 100644 --- a/UI/Inventory/InventoryGrid.tscn +++ b/UI/Inventory/InventoryGrid.tscn @@ -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"] anchors_preset = 15 @@ -6,3 +9,6 @@ anchor_right = 1.0 anchor_bottom = 1.0 grow_horizontal = 2 grow_vertical = 2 +columns = 5 +script = ExtResource("1_7128g") +_slotScene = ExtResource("2_b6vp8") diff --git a/UI/Inventory/InventorySlot.cs b/UI/Inventory/InventorySlot.cs index e064fda..d8a4de8 100644 --- a/UI/Inventory/InventorySlot.cs +++ b/UI/Inventory/InventorySlot.cs @@ -4,16 +4,31 @@ using GodotUtilities.SourceGenerators; namespace SupaLidlGame.UI.Inventory; -[Scene] -public partial class InventorySlot : ColorRect +public partial class InventorySlot : Container { - [Node("TextureRect")] - protected TextureRect _textureRect; + private bool _isSelected = false; - [Node("Selected")] - protected NinePatchRect _frame; + public bool IsSelected + { + 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; @@ -41,12 +56,10 @@ public partial class InventorySlot : ColorRect "res://Assets/Sprites/UI/hotbar-inactive.png"); } - public override void _Notification(int what) + public override void _Ready() { - if (what == NotificationSceneInstantiated) - { - WireNodes(); - } - base._Notification(what); + _textureRect = GetNode("TextureRect"); + _frame = GetNode("Frame"); + _selectedFrame = GetNode("SelectedFrame"); } } diff --git a/UI/Inventory/InventorySlot.tscn b/UI/Inventory/InventorySlot.tscn index bfc1161..e27739b 100644 --- a/UI/Inventory/InventorySlot.tscn +++ b/UI/Inventory/InventorySlot.tscn @@ -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"] -[node name="InventorySlot" type="ColorRect"] +[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_6jbma"] + +[node name="InventorySlot" type="PanelContainer"] custom_minimum_size = Vector2(32, 32) -color = Color(1, 1, 1, 0) -script = ExtResource("1_tj1me") +theme_override_styles/panel = SubResource("StyleBoxEmpty_6jbma") +script = ExtResource("1_fju5i") [node name="TextureRect" type="TextureRect" parent="."] -layout_mode = 1 -anchors_preset = 15 -anchor_right = 1.0 -anchor_bottom = 1.0 -grow_horizontal = 2 -grow_vertical = 2 +layout_mode = 2 stretch_mode = 3 -[node name="Selected" 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="."] +[node name="Frame" type="NinePatchRect" parent="."] self_modulate = Color(1, 1, 1, 0.5) -layout_mode = 1 -anchors_preset = 15 -anchor_right = 1.0 -anchor_bottom = 1.0 -grow_horizontal = 2 -grow_vertical = 2 +layout_mode = 2 texture = ExtResource("2_m56j3")