From 667ef6fb9b011244f55f9fa826b5a4501ee3a486 Mon Sep 17 00:00:00 2001 From: cblech Date: Mon, 7 Apr 2025 02:52:57 +0200 Subject: [PATCH] Added open close inventory --- prefabs/UI/Inventory/Inventory.tscn | 14 +++---- prefabs/UI/Inventory/Slots.cs | 7 ++++ prefabs/UI/Inventory/Slots.cs.uid | 1 + project.godot | 5 +++ .../CSharp/Common/Inventory/InventoryUi.cs | 37 +++++++++++++++++++ 5 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 prefabs/UI/Inventory/Slots.cs create mode 100644 prefabs/UI/Inventory/Slots.cs.uid diff --git a/prefabs/UI/Inventory/Inventory.tscn b/prefabs/UI/Inventory/Inventory.tscn index 15356f4..95b0fdd 100644 --- a/prefabs/UI/Inventory/Inventory.tscn +++ b/prefabs/UI/Inventory/Inventory.tscn @@ -1,6 +1,7 @@ -[gd_scene load_steps=4 format=3 uid="uid://cgjc4wurbgimy"] +[gd_scene load_steps=5 format=3 uid="uid://cgjc4wurbgimy"] [ext_resource type="Script" uid="uid://b7vlkecrn0t5c" path="res://scripts/CSharp/Common/Inventory/InventoryUi.cs" id="1_6wusm"] +[ext_resource type="Script" uid="uid://6def3c0mcnoq" path="res://prefabs/UI/Inventory/Slots.cs" id="2_5fdxq"] [ext_resource type="Script" uid="uid://b2jhdxcrhtm2d" path="res://scripts/CSharp/Common/Inventory/InventoryTestScript.cs" id="3_exrk4"] [ext_resource type="Resource" uid="uid://datee0flk1e84" path="res://resources/items/pickaxe.tres" id="4_5fdxq"] @@ -16,17 +17,16 @@ script = ExtResource("1_6wusm") [node name="Slots" type="GridContainer" parent="."] custom_minimum_size = Vector2(500, 200) layout_mode = 1 -anchors_preset = 8 +anchors_preset = -1 anchor_left = 0.5 -anchor_top = 0.5 +anchor_top = 1.0 anchor_right = 0.5 -anchor_bottom = 0.5 -offset_left = -335.0 -offset_right = 341.0 +anchor_bottom = 1.0 offset_bottom = 200.0 grow_horizontal = 2 -grow_vertical = 2 +grow_vertical = 0 columns = 10 +script = ExtResource("2_5fdxq") [node name="InventoryTester" type="Node" parent="."] script = ExtResource("3_exrk4") diff --git a/prefabs/UI/Inventory/Slots.cs b/prefabs/UI/Inventory/Slots.cs new file mode 100644 index 0000000..8294a2c --- /dev/null +++ b/prefabs/UI/Inventory/Slots.cs @@ -0,0 +1,7 @@ +using Godot; +using System; + +public partial class Slots : GridContainer +{ + +} diff --git a/prefabs/UI/Inventory/Slots.cs.uid b/prefabs/UI/Inventory/Slots.cs.uid new file mode 100644 index 0000000..1b60808 --- /dev/null +++ b/prefabs/UI/Inventory/Slots.cs.uid @@ -0,0 +1 @@ +uid://6def3c0mcnoq diff --git a/project.godot b/project.godot index d163539..bba5e15 100644 --- a/project.godot +++ b/project.godot @@ -115,6 +115,11 @@ interact={ "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":69,"key_label":0,"unicode":101,"location":0,"echo":false,"script":null) ] } +ui_inventory_open_close={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":69,"key_label":0,"unicode":101,"location":0,"echo":false,"script":null) +] +} [internationalization] diff --git a/scripts/CSharp/Common/Inventory/InventoryUi.cs b/scripts/CSharp/Common/Inventory/InventoryUi.cs index 2774d11..086d8da 100644 --- a/scripts/CSharp/Common/Inventory/InventoryUi.cs +++ b/scripts/CSharp/Common/Inventory/InventoryUi.cs @@ -1,3 +1,4 @@ +#nullable enable using Godot; namespace Babushka.scripts.CSharp.Common.Inventory; @@ -9,6 +10,9 @@ public partial class InventoryUi : Control private int? _slotOnMouse; + private bool _inventoryExtended = false; + private Tween? _inventoryExtensionTween; + public override void _Ready() { GD.Print("Ready inventory ui"); @@ -59,9 +63,12 @@ public partial class InventoryUi : Control 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}"); } @@ -74,4 +81,34 @@ public partial class InventoryUi : Control SetSlotContent(); } } + + public override void _Process(double delta) + { + if (Input.IsActionJustPressed("ui_inventory_open_close")) + { + _inventoryExtensionTween?.Kill(); + + _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); + + _slotOnMouse = null; + } + } + } }