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

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

@ -1,11 +1,13 @@
using Godot;
using System.Collections.Generic;
using System.Linq;
public partial class DynamicTabManager : TabContainer
{
private PackedScene itemPanelScene;
private PackedScene craftingItemScene;
private PackedScene productionLineItemScene;
private PackedScene manualCollectionItemScene;
public override void _Ready()
{
@ -38,6 +40,14 @@ public partial class DynamicTabManager : TabContainer
return;
}
// 加载ManualCollectionItem场景用于手动采集
manualCollectionItemScene = GD.Load<PackedScene>("res://scenes/ManualCollectionItem.tscn");
if (manualCollectionItemScene == null)
{
GD.PrintErr("无法加载ManualCollectionItem场景");
return;
}
GD.Print("开始初始化标签页");
// 延迟初始化以确保所有管理器都已初始化
CallDeferred(nameof(InitializeTabs));
@ -101,9 +111,10 @@ public partial class DynamicTabManager : TabContainer
// 为合成相关的分类创建块
foreach (var category in allCategories)
{
// 合成标签包含:手动采集、冶炼、建筑设施
// 合成标签包含:手动采集、冶炼、制作物品、建筑设施
if (category.CategoryName == "手动采集" ||
category.CategoryName == "冶炼" ||
category.CategoryName == "制作物品" ||
category.CategoryName == "建筑设施")
{
CreateCategoryBlock(vboxContainer, category, "合成");
@ -134,14 +145,14 @@ public partial class DynamicTabManager : TabContainer
if (productionLineManager == null)
{
GD.PrintErr("ProductionLineManager 实例为null创建空的生产线标签");
// 创建一个提示标签
var noDataLabel = new Label();
noDataLabel.Text = "生产线管理器未初始化";
noDataLabel.HorizontalAlignment = HorizontalAlignment.Center;
noDataLabel.VerticalAlignment = VerticalAlignment.Center;
vboxContainer.AddChild(noDataLabel);
// 添加到TabContainer
AddChild(scrollContainer);
SetTabTitle(GetTabCount() - 1, "生产线");
@ -155,7 +166,7 @@ public partial class DynamicTabManager : TabContainer
if (categories.Count == 0)
{
GD.Print("没有找到生产线分类,创建提示标签");
// 创建一个提示标签
var noDataLabel = new Label();
noDataLabel.Text = "暂无生产线配置";
@ -165,6 +176,8 @@ public partial class DynamicTabManager : TabContainer
}
else
{
// 为每个分类创建块
foreach (var category in categories)
{
@ -176,7 +189,7 @@ public partial class DynamicTabManager : TabContainer
// 添加到TabContainer
AddChild(scrollContainer);
SetTabTitle(GetTabCount() - 1, "生产线");
GD.Print("生产线标签创建完成");
}
@ -215,27 +228,19 @@ public partial class DynamicTabManager : TabContainer
categoryContainer.AddChild(titleContainer);
// 添加小间距
var spacer = new Control();
spacer.CustomMinimumSize = new Vector2(0, 5);
categoryContainer.AddChild(spacer);
// 创建流容器来放置生产线(完全按照合成菜单的方式)
var flowContainer = new FlowContainer();
flowContainer.SizeFlagsHorizontal = Control.SizeFlags.ExpandFill;
categoryContainer.AddChild(flowContainer);
// 创建生产线网格 - 使用VBoxContainer实现垂直堆叠
var productionContainer = new VBoxContainer();
productionContainer.Name = $"{category}ProductionLines";
productionContainer.SizeFlagsHorizontal = Control.SizeFlags.ExpandFill;
productionContainer.AddThemeConstantOverride("separation", 8); // 垂直间距
// 添加该分类的所有生产线
AddProductionLinesToContainer(productionContainer, category);
categoryContainer.AddChild(productionContainer);
AddProductionLinesToContainer(flowContainer, category);
// 添加到父容器
parentContainer.AddChild(categoryContainer);
}
private void AddProductionLinesToContainer(VBoxContainer container, string category)
private void AddProductionLinesToContainer(FlowContainer container, string category)
{
GD.Print($"为分类 '{category}' 添加生产线");
@ -298,7 +303,7 @@ public partial class DynamicTabManager : TabContainer
labelSpacer.CustomMinimumSize = new Vector2(10, 0);
titleContainer.AddChild(labelSpacer);
// HSeparator 横线分隔符
// 使用HSeparator横线分隔符
var separator = new HSeparator();
separator.SizeFlagsHorizontal = Control.SizeFlags.ExpandFill;
separator.SizeFlagsVertical = Control.SizeFlags.ShrinkCenter;
@ -306,71 +311,46 @@ public partial class DynamicTabManager : TabContainer
categoryContainer.AddChild(titleContainer);
// 添加小间距
var spacer = new Control();
spacer.CustomMinimumSize = new Vector2(0, 5);
categoryContainer.AddChild(spacer);
// 创建物品网格 - 使用HFlowContainer实现自适应宽度
var flowContainer = new HFlowContainer();
flowContainer.Name = $"{category.CategoryName}Grid";
// 创建流容器来放置物品
var flowContainer = new FlowContainer();
flowContainer.SizeFlagsHorizontal = Control.SizeFlags.ExpandFill;
flowContainer.AddThemeConstantOverride("h_separation", 10); // 水平间距
flowContainer.AddThemeConstantOverride("v_separation", 8); // 垂直间距
// 添加该分类的所有物品
AddItemsToFlow(flowContainer, category.CategoryName, tabType);
categoryContainer.AddChild(flowContainer);
// 添加到父容器
parentContainer.AddChild(categoryContainer);
}
private void AddItemsToFlow(HFlowContainer flowContainer, string categoryName, string tabType)
{
GD.Print($"为分类 '{categoryName}' 添加物品到流容器,标签类型: {tabType}");
var categoryManager = ResourceCategoryManager.Instance;
if (categoryManager == null)
// 获取分类中的物品数据
var items = ResourceCategoryManager.Instance?.GetItemsByCategory(category.CategoryName);
if (items == null)
{
GD.PrintErr("ResourceCategoryManager 实例为null");
GD.PrintErr($"无法获取分类 {category.CategoryName} 的物品数据");
return;
}
var items = categoryManager.GetItemsByCategory(categoryName);
GD.Print($"分类 '{categoryName}' 中有 {items.Count} 个物品");
GD.Print($"分类 {category.CategoryName} 包含 {items.Count} 个物品");
foreach (var kvp in items)
// 为每个物品创建对应的面板
foreach (var itemPair in items)
{
var itemId = kvp.Key;
var itemData = kvp.Value;
GD.Print($"创建物品面板: {itemId} - {itemData.Name}");
string itemId = itemPair.Key;
var itemData = itemPair.Value;
string categoryName = category.CategoryName;
Control itemPanel;
// 手动采集始终使用ItemPanel保持按住采集功能
// 手动采集使用新的ManualCollectionItem
if (categoryName == "手动采集")
{
// 手动采集使用ItemPanel
itemPanel = itemPanelScene.Instantiate<Control>();
// 使用专门的手动采集场景
itemPanel = manualCollectionItemScene.Instantiate<ManualCollectionItem>();
if (itemPanel == null)
{
GD.PrintErr($"无法实例化ItemPanel for {itemId}");
GD.PrintErr($"无法实例化ManualCollectionItem for {itemId}");
continue;
}
// 添加手动采集脚本
var manualCollectionScript = new ManualCollectionPanel();
manualCollectionScript.SetAnchorsAndOffsetsPreset(Control.LayoutPreset.FullRect);
manualCollectionScript.MouseFilter = Control.MouseFilterEnum.Stop; // 拦截鼠标事件
itemPanel.AddChild(manualCollectionScript);
manualCollectionScript.SetItemId(itemId);
GD.Print($"为 {itemId} 添加手动采集功能");
// 设置手动采集物品数据
var manualCollectionItem = itemPanel as ManualCollectionItem;
manualCollectionItem?.SetupItem(itemId, itemData);
// 设置物品数据(手动采集不是生产设备)
SetupItemPanel(itemPanel, itemData, false);
GD.Print($"为 {itemId} 创建手动采集面板");
}
// 其他合成物品根据标签类型选择面板
else if (tabType == "合成")
@ -382,7 +362,7 @@ public partial class DynamicTabManager : TabContainer
GD.PrintErr($"无法实例化CraftingItem for {itemId}");
continue;
}
// 设置合成物品数据
SetupCraftingItem(itemPanel, itemData, categoryName);
}
@ -406,6 +386,11 @@ public partial class DynamicTabManager : TabContainer
// 添加到流容器
flowContainer.AddChild(itemPanel);
}
// 添加分类块到父容器
parentContainer.AddChild(categoryContainer);
GD.Print($"成功创建分类块: {category.CategoryName},包含 {items.Count} 个物品");
}
private void SetupItemPanel(Control itemPanel, GameData.ItemData itemData, bool isProductionDevice = true)
@ -417,34 +402,27 @@ public partial class DynamicTabManager : TabContainer
if (iconTexture != null && !string.IsNullOrEmpty(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 texture)
{
iconTexture.Texture = texture;
GD.Print($"设置图标成功: {itemData.IconPath}");
}
else
{
GD.PrintErr($"无法加载图标: {itemData.IconPath}");
GD.PrintErr($"资源不是Texture2D类型: {itemData.IconPath}");
LoadDefaultIcon(iconTexture);
}
}
else
{
GD.Print($"图标文件不存在: {itemData.IconPath},使用默认图标");
// 默认使用icon.svg
var defaultIcon = GD.Load<Texture2D>("res://assets/textures/icon.svg");
if (defaultIcon != null)
{
iconTexture.Texture = defaultIcon;
}
else
{
GD.PrintErr("无法加载默认图标-1");
}
LoadDefaultIcon(iconTexture);
}
}
// 设置名称
@ -525,23 +503,25 @@ public partial class DynamicTabManager : TabContainer
if (iconTexture != null && !string.IsNullOrEmpty(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 texture)
{
iconTexture.Texture = texture;
GD.Print($"设置合成物品图标成功: {itemData.IconPath}");
}
else
{
LoadDefaultIconForCrafting(iconTexture);
GD.PrintErr($"资源不是Texture2D类型: {itemData.IconPath}");
LoadDefaultIcon(iconTexture);
}
}
else
{
GD.Print($"合成物品图标文件不存在: {itemData.IconPath},使用默认图标");
LoadDefaultIconForCrafting(iconTexture);
LoadDefaultIcon(iconTexture);
}
}
@ -565,14 +545,15 @@ public partial class DynamicTabManager : TabContainer
{
// 显示第一个材料,如果有多个材料可以显示"..."
var firstIngredient = recipe.Ingredients[0];
var itemName = GameData.Instance?.GetItem(firstIngredient.ItemId)?.Name ?? firstIngredient.ItemId;
if (recipe.Ingredients.Count == 1)
if (recipe.Ingredients.Count <= 3)
{
materialsLabel.Text = $"材料: {firstIngredient.Quantity}x{itemName}";
materialsLabel.Text = $"材料:{string.Join(" ", recipe.Ingredients.Select(s => $"{s.Quantity}x{ GameData.Instance?.GetItem(s.ItemId)?.Name ?? s.ItemId}"))}";
}
else
{
var itemName = GameData.Instance?.GetItem(firstIngredient.ItemId)?.Name ?? firstIngredient.ItemId;
materialsLabel.Text = $"材料: {firstIngredient.Quantity}x{itemName}...";
}
}
@ -635,7 +616,7 @@ public partial class DynamicTabManager : TabContainer
craftButton.Disabled = false;
craftButton.Text = "合成";
craftButton.Modulate = new Color(1.0f, 1.0f, 1.0f, 1.0f);
// 连接按钮点击事件
craftButton.Pressed += () => OnCraftButtonPressed(itemData.Id, craftingItem);
}
@ -651,6 +632,19 @@ public partial class DynamicTabManager : TabContainer
}
}
private void LoadDefaultIcon(TextureRect iconTexture)
{
var defaultIcon = GD.Load<Texture2D>("res://assets/textures/icon.svg");
if (defaultIcon != null)
{
iconTexture.Texture = defaultIcon;
}
else
{
GD.PrintErr("无法加载默认图标");
}
}
private void LoadDefaultIconForCrafting(TextureRect iconTexture)
{
var defaultIcon = GD.Load<Texture2D>("res://assets/textures/icon.svg");
@ -667,7 +661,7 @@ public partial class DynamicTabManager : TabContainer
private void OnCraftButtonPressed(string itemId, Control craftingItem)
{
GD.Print($"点击合成按钮: {itemId}");
// 检查合成配方管理器是否可用
if (CraftingRecipeManager.Instance == null)
{
@ -713,7 +707,7 @@ public partial class DynamicTabManager : TabContainer
{
int currentAmount = InventoryManager.Instance?.GetItemQuantity(ingredient.ItemId) ?? 0;
int requiredAmount = ingredient.Quantity * quantity;
if (currentAmount < requiredAmount)
{
canCraftAll = false;
@ -764,7 +758,7 @@ public partial class DynamicTabManager : TabContainer
}
// 尝试通过路径查找CraftingQueueManager
var craftingQueue = gameScene.GetNode<CraftingQueueManager>("HSplitContainer/LeftPanel/VBoxContainer/CraftingQueue");
var craftingQueue = gameScene.GetNode<CraftingQueueManager>("HBoxContainer/LeftPanel/VBoxContainer/CraftingQueue");
if (craftingQueue == null)
{
GD.PrintErr("无法找到CraftingQueueManager节点");
@ -785,12 +779,12 @@ public partial class DynamicTabManager : TabContainer
{
// 设置初始值
quantityInput.Text = "1";
// 如果没有配方,禁用数量控制
quantityInput.Editable = hasRecipe;
minusButton.Disabled = !hasRecipe;
plusButton.Disabled = !hasRecipe;
if (!hasRecipe)
{
quantityInput.Modulate = new Color(0.6f, 0.6f, 0.6f, 1.0f);
@ -799,7 +793,8 @@ public partial class DynamicTabManager : TabContainer
}
// 连接减号按钮事件
minusButton.Pressed += () => {
minusButton.Pressed += () =>
{
int currentValue = GetQuantityValue(quantityInput);
if (currentValue > 1)
{
@ -808,7 +803,8 @@ public partial class DynamicTabManager : TabContainer
};
// 连接加号按钮事件
plusButton.Pressed += () => {
plusButton.Pressed += () =>
{
int currentValue = GetQuantityValue(quantityInput);
if (currentValue < 99) // 限制最大值为99
{
@ -817,7 +813,8 @@ public partial class DynamicTabManager : TabContainer
};
// 连接输入框文本变化事件
quantityInput.TextChanged += (string newText) => {
quantityInput.TextChanged += (string newText) =>
{
ValidateQuantityInput(quantityInput, newText);
};
}