using System;
using System.Collections.Generic;
using Godot;
namespace ColdMint.scripts.utils;
///
/// UI Group
/// UI组
///
public partial class UiGroup : Control
{
private readonly HashSet _visibleControls = [];
private readonly Dictionary> _controlFunc = new();
///
/// Holds the node that has been instantiated
/// 持有已实例化的节点
///
private readonly Dictionary _instantiatedControl = new();
///
/// Registered control node
/// 注册控制节点
///
///
///key
///控制节点的key
///
///
///Creates a function to control the node. UiGroup delays calling this function to create the node.
///创建控制节点的函数,UiGroup会延迟调用这个函数创建节点。
///
public void RegisterControl(string key, Func func)
{
_controlFunc.TryAdd(key, func);
}
///
/// Obtain or create a controller node
/// 获取或者创建控制节点
///
///
///
private Control? GetOrCreateControl(string key)
{
if (_instantiatedControl.TryGetValue(key, out var instantiatedControl))
{
return instantiatedControl;
}
if (!_controlFunc.TryGetValue(key, out var func))
{
return null;
}
var control = func.Invoke();
if (control == null)
{
return null;
}
control.Hide();
control.TreeExited += () => { OnTreeExited(key, control); };
NodeUtils.CallDeferredAddChild(this, control);
_instantiatedControl.Add(key, control);
return control;
}
///
/// Hide all nodes
/// 隐藏全部节点
///
public void HideAllControl()
{
if (_visibleControls.Count == 0)
{
return;
}
foreach (var visibleControl in _visibleControls)
{
visibleControl.Hide();
}
_visibleControls.Clear();
Hide();
}
///
/// Hide a node
/// 隐藏某个节点
///
///
///
public bool HideControl(string key)
{
if (!_instantiatedControl.TryGetValue(key, out var control))
{
return false;
}
return HideControl(control);
}
///
/// Hide a node
/// 隐藏某个节点
///
///
///
public bool HideControl(Control control)
{
if (!control.IsVisible())
{
return false;
}
control.Hide();
_visibleControls.Remove(control);
ChangeSelfVisibility();
return true;
}
///
/// Show node
/// 显示某个节点
///
///
///
///A callback function before the display node where you can generate rendered page content. For example, set the title
///在显示节点之前的回调函数,您可以在此函数内生成渲染页面内容。例如:设置标题
///
///
public bool ShowControl(string key, Action? beforeDisplayControl = null)
{
var control = GetOrCreateControl(key);
if (control == null)
{
return false;
}
if (control.IsVisible())
{
return false;
}
if (beforeDisplayControl != null)
{
beforeDisplayControl.Invoke(control);
}
control.Show();
_visibleControls.Add(control);
ChangeSelfVisibility();
return true;
}
///
/// ChangeSelfVisibility
/// 改变自身的可见度
///
private void ChangeSelfVisibility()
{
if (_visibleControls.Count == 0)
{
Hide();
}
else
{
Show();
}
}
private void OnTreeExited(string key, Control control)
{
//The Hide method is not called when a node exits from the tree, so remove the node here to prevent empty references.
//当节点从节点树内退出时,并不会调用Hide方法,所以在这里移除节点,防止产生空引用。
_visibleControls.Remove(control);
_instantiatedControl.Remove(key);
}
}