Backpack Ui will not prevent clicking the button below anymore.

背包Ui不会阻止点击下面的按钮了。
This commit is contained in:
Cold-Mint 2024-06-17 22:12:51 +08:00
parent 8391396191
commit c1f0e30671
Signed by: Cold-Mint
GPG Key ID: C5A9BF8A98E0CE99
10 changed files with 90 additions and 54 deletions

View File

@ -13,7 +13,10 @@ size = Vector2(21, 29)
collision_layer = 8
collision_mask = 38
script = ExtResource("1_slakl")
NumberSlots = 30
Id = "packsack"
UniqueName = null
UniqueDescription = null
_minContactInjury = null
_maxContactInjury = null

View File

@ -7,6 +7,7 @@
offset_right = 38.0
offset_bottom = 38.0
size_flags_vertical = 3
mouse_filter = 0
theme_override_constants/margin_left = 3
theme_override_constants/margin_top = 3
theme_override_constants/margin_right = 3
@ -15,6 +16,7 @@ script = ExtResource("1_fbwot")
[node name="BackgroundTexture" type="TextureRect" parent="."]
layout_mode = 2
mouse_filter = 2
texture = ExtResource("1_y2wyt")
[node name="IconTextureRect" type="TextureRect" parent="BackgroundTexture"]
@ -28,10 +30,12 @@ offset_right = -3.0
offset_bottom = -3.0
grow_horizontal = 2
grow_vertical = 2
mouse_filter = 2
expand_mode = 1
[node name="Control" type="Control" parent="."]
layout_mode = 2
mouse_filter = 2
[node name="QuantityLabel" type="Label" parent="Control"]
layout_mode = 1

View File

@ -9,46 +9,37 @@ anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
mouse_filter = 2
script = ExtResource("1_wm4bu")
[node name="ColorRect" type="ColorRect" parent="."]
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
color = Color(0, 0, 0, 0.294118)
[node name="TitleLabel" type="Label" parent="."]
layout_mode = 1
anchors_preset = 10
anchor_right = 1.0
offset_bottom = 30.0
offset_bottom = 25.0
grow_horizontal = 2
text = "ui_name"
horizontal_alignment = 1
[node name="GridContainer" type="GridContainer" parent="."]
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
offset_left = 36.0
offset_top = 57.0
offset_right = -34.0
offset_bottom = -23.0
grow_horizontal = 2
grow_vertical = 2
[node name="ExitButton" type="Button" parent="."]
layout_mode = 1
anchors_preset = 1
anchor_left = 1.0
anchor_right = 1.0
offset_left = -75.0
offset_top = 8.0
offset_right = -8.0
offset_bottom = 41.0
offset_left = -91.0
offset_top = 11.0
offset_right = -24.0
offset_bottom = 44.0
grow_horizontal = 0
text = "ui_close"
[node name="HFlowContainer" type="HFlowContainer" parent="."]
layout_mode = 1
anchors_preset = 10
anchor_right = 1.0
offset_left = 26.0
offset_top = 70.0
offset_right = -29.0
offset_bottom = 389.0
grow_horizontal = 2
mouse_filter = 2

View File

@ -91,6 +91,7 @@ anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
mouse_filter = 2
[node name="GameOverMenu" parent="CanvasLayer" instance=ExtResource("6_yjmrv")]
visible = false

View File

@ -219,7 +219,7 @@ public interface IItemContainer : IEnumerable<ItemSlotNode>
/// <para>添加物品槽</para>
/// </summary>
/// <param name="rootNode"></param>
void AddItemSlot(Node rootNode);
ItemSlotNode? AddItemSlot(Node rootNode);
/// <summary>
/// <para>SelectTheNextItemSlot</para>

View File

