Skip to content
This repository has been archived by the owner on Aug 24, 2024. It is now read-only.

Version 3.1.4 #124

Merged
merged 3 commits into from
Jul 12, 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
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
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
Expand All @@ -16,7 +19,7 @@ public class BetterButtonsEditor : ExtendedEditor
{
private Dictionary<int, IEnumerable<KeyValuePair<MethodInfo, EditorButtonAttribute>>> _methodButtonsAttributes =
new Dictionary<int, IEnumerable<KeyValuePair<MethodInfo, EditorButtonAttribute>>>();

public BetterButtonsEditor(Object target, SerializedObject serializedObject) : base(target, serializedObject)
{
}
Expand All @@ -31,75 +34,71 @@ public override void OnEnable()
_methodButtonsAttributes = EditorButtonUtility.GetSortedMethodAttributes(type);
}

private void DrawButton(KeyValuePair<MethodInfo, EditorButtonAttribute> button, GUIStyle guiStyle)
private Button DrawButton(MethodInfo methodInfo, EditorButtonAttribute attribute)
{
var attribute = button.Value;
var methodInfo = button.Key;

if (GUILayout.Button(attribute.GetDisplayName(methodInfo.PrettyMemberName()), guiStyle))
var button = new Button
{
using (var changeScope = new EditorGUI.ChangeCheckScope())
{
methodInfo.Invoke(_target, attribute.InvokeParams);
if (changeScope.changed)
{
EditorUtility.SetDirty(_target);
_serializedObject.ApplyModifiedProperties();
}
}
}
text = attribute.GetDisplayName(methodInfo.PrettyMemberName())
};
button.RegisterCallback<ClickEvent, (MethodInfo, EditorButtonAttribute)>(OnClick, (methodInfo, attribute));
return button;
}

private void DrawButtons(Dictionary<int, IEnumerable<KeyValuePair<MethodInfo, EditorButtonAttribute>>> buttons)
private void OnClick(ClickEvent clickEvent, (MethodInfo methodInfo, EditorButtonAttribute attribute) data)
{
var guiStyle = new GUIStyle(GUI.skin.button)
{
stretchWidth = true,
richText = true,
wordWrap = true
};
_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));
EditorGUILayout.BeginVertical();
(info, attributes) => new KeyValuePair<MethodInfo, IEnumerable<EditorButtonAttribute>>(info, attributes));
var verticalElement = VisualElementUtility.CreateVerticalGroup();

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

foreach (var attribute in group.Value)
DrawButton(new KeyValuePair<MethodInfo, EditorButtonAttribute>(group.Key, attribute),
guiStyle);
EditorGUILayout.EndHorizontal();
{
var buttonElement = DrawButton(group.Key, attribute);
horizontalElement.Add(buttonElement);
}
}

EditorGUILayout.EndVertical();
}
else
{
EditorGUILayout.BeginHorizontal();
foreach (var pair in button.Value) DrawButton(pair, guiStyle);
EditorGUILayout.EndHorizontal();
var horizontalElement = VisualElementUtility.CreateHorizontalGroup();
foreach (var (key, value) in button.Value)
{
var element = DrawButton(key, value);
horizontalElement.Add(element);
}
}
}

return container;
}

public override void OnInspectorGUI()
public override VisualElement CreateInspectorGUI()
{
_serializedObject.Update();
DrawButtons(_methodButtonsAttributes);
_serializedObject.ApplyModifiedProperties();
var buttons = DrawButtons(_methodButtonsAttributes);
return buttons;
}

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

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
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 HideTransformButtonUtility _hideTransformDrawer;
private HideTransformButtonHelper _hideTransformHelper;

public GizmosEditor(Object target, SerializedObject serializedObject) : base(target, serializedObject)
{
Expand All @@ -29,15 +30,15 @@ public override void OnEnable()

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

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

private bool AttributeFound()
private bool IsAttributeFound()
{
var iterator = _serializedObject.GetIterator().Copy();
var attributeFound = false;
Expand All @@ -57,15 +58,17 @@ private bool AttributeFound()
return attributeFound;
}

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

return null;
}

