Skip to content

Commit

Permalink
Update DataBind
Browse files Browse the repository at this point in the history
  • Loading branch information
kochounoyume committed Jan 16, 2025
1 parent 35a5a27 commit c80b13d
Show file tree
Hide file tree
Showing 13 changed files with 188 additions and 102 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#if ENABLE_UGUI
#nullable enable
#nullable enable

using System;
using System.Linq;
Expand Down Expand Up @@ -34,7 +33,8 @@ private static readonly (Type type, string path)[] s_cache = TypeCache.GetTypesD
}
return (x, attribute.path);
})
.OrderBy(static x => x.path)
.OrderBy(static x => x.path.StartsWith("Other", StringComparison.Ordinal) ? 1 : 0)
.ThenBy(static x => x.path)
.ToArray();

private readonly IItemSelectHandler? _handler;
Expand Down Expand Up @@ -65,9 +65,10 @@ protected override AdvancedDropdownItem BuildRoot()
continue;
}

var genericType = DataBindUtils.GetTargetType(type);
var child = new Item(split, type)
{
icon = IconUtils.LoadUGUIIcon(split) ?? (parent == root ? IconUtils.LoadCsIcon() : null)
icon = genericType.Name == split ? DataBindUtils.LoadMiniTypeThumbnail(genericType) : null
};
parent.AddChild(child);
parent = child;
Expand All @@ -85,6 +86,4 @@ protected override void ItemSelected(AdvancedDropdownItem item)
}
}
}
}

#endif
}
49 changes: 49 additions & 0 deletions Packages/MinimalUtility/Editor/DataBind/DataBindUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#nullable enable

using System;
using UnityEditor;
using UnityEngine;

namespace MinimalUtility.Editor.DataBind
{
using MinimalUtility.DataBind;

internal static class DataBindUtils
{
public static Type GetTargetType(Type type)
{
var baseType = type.BaseType;
while (baseType != null)
{
if (baseType.IsGenericType && baseType.GetGenericTypeDefinition() == typeof(TargetBindElement<>))
{
return baseType.GetGenericArguments()[0];
}
baseType = baseType.BaseType;
}
throw new InvalidOperationException("TargetBindElement<> not found in the type hierarchy.");
}

public static Texture2D? LoadRemoveIcon()
{
return EditorGUIUtility.Load("Toolbar Minus") as Texture2D;
}

public static Texture2D? LoadMiniTypeThumbnail(Type type)
{
#if ENABLE_TEXTMESHPRO
if (type == typeof(TMPro.TextMeshProUGUI) || type == typeof(TMPro.TMP_InputField))
{
#if UNITY_2023_2_OR_NEWER
return AssetDatabase.LoadAssetAtPath<Texture2D>(
"Packages/com.unity.ugui/Editor Resources/Gizmos/TMP - Text Component Icon.psd");
#else
return AssetDatabase.LoadAssetAtPath<Texture2D>(
"Packages/com.unity.textmeshpro/Editor Resources/Gizmos/TMP - Text Component Icon.psd");
#endif
}
#endif
return AssetPreview.GetMiniTypeThumbnail(type) ?? EditorGUIUtility.Load("Cs Script Icon") as Texture2D;
}
}
}
3 changes: 3 additions & 0 deletions Packages/MinimalUtility/Editor/DataBind/DataBindUtils.cs.meta

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

7 changes: 2 additions & 5 deletions Packages/MinimalUtility/Editor/DataBind/DataBindViewEditor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#if ENABLE_UGUI
#nullable enable
#nullable enable

using System;
using System.Reflection;
Expand Down Expand Up @@ -95,6 +94,4 @@ private static VisualElement CreateBox(string label)
return box;
}
}
}

#endif
}
7 changes: 2 additions & 5 deletions Packages/MinimalUtility/Editor/DataBind/IItemSelectHandler.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
#if ENABLE_UGUI
#nullable enable
#nullable enable

namespace MinimalUtility.Editor.DataBind
{
internal interface IItemSelectHandler
{
void OnItemSelected(System.Type type);
}
}

#endif
}
64 changes: 0 additions & 64 deletions Packages/MinimalUtility/Editor/DataBind/IconUtils.cs

This file was deleted.

3 changes: 0 additions & 3 deletions Packages/MinimalUtility/Editor/DataBind/IconUtils.cs.meta

This file was deleted.

