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

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);
};
}

View File

@ -0,0 +1,327 @@
using Godot;
public partial class ManualCollectionItem : Panel
{
// 采集相关参数
private const float COLLECTION_TIME = 1.0f; // 采集时间1秒
// 状态变量
private bool isCollecting = false;
private float collectionProgress = 0.0f;
private string itemId;
private GameData.ItemData itemData;
// UI组件引用
private TextureRect iconTexture;
private Label nameLabel;
private Label statusLabel;
private ColorRect progressFill;
// 进度条颜色
private Color defaultProgressColor = new Color(0.3f, 0.8f, 0.3f, 1.0f); // 绿色
private Color collectingProgressColor = new Color(1.0f, 0.8f, 0.3f, 1.0f); // 采集时的橙色
public override void _Ready()
{
// 获取UI组件引用
iconTexture = GetNode<TextureRect>("MarginContainer/VBoxContainer/TopRow/IconTexture");
nameLabel = GetNode<Label>("MarginContainer/VBoxContainer/TopRow/InfoContainer/NameLabel");
statusLabel = GetNode<Label>("MarginContainer/VBoxContainer/TopRow/InfoContainer/StatusLabel");
progressFill = GetNode<ColorRect>("MarginContainer/VBoxContainer/ProgressContainer/ProgressFill");
// 瞬间采集模式不需要设置进度条初始颜色
// progressFill.Color = defaultProgressColor;
// 连接鼠标事件
GuiInput += OnGuiInput;
// 如果物品数据已经设置,现在应用它
if (itemData != null)
{
ApplyItemData();
}
GD.Print("ManualCollectionItem _Ready 完成");
}
public void SetupItem(string id, GameData.ItemData data)
{
itemId = id;
itemData = data;
// 如果UI组件还没有初始化延迟设置
if (nameLabel == null)
{
GD.Print("UI组件还未初始化延迟设置手动采集物品数据");
CallDeferred(nameof(ApplyItemData));
return;
}
ApplyItemData();
}
private void ApplyItemData()
{
if (itemData == null)
{
GD.PrintErr("itemData为null无法应用数据");
return;
}
// 确保UI组件已初始化
if (nameLabel == null)
{
GD.PrintErr("nameLabel仍然为nullUI初始化可能失败");
return;
}
// 设置物品名称
nameLabel.Text = itemData.Name;
// 加载图标
LoadItemIcon();
GD.Print($"成功设置手动采集物品: {itemData.Name}");
}
private void LoadItemIcon()
{
if (itemData == null || iconTexture == null) return;
if (!string.IsNullOrEmpty(itemData.IconPath) && ResourceLoader.Exists(itemData.IconPath))
{
// 尝试加载为Texture2D资源
var resource = ResourceLoader.Load(itemData.IconPath);
if (resource is Texture2D texture)
{
iconTexture.Texture = texture;
}
else
{
GD.PrintErr($"资源不是Texture2D类型: {itemData.IconPath}");
// 使用默认图标
var defaultTexture = GD.Load<Texture2D>("res://assets/textures/icon.svg");
if (defaultTexture != null)
{
iconTexture.Texture = defaultTexture;
}
}
}
else
{
// 使用默认图标
var defaultTexture = GD.Load<Texture2D>("res://assets/textures/icon.svg");
if (defaultTexture != null)
{
iconTexture.Texture = defaultTexture;
}
}
}
private void OnGuiInput(InputEvent @event)
{
if (@event is InputEventMouseButton mouseEvent)
{
if (mouseEvent.ButtonIndex == MouseButton.Left)
{
// 瞬间采集模式 - 点击一次立即完成一次采集
if (mouseEvent.Pressed)
{
InstantCollection();
}
// 原来的进度条采集模式(已注释)
/*
// 点击采集模式 - 只在按下时触发一次采集
if (mouseEvent.Pressed)
{
StartSingleCollection();
}
// 按住采集模式(已注释)
if (mouseEvent.Pressed)
{
// 开始采集
StartCollection();
}
else
{
// 停止采集
StopCollection();
}
*/
}
}
}
// 新增:瞬间采集方法
private void InstantCollection()
{
if (string.IsNullOrEmpty(itemId)) return;
// 添加物品到库存
if (InventoryManager.Instance != null)
{
InventoryManager.Instance.AddItem(itemId, 1);
GD.Print($"瞬间采集完成,获得: {itemData?.Name ?? itemId} x1");
}
// 简单的视觉反馈 - 短暂改变状态文本
if (statusLabel != null)
{
statusLabel.Text = "采集!";
statusLabel.Modulate = collectingProgressColor;
// 0.1秒后恢复状态
GetTree().CreateTimer(0.1).Timeout += () => {
if (statusLabel != null)
{
statusLabel.Text = "点击采集";
statusLabel.Modulate = new Color(0.7f, 0.9f, 0.7f, 1.0f);
}
};
}
}
// 新增:单次点击采集方法
private void StartSingleCollection()
{
if (string.IsNullOrEmpty(itemId)) return;
// 如果正在采集中,忽略点击
if (isCollecting) return;
isCollecting = true;
collectionProgress = 0.0f;
// 更新状态标签和进度条颜色
if (statusLabel != null)
{
statusLabel.Text = "采集中...";
statusLabel.Modulate = collectingProgressColor;
}
if (progressFill != null)
{
progressFill.Color = collectingProgressColor;
}
GD.Print($"开始采集: {itemData?.Name ?? itemId}");
}
private void CompleteCollection()
{
if (string.IsNullOrEmpty(itemId)) return;
// 添加物品到库存
if (InventoryManager.Instance != null)
{
InventoryManager.Instance.AddItem(itemId, 1);
GD.Print($"采集完成,获得: {itemData?.Name ?? itemId} x1");
}
// 点击采集模式 - 采集完成后停止
isCollecting = false;
collectionProgress = 0.0f;
UpdateStatusDisplay();
// 按住采集模式的连续采集逻辑(已注释但保留)
/*
// 重置采集状态
collectionProgress = 0.0f;
// 如果还在按住,继续下一轮采集
if (isCollecting)
{
GD.Print($"继续采集: {itemData?.Name ?? itemId}");
}
else
{
// 恢复状态显示
UpdateStatusDisplay();
}
*/
}
private void UpdateStatusDisplay()
{
if (statusLabel != null)
{
statusLabel.Text = "点击采集";
statusLabel.Modulate = new Color(0.7f, 0.9f, 0.7f, 1.0f);
}
if (progressFill != null)
{
progressFill.Color = defaultProgressColor;
if (!isCollecting)
{
progressFill.AnchorRight = 0.0f;
}
}
}
private void UpdateProgressBar()
{
if (progressFill != null)
{
if (isCollecting)
{
// 采集中显示当前进度
progressFill.AnchorRight = collectionProgress;
}
else
{
// 非采集状态显示0进度
progressFill.AnchorRight = 0.0f;
}
}
}
public override void _Process(double delta)
{
// 瞬间采集模式不需要进度条更新
// 原来的进度条更新逻辑(已注释)
/*
if (isCollecting)
{
// 更新采集进度
collectionProgress += (float)delta / COLLECTION_TIME;
// 限制进度在0-1之间
collectionProgress = Mathf.Clamp(collectionProgress, 0.0f, 1.0f);
// 更新进度条显示
UpdateProgressBar();
// 检查是否完成采集
if (collectionProgress >= 1.0f)
{
CompleteCollection();
}
}
*/
}
public override void _ExitTree()
{
// 清理事件
GuiInput -= OnGuiInput;
}
// 按住采集相关方法(已注释但保留)
/*
private void StopCollection()
{
if (!isCollecting) return;
isCollecting = false;
collectionProgress = 0.0f;
// 恢复状态显示
UpdateStatusDisplay();
GD.Print($"停止采集: {itemData?.Name ?? itemId}");
}
*/
}

