生产线样式,添加图标测试
This commit is contained in:
327
scripts/ui/ManualCollectionItem.cs
Normal file
327
scripts/ui/ManualCollectionItem.cs
Normal 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仍然为null,UI初始化可能失败");
|
||||
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}");
|
||||
}
|
||||
*/
|
||||
}
|
||||
Reference in New Issue
Block a user