Skip to content

Commit

Permalink
Fix DataBindView Editor
Browse files Browse the repository at this point in the history
  • Loading branch information
kochounoyume committed Jan 17, 2025
1 parent ccde882 commit 7ac85cb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 41 deletions.
7 changes: 2 additions & 5 deletions Packages/MinimalUtility/Editor/DataBind/DataBindUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace MinimalUtility.Editor.DataBind

internal static class DataBindUtils
{
private static readonly Lazy<Texture2D?> s_removeIcon =
new(static () => EditorGUIUtility.Load("Toolbar Minus") as Texture2D);
public static Type GetTargetType(Type type)
{
var baseType = type.BaseType;
Expand All @@ -24,11 +26,6 @@ public static Type GetTargetType(Type type)
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
Expand Down
63 changes: 32 additions & 31 deletions Packages/MinimalUtility/Editor/DataBind/DataBindViewEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,30 @@ namespace MinimalUtility.Editor.DataBind
internal sealed class DataBindViewEditor : UnityEditor.Editor, IItemSelectHandler
{
private SerializedProperty? _bindElementsProperty;
private AddBindElementDropdown? _addBindElementDropdown;

public override VisualElement CreateInspectorGUI()
{
_bindElementsProperty = serializedObject.FindProperty("_elements");
_addBindElementDropdown = new AddBindElementDropdown(new AdvancedDropdownState(), this);

var box = CreateBox("Bind Elements");
var root = new VisualElement();
root.Add(CreateBindElementsField(new AddBindElementDropdown(new AdvancedDropdownState(), this)));
return root;
}

box.Add(new ListView
{
bindingPath = "_elements",
reorderMode = ListViewReorderMode.Animated,
showBorder = true,
showAddRemoveFooter = false,
showFoldoutHeader = false,
showBoundCollectionSize = false,
virtualizationMethod = CollectionVirtualizationMethod.DynamicHeight,
selectionType = SelectionType.None
});
void IItemSelectHandler.OnItemSelected(Type type)
{
if (_bindElementsProperty == null) return;
var last = _bindElementsProperty.arraySize;
_bindElementsProperty.InsertArrayElementAtIndex(_bindElementsProperty.arraySize);
var property = _bindElementsProperty.GetArrayElementAtIndex(last);
var constructor = type.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance,
null, Type.EmptyTypes, null);
property.managedReferenceValue = constructor?.Invoke(null);
serializedObject.ApplyModifiedProperties();
}

private static VisualElement CreateBindElementsField(AddBindElementDropdown addBindElementDropdown)
{
var addButton = new Button()
{
text = "Add Element",
Expand All @@ -49,24 +52,22 @@ public override VisualElement CreateInspectorGUI()
{
var button = (Button)eve.target;
dropdown.Show(button.worldBound);
}, _addBindElementDropdown);
box.Add(addButton);
}, addBindElementDropdown);

var root = new VisualElement();
root.Add(box);
return root;
}

void IItemSelectHandler.OnItemSelected(Type type)
{
if (_bindElementsProperty == null) return;
var last = _bindElementsProperty.arraySize;
_bindElementsProperty.InsertArrayElementAtIndex(_bindElementsProperty.arraySize);
var property = _bindElementsProperty.GetArrayElementAtIndex(last);
var constructor = type.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance,
null, Type.EmptyTypes, null);
property.managedReferenceValue = constructor?.Invoke(null);
serializedObject.ApplyModifiedProperties();
var box = CreateBox("Bind Elements");
box.Add(new ListView
{
bindingPath = "_elements",
reorderMode = ListViewReorderMode.Animated,
showBorder = true,
showAddRemoveFooter = false,
showFoldoutHeader = false,
showBoundCollectionSize = false,
virtualizationMethod = CollectionVirtualizationMethod.DynamicHeight,
selectionType = SelectionType.None
});
box.Add(addButton);
return box;
}

private static VisualElement CreateBox(string label)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ public override VisualElement CreatePropertyGUI(SerializedProperty property)
var splits = attr.path.Split('/');
var genericType = DataBindUtils.GetTargetType(type);

var root = new Foldout()
{
focusable = true
};
var root = new Foldout { focusable = true };
var foldoutCheck = root.Q(className: Foldout.checkmarkUssClassName);
foldoutCheck.parent.Add(new Image()
{
Expand Down Expand Up @@ -55,7 +52,7 @@ public override VisualElement CreatePropertyGUI(SerializedProperty property)
top = 4f,
right = 4f,
position = Position.Absolute,
backgroundImage = DataBindUtils.LoadRemoveIcon(),
backgroundImage = DataBindUtils.removeIcon,
backgroundColor = Color.white
}
};
Expand Down

0 comments on commit 7ac85cb

Please sign in to comment.