Skip to content

Commit

Permalink
Base Attribute implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
uurha committed Aug 12, 2024
1 parent b79d48f commit ab83bf5
Show file tree
Hide file tree
Showing 368 changed files with 9,133 additions and 45 deletions.
8 changes: 8 additions & 0 deletions Assets/BetterAttributes.meta

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

8 changes: 8 additions & 0 deletions Assets/BetterAttributes/Editor.meta

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

Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "{project_name}.Editor",
"rootNamespace": "Better.{short_project_name}.EditorAddons",
"name": "BetterAttributes.Editor",
"rootNamespace": "Better.Attributes.EditorAddons",
"references": [
"GUID:443314a5a4e67c14a88ae223776b6554",
"GUID:35101f455c979e94c9a0a4793484b7fd",
"GUID:01df13aca8d01e24a911bcc3e8277031",
"GUID:6adbd2a12d24f5d429f7ea1b1086e835"
"GUID:8bd4b41f8da90144d9006c4d926c9679",
"GUID:55fbe2a01ca11514f94a66e7102c8895"
],
"includePlatforms": [
"Editor"
Expand All @@ -17,4 +18,4 @@
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
}

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

3 changes: 3 additions & 0 deletions Assets/BetterAttributes/Editor/EditorAddons.meta

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

3 changes: 3 additions & 0 deletions Assets/BetterAttributes/Editor/EditorAddons/Comparers.meta

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using Better.Commons.Runtime.Comparers;

namespace Better.Attributes.EditorAddons.Comparers
{
public class AnyTypeComparer : BaseComparer<AnyTypeComparer,Type>, IEqualityComparer<Type>
{
public bool Equals(Type x, Type y)
{
if (ReferenceEquals(x, y)) return true;
if (ReferenceEquals(x, null)) return false;
if (ReferenceEquals(y, null)) return false;
if (x.IsAssignableFrom(y) || x == y) return true;
return x == typeof(Type);
}

public int GetHashCode(Type obj)
{
return 0;
}
}
}

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Better.Attributes.Runtime;
using Better.Commons.EditorAddons.CustomEditors.Attributes;
using Better.Commons.EditorAddons.CustomEditors.Base;
using Better.Commons.EditorAddons.Extensions;
using Better.Commons.EditorAddons.Utility;
using Better.Commons.Runtime.Extensions;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
using Object = UnityEngine.Object;

