Skip to content

Commit

Permalink
Merge pull request #10 from techno-dwarf-works/feature/refactoring
Browse files Browse the repository at this point in the history
Version 0.0.2
  • Loading branch information
uurha committed Oct 6, 2024
1 parent 524386b commit 6bd91bc
Show file tree
Hide file tree
Showing 249 changed files with 1,207 additions and 323 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
36 changes: 36 additions & 0 deletions Editor/CustomEditors/ButtonsEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Better.Attributes.EditorAddons.Drawers.EditorButton;
using Better.Commons.EditorAddons.CustomEditors.Attributes;
using Better.Commons.EditorAddons.CustomEditors.Base;
using UnityEditor;
using UnityEngine.UIElements;
using Object = UnityEngine.Object;

namespace Better.Attributes.EditorAddons.CustomEditors
{
[MultiEditor(typeof(Object), true, Order = 999)]
public class ButtonsEditor : ExtendedEditor
{
public ButtonsEditor(Object target, SerializedObject serializedObject) : base(target, serializedObject)
{
}

public override void OnDisable()
{
}

public override void OnEnable()
{
}

public override VisualElement CreateInspectorGUI()
{
var container = new EditorButtonDrawer(_serializedObject);
container.CreateFromTarget(_target);
return container;
}

public override void OnChanged(SerializedObject serializedObject)
{
}
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions Editor/Drawers/EditorButton.meta

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

172 changes: 172 additions & 0 deletions Editor/Drawers/EditorButton/EditorButtonDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Better.Attributes.EditorAddons.Drawers.Parameters;
using Better.Attributes.EditorAddons.Utilities;
using Better.Attributes.Runtime;
using Better.Commons.EditorAddons.Enums;
using Better.Commons.EditorAddons.Extensions;
using Better.Commons.EditorAddons.Utility;
using Better.Commons.Runtime.Extensions;
using Better.Commons.Runtime.UIElements;
using Better.Commons.Runtime.Utility;
using UnityEditor;
using UnityEngine.UIElements;

namespace Better.Attributes.EditorAddons.Drawers.EditorButton
{
public class EditorButtonDrawer : VisualElement
{
private readonly SerializedObject _serializedObject;
private Dictionary<int, IEnumerable<KeyValuePair<MethodInfo, EditorButtonAttribute>>> _buttons;
private object _target;

public EditorButtonDrawer(SerializedObject serializedObject)
{
_serializedObject = serializedObject;
}

public void CreateFromTarget(object target)
{
if (target == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(target));
return;
}

_target = target;
Clear();
var type = _target.GetType();
Add(type);
}

private void Add(Type targetType)
{
var methodAttributes = EditorButtonUtility.GetSortedMethodAttributes(targetType);
foreach (var (captureGroup, keyValuePairs) in methodAttributes)
{
if (captureGroup == -1)
{
var grouped = keyValuePairs.GroupBy(key => key.Key, pair => pair.Value, (methodInfo, attributes) => (methodInfo, attributes));
var verticalElement = VisualElementUtility.CreateVerticalGroup();
Add(verticalElement);

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

foreach (var attribute in group.attributes)
{
var visualElement = CreateFromMethodInfo(group.methodInfo, attribute);
horizontalElement.Add(visualElement);
}
}
}
else
{
var horizontalElement = VisualElementUtility.CreateHorizontalGroup();
Add(horizontalElement);
foreach (var (key, value) in keyValuePairs)
{
var visualElement = CreateFromMethodInfo(key, value);
horizontalElement.Add(visualElement);
}
}
}
}

private VisualElement CreateFromMethodInfo(MethodInfo methodInfo, EditorButtonAttribute attribute)
{
var parameters = methodInfo.GetParameters();
foreach (var parameterInfo in parameters)
{
if (!ParameterFieldProvider.IsSupported(parameterInfo.ParameterType) || parameterInfo.IsOut)
{
return CreateNotSupportedHelpBox(methodInfo, parameterInfo);
}
}

return CreateButton(methodInfo, attribute);
}

private static HelpBox CreateNotSupportedHelpBox(MethodInfo methodInfo, ParameterInfo parameterInfo)
{
string message;
if (parameterInfo.IsOut)
{
message = $"Parameter({parameterInfo.Name}) with \"out\" modificator in {methodInfo.Name} not supported";
}
else
{
message = $"Parameter({parameterInfo.Name}) with type {parameterInfo.ParameterType} in {methodInfo.Name} not supported";
}

var helpBox = VisualElementUtility.HelpBox(message, HelpBoxMessageType.Error);
helpBox.style.FlexGrow(StyleDefinition.OneStyleFloat);
return helpBox;
}

private VisualElement CreateButton(MethodInfo methodInfo, EditorButtonAttribute attribute)
{
var prettyMemberName = methodInfo.PrettyMemberName();

var verticalGroup = VisualElementUtility.CreateVerticalGroup();
verticalGroup.name = $"{prettyMemberName}__{nameof(verticalGroup)}";
verticalGroup.style
.FlexGrow(StyleDefinition.OneStyleFloat)
.FlexBasis(0.5f);

var horizontalGroup = VisualElementUtility.CreateHorizontalGroup();
horizontalGroup.name = $"{prettyMemberName}__{nameof(horizontalGroup)}";
horizontalGroup.style
.FlexGrow(StyleDefinition.OneStyleFloat)
.MaxHeight(StyleDefinition.ButtonHeight);

verticalGroup.Add(horizontalGroup);

var button = new Button
{
text = attribute.GetDisplayName(prettyMemberName),
name = prettyMemberName
};

var parameters = methodInfo.GetParameters();
var datas = parameters.Select(info => new Parameter(info)).ToArray();

var parametersElement = new ParametersElementDrawer(datas);
verticalGroup.Add(parametersElement);

if (!datas.IsNullOrEmpty())
{
var toggle = new ToggleButton(value => parametersElement.style.SetVisible(value));
toggle.AddIcon(IconType.GrayDropdown);
toggle.style.Padding(1f);

toggle.text = string.Empty;
horizontalGroup.Add(toggle);
}
else
{
parametersElement.style.SetVisible(false);
}

horizontalGroup.Add(button);
button.style.FlexGrow(StyleDefinition.OneStyleFloat);
button.RegisterCallback<ClickEvent, MethodInfo, ParametersElementDrawer>(OnClick, methodInfo, parametersElement);
return verticalGroup;
}

private void OnClick(ClickEvent clickEvent, (MethodInfo methodInfo, ParametersElementDrawer parameters) data)
{
_serializedObject.Update();
var parameters = data.parameters;

//TODO: Validate parameters count and types
data.methodInfo.Invoke(_target, parameters.GetData());
EditorUtility.SetDirty(_serializedObject.targetObject);
_serializedObject.ApplyModifiedProperties();
}
}
}
3 changes: 3 additions & 0 deletions Editor/Drawers/EditorButton/EditorButtonDrawer.cs.meta

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

