Merge pull request #38 from AnnulusGames/refactoring

Refactoring
This commit is contained in:
Annulus Games 2024-02-18 11:22:36 +09:00 committed by GitHub
commit 6e9e759cad
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 107 additions and 125 deletions

View File

@ -8,19 +8,19 @@ namespace Alchemy.Editor
{
public abstract class AlchemyAttributeDrawer
{
internal SerializedObject _serializedObject;
internal SerializedProperty _serializedProperty;
internal object _target;
internal MemberInfo _memberInfo;
internal Attribute _attribute;
internal VisualElement _element;
SerializedObject serializedObject;
SerializedProperty serializedProperty;
object target;
MemberInfo memberInfo;
Attribute attribute;
VisualElement targetElement;
public SerializedObject SerializedObject => _serializedObject;
public SerializedProperty SerializedProperty => _serializedProperty;
public object Target => _target;
public MemberInfo MemberInfo => _memberInfo;
public Attribute Attribute => _attribute;
public VisualElement Element => _element;
public SerializedObject SerializedObject => serializedObject;
public SerializedProperty SerializedProperty => serializedProperty;
public object Target => target;
public MemberInfo MemberInfo => memberInfo;
public Attribute Attribute => attribute;
public VisualElement TargetElement => targetElement;
public abstract void OnCreateElement();
@ -34,12 +34,12 @@ namespace Alchemy.Editor
if (processorType == null) continue;
var processor = (AlchemyAttributeDrawer)Activator.CreateInstance(processorType);
processor._serializedObject = serializedObject;
processor._serializedProperty = property;
processor._target = target;
processor._memberInfo = memberInfo;
processor._attribute = attribute;
processor._element = memberElement;
processor.serializedObject = serializedObject;
processor.serializedProperty = property;
processor.target = target;
processor.memberInfo = memberInfo;
processor.attribute = attribute;
processor.targetElement = memberElement;
processor.OnCreateElement();
}

View File

@ -8,12 +8,12 @@ namespace Alchemy.Editor
public abstract VisualElement CreateRootElement(string label);
public virtual VisualElement GetGroupElement(Attribute attribute) => null;
public string UniqueId => _uniqueId;
string _uniqueId;
public string UniqueId => uniqueId;
string uniqueId;
internal void SetUniqueId(string id)
{
this._uniqueId = id;
this.uniqueId = id;
}
}
}

View File

@ -1,5 +1,7 @@
using System.IO;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using Alchemy.Hierarchy;
namespace Alchemy.Editor
@ -31,6 +33,44 @@ namespace Alchemy.Editor
File.WriteAllText(SettingsPath, JsonUtility.ToJson(instance, true));
}
static readonly string SettingsMenuName = "Project/Alchemy";
[SettingsProvider]
internal static SettingsProvider CreateSettingsProvider()
{
return new SettingsProvider(SettingsMenuName, SettingsScope.Project)
{
label = "Alchemy",
keywords = new HashSet<string>(new[] { "Alchemy, Inspector, Hierarchy" }),
guiHandler = searchContext =>
{
var serializedObject = new SerializedObject(GetOrCreateSettings());
using (new EditorGUILayout.HorizontalScope())
{
GUILayout.Space(10f);
using (new EditorGUILayout.VerticalScope())
{
EditorGUILayout.LabelField("Hierarchy", EditorStyles.boldLabel);
using (var changeCheck = new EditorGUI.ChangeCheckScope())
{
EditorGUILayout.PropertyField(serializedObject.FindProperty("hierarchyObjectMode"));
EditorGUILayout.PropertyField(serializedObject.FindProperty("showHierarchyToggles"), new GUIContent("Show Toggles"));
EditorGUILayout.PropertyField(serializedObject.FindProperty("showComponentIcons"));
if (changeCheck.changed)
{
serializedObject.ApplyModifiedProperties();
SaveSettings();
}
}
}
}
},
};
}
[SerializeField] HierarchyObjectMode hierarchyObjectMode = HierarchyObjectMode.RemoveInBuild;
[SerializeField] bool showHierarchyToggles;
[SerializeField] bool showComponentIcons;

