-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
869 additions
and
717 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
72 changes: 72 additions & 0 deletions
72
Editor/Drawers/HandleDrawers/DrawLabel/DrawLabelAttributeDrawer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using SaintsField.Editor.Core; | ||
using SaintsField.Editor.Utils; | ||
using UnityEditor; | ||
using UnityEngine; | ||
|
||
namespace SaintsField.Editor.Drawers.HandleDrawers.DrawLabel | ||
{ | ||
[CustomPropertyDrawer(typeof(DrawLabelAttribute))] | ||
public partial class DrawLabelAttributeDrawer: SaintsPropertyDrawer | ||
{ | ||
private class LabelInfo | ||
{ | ||
public Space Space; | ||
|
||
public string Content; | ||
public bool IsCallback; | ||
public string ActualContent; | ||
public EColor EColor; | ||
|
||
public Util.TargetWorldPosInfo TargetWorldPosInfo; | ||
|
||
public GUIStyle GUIStyle; | ||
} | ||
|
||
|
||
|
||
private static void OnSceneGUIInternal(SceneView _, LabelInfo labelInfo) | ||
{ | ||
// ReSharper disable once ReplaceWithStringIsNullOrEmpty | ||
// ReSharper disable once MergeIntoLogicalPattern | ||
if (labelInfo.ActualContent == null || labelInfo.ActualContent == "") | ||
{ | ||
return; | ||
} | ||
|
||
if (!string.IsNullOrEmpty(labelInfo.TargetWorldPosInfo.Error)) | ||
{ | ||
return; | ||
} | ||
|
||
if(labelInfo.GUIStyle == null) | ||
{ | ||
if (labelInfo.EColor == EColor.White) | ||
{ | ||
labelInfo.GUIStyle = GUI.skin.label; | ||
} | ||
else | ||
{ | ||
labelInfo.GUIStyle = new GUIStyle | ||
{ | ||
normal = { textColor = labelInfo.EColor.GetColor() }, | ||
}; | ||
} | ||
} | ||
|
||
Vector3 pos = labelInfo.TargetWorldPosInfo.IsTransform | ||
? labelInfo.TargetWorldPosInfo.Transform.position | ||
: labelInfo.TargetWorldPosInfo.WorldPos; | ||
Handles.Label(pos, labelInfo.ActualContent, labelInfo.GUIStyle); | ||
} | ||
|
||
~DrawLabelAttributeDrawer() | ||
{ | ||
SceneView.duringSceneGui -= OnSceneGUIIMGUI; | ||
#if UNITY_2021_3_OR_NEWER | ||
SceneView.duringSceneGui -= OnSceneGUIUIToolkit; | ||
#endif | ||
} | ||
} | ||
} |
File renamed without changes.
116 changes: 116 additions & 0 deletions
116
Editor/Drawers/HandleDrawers/DrawLabel/DrawLabelAttributeDrawerIMGUI.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using SaintsField.Editor.Utils; | ||
using UnityEditor; | ||
using UnityEngine; | ||
|
||
namespace SaintsField.Editor.Drawers.HandleDrawers.DrawLabel | ||
{ | ||
public partial class DrawLabelAttributeDrawer | ||
{ | ||
#region IMGUI | ||
|
||
private readonly Dictionary<string, LabelInfo> IdToLabelInfo = new Dictionary<string, LabelInfo>(); | ||
private static string GetKey(SerializedProperty property) => $"{property.serializedObject.targetObject.GetInstanceID()}_{property.propertyPath}"; | ||
|
||
private string _cacheKey = ""; | ||
|
||
protected override void ImGuiOnDispose() | ||
{ | ||
base.ImGuiOnDispose(); | ||
// ReSharper disable once InvertIf | ||
SceneView.duringSceneGui -= OnSceneGUIIMGUI; | ||
IdToLabelInfo.Remove(_cacheKey); | ||
|
||
} | ||
|
||
protected override bool WillDrawBelow(SerializedProperty property, ISaintsAttribute saintsAttribute, | ||
int index, | ||
FieldInfo info, | ||
object parent) | ||
{ | ||
return true; | ||
} | ||
|
||
protected override float GetBelowExtraHeight(SerializedProperty property, GUIContent label, | ||
float width, | ||
ISaintsAttribute saintsAttribute, int index, FieldInfo info, object parent) | ||
{ | ||
return 0; | ||
} | ||
|
||
protected override Rect DrawBelow(Rect position, SerializedProperty property, | ||
GUIContent label, ISaintsAttribute saintsAttribute, int index, FieldInfo info, object parent) | ||
{ | ||
_cacheKey = GetKey(property); | ||
// ReSharper disable once InvertIf | ||
if (!IdToLabelInfo.TryGetValue(_cacheKey, out LabelInfo labelInfo)) | ||
{ | ||
DrawLabelAttribute drawLabelAttribute = (DrawLabelAttribute)saintsAttribute; | ||
|
||
Util.TargetWorldPosInfo targetWorldPosInfo = Util.GetPropertyTargetWorldPosInfo(drawLabelAttribute.Space, property, info, parent); | ||
if (targetWorldPosInfo.Error != "") | ||
{ | ||
Debug.LogError(targetWorldPosInfo.Error); | ||
return position; | ||
} | ||
|
||
labelInfo = new LabelInfo | ||
{ | ||
Space = drawLabelAttribute.Space, | ||
Content = drawLabelAttribute.Content, | ||
ActualContent = drawLabelAttribute.Content, | ||
IsCallback = drawLabelAttribute.IsCallback, | ||
EColor = drawLabelAttribute.EColor, | ||
TargetWorldPosInfo = targetWorldPosInfo, | ||
GUIStyle = drawLabelAttribute.EColor == EColor.White | ||
? GUI.skin.label | ||
: new GUIStyle | ||
{ | ||
normal = { textColor = drawLabelAttribute.EColor.GetColor() }, | ||
}, | ||
}; | ||
IdToLabelInfo[_cacheKey] = labelInfo; | ||
ImGuiEnsureDispose(property.serializedObject.targetObject); | ||
SceneView.duringSceneGui += OnSceneGUIIMGUI; | ||
SceneView.RepaintAll(); | ||
return position; | ||
} | ||
|
||
if (!labelInfo.TargetWorldPosInfo.IsTransform) | ||
{ | ||
labelInfo.TargetWorldPosInfo = Util.GetPropertyTargetWorldPosInfo(labelInfo.Space, property, info, parent); | ||
} | ||
|
||
if (!labelInfo.IsCallback) | ||
{ | ||
return position; | ||
} | ||
|
||
(string valueError, object value) = Util.GetOf<object>(labelInfo.Content, null, property, info, parent); | ||
if (valueError != "") | ||
{ | ||
Debug.LogError(valueError); | ||
return position; | ||
} | ||
|
||
if (value is IWrapProp wrapProp) | ||
{ | ||
value = Util.GetWrapValue(wrapProp); | ||
} | ||
|
||
labelInfo.ActualContent = $"{value}"; | ||
return position; | ||
} | ||
|
||
private void OnSceneGUIIMGUI(SceneView sceneView) | ||
{ | ||
if (IdToLabelInfo.TryGetValue(_cacheKey, out LabelInfo labelInfo)) | ||
{ | ||
OnSceneGUIInternal(sceneView, labelInfo); | ||
} | ||
} | ||
|
||
#endregion | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
Editor/Drawers/HandleDrawers/DrawLabel/DrawLabelAttributeDrawerIMGUI.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
101 changes: 101 additions & 0 deletions
101
Editor/Drawers/HandleDrawers/DrawLabel/DrawLabelAttributeDrawerUIToolkit.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
#if UNITY_2021_3_OR_NEWER | ||
using System; | ||
using System.Reflection; | ||
using SaintsField.Editor.Utils; | ||
using UnityEditor; | ||
using UnityEngine; | ||
using UnityEngine.UIElements; | ||
|
||
namespace SaintsField.Editor.Drawers.HandleDrawers.DrawLabel | ||
{ | ||
public partial class DrawLabelAttributeDrawer | ||
{ | ||
#region UIToolkit | ||
private static string NameDrawLabel(SerializedProperty property) => $"{property.propertyPath}_DrawLabel"; | ||
|
||
private LabelInfo _labelInfoUIToolkit; | ||
|
||
protected override VisualElement CreateBelowUIToolkit(SerializedProperty property, | ||
ISaintsAttribute saintsAttribute, int index, VisualElement container, FieldInfo info, object parent) | ||
{ | ||
DrawLabelAttribute drawLabelAttribute = (DrawLabelAttribute)saintsAttribute; | ||
Util.TargetWorldPosInfo targetWorldPosInfo = Util.GetPropertyTargetWorldPosInfo(drawLabelAttribute.Space, property, info, parent); | ||
if (targetWorldPosInfo.Error != "") | ||
{ | ||
return new HelpBox(targetWorldPosInfo.Error, HelpBoxMessageType.Error); | ||
} | ||
|
||
_labelInfoUIToolkit = new LabelInfo | ||
{ | ||
Content = drawLabelAttribute.Content, | ||
ActualContent = drawLabelAttribute.Content, | ||
IsCallback = drawLabelAttribute.IsCallback, | ||
EColor = drawLabelAttribute.EColor, | ||
TargetWorldPosInfo = targetWorldPosInfo, | ||
}; | ||
|
||
return null; | ||
} | ||
|
||
protected override void OnAwakeUIToolkit(SerializedProperty property, ISaintsAttribute saintsAttribute, int index, VisualElement container, | ||
Action<object> onValueChangedCallback, FieldInfo info, object parent) | ||
{ | ||
VisualElement child = new VisualElement | ||
{ | ||
name = NameDrawLabel(property), | ||
}; | ||
child.RegisterCallback<AttachToPanelEvent>(_ => SceneView.duringSceneGui += OnSceneGUIUIToolkit); | ||
child.RegisterCallback<DetachFromPanelEvent>(_ => SceneView.duringSceneGui -= OnSceneGUIUIToolkit); | ||
container.Add(child); | ||
} | ||
|
||
protected override void OnUpdateUIToolkit(SerializedProperty property, ISaintsAttribute saintsAttribute, | ||
int index, | ||
VisualElement container, Action<object> onValueChanged, FieldInfo info) | ||
{ | ||
if (_labelInfoUIToolkit.IsCallback) | ||
{ | ||
object parent = SerializedUtils.GetFieldInfoAndDirectParent(property).parent; | ||
|
||
(string error, object value) = | ||
Util.GetOf<object>(_labelInfoUIToolkit.Content, null, property, info, parent); | ||
|
||
if (error != "") | ||
{ | ||
#if SAINTSFIELD_DEBUG | ||
Debug.LogError(error); | ||
#endif | ||
return; | ||
} | ||
|
||
if (value is IWrapProp wrapProp) | ||
{ | ||
value = Util.GetWrapValue(wrapProp); | ||
} | ||
|
||
_labelInfoUIToolkit.ActualContent = $"{value}"; | ||
} | ||
|
||
if (!_labelInfoUIToolkit.TargetWorldPosInfo.IsTransform) | ||
{ | ||
DrawLabelAttribute drawLabelAttribute = (DrawLabelAttribute)saintsAttribute; | ||
object parent = SerializedUtils.GetFieldInfoAndDirectParent(property).parent; | ||
if(parent != null) | ||
{ | ||
_labelInfoUIToolkit.TargetWorldPosInfo = Util.GetPropertyTargetWorldPosInfo(drawLabelAttribute.Space, property, info, parent); | ||
} | ||
} | ||
} | ||
|
||
private GUIStyle _guiStyleUIToolkit; | ||
|
||
private void OnSceneGUIUIToolkit(SceneView sceneView) | ||
{ | ||
OnSceneGUIInternal(sceneView, _labelInfoUIToolkit); | ||
} | ||
#endregion | ||
|
||
|
||
} | ||
} | ||
#endif |
3 changes: 3 additions & 0 deletions
3
Editor/Drawers/HandleDrawers/DrawLabel/DrawLabelAttributeDrawerUIToolkit.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.