This commit is contained in:
CortexCore
2023-10-06 23:43:19 +08:00
parent ebf9c1f526
commit 2c4710bc5d
186 changed files with 111802 additions and 764 deletions

View File

@@ -0,0 +1,45 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.UIElements;
namespace BITKit.UX
{
public class CustomTextField : TextField
{
public const string errorUssName = "error";
public new class UxmlTraits : TextField.UxmlTraits
{
private readonly UxmlStringAttributeDescription m_RegexAttribute = new ()
{
name = "Regex"
};
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
{
base.Init(ve, bag, cc);
var textField = (CustomTextField)ve;
textField.Regex = m_RegexAttribute.GetValueFromBag(bag, cc);
}
}
public new class UxmlFactory : UxmlFactory<CustomTextField, UxmlTraits> { }
public CustomTextField():base()
{
this.RegisterValueChangedCallback(OnValueChangedInternal);
}
public string Regex { get; set; }
public event Action<string> OnValueChanged;
private void OnValueChangedInternal(ChangeEvent<string> evt)
{
var isMatch = string.IsNullOrEmpty(Regex) || System.Text.RegularExpressions.Regex.IsMatch(evt.newValue, Regex);
this.Q("unity-text-input").EnableInClassList(errorUssName, !isMatch);
if (isMatch)
{
OnValueChanged?.Invoke(evt.newValue);
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3d3cd768577652b48a96e0ede1713b89
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,96 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using UnityEngine;
using UnityEngine.UIElements;
namespace BITKit.UX
{
/// <summary>
/// 基于Json的Table生成元素
/// </summary>
public class JsonBasedTable : VisualElement
{
public new class UxmlTraits : VisualElement.UxmlTraits
{
private readonly UxmlStringAttributeDescription m_JsonAttribute = new ()
{
name = "Json"
};
private readonly UxmlBoolAttributeDescription m_allowWarningsAttribute = new ()
{
name = "allowWarnings",
defaultValue = false
};
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
{
base.Init(ve, bag, cc);
var table = (JsonBasedTable)ve;
table.Json = m_JsonAttribute.GetValueFromBag(bag, cc);
table.AllowWarning = m_allowWarningsAttribute.GetValueFromBag(bag, cc);
}
}
public new class UxmlFactory : UxmlFactory<JsonBasedTable, UxmlTraits> { }
private string _json;
public string Json
{
get => _json;
set
{
_json = value;
ApplyJson();
}
}
public bool AllowWarning { get; set; }
private readonly List<VisualElement> instanceColumns = new();
private void ApplyJson()
{
try
{
Clear();
style.flexDirection = FlexDirection.Row;
style.justifyContent = Justify.SpaceAround;
instanceColumns.Clear();
if (string.IsNullOrEmpty(Json)) return;
var jArray = JArray.Parse(Json);
var colLength = jArray.Max(x => x.Count());
var rowLength = jArray.Count;
for (var i = 0; i < colLength; i++)
{
instanceColumns.Add(this.Create<VisualElement>());
}
for (var y = 0; y < rowLength; y++)
{
var array = jArray[y] as JArray;
for (var x = 0; x < colLength; x++)
{
var instance = instanceColumns[x];
if (x >= array!.Count)
{
instance.Create<VisualElement>();
}
else
{
var label = instance.Create<Label>();
label.text = array[x].ToString();
}
}
}
}
catch (Exception)
{
if (AllowWarning)
throw;
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f72eadf2897dad54f9bee6f5856de39c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,3 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
@@ -50,6 +51,7 @@ namespace BITKit.UX
_container.pickingMode = PickingMode.Ignore;
}
public override VisualElement contentContainer => _container;
public event Action<int> OnTabChanged;
private int currentTab=-1;
public int CurrentTab
@@ -116,6 +118,7 @@ namespace BITKit.UX
}
if(_container.Children().TryGet(index,out var element))
element.SetActive(true);
OnTabChanged?.Invoke(index);
}
}
}