Skip to content

Commit

Permalink
Ver 2024.2.26 Preview
Browse files Browse the repository at this point in the history
1. Integrate most interfaces into the UBind static class and call them uniformly
2. Some APIs and class names have been changed and are incompatible with old versions.
3. The version number is changed to year and date format, consistent with other UPlugin series plug-ins.
4. Optimize editor style
  • Loading branch information
ls9512 committed Feb 26, 2024
1 parent d16fb5c commit 2893033
Show file tree
Hide file tree
Showing 34 changed files with 430 additions and 356 deletions.
12 changes: 6 additions & 6 deletions .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* 4.1. [Attribute](#Attribute)
* 4.1.1. [Bind Value Attribute](#BindValueAttribute)
* 4.1.2. [Bind Type Attribute](#BindTypeAttribute)
* 4.2. [Data Context](#DataContext)
* 4.2. [Data Container](#DataContainer)
* 4.3. [Data Binder](#DataBinder)
* 4.4. [Data Converter](#DataConverter)
* 4.5. [Component Binder](#ComponentBinder)
Expand Down Expand Up @@ -158,12 +158,12 @@ public class ExanokeMonoBehaviour : MonoBehaviour

public void OnEnable()
{
BindMap.Bind(this);
UBind.RegisterMap(this);
}

public void OnDisable()
{
BindMap.UnBind(this);
UBind.DeRegisterMap(this);
}
}
```
Expand Down Expand Up @@ -253,7 +253,7 @@ When the data source does not have the ability to actively trigger data changes,
It is used to mark attributes and fields that need to be bound in a class that has the ability to handle binding relationships. It is only recommended to bind common basic data types. The marked object will dynamically create a **RuntimeValueBinder** for processing.
#### 4.1.2. <a name='BindTypeAttribute'></a>Bind Type Attribute
Unlike **BindValueAttribute**, it is used to mark custom class and structure type objects, and **RuntimeTypeBinder** is dynamically created for processing.
### 4.2. <a name='DataContext'></a>Data Context
### 4.2. <a name='DataContainer'></a>Data Container
The data container is used to maintain a set of Data Binder, which can contain multiple data sources and data destinations. Each data container is independent of each other.

### 4.3. <a name='DataBinder'></a>Data Binder
Expand Down Expand Up @@ -289,7 +289,7 @@ Used to cache all the binding structure information of the objects marked by **B

***

## 5. <a name='Built-incomponents'></a>Built-in components
## 5. <a name='Built-incomponents'></a>Built-in Components
|Component|Binding object type|Default binding property|Binding property type|
|-|-|-|-|
|Text Binder|Text|text|string|
Expand All @@ -311,7 +311,7 @@ Used to cache all the binding structure information of the objects marked by **B

***

## 6. <a name='Customextension'></a>Custom extension
## 6. <a name='Customextension'></a>Custom Extension
### 6.1. <a name='CustomDataBinder'></a>Custom Data Binder
Implement a runtime data binder for a specific object and property by inheriting **DataBinder**:
``` cs
Expand Down
14 changes: 7 additions & 7 deletions .github/README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* 4.1. [Attribute](#Attribute)
* 4.1.1. [Bind Value Attribute](#BindValueAttribute)
* 4.1.2. [Bind Type Attribute](#BindTypeAttribute)
* 4.2. [Data Context](#DataContext)
* 4.2. [Data Container](#DataContainer)
* 4.3. [Data Binder](#DataBinder)
* 4.4. [Data Converter](#DataConverter)
* 4.5. [Component Binder](#ComponentBinder)
Expand Down Expand Up @@ -158,12 +158,12 @@ public class ExanokeMonoBehaviour : MonoBehaviour

public void OnEnable()
{
BindMap.Bind(this);
UBind.RegisterMap(this);
}

public void OnDisable()
{
BindMap.UnBind(this);
UBind.DeRegisterMap(this);
}
}
```
Expand Down Expand Up @@ -253,7 +253,7 @@ public class ExampleClass
用于在拥有处理绑定关系能力的类中标记需要绑定的属性和字段,只推荐绑定常见基础数据类型。被标记对象会动态创建 **RuntimeValueBinder** 进行处理。
#### 4.1.2. <a name='BindTypeAttribute'></a>Bind Type Attribute
**BindValueAttribute** 的不同,用于标记自定义类和结构体类型对象,会动态创建 **RuntimeTypeBinder** 进行处理。
### 4.2. <a name='DataContext'></a>Data Context
### 4.2. <a name='DataContainer'></a>Data Container
数据容器,用于维护一组 Data Binder,可以包含多个数据源和数据目标点。每个数据容器相互独立。

### 4.3. <a name='DataBinder'></a>Data Binder
Expand All @@ -275,7 +275,7 @@ Convert.ChangeType(object data, Type type);
```
可以按需要使用以下接口提前注册特定类型的自定义转换器:
``` cs
DataConvert.Register(Type sourceType, Type targetType, DataConverter dataConverter);
UBind.RegisterConverter(Type sourceType, Type targetType, DataConverter dataConverter);
```

### 4.5. <a name='ComponentBinder'></a>Component Binder
Expand All @@ -285,7 +285,7 @@ DataConvert.Register(Type sourceType, Type targetType, DataConverter dataConvert
基于 **MonoBehaviour** 生命周期实现,用于统一维护需要主动更新数据的 **DataBinder** 的更新周期。

### 4.7. <a name='BindMap'></a>Bind Map

用于缓存类中所有被 **BindAttribute** 所标记的字段和属性信息。

***

Expand Down Expand Up @@ -365,7 +365,7 @@ public class CustomDataConverter : DataConverter
``` cs
var sourcetType = sourceData.GetType();
var targetType = targetData.GetType();
DataConverter.Register((sourceType, targetType), new CustomDataConverter());
UBind.RegisterConverter((sourceType, targetType), new CustomDataConverter());
```

***
Expand Down
6 changes: 3 additions & 3 deletions Editor/Script/BaseBinderEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ namespace Aya.DataBinding
{
public class BaseBinderEditor : Editor
{
protected void DrawContextKey(SerializedProperty property)
protected void DrawContainerKey(SerializedProperty property)
{
if (property.stringValue == DataContext.Default)
if (property.stringValue == DataContainer.Default)
{
using (new ColorScope(Color.gray))
{
Expand All @@ -34,7 +34,7 @@ protected void DrawDataKey(SerializedProperty property)

protected void DrawDirection(SerializedProperty property)
{
EditorGUILayout.PropertyField(property);
EditorUtil.DrawToolbarEnum(property, typeof(DataDirection));
}

protected void DrawTarget<TComponent>(SerializedProperty property, Transform root) where TComponent : Component
Expand Down
6 changes: 3 additions & 3 deletions Editor/Script/ComponentBinderEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ public abstract class ComponentBinderEditor<TComponent, TValue, TDataBinder> : B
{
public ComponentBinder<TComponent, TValue, TDataBinder> ComponentBinder => target as ComponentBinder<TComponent, TValue, TDataBinder>;

protected SerializedProperty ContextKeyProperty;
protected SerializedProperty ContainerKeyProperty;
protected SerializedProperty DataKeyProperty;
protected SerializedProperty DirectionProperty;
protected SerializedProperty UpdateTypeProperty;
protected SerializedProperty TargetProperty;

public virtual void OnEnable()
{
ContextKeyProperty = serializedObject.FindProperty(nameof(ComponentBinder.Context));
ContainerKeyProperty = serializedObject.FindProperty(nameof(ComponentBinder.Container));
DataKeyProperty = serializedObject.FindProperty(nameof(ComponentBinder.Key));
DirectionProperty = serializedObject.FindProperty(nameof(ComponentBinder.Direction));
UpdateTypeProperty = serializedObject.FindProperty(nameof(ComponentBinder.UpdateType));
Expand All @@ -30,7 +30,7 @@ public override void OnInspectorGUI()
{
serializedObject.Update();

DrawContextKey(ContextKeyProperty);
DrawContainerKey(ContainerKeyProperty);
DrawDataKey(DataKeyProperty);
DrawDirection(DirectionProperty);

Expand Down
6 changes: 3 additions & 3 deletions Editor/Script/TypeBinderEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class TypeBinderEditor : BaseBinderEditor
{
public TypeBinder TypeBinder => target as TypeBinder;

protected SerializedProperty ContextKeyProperty;
protected SerializedProperty ContainerKeyProperty;
protected SerializedProperty DataKeyProperty;
protected SerializedProperty DirectionProperty;
protected SerializedProperty UpdateTypeProperty;
Expand All @@ -21,7 +21,7 @@ public class TypeBinderEditor : BaseBinderEditor

public virtual void OnEnable()
{
ContextKeyProperty = serializedObject.FindProperty("Context");
ContainerKeyProperty = serializedObject.FindProperty("Container");
DataKeyProperty = serializedObject.FindProperty("Key");
DirectionProperty = serializedObject.FindProperty("Direction");
UpdateTypeProperty = serializedObject.FindProperty("UpdateType");
Expand All @@ -35,7 +35,7 @@ public override void OnInspectorGUI()
{
serializedObject.Update();

DrawContextKey(ContextKeyProperty);
DrawContainerKey(ContainerKeyProperty);
DrawDataKey(DataKeyProperty);
DrawDirection(DirectionProperty);

Expand Down
8 changes: 3 additions & 5 deletions Editor/Script/Util/EditorStyle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ public static GUIStyle RichLabel
{
get
{
if (_richLabel == null)
{
_richLabel = EditorStyles.label;
_richLabel.richText = true;
}
if (_richLabel != null) return _richLabel;
_richLabel = EditorStyles.label;
_richLabel.richText = true;

return _richLabel;
}
Expand Down
87 changes: 65 additions & 22 deletions Editor/Script/Util/EditorUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ public static SearchableDropdownItem CreateAssemblyMenu()
root.AddSeparator();

var assemblies = TypeCaches.Assemblies;
foreach (var assembly in assemblies)
for (var i = 0; i < assemblies.Length; i++)
{
var assembly = assemblies[i];
var assemblyName = assembly.GetName().Name;
var child = new SearchableDropdownItem(assemblyName, assemblyName);
root.AddChild(child);
Expand Down Expand Up @@ -57,19 +58,18 @@ public static SearchableDropdownItem CreateTypeMenu(string assemblyName)
root.AddSeparator();

var assembly = TypeCaches.GetAssemblyByName(assemblyName);
if (assembly != null)
if (assembly == null) return root;
var types = assembly.GetTypes();
for (var i = 0; i < types.Length; i++)
{
var types = assembly.GetTypes();
foreach (var type in types)
{
if (type.IsAbstract) continue;
if (type.IsInterface) continue;
if (type.IsGenericType) continue;
if (type.IsEnum) continue;
var type = types[i];
if (type.IsAbstract) continue;
if (type.IsInterface) continue;
if (type.IsGenericType) continue;
if (type.IsEnum) continue;

var child = new SearchableDropdownItem(type.Name, type.FullName);
root.AddChild(child);
}
var child = new SearchableDropdownItem(type.Name, type.FullName);
root.AddChild(child);
}

return root;
Expand Down Expand Up @@ -113,8 +113,9 @@ public static SearchableDropdownItem CreateComponentsTreeMenu<TComponent>(Transf
private static void CreateComponentsTreeMenuRecursion<TComponent>(SearchableDropdownItem root, Transform parent, string path) where TComponent : Component
{
var components = parent.GetComponents<TComponent>();
foreach (var component in components)
for (var i = 0; i < components.Length; i++)
{
var component = components[i];
var componentName = component.GetType().Name;
var child = new SearchableDropdownItem(path + componentName, component)
{
Expand Down Expand Up @@ -192,8 +193,9 @@ public static GenericMenu CreatePropertyMenu(Type type, SerializedProperty prope

var propertyInfos = TypeCaches.GetTypeProperties(type);
var prefix = "Property/";
foreach (var propertyInfo in propertyInfos)
for (var i = 0; i < propertyInfos.Count; i++)
{
var propertyInfo = propertyInfos[i];
// if (!TypeCaches.BindableTypes.Contains(propertyInfo.PropertyType)) continue;
var displayName = propertyInfo.Name + "\t\t" + propertyInfo.PropertyType.Name;
menu.AddItem(new GUIContent(prefix + displayName), propertyInfo.Name == property.stringValue, () =>
Expand All @@ -206,8 +208,9 @@ public static GenericMenu CreatePropertyMenu(Type type, SerializedProperty prope

var filedInfos = TypeCaches.GetTypeFields(type);
prefix = "Field/";
foreach (var fieldInfo in filedInfos)
for (var i = 0; i < filedInfos.Count; i++)
{
var fieldInfo = filedInfos[i];
// if (!TypeCaches.BindableTypes.Contains(fieldInfo.FieldType)) continue;
var displayName = fieldInfo.Name + "\t\t" + fieldInfo.FieldType.Name;
menu.AddItem(new GUIContent(prefix + displayName), fieldInfo.Name == property.stringValue, () =>
Expand Down Expand Up @@ -236,12 +239,7 @@ public static void PropertyTreeMenu(string propertyName, Type type, SerializedPr
return (propertyInfo.Name, propertyInfo.PropertyType.Name);
}

if (filedInfo != null)
{
return (filedInfo.Name, filedInfo.FieldType.Name);
}

return (EditorStyle.NoneStr, "");
return filedInfo != null ? (filedInfo.Name, filedInfo.FieldType.Name) : (EditorStyle.NoneStr, "");
}

var (currentPropertyName, currentPropertyTypeName) = GetCurrentPropertyInfo();
Expand Down Expand Up @@ -328,7 +326,52 @@ public static void ColorLine(Color color, float height)
GUILayout.EndVertical();
var rect = GUILayoutUtility.GetLastRect();
EditorGUI.DrawRect(rect, color);
}
}

#endregion

#region Enum

public static void DrawToolbarEnum(SerializedProperty property, Type enumType)
{
DrawToolbarEnum(property, property.displayName, enumType);
}

public static void DrawToolbarEnum(SerializedProperty property, string propertyName, Type enumType)
{
property.intValue = DrawToolbarEnum(property.intValue, propertyName, enumType);
}

public static int DrawToolbarEnum(int value, string propertyName, Type enumType)
{
using (GUIHorizontal.Create())
{
GUILayout.Label(propertyName, EditorStyles.label, GUILayout.Width(EditorGUIUtility.labelWidth));
var buttons = Enum.GetNames(enumType);
var style = EditorStyles.miniButton;
style.margin = new RectOffset();
var rect = EditorGUILayout.GetControlRect(false, EditorGUIUtility.singleLineHeight, style);
var btnWidth = rect.width / buttons.Length;
for (var i = 0; i < buttons.Length; i++)
{
var button = buttons[i];
var index = i;
var btnRect = rect;
btnRect.x += i * btnWidth;
btnRect.width = btnWidth;
using (GUIColorArea.Create(Color.white, Color.gray * 1.5f, value == index))
{
var btn = GUI.Button(btnRect, button, style);
if (btn)
{
return index;
}
}
}
}

return value;
}

#endregion
}
Expand Down
39 changes: 35 additions & 4 deletions Editor/Script/Util/GUIStruct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,44 @@ public static void SetState(Object target, string title, bool value)

public static Dictionary<string, bool> GetStateDic(Object target)
{
if (!StateCacheDic.TryGetValue(target, out var stateDic))
if (StateCacheDic.TryGetValue(target, out var stateDic)) return stateDic;
stateDic = new Dictionary<string, bool>();
StateCacheDic.Add(target, stateDic);

return stateDic;
}
}

internal struct GUIColorArea : IDisposable
{
public Color OriginalColor;

public static GUIColorArea Create(Color color, bool check = true)
{
return new GUIColorArea(color, Color.white, check);
}

public static GUIColorArea Create(Color enableColor, Color disableColor, bool check = true)
{
return new GUIColorArea(enableColor, disableColor, check);
}

public GUIColorArea(Color enableColor, Color disableColor, bool check = true)
{
OriginalColor = GUI.color;
if (check)
{
GUI.color = enableColor;
}
else
{
stateDic = new Dictionary<string, bool>();
StateCacheDic.Add(target, stateDic);
GUI.color = disableColor;
}
}

return stateDic;
public void Dispose()
{
GUI.color = OriginalColor;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Extension.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2893033

Please sign in to comment.