(client):feat:添加对列表定义读取的支持,ui管理器将对没有键的窗口也进行统一管理
This commit is contained in:
@ -34,17 +34,49 @@ namespace Data
|
||||
base.Init(xmlDef);
|
||||
|
||||
var nodes = xmlDef.Elements("DrawNodeDef");
|
||||
if (nodes.Count() == 0)
|
||||
var xElements = nodes as XElement[] ?? nodes.ToArray();
|
||||
if (!xElements.Any())
|
||||
return false;
|
||||
foreach (var node in nodes)
|
||||
{
|
||||
var drawNode = new DrawNodeDef();
|
||||
drawNode.Init(node);
|
||||
drawNodes.Add(drawNode);
|
||||
}
|
||||
foreach (var node in xElements)
|
||||
{
|
||||
var drawNode = new DrawNodeDef();
|
||||
drawNode.Init(node);
|
||||
drawNodes.Add(drawNode);
|
||||
}
|
||||
|
||||
return true;;
|
||||
}
|
||||
// 重载 == 运算符
|
||||
public static bool operator ==(DrawingOrderDef a, DrawingOrderDef b)
|
||||
{
|
||||
if (ReferenceEquals(a, b)) return true; // 如果是同一个对象,直接返回 true
|
||||
if (ReferenceEquals(a, null) || ReferenceEquals(b, null)) return false; // 如果其中一个为 null,返回 false
|
||||
|
||||
return AreEqual(a, b);
|
||||
}
|
||||
|
||||
// 重载 != 运算符
|
||||
public static bool operator !=(DrawingOrderDef a, DrawingOrderDef b)
|
||||
{
|
||||
return !(a == b);
|
||||
}
|
||||
|
||||
// 判断两个 DrawingOrderDef 是否相等
|
||||
private static bool AreEqual(DrawingOrderDef a, DrawingOrderDef b)
|
||||
{
|
||||
// 比较 drawNodes 的数量
|
||||
if (a.drawNodes.Count != b.drawNodes.Count)
|
||||
return false;
|
||||
|
||||
// 递归比较每个 DrawNodeDef
|
||||
for (int i = 0; i < a.drawNodes.Count; i++)
|
||||
{
|
||||
if (!DrawNodeDef.AreEqual(a.drawNodes[i], b.drawNodes[i]))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public class DrawNodeDef : Define
|
||||
@ -77,16 +109,16 @@ namespace Data
|
||||
public Vector2 StringToVector(string vectorDef)
|
||||
{
|
||||
// 去掉可能存在的括号和多余的空格
|
||||
string cleanedInput = vectorDef.Replace("(", "").Replace(")", "").Trim();
|
||||
var cleanedInput = vectorDef.Replace("(", "").Replace(")", "").Trim();
|
||||
|
||||
// 使用正则表达式匹配两个浮点数
|
||||
Match match = Regex.Match(cleanedInput, @"\s*(-?\d+(\.\d*)?)\s*[, ]\s*(-?\d+(\.\d*)?)\s*");
|
||||
var match = Regex.Match(cleanedInput, @"\s*(-?\d+(\.\d*)?)\s*[, ]\s*(-?\d+(\.\d*)?)\s*");
|
||||
|
||||
if (match.Success)
|
||||
{
|
||||
// 提取匹配到的两个浮点数
|
||||
float x = float.Parse(match.Groups[1].Value);
|
||||
float y = float.Parse(match.Groups[3].Value);
|
||||
var x = float.Parse(match.Groups[1].Value);
|
||||
var y = float.Parse(match.Groups[3].Value);
|
||||
|
||||
// 返回 Vector2 对象
|
||||
return new Vector2(x, y);
|
||||
@ -96,6 +128,30 @@ namespace Data
|
||||
return Vector2.zero;
|
||||
}
|
||||
}
|
||||
|
||||
// 判断两个 DrawNodeDef 是否相等
|
||||
public static bool AreEqual(DrawNodeDef a, DrawNodeDef b)
|
||||
{
|
||||
if (ReferenceEquals(a, b)) return true; // 如果是同一个对象,直接返回 true
|
||||
if (ReferenceEquals(a, null) || ReferenceEquals(b, null)) return false; // 如果其中一个为 null,返回 false
|
||||
// 比较基本属性
|
||||
if (a.drawNodeType != b.drawNodeType ||
|
||||
a.nodeName != b.nodeName ||
|
||||
a.position != b.position ||
|
||||
Math.Abs(a.FPS - b.FPS) > 0.001f) // 浮点数比较需要考虑精度
|
||||
return false;
|
||||
// 比较 children 的数量
|
||||
if (a.children.Count != b.children.Count)
|
||||
return false;
|
||||
// 递归比较每个子节点
|
||||
for (var i = 0; i < a.children.Count; i++)
|
||||
{
|
||||
if (!AreEqual(a.children[i], b.children[i]))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -279,8 +279,14 @@ namespace Data
|
||||
value = reference;
|
||||
}
|
||||
}
|
||||
else if(field.FieldType.IsArray)
|
||||
{
|
||||
value = ProcessArrayField(field, element);
|
||||
}
|
||||
else
|
||||
{
|
||||
value = Convert.ChangeType(element.Value, field.FieldType);
|
||||
}
|
||||
field.SetValue(define, value);
|
||||
}
|
||||
catch (Exception ex)
|
||||
@ -289,7 +295,44 @@ namespace Data
|
||||
}
|
||||
}
|
||||
}
|
||||
private static object ProcessArrayField(FieldInfo field, XElement element)
|
||||
{
|
||||
Type elementType = field.FieldType.GetElementType();
|
||||
if (elementType == null) return null;
|
||||
|
||||
var arrayElements = new List<object>();
|
||||
foreach (var liElement in element.Elements())
|
||||
{
|
||||
if (elementType.IsSubclassOf(typeof(Define)))
|
||||
{
|
||||
Define nestedDefine = (Define)Activator.CreateInstance(elementType);
|
||||
DefaultInitDefine(nestedDefine, liElement, elementType);
|
||||
arrayElements.Add(nestedDefine);
|
||||
}
|
||||
else if (elementType.IsArray) // 嵌套数组处理
|
||||
{
|
||||
// 递归处理嵌套数组
|
||||
var nestedArray = ProcessArrayField(
|
||||
new { FieldType = elementType }.GetType().GetField("FieldType"),
|
||||
liElement
|
||||
);
|
||||
arrayElements.Add(nestedArray);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 基本类型处理
|
||||
arrayElements.Add(Convert.ChangeType(liElement.Value, elementType));
|
||||
}
|
||||
}
|
||||
|
||||
// 构建结果数组
|
||||
Array resultArray = Array.CreateInstance(elementType, arrayElements.Count);
|
||||
for (int i = 0; i < arrayElements.Count; i++)
|
||||
{
|
||||
resultArray.SetValue(arrayElements[i], i);
|
||||
}
|
||||
return resultArray;
|
||||
}
|
||||
/// <summary>
|
||||
/// 从 List<c>XDocument</c> 中查找指定根元素名称的文档。
|
||||
/// </summary>
|
||||
|
@ -1,4 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace Data
|
||||
{
|
||||
@ -11,6 +14,10 @@ namespace Data
|
||||
drawingOrder_up,
|
||||
drawingOrder_left,
|
||||
drawingOrder_right;
|
||||
|
||||
public string[] behaviorTree;
|
||||
|
||||
|
||||
public DrawingOrderDef GetDrawingOrder(Orientation orientation)
|
||||
{
|
||||
// 定义一个临时变量用于存储结果
|
||||
@ -48,4 +55,33 @@ namespace Data
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public class BehaviorTreeDef : Define
|
||||
{
|
||||
public BehaviorTreeDef[] childTree;
|
||||
public string className;
|
||||
|
||||
public override bool Init(XElement xmlDef)
|
||||
{
|
||||
base.Init(xmlDef);
|
||||
|
||||
var nodes = xmlDef.Elements("Node");
|
||||
var xElements = nodes as XElement[] ?? nodes.ToArray();
|
||||
if (!xElements.Any())
|
||||
return false;
|
||||
|
||||
className = xmlDef.Attribute("className")?.Value;
|
||||
List<BehaviorTreeDef> children = new();
|
||||
foreach (var node in xElements)
|
||||
{
|
||||
var childNode = new BehaviorTreeDef();
|
||||
childNode.Init(node);
|
||||
children.Add(childNode);
|
||||
}
|
||||
|
||||
childTree = children.ToArray();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user