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

Commit

Permalink
Merge pull request #68
Browse files Browse the repository at this point in the history
dev
  • Loading branch information
uurha committed Apr 23, 2023
1 parent 395320b commit 21ae3b9
Show file tree
Hide file tree
Showing 20 changed files with 159 additions and 113 deletions.
3 changes: 0 additions & 3 deletions Editor/EditorAddons/BetterAttributesEditor.cs.meta

This file was deleted.

3 changes: 3 additions & 0 deletions Editor/EditorAddons/CustomEditors.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
Expand Up @@ -2,36 +2,41 @@
using System.Linq;
using System.Reflection;
using Better.Attributes.Runtime;
using Better.EditorTools.CustomEditors;
using Better.Extensions.Runtime;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;

namespace Better.Attributes.EditorAddons
namespace Better.Attributes.EditorAddons.CustomEditors
{
[CanEditMultipleObjects]
[CustomEditor(typeof(Object), true)]
public class BetterAttributesEditor : Editor
[BetterEditor(typeof(Object), true, Order = 999)]
public class BetterButtonsEditor : EditorExtension
{
private Dictionary<int, IEnumerable<KeyValuePair<MethodInfo, EditorButtonAttribute>>> _methodButtonsAttributes =
new Dictionary<int, IEnumerable<KeyValuePair<MethodInfo, EditorButtonAttribute>>>();

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

protected Object _bufferTarget;
public override void OnDisable()
{
}

protected virtual void OnEnable()
public override void OnEnable()
{
_bufferTarget = target;
var type = _bufferTarget.GetType();
var type = _target.GetType();
_methodButtonsAttributes = type.GetSortedMethodAttributes();
}

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

if (GUILayout.Button(attribute.GetDisplayName(methodInfo.PrettyMemberName()), guiStyle))
methodInfo.Invoke(_bufferTarget, attribute.InvokeParams);
methodInfo.Invoke(_target, attribute.InvokeParams);
}

private void DrawButtons(Dictionary<int, IEnumerable<KeyValuePair<MethodInfo, EditorButtonAttribute>>> buttons)
Expand Down Expand Up @@ -76,10 +81,14 @@ private void DrawButtons(Dictionary<int, IEnumerable<KeyValuePair<MethodInfo, Ed

public override void OnInspectorGUI()
{
base.OnInspectorGUI();
serializedObject.Update();
_serializedObject.Update();
DrawButtons(_methodButtonsAttributes);
serializedObject.ApplyModifiedProperties();
_serializedObject.ApplyModifiedProperties();
}

public override void OnChanged()
{

}
}
}
11 changes: 11 additions & 0 deletions Editor/EditorAddons/CustomEditors/BetterButtonsEditor.cs.meta

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

67 changes: 67 additions & 0 deletions Editor/EditorAddons/CustomEditors/GizmosEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using Better.Attributes.EditorAddons.Drawers.Gizmo;
using Better.Attributes.Runtime.Gizmo;
using Better.EditorTools;
using Better.EditorTools.CustomEditors;
using UnityEditor;
using UnityEngine;

namespace Better.Attributes.EditorAddons.CustomEditors
{
[BetterEditor(typeof(Object), true, Order = -999)]
public class GizmosEditor : EditorExtension
{
private HideTransformButtonUtility _hideTransformDrawer;

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

public override void OnDisable()
{
}

public override void OnEnable()
{
var attributeFound = AttributeFound();

if (attributeFound)
{
_hideTransformDrawer = new HideTransformButtonUtility();
}
}

private bool AttributeFound()
{
var iterator = _serializedObject.GetIterator().Copy();
var attributeFound = false;
while (iterator.Next(true))
{
var data = iterator.GetAttributes<GizmoAttribute>();
if (data == null)
{
continue;
}

if (data.Count <= 0) continue;
attributeFound = true;
break;
}

return attributeFound;
}

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

public override void OnChanged()
{
var attributeFound = AttributeFound();
_hideTransformDrawer = attributeFound ? new HideTransformButtonUtility() : null;
}
}
}
3 changes: 3 additions & 0 deletions Editor/EditorAddons/CustomEditors/GizmosEditor.cs.meta

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