namespace Better.Attributes.EditorAddons.CustomEditors
{
[MultiEditor(typeof(Object), true, Order = 999)]
public class ButtonsEditor : ExtendedEditor
{
private Dictionary<int, IEnumerable<KeyValuePair<MethodInfo, EditorButtonAttribute>>> _methodButtonsAttributes =
new Dictionary<int, IEnumerable<KeyValuePair<MethodInfo, EditorButtonAttribute>>>();

public ButtonsEditor(Object target, SerializedObject serializedObject) : base(target, serializedObject)
{
}

public override void OnDisable()
{
}

public override void OnEnable()
{
var type = _target.GetType();
_methodButtonsAttributes = EditorButtonUtility.GetSortedMethodAttributes(type);
}

private Button DrawButton(MethodInfo methodInfo, EditorButtonAttribute attribute)
{
var button = new Button
{
text = attribute.GetDisplayName(methodInfo.PrettyMemberName()),
name = methodInfo.PrettyMemberName()
};
button.style.FlexGrow(StyleDefinition.OneStyleFloat);
button.RegisterCallback<ClickEvent, (MethodInfo, EditorButtonAttribute)>(OnClick, (methodInfo, attribute));
return button;
}

private void OnClick(ClickEvent clickEvent, (MethodInfo methodInfo, EditorButtonAttribute attribute) data)
{
_serializedObject.Update();
data.methodInfo.Invoke(_target, data.attribute.InvokeParams);
EditorUtility.SetDirty(_target);
_serializedObject.ApplyModifiedProperties();
}


private VisualElement DrawButtons(Dictionary<int, IEnumerable<KeyValuePair<MethodInfo, EditorButtonAttribute>>> buttons)
{
var container = new VisualElement();

foreach (var button in buttons)
{
if (button.Key == -1)
{
var grouped = button.Value.GroupBy(key => key.Key, pair => pair.Value,
(info, attributes) => new KeyValuePair<MethodInfo, IEnumerable<EditorButtonAttribute>>(info, attributes));
var verticalElement = VisualElementUtility.CreateVerticalGroup();
container.Add(verticalElement);

foreach (var group in grouped)
{
var horizontalElement = VisualElementUtility.CreateHorizontalGroup();
verticalElement.Add(horizontalElement);

foreach (var attribute in group.Value)
{
var buttonElement = DrawButton(group.Key, attribute);
horizontalElement.Add(buttonElement);
}
}
}
else
{
var horizontalElement = VisualElementUtility.CreateHorizontalGroup();
container.Add(horizontalElement);
foreach (var (key, value) in button.Value)
{
var element = DrawButton(key, value);
horizontalElement.Add(element);
}
}
}

return container;
}

public override VisualElement CreateInspectorGUI()
{
var buttons = DrawButtons(_methodButtonsAttributes);
return buttons;
}

public override void OnChanged(SerializedObject serializedObject)
{
}
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System.Reflection;
using Better.Attributes.EditorAddons.Drawers.Gizmo;
using Better.Attributes.Runtime.Gizmo;
using Better.Commons.EditorAddons.CustomEditors.Attributes;
using Better.Commons.EditorAddons.CustomEditors.Base;
using Better.Commons.EditorAddons.Extensions;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

namespace Better.Attributes.EditorAddons.CustomEditors
{
[MultiEditor(typeof(Object), true, Order = -999)]
public class GizmosEditor : ExtendedEditor
{
private HideTransformButtonHelper _hideTransformHelper;

public GizmosEditor(Object target, SerializedObject serializedObject) : base(target, serializedObject)
{
}

public override void OnDisable()
{
}

public override void OnEnable()
{
CheckAttribute();
}

private void CheckAttribute()
{
var attributeFound = IsAttributeFound();

if (attributeFound && !(_serializedObject.targetObject is ScriptableObject))
{
_hideTransformHelper = new HideTransformButtonHelper();
}
}

private bool IsAttributeFound()
{
var iterator = _serializedObject.GetIterator().Copy();
var attributeFound = false;
while (iterator.Next(true))
{
var data = iterator.GetFieldInfoAndStaticTypeFromProperty();
if (data == null)
{
continue;
}

if (data.FieldInfo.GetCustomAttribute<GizmoLocalAttribute>() == null && data.FieldInfo.GetCustomAttribute<GizmoAttribute>() == null) continue;
attributeFound = true;
break;
}

return attributeFound;
}

public override VisualElement CreateInspectorGUI()
{
if (_hideTransformHelper != null)
{
return _hideTransformHelper.DrawHideTransformButton();
}

return null;
}

public override void OnChanged(SerializedObject serializedObject)
{
CheckAttribute();
}
}
}

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

8 changes: 8 additions & 0 deletions Assets/BetterAttributes/Editor/EditorAddons/Drawers.meta

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Better.Attributes.Runtime.Headers;
using UnityEditor;
using UnityEngine;

namespace Better.Attributes.EditorAddons.Drawers.Decorators
{
[CustomPropertyDrawer(typeof(IconHeaderAttribute))]
internal sealed class IconHeaderDrawer : DecoratorDrawer
{
private Texture _loadedTexture;
private float _height;

public override void OnGUI(Rect position)
{
position.yMin += EditorGUIUtility.singleLineHeight * 0.5f;
position = EditorGUI.IndentedRect(position);
if (!(attribute is IconHeaderAttribute iconHeaderAttribute)) return;
if (_loadedTexture == null)
{
var path = AssetDatabase.GUIDToAssetPath(iconHeaderAttribute.Guid);
_loadedTexture = AssetDatabase.LoadAssetAtPath<Texture>(path);
}

var texture = (Texture)Texture2D.whiteTexture;
if (_loadedTexture != null)
{
texture = _loadedTexture;
}

var imageAspect = texture.width / (float)texture.height;
_height = EditorGUIUtility.currentViewWidth / imageAspect;
GUI.DrawTexture(position, texture, ScaleMode.ScaleToFit, iconHeaderAttribute.UseTransparency, imageAspect);
}

public override float GetHeight()
{
return _height;
}
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Better.Attributes.Runtime.Headers;
using UnityEditor;
using UnityEngine;

namespace Better.Attributes.EditorAddons.Drawers.Decorators
{
[CustomPropertyDrawer(typeof(PrefabHeaderAttribute))]
internal sealed class PrefabHeaderDrawer : DecoratorDrawer
{
public override void OnGUI(Rect position)
{
position.yMin += EditorGUIUtility.singleLineHeight * 0.5f;
position = EditorGUI.IndentedRect(position);
GUI.Label(position, (attribute as HeaderAttribute)?.header, EditorStyles.boldLabel);
}

public override float GetHeight()
{
return EditorGUIUtility.singleLineHeight * 1.5f;
}
}
}

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

Loading

0 comments on commit ab83bf5

Please sign in to comment.