Skip to content

Commit

Permalink
Merge pull request #59 from techno-dwarf-works/feature/refactoring
Browse files Browse the repository at this point in the history
Version 0.0.56
  • Loading branch information
uurha authored Sep 9, 2024
2 parents d5672bf + 7ca0b83 commit ecc6729
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 96 deletions.
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[submodule "BetterInternalCore"]
path = Assets/BetterInternalCore
url = [email protected]:techno-dwarf-works/better-internal-core.git
branch = upm
137 changes: 50 additions & 87 deletions Assets/BetterCommons/Editor/Drawers/SerializeReferenceField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,8 @@ public SerializeReferenceField(SerializedProperty property, string label)
PropertyType = property.propertyType;
PropertyField = CreatePropertyField(property, label);

#if !UNITY_2022_2_OR_NEWER
var hasLabel = HasLabel(property);
if (!hasLabel && TryCreateBufferLabel(property, out var bufferLabel))
{
_bufferLabel = bufferLabel;
Insert(0, _bufferLabel);
}
#endif

Add(PropertyField);

_updateSchedule = schedule.Execute(Update).Every(_updateInterval);
style.FlexGrow(StyleDefinition.OneStyleFloat);
RegisterCallback<AttachToPanelEvent>(OnAttachToPanel);
Expand Down Expand Up @@ -103,12 +94,7 @@ private void Update()
}

#if !UNITY_2022_2_OR_NEWER
var hasLabel = HasLabel(property);
if (hasLabel && _bufferLabel != null)
{
_bufferLabel.RemoveFromHierarchy();
_bufferLabel = null;
}
ValidateLabel(property);
#endif

var newType = property.managedReferenceFullTypename;
Expand All @@ -127,11 +113,7 @@ private void Update()
finally
{
#if !UNITY_2022_2_OR_NEWER
if (!hasLabel && TryCreateBufferLabel(property, out var label))
{
_bufferLabel = label;
Insert(0, _bufferLabel);
}
ValidateLabel(property);
#endif
}

Expand All @@ -154,6 +136,31 @@ private void Update()
property.Dispose();
}

private void ValidateLabel(SerializedProperty property)
{
if (!IsLast())
{
if (_bufferLabel == null) return;
_bufferLabel.RemoveFromHierarchy();
_bufferLabel = null;

return;
}

var hasLabel = HasLabel();
switch (hasLabel)
{
case true when _bufferLabel != null:
_bufferLabel.RemoveFromHierarchy();
_bufferLabel = null;
break;
case false when _bufferLabel == null && TryCreateBufferLabel(property, out var bufferLabel):
_bufferLabel = bufferLabel;
Insert(0, _bufferLabel);
break;
}
}

private void ReactToEditorChange()
{
UpdateSerializedObjectIfNeeded();
Expand All @@ -174,87 +181,38 @@ private void UpdateSerializedObjectIfNeeded()
EditorApplication.delayCall += ClearRecentObjects;
}

private bool HasLabel(SerializedProperty property)
private bool HasLabel()
{
return ValidateBaseField(property);
return HasValidLabel<Toggle, bool>();
}