File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,12 @@ public virtual void SetProperty(SerializedProperty property, Type fieldType)

private string GetCompiledName()
{
if (Validate())
if (_serializedProperty.IsArrayElement())
{
if (_serializedProperty.IsArrayElement())
{
return $"{ObjectNames.NicifyVariableName(_serializedProperty.GetArrayPath())}";
}

return _serializedProperty.displayName;
return $"{ObjectNames.NicifyVariableName(_serializedProperty.GetArrayPath())}";
}

return string.Empty;
return _serializedProperty.displayName;
}

public void SetMode(bool value)
Expand All @@ -59,40 +54,8 @@ public override void Deconstruct()
_serializedProperty = null;
}

public virtual bool Validate()
{
try
{
if (_serializedProperty == null)
{
return false;
}

if (!_serializedProperty.Verify())
{
return false;
}

if (_serializedProperty.IsDisposed())
{
return false;
}

return _serializedProperty.serializedObject.targetObject != null;
}
catch
{
return false;
}
}

private protected void SetValueAndApply(object value)
{
if (!Validate())
{
return;
}

if (_fieldType.IsEquivalentTo(typeof(Vector2)))
_serializedProperty.vector2Value = (Vector2)value;
else if (_fieldType.IsEquivalentTo(typeof(Vector3)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,44 +36,25 @@ private void OnSceneGUIDelegate(SceneView sceneView)
{
if (sceneView.drawGizmos)
{
ValidationUtility.ValidateCachedProperties(Handlers);
Handlers?.Revalidate();
Apply(sceneView);
}
}

private void Apply(SceneView sceneView)
{
List<SerializedProperty> keysToRemove = null;
Handlers.Revalidate();

foreach (var gizmo in Handlers)
{
var valueWrapper = gizmo.Value.Handler;
if (valueWrapper.Validate())
{
valueWrapper.Apply(sceneView);
}
else
{
if (keysToRemove == null)
{
keysToRemove = new List<SerializedProperty>();
}

keysToRemove.Add(gizmo.Key);
}
}

if (keysToRemove != null)
{
foreach (var property in keysToRemove)
{
Handlers.Remove(property);
}
valueWrapper.Apply(sceneView);
}
}

protected override void Deconstruct()
protected override void ContainerReleased(ElementsContainer container)
{
base.Deconstruct();
base.ContainerReleased(container);
SceneView.duringSceneGui -= OnSceneGUIDelegate;
}

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ protected override BaseHandlersTypeCollection GenerateCollection()
{ typeof(Type), typeof(RenameFieldHandler) }
}
},
{
typeof(DetailedAttribute), new Dictionary<Type, Type>(AnyTypeComparer.Instance)
{
{ typeof(Type), typeof(DetailedHandler) }
}
},
};
}

Expand Down
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 6bd91bc

Please sign in to comment.