From 82295f52b53f769943cb0c140cdd4fc69fbbcc8b Mon Sep 17 00:00:00 2001 From: xiaolei Date: Mon, 11 Jun 2018 19:15:25 +0800 Subject: [PATCH] [unity] Better animation inspector preview event tooltips. (#1122) * animation event tips modified * [unity] Format new tooltip code. --- .../Editor/SkeletonDataAssetInspector.cs | 38 ++++++++++++++----- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/spine-unity/Assets/spine-unity/Editor/SkeletonDataAssetInspector.cs b/spine-unity/Assets/spine-unity/Editor/SkeletonDataAssetInspector.cs index ee2c2945d..aa5a4b809 100644 --- a/spine-unity/Assets/spine-unity/Editor/SkeletonDataAssetInspector.cs +++ b/spine-unity/Assets/spine-unity/Editor/SkeletonDataAssetInspector.cs @@ -682,6 +682,7 @@ namespace Spine.Unity.Editor { List currentAnimationEvents = new List(); List currentAnimationEventTimes = new List(); + List currentAnimationEventTooltips = new List(); public bool IsValid { get { return skeletonAnimation != null && skeletonAnimation.valid; } } @@ -1071,7 +1072,7 @@ namespace Spine.Unity.Editor { int loopCount = (int)(t.TrackTime / t.TrackEnd); float currentTime = t.TrackTime - (t.TrackEnd * loopCount); float normalizedTime = currentTime / t.Animation.Duration; - float wrappedTime = normalizedTime % 1; + float wrappedTime = normalizedTime % 1f; lineRect.x = barRect.x + (lineRectWidth * wrappedTime) - 0.5f; lineRect.width = 2; @@ -1080,11 +1081,13 @@ namespace Spine.Unity.Editor { GUI.DrawTexture(lineRect, EditorGUIUtility.whiteTexture); GUI.color = Color.white; + currentAnimationEventTooltips = currentAnimationEventTooltips ?? new List(); + currentAnimationEventTooltips.Clear(); for (int i = 0; i < currentAnimationEvents.Count; i++) { - float fr = currentAnimationEventTimes[i]; + float eventTime = currentAnimationEventTimes[i]; var userEventIcon = Icons.userEvent; var evRect = new Rect(barRect) { - x = Mathf.Clamp(((fr / t.Animation.Duration) * lineRectWidth) - (userEventIcon.width / 2), barRect.x, float.MaxValue), + x = Mathf.Max(((eventTime / t.Animation.Duration) * lineRectWidth) - (userEventIcon.width / 2), barRect.x), y = barRect.y + userEventIcon.height, width = userEventIcon.width, height = userEventIcon.height @@ -1094,16 +1097,29 @@ namespace Spine.Unity.Editor { Event ev = Event.current; if (ev.type == EventType.Repaint) { if (evRect.Contains(ev.mousePosition)) { - GUIStyle tooltipStyle = EditorStyles.helpBox; - Rect tooltipRect = new Rect(evRect); - tooltipRect.width = tooltipStyle.CalcSize(new GUIContent(currentAnimationEvents[i].Data.Name)).x; + string eventName = currentAnimationEvents[i].Data.Name; + Rect tooltipRect = new Rect(evRect) { + width = EditorStyles.helpBox.CalcSize(new GUIContent(eventName)).x + }; tooltipRect.y -= 4; + tooltipRect.y -= tooltipRect.height * currentAnimationEventTooltips.Count; // Avoid several overlapping tooltips. tooltipRect.x += 4; - GUI.Label(tooltipRect, currentAnimationEvents[i].Data.Name, tooltipStyle); - GUI.tooltip = currentAnimationEvents[i].Data.Name; + + // Handle tooltip overflowing to the right. + float rightEdgeOverflow = (tooltipRect.x + tooltipRect.width) - (barRect.x + barRect.width); + if (rightEdgeOverflow > 0) + tooltipRect.x -= rightEdgeOverflow; + + currentAnimationEventTooltips.Add(new SpineEventTooltip { rect = tooltipRect, text = eventName }); } } } + + // Draw tooltips. + for (int i = 0; i < currentAnimationEventTooltips.Count; i++) { + GUI.Label(currentAnimationEventTooltips[i].rect, currentAnimationEventTooltips[i].text, EditorStyles.helpBox); + GUI.tooltip = currentAnimationEventTooltips[i].text; + } } } @@ -1130,7 +1146,11 @@ namespace Spine.Unity.Editor { previewGameObject = null; } } + + internal struct SpineEventTooltip { + public Rect rect; + public string text; + } } - }