public override void OnChanged()
public override void OnChanged(SerializedObject serializedObject)
{
CheckAttribute();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,91 +1,60 @@
using System.Reflection;
using Better.Attributes.EditorAddons.Drawers.Utility;
using Better.Attributes.EditorAddons.Drawers.WrapperCollections;
using Better.Attributes.Runtime.DrawInspector;
using Better.Commons.EditorAddons.Drawers.Attributes;
using Better.Commons.EditorAddons.Drawers.Base;
using Better.Commons.EditorAddons.Drawers.Caching;
using Better.Attributes.Runtime.DrawInspector;
using Better.Commons.EditorAddons.Drawers;
using Better.Commons.EditorAddons.Drawers.Container;
using Better.Commons.EditorAddons.Enums;
using Better.Commons.EditorAddons.Extensions;
using Better.Commons.EditorAddons.Utility;
using Better.Commons.Runtime.Drawers.Attributes;
using UnityEditor;
using UnityEngine;
using UnityEditor.UIElements;
using UnityEngine.UIElements;

namespace Better.Attributes.EditorAddons.Drawers.DrawInspector
{
[MultiCustomPropertyDrawer(typeof(DrawInspectorAttribute))]
public class DrawInspectorDrawer : MultiFieldDrawer<DrawInspectorWrapper>
[CustomPropertyDrawer(typeof(DrawInspectorAttribute), true)]
public class DrawInspectorDrawer : BasePropertyDrawer<DrawInspectorHandler, DrawInspectorAttribute>
{
private bool _isOpen;
private DrawInspectors Collection => _wrappers as DrawInspectors;

protected override void Deconstruct()
{
_wrappers?.Deconstruct();
}

protected override bool PreDraw(ref Rect position, SerializedProperty property, GUIContent label)
protected override void PopulateContainer(ElementsContainer container)
{
var fieldType = GetFieldOrElementType();
if (!DrawInspectorUtility.Instance.IsSupported(fieldType))
var property = container.SerializedProperty;
if (!TypeHandlersBinder.IsSupported(fieldType))
{
EditorGUI.BeginChangeCheck();
DrawField(position, property, label);
ExtendedGUIUtility.NotSupportedAttribute(position, property, label, fieldType, _attribute.GetType());
return false;
container.AddNotSupportedBox(fieldType, Attribute.GetType());
return;
}

var cache = ValidateCachedProperties(property, DrawInspectorUtility.Instance);
if (!cache.IsValid)
{
Collection.SetProperty(property);
}
container.SerializedPropertyChanged += OnPropertyChanged;
var handler = GetHandler(property);

_isOpen = Collection.IsOpen(property);
if (property.objectReferenceValue)
{
label.image = (_isOpen ? IconType.Minus : IconType.PlusMore).GetIcon();
}
var inspectorElement = handler.GetInspectorContainer(property);
container.CreateElementFrom(inspectorElement);
handler.SetupInspector();

var copy = ExtendedGUIUtility.GetClickRect(position, label);
copy.height = EditorGUIUtility.singleLineHeight;
if (ExtendedGUIUtility.IsClickedAt(copy))
{
Collection.SetOpen(property, !_isOpen);
}

return true;
var isOpen = handler.IsOpen();
handler.SetOpen(isOpen);
var iconType = isOpen ? IconType.Minus : IconType.PlusMore;
container.AddClickableIcon(iconType, property, OnIconClickEvent);
}

protected override HeightCacheValue GetPropertyHeight(SerializedProperty property, GUIContent label)
private void OnPropertyChanged(ElementsContainer elementsContainer)
{
var additionalHeight = 0f;
if (Collection != null && Collection.IsOpen(property))
{
additionalHeight = Collection.GetHeight(property);
}

return HeightCacheValue.GetAdditive(additionalHeight);
var handler = GetHandler(elementsContainer.SerializedProperty);
handler.SetupInspector();
}

protected override Rect PreparePropertyRect(Rect original)
private void OnIconClickEvent(ClickEvent clickEvent, (SerializedProperty property, Image icon) data)
{
return original;
UpdateState(data.property, data.icon);
}

protected override void PostDraw(Rect position, SerializedProperty property, GUIContent label)
private void UpdateState(SerializedProperty property, Image icon)
{
Collection.PostDraw(property, position);
}
var handler = GetHandler(property);
if (!handler.CanOpen()) return;
var isOpen = !handler.IsOpen();

protected override WrapperCollection<DrawInspectorWrapper> GenerateCollection()
{
return new DrawInspectors();
}

public DrawInspectorDrawer(FieldInfo fieldInfo, MultiPropertyAttribute attribute) : base(fieldInfo, attribute)
{
var iconType = isOpen ? IconType.Minus : IconType.PlusMore;
icon.image = iconType.GetIcon();
handler.SetOpen(isOpen);
}
}
}
Loading
Loading