private bool ValidateBaseField(SerializedProperty property)
private bool IsLast()
{
var propertyType = property.propertyType;
switch (propertyType)
{
case SerializedPropertyType.Generic:
return property.isArray && HasValidLabel<IntegerField>();
case SerializedPropertyType.Integer:
return HasValidLabel<IntegerField>();
case SerializedPropertyType.Boolean:
return HasValidLabel<Toggle>();
case SerializedPropertyType.Float:
return HasValidLabel<FloatField>();
case SerializedPropertyType.String:
return HasValidLabel<TextField>();
case SerializedPropertyType.Color:
return HasValidLabel<ColorField>();
case SerializedPropertyType.ObjectReference:
return HasValidLabel<ObjectField>();
case SerializedPropertyType.LayerMask:
return HasValidLabel<LayerMaskField>();
case SerializedPropertyType.Enum:
return HasValidLabel<PopupField<string>>();
case SerializedPropertyType.Vector2:
return HasValidLabel<Vector2Field>();
case SerializedPropertyType.Vector3:
return HasValidLabel<Vector3Field>();
case SerializedPropertyType.Vector4:
return HasValidLabel<Vector4Field>();
case SerializedPropertyType.Rect:
return HasValidLabel<RectField>();
case SerializedPropertyType.ArraySize:
return HasValidLabel<IntegerField>();
case SerializedPropertyType.Character:
return HasValidLabel<TextField>();
case SerializedPropertyType.AnimationCurve:
return HasValidLabel<CurveField>();
case SerializedPropertyType.Bounds:
return HasValidLabel<BoundsField>();
case SerializedPropertyType.Gradient:
return HasValidLabel<GradientField>();
case SerializedPropertyType.Quaternion:
return HasValidLabel<Vector4Field>();
case SerializedPropertyType.ExposedReference:
return false;
case SerializedPropertyType.FixedBufferSize:
return false;
case SerializedPropertyType.Vector2Int:
return HasValidLabel<Vector2IntField>();
case SerializedPropertyType.Vector3Int:
return HasValidLabel<Vector3IntField>();
case SerializedPropertyType.RectInt:
return HasValidLabel<RectIntField>();
case SerializedPropertyType.BoundsInt:
return HasValidLabel<BoundsIntField>();
case SerializedPropertyType.Hash128:
return HasValidLabel<Hash128Field>();
default:
return false;
}
var builder = this.Query().Descendents<SerializeReferenceField>().Build();
var query = builder.Last();
return query == this;
}

private bool HasValidLabel<TValueType>()
private bool HasValidLabel<TBaseField, TValueType>() where TBaseField : BaseField<TValueType>
{
if (TryGetBaseField<TValueType>(out var baseField))
if (TryGetBaseField<TBaseField, TValueType>(out var baseField))
{
return baseField.labelElement != null;
}

return false;
}

private bool TryGetBaseField<TValueType>(out BaseField<TValueType> baseField)
private bool TryGetBaseField<TBaseField, TValueType>(out BaseField<TValueType> baseField) where TBaseField : BaseField<TValueType>
{
baseField = PropertyField.Query().Children<BaseField<TValueType>>().First();
var validParent = baseField.parent == PropertyField;
baseField = PropertyField.Query().Children<TBaseField>().First();

if (baseField == null)
{
return false;
}

var validParent = baseField.GetFirstAncestorOfType<PropertyField>() == PropertyField;
if (!validParent)
{
baseField = null;
Expand Down Expand Up @@ -334,5 +292,10 @@ private UndoPropertyModification[] OnPropertyModification(UndoPropertyModificati

return modifications;
}

~SerializeReferenceField()
{
_updateSchedule?.Pause();
}
}
}
2 changes: 1 addition & 1 deletion Assets/BetterCommons/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.tdw.better.commons",
"displayName": "Better Commons",
"version": "0.0.55",
"version": "0.0.56",
"unity": "2021.3",
"description": " ",
"dependencies": {
Expand Down
1 change: 1 addition & 0 deletions Assets/BetterInternalCore
Submodule BetterInternalCore added at dd654e
8 changes: 8 additions & 0 deletions Assets/BetterInternalCore.meta

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

1 change: 0 additions & 1 deletion Packages/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"dependencies": {
"com.tdw.better.internal.core": "0.0.2",
"com.unity.collab-proxy": "2.0.1",
"com.unity.feature.development": "1.0.1",
"com.unity.ide.rider": "3.0.18",
Expand Down
7 changes: 0 additions & 7 deletions Packages/packages-lock.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
{
"dependencies": {
"com.tdw.better.internal.core": {
"version": "0.0.2",
"depth": 0,
"source": "registry",
"dependencies": {},
"url": "https://package.openupm.com"
},
"com.unity.collab-proxy": {
"version": "2.0.1",
"depth": 0,
Expand Down

0 comments on commit ecc6729

Please sign in to comment.