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)
{

View File

@@ -54,7 +54,7 @@ namespace BITKit.UX
{
Clear();
style.flexDirection = FlexDirection.Row;
style.justifyContent = Justify.SpaceAround;
style.justifyContent = Justify.FlexStart;
instanceColumns.Clear();
if (string.IsNullOrEmpty(Json)) return;
@@ -64,7 +64,9 @@ namespace BITKit.UX
var rowLength = jArray.Count;
for (var i = 0; i < colLength; i++)
{
instanceColumns.Add(this.Create<VisualElement>());
var newContainer = this.Create<VisualElement>();
newContainer.name = $"{nameof(VisualElement)}-{i}";
instanceColumns.Add(newContainer);
}
for (var y = 0; y < rowLength; y++)
{

View File

@@ -75,13 +75,23 @@ namespace BITKit.UX
BITKit.UX.UXUtils.Inject(this);
if(IsValid && autoEntry)
UXService.Entry(this);
var returnButton= document.rootVisualElement.Q("return-button");
returnButton?.RegisterCallback<MouseDownEvent>(x =>
{
if (x.button is 0)
OnReturn();
});
}
public bool IsEntered { get; set; }
public void Entry()
{
UXService.Entry(this);
}
protected virtual void OnReturn()
{
UXService.Return();
}
protected virtual void OnEnable()=>UXService.Register(this);
protected virtual void OnDisable()=>UXService.UnRegister(this);
protected virtual void OnPanelEntry(){}