生产线样式,添加图标测试

This commit is contained in:
2025-06-19 00:02:41 +08:00
parent 50dc434ed9
commit b3d1135e23
23 changed files with 868 additions and 384 deletions

View File

@ -93,6 +93,8 @@ public partial class InventoryManager : Node
AddItem("water", 200);
AddItem("iron_ingot", 25);
AddItem("copper_ingot", 15);
AddItem("magnet", 8); // 添加磁铁,用于合成磁线圈
AddItem("gear", 6); // 添加齿轮,用于合成钻机
}
// 添加物品到库存

View File

@ -10,6 +10,47 @@ public partial class InventoryTableManager : VBoxContainer
{
GD.Print("InventoryTableManager _Ready 开始");
// 设置父级ScrollContainer的滚动条宽度
var scrollContainer = GetParent() as ScrollContainer;
if (scrollContainer != null)
{
// 直接在滚动条组件上设置主题覆盖
var vScrollBar = scrollContainer.GetVScrollBar();
var hScrollBar = scrollContainer.GetHScrollBar();
if (vScrollBar != null)
{
// 尝试多种不同的属性名称
vScrollBar.AddThemeConstantOverride("width", 1);
vScrollBar.AddThemeConstantOverride("scroll_bar_width", 1);
vScrollBar.AddThemeConstantOverride("scrollbar_width", 1);
vScrollBar.AddThemeConstantOverride("bar_width", 1);
// 创建超细的样式框
var thinStyle = new StyleBoxFlat();
thinStyle.BgColor = new Color(0.3f, 0.3f, 0.3f, 0.6f);
thinStyle.SetContentMarginAll(0);
var thinGrabber = new StyleBoxFlat();
thinGrabber.BgColor = new Color(0.7f, 0.7f, 0.7f, 0.8f);
thinGrabber.SetContentMarginAll(0);
// 应用样式
vScrollBar.AddThemeStyleboxOverride("scroll", thinStyle);
vScrollBar.AddThemeStyleboxOverride("grabber", thinGrabber);
vScrollBar.AddThemeStyleboxOverride("grabber_highlight", thinGrabber);
vScrollBar.AddThemeStyleboxOverride("grabber_pressed", thinGrabber);
// 设置最小尺寸
vScrollBar.CustomMinimumSize = new Vector2(1, 0);
GD.Print("设置垂直滚动条样式和宽度");
}
GD.Print("已直接在滚动条组件上应用超细样式");
}
// 加载库存物品场景
inventoryItemScene = GD.Load<PackedScene>("res://scenes/InventoryItem.tscn");
if (inventoryItemScene == null)
@ -96,6 +137,11 @@ public partial class InventoryTableManager : VBoxContainer
separator.SizeFlagsHorizontal = Control.SizeFlags.ExpandFill;
separator.SizeFlagsVertical = Control.SizeFlags.ShrinkCenter;
titleContainer.AddChild(separator);
// 添加右侧间距,避免内容紧贴滚动条
var rightSpacer = new Control();
rightSpacer.CustomMinimumSize = new Vector2(10, 0);
titleContainer.AddChild(rightSpacer);
categoryContainer.AddChild(titleContainer);
@ -111,7 +157,17 @@ public partial class InventoryTableManager : VBoxContainer
itemsContainer.SizeFlagsHorizontal = Control.SizeFlags.ExpandFill;
itemsContainer.AddThemeConstantOverride("h_separation", 4); // 减少水平间距
itemsContainer.AddThemeConstantOverride("v_separation", 4); // 垂直间距
categoryContainer.AddChild(itemsContainer);
// 创建一个HBoxContainer来包装GridContainer以便添加右侧间距
var itemsWrapper = new HBoxContainer();
itemsWrapper.AddChild(itemsContainer);
// 添加右侧间距
var itemsRightSpacer = new Control();
itemsRightSpacer.CustomMinimumSize = new Vector2(10, 0);
itemsWrapper.AddChild(itemsRightSpacer);
categoryContainer.AddChild(itemsWrapper);
// 保存容器引用
categoryContainers[category.CategoryName] = itemsContainer;
@ -193,23 +249,37 @@ public partial class InventoryTableManager : VBoxContainer
var iconTexture = inventoryItem.GetNode<TextureRect>("MarginContainer/HBoxContainer/IconTexture");
if (iconTexture != null && !string.IsNullOrEmpty(itemData.IconPath))
{
GD.Print($"图标路径: {itemData.IconPath}");
// 检查文件是否存在
if (Godot.FileAccess.FileExists(itemData.IconPath))
if (ResourceLoader.Exists(itemData.IconPath))
{
var texture = GD.Load<Texture2D>(itemData.IconPath);
if (texture != null)
// 尝试加载为Texture2D资源
var resource = ResourceLoader.Load(itemData.IconPath);
if (resource is Texture2D textureResource)
{
iconTexture.Texture = texture;
iconTexture.Texture = textureResource;
}
else
{
GD.PrintErr($"资源不是Texture2D类型: {itemData.IconPath}");
// 使用默认图标
var defaultIcon = GD.Load<Texture2D>("res://assets/textures/icon.svg");
if (defaultIcon != null)
{
iconTexture.Texture = defaultIcon;
}
}
}
else
{
// 使用默认图标
var defaultIcon = GD.Load<Texture2D>("res://assets/textures/icon.svg");
if (defaultIcon != null)
{
iconTexture.Texture = defaultIcon;
}
// GD.Print($"图标文件不存在: {itemData.IconPath}使用默认图标");
// // 使用默认图标
// var defaultIcon = GD.Load<Texture2D>("res://assets/textures/icon.svg");
// if (defaultIcon != null)
// {
// iconTexture.Texture = defaultIcon;
// }
}
}