View File

@ -1,47 +0,0 @@
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace Alchemy.Editor
{
internal static class AlchemySettingsProvider
{
static readonly string MenuName = "Project/Alchemy";
[SettingsProvider]
public static SettingsProvider CreateSettingsProvider()
{
return new SettingsProvider(MenuName, SettingsScope.Project)
{
label = "Alchemy",
keywords = new HashSet<string>(new[] { "Alchemy, Inspector, Hierarchy" }),
guiHandler = searchContext =>
{
var serializedObject = new SerializedObject(AlchemySettings.GetOrCreateSettings());
using (new EditorGUILayout.HorizontalScope())
{
GUILayout.Space(10f);
using (new EditorGUILayout.VerticalScope())
{
EditorGUILayout.LabelField("Hierarchy", EditorStyles.boldLabel);
using (var changeCheck = new EditorGUI.ChangeCheckScope())
{
EditorGUILayout.PropertyField(serializedObject.FindProperty("hierarchyObjectMode"));
EditorGUILayout.PropertyField(serializedObject.FindProperty("showHierarchyToggles"), new GUIContent("Show Toggles"));
EditorGUILayout.PropertyField(serializedObject.FindProperty("showComponentIcons"));
if (changeCheck.changed)
{
serializedObject.ApplyModifiedProperties();
AlchemySettings.SaveSettings();
}
}
}
}
},
};
}
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 7430b79e486304556b32ebe13b6674b2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -13,7 +13,7 @@ namespace Alchemy.Editor.Drawers
{
public override void OnCreateElement()
{
Element.SetEnabled(false);
TargetElement.SetEnabled(false);
}
}
@ -24,12 +24,12 @@ namespace Alchemy.Editor.Drawers
public override void OnCreateElement()
{
Element.RegisterCallback<GeometryChangedEvent>(x => AddPadding());
TargetElement.RegisterCallback<GeometryChangedEvent>(x => AddPadding());
}
void AddPadding()
{
var label = Element.Q<Label>();
var label = TargetElement.Q<Label>();
if (label == null) return;
label.style.paddingLeft = ((IndentAttribute)Attribute).indent * IndentPadding;
}
@ -40,7 +40,7 @@ namespace Alchemy.Editor.Drawers
{
public override void OnCreateElement()
{
Element.style.display = Application.isPlaying ? DisplayStyle.None : DisplayStyle.Flex;
TargetElement.style.display = Application.isPlaying ? DisplayStyle.None : DisplayStyle.Flex;
}
}
@ -49,7 +49,7 @@ namespace Alchemy.Editor.Drawers
{
public override void OnCreateElement()
{
Element.style.display = !Application.isPlaying ? DisplayStyle.None : DisplayStyle.Flex;
TargetElement.style.display = !Application.isPlaying ? DisplayStyle.None : DisplayStyle.Flex;
}
}
@ -58,7 +58,7 @@ namespace Alchemy.Editor.Drawers
{
public override void OnCreateElement()
{
if (Application.isPlaying) Element.SetEnabled(false);
if (Application.isPlaying) TargetElement.SetEnabled(false);
}
}
@ -67,7 +67,7 @@ namespace Alchemy.Editor.Drawers
{
public override void OnCreateElement()
{
if (!Application.isPlaying) Element.SetEnabled(false);
if (!Application.isPlaying) TargetElement.SetEnabled(false);
}
}
@ -76,13 +76,13 @@ namespace Alchemy.Editor.Drawers
{
public override void OnCreateElement()
{
if (Element is AlchemyPropertyField field)
if (TargetElement is AlchemyPropertyField field)
{
field.Label = string.Empty;
return;
}
var labelElement = Element.Q<Label>();
var labelElement = TargetElement.Q<Label>();
if (labelElement == null) return;
labelElement.text = string.Empty;
}
@ -95,7 +95,7 @@ namespace Alchemy.Editor.Drawers
{
var labelTextAttribute = (LabelTextAttribute)Attribute;
switch (Element)
switch (TargetElement)
{
case AlchemyPropertyField alchemyPropertyField:
alchemyPropertyField.Label = labelTextAttribute.Text;
@ -104,7 +104,7 @@ namespace Alchemy.Editor.Drawers
button.text = labelTextAttribute.Text;
break;
default:
var labelElement = Element.Q<Label>();
var labelElement = TargetElement.Q<Label>();
if (labelElement == null) return;
labelElement.text = labelElement.text;
break;
@ -119,7 +119,7 @@ namespace Alchemy.Editor.Drawers
{
var width = ((LabelWidthAttribute)Attribute).Width;
if (Element is AlchemyPropertyField field && field.FieldElement is PropertyField)
if (TargetElement is AlchemyPropertyField field && field.FieldElement is PropertyField)
{
var executed = false;
field.schedule.Execute(() =>
@ -129,7 +129,7 @@ namespace Alchemy.Editor.Drawers
GUIHelper.SetMinAndCurrentWidth(label, width);
executed = true;
}).Until(() => executed);
return;
}
@ -143,7 +143,7 @@ namespace Alchemy.Editor.Drawers
protected override void OnInspectorChanged()
{
var condition = ReflectionHelper.GetValueBool(Target, ((HideIfAttribute)Attribute).Condition);
Element.style.display = condition ? DisplayStyle.None : DisplayStyle.Flex;
TargetElement.style.display = condition ? DisplayStyle.None : DisplayStyle.Flex;
}
}
@ -153,7 +153,7 @@ namespace Alchemy.Editor.Drawers
protected override void OnInspectorChanged()
{
var condition = ReflectionHelper.GetValueBool(Target, ((ShowIfAttribute)Attribute).Condition);
Element.style.display = !condition ? DisplayStyle.None : DisplayStyle.Flex;
TargetElement.style.display = !condition ? DisplayStyle.None : DisplayStyle.Flex;
}
}
@ -163,7 +163,7 @@ namespace Alchemy.Editor.Drawers
protected override void OnInspectorChanged()
{
var condition = ReflectionHelper.GetValueBool(Target, ((DisableIfAttribute)Attribute).Condition);
Element.SetEnabled(!condition);
TargetElement.SetEnabled(!condition);
}
}
@ -173,7 +173,7 @@ namespace Alchemy.Editor.Drawers
protected override void OnInspectorChanged()
{
var condition = ReflectionHelper.GetValueBool(Target, ((EnableIfAttribute)Attribute).Condition);
Element.SetEnabled(condition);
TargetElement.SetEnabled(condition);
}
}
@ -189,8 +189,8 @@ namespace Alchemy.Editor.Drawers
var message = ((RequiredAttribute)Attribute).Message ?? ObjectNames.NicifyVariableName(SerializedProperty.displayName) + " is required.";
helpBox = new HelpBox(message, HelpBoxMessageType.Error);
var parent = Element.parent;
parent.Insert(parent.IndexOf(Element), helpBox);
var parent = TargetElement.parent;
parent.Insert(parent.IndexOf(TargetElement), helpBox);
base.OnCreateElement();
}
@ -211,8 +211,8 @@ namespace Alchemy.Editor.Drawers
var message = ((ValidateInputAttribute)Attribute).Message ?? ObjectNames.NicifyVariableName(SerializedProperty.displayName) + " is not valid.";
helpBox = new HelpBox(message, HelpBoxMessageType.Error);
var parent = Element.parent;
parent.Insert(parent.IndexOf(Element), helpBox);
var parent = TargetElement.parent;
parent.Insert(parent.IndexOf(TargetElement), helpBox);
base.OnCreateElement();
}
@ -234,8 +234,8 @@ namespace Alchemy.Editor.Drawers
var att = (HelpBoxAttribute)Attribute;
helpBox = new HelpBox(att.Message, att.MessageType);
var parent = Element.parent;
parent.Insert(parent.IndexOf(Element), helpBox);
var parent = TargetElement.parent;
parent.Insert(parent.IndexOf(TargetElement), helpBox);
}
}
@ -245,10 +245,10 @@ namespace Alchemy.Editor.Drawers
public override void OnCreateElement()
{
var att = (HorizontalLineAttribute)Attribute;
var parent = Element.parent;
var parent = TargetElement.parent;
var lineColor = att.Color == default ? GUIHelper.LineColor : att.Color;
var line = GUIHelper.CreateLine(lineColor, EditorGUIUtility.standardVerticalSpacing * 4f);
parent.Insert(parent.IndexOf(Element), line);
parent.Insert(parent.IndexOf(TargetElement), line);
}
}
@ -258,7 +258,7 @@ namespace Alchemy.Editor.Drawers
public override void OnCreateElement()
{
var att = (TitleAttribute)Attribute;
var parent = Element.parent;
var parent = TargetElement.parent;
var title = new Label(att.TitleText)
{
@ -269,7 +269,7 @@ namespace Alchemy.Editor.Drawers
marginBottom = -2f
}
};
parent.Insert(parent.IndexOf(Element), title);
parent.Insert(parent.IndexOf(TargetElement), title);
if (att.SubitleText != null)
{
@ -283,11 +283,11 @@ namespace Alchemy.Editor.Drawers
unityTextAlign = TextAnchor.MiddleLeft
}
};
parent.Insert(parent.IndexOf(Element), subtitle);
parent.Insert(parent.IndexOf(TargetElement), subtitle);
}
var line = GUIHelper.CreateLine(GUIHelper.LineColor, EditorGUIUtility.standardVerticalSpacing * 3f);
parent.Insert(parent.IndexOf(Element), line);
parent.Insert(parent.IndexOf(TargetElement), line);
}
}
@ -325,8 +325,8 @@ namespace Alchemy.Editor.Drawers
EditorGUI.LabelField(labelPosition, labelContent, textStyle);
});
var parent = Element.parent;
parent.Insert(parent.IndexOf(Element), blockquote);
var parent = TargetElement.parent;
parent.Insert(parent.IndexOf(TargetElement), blockquote);
}
}
@ -335,7 +335,7 @@ namespace Alchemy.Editor.Drawers
{
public override void OnCreateElement()
{
Element.TrackPropertyValue(SerializedProperty, property =>
TargetElement.TrackPropertyValue(SerializedProperty, property =>
{
var methodName = ((OnValueChangedAttribute)Attribute).MethodName;

View File

@ -25,25 +25,25 @@ namespace Alchemy.Editor
public static IEnumerable<Type> GetBaseClassesAndInterfaces(Type type, bool includeSelf = false)
{
List<Type> allTypes = new();
if (includeSelf) allTypes.Add(type);
if (includeSelf) yield return type;
if (type.BaseType == typeof(object))
{
allTypes.AddRange(type.GetInterfaces());
foreach (var interfaceType in type.GetInterfaces())
{
yield return interfaceType;
}
}
else
{
allTypes.AddRange(
Enumerable.Repeat(type.BaseType, 1)
.Concat(type.GetInterfaces())
.Concat(GetBaseClassesAndInterfaces(type.BaseType))
.Distinct()
);
foreach (var baseType in Enumerable.Repeat(type.BaseType, 1)
.Concat(type.GetInterfaces())
.Concat(GetBaseClassesAndInterfaces(type.BaseType))
.Distinct())
{
yield return baseType;
}
}
return allTypes;
}
public static bool HasDefaultConstructor(Type type)

View File

@ -6,13 +6,13 @@ namespace Alchemy.Editor.Drawers
{
public override void OnCreateElement()
{
Element.TrackSerializedObjectValue(SerializedObject, x =>
TargetElement.TrackSerializedObjectValue(SerializedObject, x =>
{
OnInspectorChanged();
});
OnInspectorChanged();
Element.schedule.Execute(() => OnInspectorChanged());
TargetElement.schedule.Execute(() => OnInspectorChanged());
}
protected abstract void OnInspectorChanged();