/************************************************************************************************************************/ private void RepaintEventsGUI(SerializableEventSequenceDrawer.Context context) { var color = GUI.color; for (int i = 0; i < EventTimes.Count; i++) { var currentColor = color; // Read Only: currentColor *= new Color(0.9f, 0.9f, 0.9f, 0.5f * alpha); if (context.SelectedEvent == i) { currentColor *= SelectedEventColor; } else { currentColor *= UnselectedEventColor; } if (i == EventTimes.Count - 1 && !_HasEndTime) { currentColor.a *= 0.65f; } GUI.color = currentColor; var area = GetEventIconArea(i); GUI.DrawTexture(area, EventIcon); } GUI.color = color; }
/************************************************************************************************************************/ /// <summary>Snaps the `seconds` value to the nearest multiple of the <see cref="AnimationClip.frameRate"/>.</summary> public void SnapToFrameRate(SerializableEventSequenceDrawer.Context context, ref float seconds) { if (AnimancerUtilities.TryGetFrameRate(context.TransitionContext.Transition, out var frameRate)) { seconds = AnimancerUtilities.Round(seconds, 1f / frameRate); } }
/************************************************************************************************************************/ #region Events /************************************************************************************************************************/ private void GatherEventTimes(SerializableEventSequenceDrawer.Context context) { EventTimes.Clear(); if (context.Times.Count > 0) { var depth = context.Times.Property.depth; var time = context.Times.GetElement(0); while (time.depth > depth) { EventTimes.Add(time.floatValue * _Duration); time.Next(false); } _EndTime = EventTimes[EventTimes.Count - 1]; if (!float.IsNaN(_EndTime)) { _HasEndTime = true; return; } } _EndTime = AnimancerEvent.Sequence.GetDefaultNormalizedEndTime(_Speed) * _Duration; _HasEndTime = false; if (EventTimes.Count == 0) { EventTimes.Add(_EndTime); } else { EventTimes[EventTimes.Count - 1] = _EndTime; } }
/************************************************************************************************************************/ private void OnMouseUp(Event currentEvent, SerializableEventSequenceDrawer.Context context) { if (currentEvent.button == 1 && _Area.Contains(currentEvent.mousePosition)) { ShowContextMenu(currentEvent, context); currentEvent.Use(); } }
/************************************************************************************************************************/ private static void RoundEventTime(SerializableEventSequenceDrawer.Context context) { var index = context.SelectedEvent; var time = context.Times.GetElement(index); var value = time.floatValue; if (TryRoundValue(ref value)) { time.floatValue = value; SerializableEventSequenceDrawer.SyncEventTimeChange(context, index, value); } }
/************************************************************************************************************************/ private static void AddContextFunction( GenericMenu menu, SerializableEventSequenceDrawer.Context context, string label, bool enabled, Action function) { menu.AddFunction(label, enabled, () => { using (context.SetAsCurrent()) { function(); GUI.changed = true; } }); }
/************************************************************************************************************************/ private void SetPreviewTime(SerializableEventSequenceDrawer.Context context, Event currentEvent) { if (_Duration > 0) { var seconds = PixelsToSeconds(currentEvent.mousePosition.x); if (currentEvent.control) { SnapToFrameRate(context, ref seconds); } TransitionPreviewWindow.PreviewNormalizedTime = seconds / _Duration; } }
/************************************************************************************************************************/ private void NudgeEventTime(SerializableEventSequenceDrawer.Context context, float offsetPixels) { var index = context.SelectedEvent; var time = context.Times.GetElement(index); var value = time.floatValue; value = NormalizedToSeconds(value); value = SecondsToPixels(value); value += offsetPixels; value = PixelsToSeconds(value); value = SecondsToNormalized(value); time.floatValue = value; SerializableEventSequenceDrawer.SyncEventTimeChange(context, index, value); }
/************************************************************************************************************************/ private void OnMouseDown(Event currentEvent, SerializableEventSequenceDrawer.Context context, ref float addEventNormalizedTime) { if (!_Area.Contains(currentEvent.mousePosition)) { return; } var selectedEventControlID = 0; var selectedEvent = -1; for (int i = 0; i < EventControlIDs.Count; i++) { var area = i < EventTimes.Count ? GetEventIconArea(i) : _Area; if (area.Contains(currentEvent.mousePosition)) { selectedEventControlID = EventControlIDs[i]; selectedEvent = i; break; } } GUIUtility.hotControl = GUIUtility.keyboardControl = selectedEventControlID; if (selectedEvent < 0 || selectedEvent >= EventTimes.Count) { SetPreviewTime(context, currentEvent); selectedEvent = -1; } if (currentEvent.type == EventType.MouseDown && currentEvent.clickCount == 2) { addEventNormalizedTime = PixelsToSeconds(currentEvent.mousePosition.x); addEventNormalizedTime = SecondsToNormalized(addEventNormalizedTime); } context.SelectedEvent = selectedEvent; currentEvent.Use(); }
/************************************************************************************************************************/ private void ShowContextMenu(Event currentEvent, SerializableEventSequenceDrawer.Context context) { context = context.Copy(); var time = SecondsToNormalized(PixelsToSeconds(currentEvent.mousePosition.x)); var hasSelectedEvent = context.SelectedEvent >= 0; var menu = new GenericMenu(); AddContextFunction(menu, context, "Add Event (Double Click)", true, () => SerializableEventSequenceDrawer.AddEvent(context, time)); AddContextFunction(menu, context, "Remove Event (Delete)", hasSelectedEvent, () => SerializableEventSequenceDrawer.RemoveEvent(context, context.SelectedEvent)); const string NudgePrefix = "Nudge Event Time/"; AddContextFunction(menu, context, NudgePrefix + "Left 1 Pixel (Left Arrow)", hasSelectedEvent, () => NudgeEventTime(context, -1)); AddContextFunction(menu, context, NudgePrefix + "Left 10 Pixels (Shift + Left Arrow)", hasSelectedEvent, () => NudgeEventTime(context, -10)); AddContextFunction(menu, context, NudgePrefix + "Right 1 Pixel (Right Arrow)", hasSelectedEvent, () => NudgeEventTime(context, 1)); AddContextFunction(menu, context, NudgePrefix + "Right 10 Pixels (Shift + Right Arrow)", hasSelectedEvent, () => NudgeEventTime(context, 10)); var canRoundTime = hasSelectedEvent; if (canRoundTime) { time = context.Times.GetElement(context.SelectedEvent).floatValue; canRoundTime = TryRoundValue(ref time); } AddContextFunction(menu, context, $"Round Event Time to {time}x (Space)", canRoundTime, () => RoundEventTime(context)); menu.ShowAsContext(); }
/// <summary>Draws the details of the <see cref="SerializableEventSequenceDrawer.Context.Callbacks"/>.</summary> private void DoEventsGUI(SerializableEventSequenceDrawer.Context context, out float addEventNormalizedTime) { addEventNormalizedTime = float.NaN; var currentEvent = Event.current; EventControlIDs.Clear(); var selectedEventControlID = -1; var baseControlID = GUIUtility.GetControlID(EventHash - 1, FocusType.Passive); for (int i = 0; i < EventTimes.Count; i++) { var controlID = GUIUtility.GetControlID(EventHash + i, FocusType.Keyboard); EventControlIDs.Add(controlID); if (context.SelectedEvent == i) { selectedEventControlID = controlID; } } EventControlIDs.Add(baseControlID); switch (currentEvent.type) { case EventType.Repaint: RepaintEventsGUI(context); break; case EventType.MouseDown: OnMouseDown(currentEvent, context, ref addEventNormalizedTime); break; case EventType.MouseUp: OnMouseUp(currentEvent, context); break; case EventType.MouseDrag: if (_Duration <= 0) { break; } var hotControl = GUIUtility.hotControl; if (hotControl == baseControlID) { SetPreviewTime(context, currentEvent); GUIUtility.ExitGUI(); } else { for (int i = 0; i < EventTimes.Count; i++) { if (hotControl == EventControlIDs[i]) { if (context.Times.Count < 1) { context.Times.Count = 1; } var seconds = PixelsToSeconds(currentEvent.mousePosition.x); if (currentEvent.control) { SnapToFrameRate(context, ref seconds); } var timeProperty = context.Times.GetElement(i); var normalizedTime = seconds / _Duration; timeProperty.floatValue = normalizedTime; SerializableEventSequenceDrawer.SyncEventTimeChange(context, i, normalizedTime); timeProperty.serializedObject.ApplyModifiedProperties(); timeProperty.serializedObject.Update(); GUIUtility.hotControl = EventControlIDs[context.SelectedEvent]; GUI.changed = true; SetPreviewTime(context, currentEvent); GUIUtility.ExitGUI(); } } } break; case EventType.KeyUp: if (GUIUtility.keyboardControl != selectedEventControlID) { break; } var exitGUI = false; switch (currentEvent.keyCode) { case KeyCode.Delete: case KeyCode.Backspace: SerializableEventSequenceDrawer.RemoveEvent(context, context.SelectedEvent); exitGUI = true; break; case KeyCode.LeftArrow: NudgeEventTime(context, Event.current.shift ? -10 : -1); break; case KeyCode.RightArrow: NudgeEventTime(context, Event.current.shift ? 10 : 1); break; case KeyCode.Space: RoundEventTime(context); break; default: return; // Don't call Use. } currentEvent.Use(); GUI.changed = true; if (exitGUI) { GUIUtility.ExitGUI(); } break; } }
/************************************************************************************************************************/ /// <summary>Draws the ruler GUI and handles input events for the specified `context`.</summary> private void DoGUI(SerializableEventSequenceDrawer.Context context, out float addEventNormalizedTime) { if (context.Property.hasMultipleDifferentValues) { GUI.Label(_Area, "Multi-editing is not supported"); addEventNormalizedTime = float.NaN; return; } var transition = context.TransitionContext.Transition; _Speed = transition.Speed; if (float.IsNaN(_Speed)) { _Speed = 1; } _Duration = context.TransitionContext.MaximumDuration; if (_Duration <= 0) { _Duration = 1; } GatherEventTimes(context); _StartTime = GetStartTime(transition.NormalizedStartTime, _Speed, _Duration); _FadeInEnd = _StartTime + transition.FadeDuration * _Speed; _FadeOutEnd = GetFadeOutEnd(_Speed, _EndTime, _Duration); _MinTime = Mathf.Min(0, _StartTime); _MinTime = Mathf.Min(_MinTime, _FadeOutEnd); _MinTime = Mathf.Min(_MinTime, EventTimes[0]); _MaxTime = Mathf.Max(_StartTime, _FadeOutEnd); if (EventTimes.Count >= 2) { _MaxTime = Mathf.Max(_MaxTime, EventTimes[EventTimes.Count - 2]); } if (_MaxTime < _Duration) { _MaxTime = _Duration; } _SecondsToPixels = _Area.width / (_MaxTime - _MinTime); DoFadeHighlightGUI(); if (AnimancerUtilities.TryGetWrappedObject(transition, out ITransitionGUI gui)) { gui.OnTimelineBackgroundGUI(); } DoEventsGUI(context, out addEventNormalizedTime); DoRulerGUI(); if (_Speed > 0) { if (_StartTime >= _EndTime) { GUI.Label(_Area, "Start Time is not before End Time"); } } else if (_Speed < 0) { if (_StartTime <= _EndTime) { GUI.Label(_Area, "Start Time is not after End Time"); } } if (gui != null) { gui.OnTimelineForegroundGUI(); } }
/************************************************************************************************************************/ /// <summary>Draws the ruler GUI and handles input events for the specified `context`.</summary> public static void DoGUI(Rect area, SerializableEventSequenceDrawer.Context context, out float addEventNormalizedTime) { using (BeginGUI(area)) Current.DoGUI(context, out addEventNormalizedTime); }