View File

@ -30,6 +30,12 @@ public partial class ProductionLineItem : Panel
progressFill = GetNode<ColorRect>("MarginContainer/VBoxContainer/ProgressContainer/ProgressFill");
iconTexture = GetNode<TextureRect>("MarginContainer/VBoxContainer/TopRow/IconTexture");
// 设置进度条初始状态
if (progressFill != null)
{
progressFill.AnchorRight = 0.0f;
}
// 连接按钮信号
addButton.Pressed += OnAddButtonPressed;
removeButton.Pressed += OnRemoveButtonPressed;
@ -130,11 +136,16 @@ public partial class ProductionLineItem : Panel
if (ResourceLoader.Exists(iconPath))
{
var texture = GD.Load<Texture2D>(iconPath);
if (texture != null)
// 尝试加载为Texture2D资源
var resource = ResourceLoader.Load(iconPath);
if (resource is Texture2D texture)
{
iconTexture.Texture = texture;
}
else
{
GD.PrintErr($"资源不是Texture2D类型: {iconPath}");
}
}
}
@ -175,8 +186,12 @@ public partial class ProductionLineItem : Panel
productionLabel.Text = "0/s";
}
// 更新进度条
progressFill.AnchorRight = status.Progress;
// 更新进度条(添加边界检查,避免闪烁)
float newProgress = Mathf.Clamp(status.Progress, 0.0f, 1.0f);
if (progressFill.AnchorRight != newProgress)
{
progressFill.AnchorRight = newProgress;
}
// 更新按钮状态
UpdateButtonStates();