This commit is contained in:
CortexCore
2024-04-06 16:33:32 +08:00
parent 637bbef7d2
commit 37e7fcea51
42 changed files with 597 additions and 149 deletions

View File

@@ -71,6 +71,10 @@ namespace BITKit.IData
Name = attribute.Name;
VisualElement.GetType().GetProperty("label",ReflectionHelper.Flags)!.SetValue(VisualElement, Name);
}
if (fieldInfo.GetCustomAttribute<ReadOnlyAttribute>() is not null)
{
VisualElement.SetEnabled(false);
}
if(VisualElement is INotifyValueChanged<TDataType> notifyValueChanged)
{
notifyValueChanged.SetValueWithoutNotify(currentValue);
@@ -80,6 +84,8 @@ namespace BITKit.IData
currentValue = x.newValue;
});
}
return VisualElement;
}

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@@ -25,6 +26,53 @@ namespace BITKit.IData
}
private static readonly Dictionary<Type,IUXDataBinder> _Binders = new();
private static readonly CacheList<IUXDataBinder> _Instances = new();
public static VisualElement Create(object target)
{
var visualElement = new VisualElement();
var fields = target.GetType().GetFields(ReflectionHelper.Flags);
foreach (var fieldInfo in fields.Where(x=>x.GetCustomAttribute<ExportAttribute>() is not null))
{
var handler = UXDataBindingService.Create(target,fieldInfo);
var ui = handler.CreateUI(target, fieldInfo);
visualElement.Add(ui);
}
foreach (var methodInfo in target.GetType().GetMethods(ReflectionHelper.Flags))
{
if (Attribute.IsDefined(methodInfo, typeof(ExportAttribute)) is false) continue;
var exportAttribute = methodInfo.GetCustomAttribute<ExportAttribute>();
var button = visualElement.Create<Button>();
button.text =string.IsNullOrEmpty(exportAttribute.Name) ? methodInfo.Name:exportAttribute.Name;
button.clicked += OnClicked;
continue;
void OnClicked()
{
try
{
methodInfo.Invoke(target, null);
}
catch (TargetInvocationException targetInvocationException)
{
if (targetInvocationException.InnerException is InGameException e is false) return;
switch (e)
{
case {InnerException:not null}:
Alert.Print(e.Message,e.InnerException.Message);
break;
default:
Alert.Print(e.Message,e.Source);
break;
}
}
}
}
return visualElement;
}
public static IUXDataBinder Create(object target,FieldInfo fieldInfo)
{