29 changes: 12 additions & 17 deletions Packages/MinimalUtility/Editor/DataBind/TargetBindElementDrawer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#if ENABLE_UGUI
#nullable enable
#nullable enable

using System.Reflection;
using MinimalUtility.DataBind;
Expand All @@ -11,7 +10,7 @@
namespace MinimalUtility.Editor.DataBind
{
[CustomPropertyDrawer(typeof(TargetBindElement<>), true)]
internal class TargetBindElementDrawer : PropertyDrawer
public class TargetBindElementDrawer : PropertyDrawer
{
public override VisualElement CreatePropertyGUI(SerializedProperty property)
{
Expand All @@ -22,14 +21,7 @@ public override VisualElement CreatePropertyGUI(SerializedProperty property)
throw new System.InvalidOperationException($"Type {property.managedReferenceValue.GetType().Name} does not have {nameof(DataBindMenuAttribute)}.");
}
var splits = attr.path.Split('/');
var icon = IconUtils.LoadUGUIIcon(splits) ?? IconUtils.LoadCsIcon();

var baseType = type.BaseType;
while (baseType is { IsGenericType: true } && baseType.GetGenericTypeDefinition() != typeof(TargetBindElement<>))
{
baseType = baseType.BaseType;
}
var genericName = baseType?.GetGenericArguments()[0].Name;
var genericType = DataBindUtils.GetTargetType(type);

var root = new Foldout()
{
Expand All @@ -38,14 +30,14 @@ public override VisualElement CreatePropertyGUI(SerializedProperty property)
var foldoutCheck = root.Q(className: Foldout.checkmarkUssClassName);
foldoutCheck.parent.Add(new Image()
{
image = icon,
image = DataBindUtils.LoadMiniTypeThumbnail(genericType),
style =
{
width = 18f,
height = 18f,
}
});
foldoutCheck.parent.Add(new Label(genericName + " - " + splits[^1])
foldoutCheck.parent.Add(new Label(genericType.Name + " - " + splits[^1])
{
style =
{
Expand All @@ -63,7 +55,7 @@ public override VisualElement CreatePropertyGUI(SerializedProperty property)
top = 4f,
right = 4f,
position = Position.Absolute,
backgroundImage = IconUtils.LoadRemoveIcon(),
backgroundImage = DataBindUtils.LoadRemoveIcon(),
backgroundColor = Color.white
}
};
Expand All @@ -84,9 +76,12 @@ public override VisualElement CreatePropertyGUI(SerializedProperty property)
});
root.Add(new PropertyField(property.FindPropertyRelative("_target")));
root.Add(new PropertyField(property.FindPropertyRelative("_propertyName")));
var optionProperty = property.FindPropertyRelative("_option");
if (optionProperty != null)
{
root.Add(new PropertyField(optionProperty));
}
return root;
}
}
}

#endif
}
8 changes: 8 additions & 0 deletions Packages/MinimalUtility/Runtime/DataBind/BindElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ public override void Bind(bool value)
}
}

[DataBindMenu("Other/" + nameof(Behaviour.enabled))]
internal sealed class CommonEnableElement : EnableElement<Behaviour>
{
private CommonEnableElement() : base()
{
}
}

[DataBindMenu(nameof(GameObject) + "/active")]
internal sealed class ActiveElement : TargetBindElement<GameObject>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
#nullable enable

using System;

namespace MinimalUtility.DataBind
{
Expand Down
31 changes: 31 additions & 0 deletions Packages/MinimalUtility/Runtime/DataBind/ParseOption.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#nullable enable

using System;
using UnityEngine;

namespace MinimalUtility.DataBind
{
[Serializable]
public struct ParseOption
{
[SerializeField, Tooltip("数字書式指定文字列")]
private string _format;

[SerializeField]
private NumberOption _numberOption;

public ReadOnlySpan<char> format => string.IsNullOrEmpty(_format) ? default : _format.AsSpan();

public NumberOption numberOption => _numberOption;
}

public enum NumberOption
{
[InspectorName("設定なし")]
None,
[InspectorName("絶対値")]
Absolute,
[InspectorName("負の値化")]
Negative
}
}
3 changes: 3 additions & 0 deletions Packages/MinimalUtility/Runtime/DataBind/ParseOption.cs.meta

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

Loading

0 comments on commit c80b13d

Please sign in to comment.