28 changes: 17 additions & 11 deletions Editor/EditorAddons/Drawers/Gizmo/BaseWrappers/GizmoWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Reflection;
using Better.Attributes.EditorAddons.Drawers.Utilities;
using Better.EditorTools.Utilities;
using UnityEditor;
Expand All @@ -12,25 +13,21 @@ namespace Better.Attributes.EditorAddons.Drawers.Gizmo
public abstract class GizmoWrapper : UtilityWrapper
{
private protected SerializedProperty _serializedProperty;
private protected Type _objectType;

private protected readonly Quaternion _defaultRotation = Quaternion.identity;
private protected readonly Vector3 _defaultPosition = Vector3.zero;
private bool _showInSceneView = true;
private Type _fieldType;

private string _path;

public bool ShowInSceneView => _showInSceneView;

public virtual void SetProperty(SerializedProperty property, Type fieldType)
{
_fieldType = fieldType;
_serializedProperty = property;
_objectType = _serializedProperty.serializedObject.targetObject.GetType();
}

public override void Deconstruct()
{
GizmoUtility.Instance.RemoveButtonDrawn(_objectType);
_path = _serializedProperty.propertyPath;
_fieldType = fieldType;
}

public void SwitchShowMode()
Expand All @@ -40,13 +37,22 @@ public void SwitchShowMode()

public abstract void Apply(SceneView sceneView);

private protected virtual bool ValidateSerializedObject()
public override void Deconstruct()
{
var serializedObject = _serializedProperty?.serializedObject;
_serializedProperty = null;
}

if (serializedObject == null) return false;
public virtual bool ValidateSerializedObject()
{
try
{
if (_serializedProperty == null) return false;
var serializedObject = _serializedProperty.serializedObject;

if (!string.Equals(_serializedProperty.propertyPath, _path)) return false;
if (serializedObject == null) return false;
if (!_serializedProperty.Copy().Next(true)) return false;

if (!GizmoUtility.Instance.IsSupported(_fieldType)) return false;
return serializedObject.targetObject != null;
}
Expand Down
20 changes: 9 additions & 11 deletions Editor/EditorAddons/Drawers/Gizmo/GizmoDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ private GizmoWrappers Collection
{
get
{
_wrappers ??= GenerateCollection();
if (_wrappers == null)
{
_wrappers = GenerateCollection();
}

return _wrappers as GizmoWrappers;
}
}
Expand All @@ -37,6 +41,10 @@ private void OnSceneGUIDelegate(SceneView sceneView)
{
if (sceneView.drawGizmos)
{
if (_wrappers == null)
{
_wrappers = GenerateCollection();
}
GizmoUtility.Instance.ValidateCachedProperties(_wrappers);
Collection?.Apply(sceneView);
}
Expand All @@ -53,11 +61,6 @@ protected override bool PreDraw(ref Rect position, SerializedProperty property,
var fieldType = fieldInfo.FieldType;
var attributeType = attribute.GetType();

if (_hideTransformDrawer == null && property.IsTargetComponent(out _))
{
_hideTransformDrawer = new HideTransformButtonUtility(property, GizmoUtility.Instance);
}

if (!GizmoUtility.Instance.IsSupported(fieldType))
{
EditorGUI.BeginChangeCheck();
Expand All @@ -73,11 +76,6 @@ protected override bool PreDraw(ref Rect position, SerializedProperty property,

EditorGUI.BeginChangeCheck();

if (_hideTransformDrawer != null)
{
_hideTransformDrawer.DrawHideTransformButton();
}

return true;
}

Expand Down
25 changes: 0 additions & 25 deletions Editor/EditorAddons/Drawers/Gizmo/HideTransformDrawer.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,13 @@
using Better.EditorTools;
using UnityEditor;
using UnityEngine;
#if UNITY_2022_1_OR_NEWER
using GizmoUtility = Better.Attributes.EditorAddons.Drawers.Utilities.GizmoUtility;
#else
using Better.Attributes.EditorAddons.Drawers.Utilities;
#endif

namespace Better.Attributes.EditorAddons.Drawers.Gizmo
{
public class HideTransformButtonUtility
{
private bool _isChecked = false;
private bool _isButtonDrawn;
private readonly SerializedProperty _serializedProperty;
private readonly GizmoUtility _gizmoDrawerUtility;

public HideTransformButtonUtility(SerializedProperty property, GizmoUtility gizmoDrawerUtility)
{
_serializedProperty = property;
_gizmoDrawerUtility = gizmoDrawerUtility;
}

public void DrawHideTransformButton()
{
if (!_serializedProperty.IsTargetComponent(out var component)) return;
var type = component.GetType();
if (!_isChecked)
{
_isButtonDrawn = _gizmoDrawerUtility.IsButtonDrawn(type);
_isChecked = true;
}

if (_isButtonDrawn) return;
var text = Tools.hidden ? "Show" : "Hide";
if (GUILayout.Button($"{text} Transform handles"))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public class BoundsLocalWrapper : BoundsBaseWrapper
public override void Apply(SceneView sceneView)
{
if (!ShowInSceneView) return;
if (!ValidateSerializedObject()) return;
if (_serializedProperty.IsTargetComponent(out var component))
{
var transform = component.transform;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public class QuaternionLocalWrapper : GizmoWrapper
public override void Apply(SceneView sceneView)
{
if (!ShowInSceneView) return;
if (!ValidateSerializedObject()) return;
if (_serializedProperty.IsTargetComponent(out var component))
{
var transform = component.transform;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public class Vector2LocalWrapper : GizmoWrapper
public override void Apply(SceneView sceneView)
{
if (!ShowInSceneView) return;
if (!ValidateSerializedObject()) return;
if (_serializedProperty.IsTargetComponent(out var component))
{
var transform = component.transform;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public class Vector3LocalWrapper : GizmoWrapper
public override void Apply(SceneView sceneView)
{
if (!ShowInSceneView) return;
if (!ValidateSerializedObject()) return;
if (_serializedProperty.IsTargetComponent(out var component))
{
var transform = component.transform;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ public class BoundsWrapper : BoundsBaseWrapper
public override void Apply(SceneView sceneView)
{
if (!ShowInSceneView) return;
if (!ValidateSerializedObject()) return;
DrawLabel($"{_serializedProperty.name}:\nCenter: {_bounds.center}\nSize: {_bounds.size}", _bounds.center,
_defaultRotation, sceneView);
_bounds.center = Handles.PositionHandle(_bounds.center, _defaultRotation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public class QuaternionWrapper : GizmoWrapper
public override void Apply(SceneView sceneView)
{
if (!ShowInSceneView) return;
if (!ValidateSerializedObject()) return;
DrawLabel($"{_serializedProperty.name}:\n{_quaternion.eulerAngles}", _defaultPosition, _quaternion,
sceneView);
_quaternion = Handles.RotationHandle(_quaternion, _defaultPosition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public class Vector2Wrapper : GizmoWrapper
public override void Apply(SceneView sceneView)
{
if (!ShowInSceneView) return;
if (!ValidateSerializedObject()) return;
DrawLabel($"{_serializedProperty.name}:\n{_vector2}", _vector2, _defaultRotation, sceneView);
_vector2 = Handles.PositionHandle(_vector2, _defaultRotation);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ public class Vector3Wrapper : GizmoWrapper
public override void Apply(SceneView sceneView)
{
if (!ShowInSceneView) return;
if (!ValidateSerializedObject()) return;
DrawLabel($"{_serializedProperty.name}:\n{_vector3}", _vector3, _defaultRotation, sceneView);
_vector3 = Handles.PositionHandle(_vector3, _defaultRotation);
_serializedProperty.vector3Value = _vector3;
SetValueAndApply(_vector3);
}

Expand Down
Loading

0 comments on commit 21ae3b9

Please sign in to comment.