@ -1,7 +1,7 @@
using ColdMint.scripts.debug;
using ColdMint.scripts.item;
using ColdMint.scripts.item.itemStacks;
using ColdMint.scripts.utils;
using Godot;
namespace ColdMint.scripts.inventory;
@ -21,6 +21,34 @@ public partial class ItemSlotNode : MarginContainer
private Texture2D? _backgroundTexture;
private Texture2D? _backgroundTextureWhenSelect;
public override Variant _GetDragData(Vector2 atPosition)
{
if (_iconTextureRect == null)
{
return base._GetDragData(atPosition);
}
var textureRect = new TextureRect();
textureRect.ExpandMode = _iconTextureRect.ExpandMode;
textureRect.Size = _iconTextureRect.Size;
textureRect.Texture = _iconTextureRect.Texture;
SetDragPreview(textureRect);
return Variant.From(this);
}
public override bool _CanDropData(Vector2 atPosition, Variant data)
{
if (_iconTextureRect == null)
{
return false;
}
//TODO:在这里判断是否可以放置物品。物品槽必须是空的。
// var itemSlotNode = data.As<ItemSlotNode>();
// itemSlotNode._itemStack
return true;
}
public bool IsSelect
{
get => _isSelect;
@ -272,9 +300,9 @@ public partial class ItemSlotNode : MarginContainer
if (debugText != null)
{
_control.TooltipText = string.Format(debugText, _itemStack.GetItem()?.Id,
TranslationServerUtils.Translate(_itemStack.Name),
_itemStack.Quantity, _itemStack.MaxQuantity, _itemStack.GetType().Name,
TranslationServerUtils.Translate(_itemStack.Description));
TranslationServerUtils.Translate(_itemStack.Name),
_itemStack.Quantity, _itemStack.MaxQuantity, _itemStack.GetType().Name,
TranslationServerUtils.Translate(_itemStack.Description));
}
}
else

View File

@ -2,14 +2,11 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using ColdMint.scripts.character;
using ColdMint.scripts.item;
using ColdMint.scripts.item.itemStacks;
using ColdMint.scripts.utils;
using Godot;
using JetBrains.Annotations;
namespace ColdMint.scripts.inventory;
@ -62,7 +59,7 @@ public class UniversalItemContainer : IItemContainer
var slots = MatchAll(slot => slot.CanAddItem(testItem));
return
Math.Min(itemStack.Quantity,
slots.Select(slot => slot.CanAddItemStack(itemStack)).Sum());
slots.Select(slot => slot.CanAddItemStack(itemStack)).Sum());
}
public bool AddItemStack(IItemStack itemStack)
@ -72,7 +69,7 @@ public class UniversalItemContainer : IItemContainer
{
if (itemSlotNode.AddItemStack(itemStack))
return true;
itemSlotNode = Match(itemStack);
}
@ -222,21 +219,22 @@ public class UniversalItemContainer : IItemContainer
/// <para>Add items tank</para>
/// <para>添加物品槽</para>
/// </summary>
public void AddItemSlot(Node rootNode)
public ItemSlotNode? AddItemSlot(Node rootNode)
{
if (_itemSlotNodes == null || _itemSlotPackedScene == null)
{
return;
return null;
}
var itemSlotNode = NodeUtils.InstantiatePackedScene<ItemSlotNode>(_itemSlotPackedScene, rootNode);
if (itemSlotNode == null)
{
return;
return null;
}
itemSlotNode.IsSelect = (_itemSlotNodes.Count) == _selectIndex;
_itemSlotNodes.Add(itemSlotNode);
return itemSlotNode;
}
public void SelectTheNextItemSlot()

View File

@ -15,6 +15,8 @@ public partial class Packsack : PickAbleTemplate
{
private PackedScene? _packedScene;
private PacksackUi? _packsackUi;
[Export]
public int NumberSlots { get; set; }
public override bool CanPutInPack => false;
@ -45,7 +47,7 @@ public partial class Packsack : PickAbleTemplate
_packsackUi.ItemContainer = ItemContainer;
}
}
GameSceneNodeHolder.BackpackUiContainer?.Show();
_packsackUi?.Show();
}
@ -55,6 +57,12 @@ public partial class Packsack : PickAbleTemplate
{
base._Ready();
ItemContainer = new UniversalItemContainer();
//When the backpack is created, the item slot is generated.
//当背包被创建时,物品槽就被生成出来了。
for (var i = 0; i < NumberSlots; i++)
{
ItemContainer.AddItemSlot(this)?.Hide();
}
_packedScene = GD.Load<PackedScene>("res://prefab/ui/packsackUI.tscn");
}
}

View File

