(client) chore
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
using System; // 添加引用,用于 Action 委托
|
||||
using System.Collections.Generic;
|
||||
using Logging;
|
||||
using Map;
|
||||
using UnityEngine;
|
||||
using Utils;
|
||||
@ -22,8 +22,9 @@ public class Program : Singleton<Program>
|
||||
|
||||
/// <summary>
|
||||
/// 当前活跃焦点的维度唯一标识符,可能为空。
|
||||
/// 变更为属性,并私有化setter,确保通过 SetFocusedDimension 方法集中管理其更新。
|
||||
/// </summary>
|
||||
public string focuseDimensionId = null;
|
||||
public string focuseDimensionId { get; private set; } = null;
|
||||
|
||||
/// <summary>
|
||||
/// 当前聚焦的维度对象实例。当 <see cref="focuseDimensionId"/> 不为空时,此属性指向对应的维度实例。
|
||||
@ -40,6 +41,12 @@ public class Program : Singleton<Program>
|
||||
/// </summary>
|
||||
public IReadOnlyDictionary<string, Dimension> RegisteredDimensions => _registeredDimensions;
|
||||
|
||||
/// <summary>
|
||||
/// 当焦点维度发生变化时触发的事件。
|
||||
/// 事件参数为新的焦点 Dimension 实例,如果焦点被清除则为 null。
|
||||
/// </summary>
|
||||
public event Action<Dimension> OnFocusedDimensionChanged;
|
||||
|
||||
/// <summary>
|
||||
/// 注册一个维度实例到 Program。
|
||||
/// </summary>
|
||||
@ -51,7 +58,7 @@ public class Program : Singleton<Program>
|
||||
Debug.LogError("[Program] Attempted to register a null Dimension.");
|
||||
return;
|
||||
}
|
||||
string id = dimension.DimensionId;
|
||||
var id = dimension.DimensionId;
|
||||
if (string.IsNullOrEmpty(id))
|
||||
{
|
||||
Debug.LogError($"[Program] Attempted to register a Dimension with an empty or null ID: {dimension.name}");
|
||||
@ -64,10 +71,12 @@ public class Program : Singleton<Program>
|
||||
}
|
||||
_registeredDimensions.Add(id, dimension);
|
||||
|
||||
// 如果注册的维度是当前焦点维度,则更新 FocusedDimension 属性
|
||||
// 修改理由:确保任何对焦点的潜在更新都通过 SetFocusedDimension 进行,
|
||||
// 从而集中管理焦点状态的同步和事件的触发。
|
||||
// 如果注册的维度恰好是当前 focuseDimensionId, SetFocusedDimension(id) 将负责更新 FocusedDimension 并触发事件。
|
||||
if (focuseDimensionId == id)
|
||||
{
|
||||
FocusedDimension = dimension;
|
||||
SetFocusedDimension(id);
|
||||
}
|
||||
}
|
||||
|
||||
@ -82,14 +91,15 @@ public class Program : Singleton<Program>
|
||||
Debug.LogWarning("[Program] Attempted to unregister a null Dimension. Skipping.");
|
||||
return;
|
||||
}
|
||||
string id = dimension.DimensionId;
|
||||
var id = dimension.DimensionId;
|
||||
if (_registeredDimensions.Remove(id))
|
||||
{
|
||||
// 如果注销的维度是当前焦点维度,则清除焦点
|
||||
// 修改理由:确保任何对焦点的潜在更新都通过 SetFocusedDimension 进行,
|
||||
// 从而集中管理焦点状态的同步和事件的触发。
|
||||
// 如果注销的维度是当前焦点维度,则调用 SetFocusedDimension(null) 清除焦点并触发事件。
|
||||
if (focuseDimensionId == id)
|
||||
{
|
||||
focuseDimensionId = null;
|
||||
FocusedDimension = null;
|
||||
SetFocusedDimension(null); // 清除焦点
|
||||
}
|
||||
}
|
||||
else
|
||||
@ -105,30 +115,55 @@ public class Program : Singleton<Program>
|
||||
/// <returns>对应的Dimension实例,如果未找到则返回null。</returns>
|
||||
public Dimension GetDimension(string dimensionId)
|
||||
{
|
||||
_registeredDimensions.TryGetValue(dimensionId, out Dimension dimension);
|
||||
_registeredDimensions.TryGetValue(dimensionId, out var dimension);
|
||||
return dimension;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置当前聚焦的维度。
|
||||
/// 这是更改焦点维度的唯一官方入口。
|
||||
/// </summary>
|
||||
/// <param name="dimensionId">要设置为焦点的维度的唯一标识符。如果传入null或空字符串,将清除当前焦点维度。</param>
|
||||
public void SetFocusedDimension(string dimensionId)
|
||||
{
|
||||
if (string.IsNullOrEmpty(dimensionId))
|
||||
// 1. 确定新的焦点维度及其ID
|
||||
Dimension newFocusedDimension = null; // 默认为清除焦点
|
||||
string newFocuseDimensionId = null;
|
||||
|
||||
if (!string.IsNullOrEmpty(dimensionId))
|
||||
{
|
||||
focuseDimensionId = null;
|
||||
FocusedDimension = null;
|
||||
if (_registeredDimensions.TryGetValue(dimensionId, out var foundDimension))
|
||||
{
|
||||
newFocuseDimensionId = dimensionId;
|
||||
newFocusedDimension = foundDimension;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 如果尝试设置未注册的维度,警告并直接返回,不改变当前焦点状态。
|
||||
Debug.LogWarning($"[Program] Attempted to set focus to unregistered dimension ID: '{dimensionId}'. Focus not changed.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
// 如果 dimensionId 为 null 或空, newFocusedDimension 和 newFocuseDimensionId 将保持为 null,表示清除焦点。
|
||||
|
||||
// 2. 优化:检查是否实际发生变化,避免不必要的更新和事件触发
|
||||
// 只有当新的ID或新的维度对象引用与当前的不一致时,才执行更新。
|
||||
// 例如,如果 focuseDimensionId 已经是 "someId",FocusedDimension 已经是 "someDimensionObject",
|
||||
// 再次调用 SetFocusedDimension("someId") 不会触发事件。
|
||||
// 但如果 focuseDimensionId 是 "someId",FocusedDimension 是 null (因为维度还未注册),
|
||||
// 而此时注册了 "someId" 对应的维度并调用 SetFocusedDimension("someId"),FocusedDimension 将从 null 变为实际对象,
|
||||
// 这算作一次变化,会触发事件。
|
||||
if (focuseDimensionId == newFocuseDimensionId && FocusedDimension == newFocusedDimension)
|
||||
{
|
||||
// Debug.Log($"[Program] SetFocusedDimension: ID '{dimensionId}' already current. No change needed.");
|
||||
return;
|
||||
}
|
||||
if (_registeredDimensions.TryGetValue(dimensionId, out Dimension newFocusedDimension))
|
||||
{
|
||||
focuseDimensionId = dimensionId;
|
||||
FocusedDimension = newFocusedDimension;
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning($"[Program] Attempted to set focus to unregistered dimension ID: '{dimensionId}'. Focus not changed.");
|
||||
}
|
||||
|
||||
// 3. 更新内部状态
|
||||
focuseDimensionId = newFocuseDimensionId;
|
||||
FocusedDimension = newFocusedDimension;
|
||||
|
||||
// 4. 触发事件
|
||||
OnFocusedDimensionChanged?.Invoke(FocusedDimension);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user