Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: LabelWidthAttribute #34

Merged
merged 1 commit into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Alchemy/Assets/Alchemy/Editor/BuiltinAttributeDrawers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,31 @@ public override void OnCreateElement()
}
}

[CustomAttributeDrawer(typeof(LabelWidthAttribute))]
public sealed class LabelWidthDrawer : AlchemyAttributeDrawer
{
public override void OnCreateElement()
{
var width = ((LabelWidthAttribute)Attribute).Width;

if (Element is AlchemyPropertyField field && field.FieldElement is PropertyField)
{
var executed = false;
field.schedule.Execute(() =>
{
var label = field.Q<Label>();
if (label == null) return;
GUIHelper.SetMinAndCurrentWidth(label, width);
executed = true;
}).Until(() => executed);

return;
}

Debug.LogWarning("The LabelWidth attribute currently only supports PropertyField and is ignored for other visual elements.");
}
}

[CustomAttributeDrawer(typeof(HideIfAttribute))]
public sealed class HideIfDrawer : TrackSerializedObjectAttributeDrawer
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine.UIElements;
Expand Down Expand Up @@ -73,6 +71,8 @@ public AlchemyPropertyField(SerializedProperty property, Type type, int depth, b

readonly VisualElement element;

public VisualElement FieldElement => element;

public string Label
{
get
Expand Down
5 changes: 5 additions & 0 deletions Alchemy/Assets/Alchemy/Editor/Internal/GUIHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,12 @@ public static IMGUIContainer CreateLine(Color color, float height)
rect.height = 1f;
EditorGUI.DrawRect(rect, color);
});
}

public static void SetMinAndCurrentWidth(VisualElement visualElement, float value)
{
visualElement.style.minWidth = value;
visualElement.style.width = value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ public sealed class LabelTextAttribute : Attribute
public string Text { get; }
}

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Method)]
public sealed class LabelWidthAttribute : Attribute
{
public LabelWidthAttribute(float width) => Width = width;
public float Width { get; }
}

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Method)]
public sealed class HideIfAttribute : Attribute
{
Expand Down