@ -14,7 +14,7 @@ public partial class PacksackUi : UiLoaderTemplate
private PackedScene? _packedScene;
private GridContainer? _gridContainer;
private HFlowContainer? _hFlowContainer;
private Label? _titleLabel;
@ -57,15 +57,16 @@ public partial class PacksackUi : UiLoaderTemplate
/// <param name="itemContainer"></param>
private void PlaceItemSlot(IItemContainer? itemContainer)
{
if (_gridContainer == null || itemContainer == null)
if (_hFlowContainer == null || itemContainer == null)
{
return;
}
NodeUtils.DeleteAllChild(_gridContainer);
NodeUtils.DeleteAllChild(_hFlowContainer);
foreach (var itemSlotNode in itemContainer)
{
itemSlotNode.Reparent(_gridContainer);
itemSlotNode.Reparent(_hFlowContainer);
itemSlotNode.Show();
}
}
@ -91,10 +92,9 @@ public partial class PacksackUi : UiLoaderTemplate
public override void InitializeUi()
{
_gridContainer = GetNode<GridContainer>("GridContainer");
_hFlowContainer = GetNode<HFlowContainer>("HFlowContainer");
_titleLabel = GetNode<Label>("TitleLabel");
_exitButton = GetNode<Button>("ExitButton");
_gridContainer.Columns = Config.HotBarSize;
//If the item container was set before this node was placed in the node tree, load it here.
//若物品容器在此节点放置到节点树之前被设置了,那么在这里加载。
PlaceItemSlot(_itemContainer);
@ -105,7 +105,11 @@ public partial class PacksackUi : UiLoaderTemplate
{
if (_exitButton != null)
{
_exitButton.Pressed += Hide;
_exitButton.Pressed += () =>
{
GameSceneNodeHolder.BackpackUiContainer?.Hide();
Hide();
};
}
}
}

View File

@ -1,10 +1,8 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using ColdMint.scripts.debug;
using ColdMint.scripts.item;
using ColdMint.scripts.item.weapon;
using Godot;
using PacksackUi = ColdMint.scripts.loader.uiLoader.PacksackUi;
@ -71,7 +69,7 @@ public static class NodeUtils
/// </param>
/// <returns></returns>
public static Node2D? GetTheNearestNode(Node2D origin, Node[] array, HashSet<Node>? exclude = null,
bool excludeInvisibleNodes = true)
bool excludeInvisibleNodes = true)
{
var closestDistance = float.MaxValue;
Node2D? closestNode = null;
@ -132,10 +130,11 @@ public static class NodeUtils
return GameSceneNodeHolder.PacksackContainer;
}
if (GameSceneNodeHolder.BackpackUiContainer!=null && childNode is PacksackUi)
if (GameSceneNodeHolder.BackpackUiContainer != null && childNode is PacksackUi)
{
return GameSceneNodeHolder.BackpackUiContainer;
}
return defaultParentNode;
}
@ -162,7 +161,7 @@ public static class NodeUtils
/// <returns></returns>
/// <seealso cref="InstantiatePackedScene{T}"/>
public static Node InstantiatePackedScene(PackedScene packedScene, Node? defaultParentNode = null,
bool assignedByRootNodeType = true)
bool assignedByRootNodeType = true)
{
var instantiateNode = packedScene.Instantiate();
//An attempt is made to place an instantiated node under a parent node only after the default parent node is set.
@ -213,14 +212,14 @@ public static class NodeUtils
/// <para>如果创建的节点无法转型至指定的类型返回null</para>
/// </returns>
public static T? InstantiatePackedScene<T>(PackedScene packedScene, Node? defaultParentNode = null,
bool assignedByRootNodeType = true)
bool assignedByRootNodeType = true)
where T : class
{
var node = InstantiatePackedScene(packedScene, defaultParentNode, assignedByRootNodeType);
// 检查类型转化,成功返回结果
if (node is T result) return result;
//如果转型失败,释放所创建的节点
LogCat.LogWarningWithFormat("warning_node_cannot_cast_to",node,nameof(T));
LogCat.LogWarningWithFormat("warning_node_cannot_cast_to", node, nameof(T));
node.QueueFree();
return null;
}