From 1e5b466c6ff8a3a44bcc7e5290ce2a8456795e0a Mon Sep 17 00:00:00 2001 From: Cold-Mint Date: Wed, 19 Jun 2024 22:33:00 +0800 Subject: [PATCH] =?UTF-8?q?Add=20whether=20the=20item=20is=20selected.Adju?= =?UTF-8?q?st=20the=20display=20logic=20of=20the=20backpack=20UI.=20?= =?UTF-8?q?=E5=8A=A0=E5=85=A5=E7=89=A9=E5=93=81=E6=98=AF=E5=90=A6=E8=A2=AB?= =?UTF-8?q?=E9=80=89=E6=8B=A9=E3=80=82=E8=B0=83=E6=95=B4=E8=83=8C=E5=8C=85?= =?UTF-8?q?UI=E7=9A=84=E6=98=BE=E7=A4=BA=E9=80=BB=E8=BE=91=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- locals/slogan.csv.import | 6 +- scripts/GameSceneNodeHolder.cs | 35 ++++++++++++ scripts/character/Player.cs | 1 + scripts/inventory/HotBar.cs | 4 +- scripts/inventory/IItemContainer.cs | 6 ++ scripts/inventory/ItemSlotNode.cs | 16 +----- scripts/inventory/UniversalItemContainer.cs | 61 ++++++++++++++++++--- scripts/item/Packsack.cs | 1 + scripts/map/events/ItemStackChangeEvent.cs | 16 ------ scripts/utils/NodeUtils.cs | 36 +++++++++++- 10 files changed, 140 insertions(+), 42 deletions(-) delete mode 100644 scripts/map/events/ItemStackChangeEvent.cs diff --git a/locals/slogan.csv.import b/locals/slogan.csv.import index 2ffa9fd..a8a05f0 100644 --- a/locals/slogan.csv.import +++ b/locals/slogan.csv.import @@ -6,10 +6,10 @@ uid="uid://cc0k86apkvut7" [deps] -files=["res://locals/Slogan.zh.translation", "res://locals/Slogan.en.translation", "res://locals/Slogan.ja.translation"] +files=["res://locals/slogan.zh.translation", "res://locals/slogan.en.translation", "res://locals/slogan.ja.translation"] -source_file="res://locals/Slogan.csv" -dest_files=["res://locals/Slogan.zh.translation", "res://locals/Slogan.en.translation", "res://locals/Slogan.ja.translation"] +source_file="res://locals/slogan.csv" +dest_files=["res://locals/slogan.zh.translation", "res://locals/slogan.en.translation", "res://locals/slogan.ja.translation"] [params] diff --git a/scripts/GameSceneNodeHolder.cs b/scripts/GameSceneNodeHolder.cs index 7ef8d08..e97cd51 100644 --- a/scripts/GameSceneNodeHolder.cs +++ b/scripts/GameSceneNodeHolder.cs @@ -1,6 +1,9 @@ using ColdMint.scripts.character; using ColdMint.scripts.inventory; +using ColdMint.scripts.item; +using ColdMint.scripts.loader.uiLoader; using ColdMint.scripts.map.events; +using ColdMint.scripts.utils; using Godot; namespace ColdMint.scripts; @@ -85,4 +88,36 @@ public static class GameSceneNodeHolder ///背包Ui容器内存放的是背包ui节点的容器。当用户使用背包时,会从背包ui容器内将其背包对于的节点展示出来。 /// public static Control? BackpackUiContainer { get; set; } + + + /// + /// Hide the knapsack node in the knapsack Ui if the knapsack UI is displayed + /// 如果背包Ui处于显示状态,那么隐藏背包UI内的背包节点 + /// + public static void HideBackpackUiContainerIfVisible() + { + if (BackpackUiContainer == null) + { + return; + } + + if (!BackpackUiContainer.Visible) + { + return; + } + + NodeUtils.ForEachNode(BackpackUiContainer, node => + { + + //If the child node is not visible, the traversal continues. + //如果子节点不可见,则继续遍历。 + if (!node.Visible) + return false; + //Until you find a visible node, hide it, and return true, ending the loop. + //直到找到可见的节点,隐藏该节点,然后返回true,结束遍历。 + node.Hide(); + return true; + + }); + } } \ No newline at end of file diff --git a/scripts/character/Player.cs b/scripts/character/Player.cs index e5f57bb..f618a05 100644 --- a/scripts/character/Player.cs +++ b/scripts/character/Player.cs @@ -264,6 +264,7 @@ public partial class Player : CharacterTemplate } ThrowItem(ItemContainer.GetSelectIndex(), 1, GetThrowVelocity()); + GameSceneNodeHolder.HideBackpackUiContainerIfVisible(); CurrentItem = null; } } diff --git a/scripts/inventory/HotBar.cs b/scripts/inventory/HotBar.cs index 84e7a27..98fa97f 100644 --- a/scripts/inventory/HotBar.cs +++ b/scripts/inventory/HotBar.cs @@ -45,6 +45,7 @@ public partial class HotBar : HBoxContainer { //Mouse wheel down //鼠标滚轮向下 + GameSceneNodeHolder.HideBackpackUiContainerIfVisible(); _itemContainer?.SelectTheNextItemSlot(); } @@ -52,6 +53,7 @@ public partial class HotBar : HBoxContainer { //Mouse wheel up //鼠标滚轮向上 + GameSceneNodeHolder.HideBackpackUiContainerIfVisible(); _itemContainer?.SelectThePreviousItemSlot(); } @@ -110,11 +112,11 @@ public partial class HotBar : HBoxContainer /// private void SelectItemSlotByHotBarShortcutKey(int shortcutKeyIndex) { + GameSceneNodeHolder.HideBackpackUiContainerIfVisible(); if (_itemContainer == null) { return; } - _itemContainer.SelectItemSlot(shortcutKeyIndex); } diff --git a/scripts/inventory/IItemContainer.cs b/scripts/inventory/IItemContainer.cs index 95bd643..b9a3345 100644 --- a/scripts/inventory/IItemContainer.cs +++ b/scripts/inventory/IItemContainer.cs @@ -31,6 +31,12 @@ public interface IItemContainer : IEnumerable /// /// bool AddItem(IItem item); + + /// + /// Whether this item container supports checking + /// 此物品容器是否支持选中 + /// + public bool SupportSelect { get; set; } /// /// Determines the number of items that can be received from the specified pile diff --git a/scripts/inventory/ItemSlotNode.cs b/scripts/inventory/ItemSlotNode.cs index 1ada763..a6be80e 100644 --- a/scripts/inventory/ItemSlotNode.cs +++ b/scripts/inventory/ItemSlotNode.cs @@ -1,7 +1,5 @@ -using System; using ColdMint.scripts.item; using ColdMint.scripts.item.itemStacks; -using ColdMint.scripts.map.events; using ColdMint.scripts.utils; using Godot; @@ -21,7 +19,6 @@ public partial class ItemSlotNode : MarginContainer private bool _isSelect; private Texture2D? _backgroundTexture; private Texture2D? _backgroundTextureWhenSelect; - public Action? ItemStackChangeEvent; public override void _Ready() { @@ -37,7 +34,7 @@ public partial class ItemSlotNode : MarginContainer public override Variant _GetDragData(Vector2 atPosition) { - if (_iconTextureRect == null || _itemStack == null) + if (_isSelect || _iconTextureRect == null || _itemStack == null) { //Drag is not allowed if there is no icon or no pile of items. //如果没有图标或者没有物品堆,那么不允许拖动。 @@ -77,8 +74,7 @@ public partial class ItemSlotNode : MarginContainer //尝试获取源物品堆时返回null。 return false; } - - //TODO:This is where we infer whether the two piles can merge.在这里推断两个物品堆是否可以融合。 + return _itemStack == null; } @@ -105,6 +101,7 @@ public partial class ItemSlotNode : MarginContainer //尝试获取源物品堆时返回null。 return; } + ReplaceItemStack(itemStack); } @@ -411,11 +408,6 @@ public partial class ItemSlotNode : MarginContainer private void SetItemStack(IItemStack? itemStack) { _itemStack = itemStack; - var stackChangeEvent = new ItemStackChangeEvent - { - ItemStack = itemStack - }; - ItemStackChangeEvent?.Invoke(stackChangeEvent); } /// @@ -429,6 +421,4 @@ public partial class ItemSlotNode : MarginContainer _iconTextureRect.Texture = _itemStack?.Icon; } } - - } \ No newline at end of file diff --git a/scripts/inventory/UniversalItemContainer.cs b/scripts/inventory/UniversalItemContainer.cs index 0b4cb1a..c6e5162 100644 --- a/scripts/inventory/UniversalItemContainer.cs +++ b/scripts/inventory/UniversalItemContainer.cs @@ -36,6 +36,9 @@ public class UniversalItemContainer : IItemContainer //_selectIndex默认为0. private int _selectIndex; + + public bool SupportSelect { get; set; } = true; + public bool CanAddItem(IItem item) { return Match(item) != null; @@ -78,12 +81,17 @@ public class UniversalItemContainer : IItemContainer public int GetSelectIndex() { + if (!SupportSelect) + { + return 0; + } + return _selectIndex; } public ItemSlotNode? GetSelectItemSlotNode() { - if (_itemSlotNodes == null || _itemSlotNodes.Count == 0) + if (!SupportSelect || _itemSlotNodes == null || _itemSlotNodes.Count == 0) { return null; } @@ -98,11 +106,35 @@ public class UniversalItemContainer : IItemContainer return null; } - public IItem? PickItemFromItemSlotBySelectIndex() => PickItemFromItemSlot(_selectIndex); + public IItem? PickItemFromItemSlotBySelectIndex() + { + if (!SupportSelect) + { + return null; + } - public IItemStack? PickItemsFromItemSlotBySelectIndex(int value) => PickItemsFromItemSlot(_selectIndex, value); + return PickItemFromItemSlot(_selectIndex); + } - public int RemoveItemFromItemSlotBySelectIndex(int number) => RemoveItemFromItemSlot(_selectIndex, number); + public IItemStack? PickItemsFromItemSlotBySelectIndex(int value) + { + if (!SupportSelect) + { + return null; + } + + return PickItemsFromItemSlot(_selectIndex, value); + } + + public int RemoveItemFromItemSlotBySelectIndex(int number) + { + if (!SupportSelect) + { + return 0; + } + + return RemoveItemFromItemSlot(_selectIndex, number); + } public int GetItemSlotCount() { @@ -232,7 +264,15 @@ public class UniversalItemContainer : IItemContainer return null; } - itemSlotNode.IsSelect = (_itemSlotNodes.Count) == _selectIndex; + if (SupportSelect) + { + itemSlotNode.IsSelect = (_itemSlotNodes.Count) == _selectIndex; + } + else + { + itemSlotNode.IsSelect = false; + } + _itemSlotNodes.Add(itemSlotNode); // itemSlotNode.ItemStackChangeEvent += @event => // { @@ -265,7 +305,7 @@ public class UniversalItemContainer : IItemContainer public void SelectTheNextItemSlot() { - if (_itemSlotNodes == null) + if (!SupportSelect || _itemSlotNodes == null) { return; } @@ -288,7 +328,7 @@ public class UniversalItemContainer : IItemContainer public void SelectThePreviousItemSlot() { - if (_itemSlotNodes == null) + if (!SupportSelect || _itemSlotNodes == null) { return; } @@ -311,7 +351,7 @@ public class UniversalItemContainer : IItemContainer public void SelectItemSlot(int newSelectIndex) { - if (newSelectIndex == _selectIndex) + if (!SupportSelect || newSelectIndex == _selectIndex) { return; } @@ -331,6 +371,11 @@ public class UniversalItemContainer : IItemContainer /// private void PrivateSelectItemSlot(int oldSelectIndex, int newSelectIndex) { + if (!SupportSelect) + { + return; + } + if (oldSelectIndex == newSelectIndex) { return; diff --git a/scripts/item/Packsack.cs b/scripts/item/Packsack.cs index 626592c..e29b3fa 100644 --- a/scripts/item/Packsack.cs +++ b/scripts/item/Packsack.cs @@ -57,6 +57,7 @@ public partial class Packsack : PickAbleTemplate { base._Ready(); ItemContainer = new UniversalItemContainer(); + ItemContainer.SupportSelect = false; //When the backpack is created, the item slot is generated. //当背包被创建时,物品槽就被生成出来了。 for (var i = 0; i < NumberSlots; i++) diff --git a/scripts/map/events/ItemStackChangeEvent.cs b/scripts/map/events/ItemStackChangeEvent.cs deleted file mode 100644 index 3fb632f..0000000 --- a/scripts/map/events/ItemStackChangeEvent.cs +++ /dev/null @@ -1,16 +0,0 @@ -using ColdMint.scripts.item.itemStacks; - -namespace ColdMint.scripts.map.events; - -/// -/// ItemStackChangeEvent -/// 物品堆改变事件 -/// -public class ItemStackChangeEvent -{ - /// - /// Changed ItemStack - /// 改变后的物品堆 - /// - public IItemStack? ItemStack { get; set; } -} \ No newline at end of file diff --git a/scripts/utils/NodeUtils.cs b/scripts/utils/NodeUtils.cs index 3e0e6f1..538659b 100644 --- a/scripts/utils/NodeUtils.cs +++ b/scripts/utils/NodeUtils.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Threading.Tasks; using ColdMint.scripts.debug; using ColdMint.scripts.item; @@ -36,6 +37,39 @@ public static class NodeUtils return deleteNumber; } + + /// + /// Traverse the child nodes of type T under the parent node + /// 遍历父节点下T类型的子节点 + /// + /// + /// + ///A function that handles callbacks and returns true to terminate the traversal of the node + ///用于处理回调的函数,返回true终止遍历节点 + /// + /// + ///When the type is specified as Node, all child nodes are returned. + ///当指定类型为Node时,将返回所有子节点。 + /// + public static void ForEachNode(Node parent, Func func) where T : Node + { + var count = parent.GetChildCount(); + if (count <= 0) + { + return; + } + + for (var i = 0; i < count; i++) + { + var node = parent.GetChild(i); + if (node is not T t) continue; + if (func.Invoke(t)) + { + break; + } + } + } + /// /// All child nodes are removed asynchronously /// 异步删除所有子节点