public USInternalKeyframe AddKeyframe(float time, float value) { USInternalKeyframe uSInternalKeyframe = null; foreach (USInternalKeyframe current in this.internalKeyframes) { if (Mathf.Approximately(current.Time, time)) { uSInternalKeyframe = current; } if (uSInternalKeyframe != null) { break; } } if (!uSInternalKeyframe) { uSInternalKeyframe = ScriptableObject.CreateInstance <USInternalKeyframe>(); this.internalKeyframes.Add(uSInternalKeyframe); } this.Duration = Mathf.Max((from keyframe in this.internalKeyframes orderby keyframe.Time select keyframe).LastOrDefault <USInternalKeyframe>().Time, time); uSInternalKeyframe.curve = this; uSInternalKeyframe.Time = time; uSInternalKeyframe.Value = value; uSInternalKeyframe.InTangent = 0f; uSInternalKeyframe.OutTangent = 0f; this.internalKeyframes.Sort(new Comparison <USInternalKeyframe>(USInternalCurve.KeyframeComparer)); this.BuildAnimationCurveFromInternalCurve(); return(uSInternalKeyframe); }
public void BuildInternalCurveFromAnimationCurve() { Keyframe[] keys = this.animationCurve.keys; for (int i = 0; i < keys.Length; i++) { Keyframe keyframe = keys[i]; USInternalKeyframe uSInternalKeyframe = null; foreach (USInternalKeyframe current in this.internalKeyframes) { if (Mathf.Approximately(keyframe.time, current.Time)) { uSInternalKeyframe = current; break; } } if (uSInternalKeyframe) { uSInternalKeyframe.ConvertFrom(keyframe); } else { USInternalKeyframe uSInternalKeyframe2 = ScriptableObject.CreateInstance <USInternalKeyframe>(); uSInternalKeyframe2.ConvertFrom(keyframe); uSInternalKeyframe2.curve = this; this.internalKeyframes.Add(uSInternalKeyframe2); this.internalKeyframes.Sort(new Comparison <USInternalKeyframe>(USInternalCurve.KeyframeComparer)); } } }
public USInternalKeyframe GetPrevKeyframe(USInternalKeyframe keyframe) { if (this.internalKeyframes.Count == 0) { return(null); } if (this.internalKeyframes[0] == keyframe) { return(null); } int num = -1; for (int i = 0; i < this.internalKeyframes.Count; i++) { if (this.internalKeyframes[i] == keyframe) { num = i; } if (num != -1) { break; } } return(this.internalKeyframes[num - 1]); }
private void DrawTangent(AnimationKeyframeRenderData keyframeRenderData, USInternalKeyframe keyframe) { var FinalRenderPosition = keyframeRenderData.RenderPosition; FinalRenderPosition.x = (FinalRenderPosition.x * XScale) - XScroll; var leftTangentRect = new Rect(keyframeRenderData.LeftTangentEnd.x - 5, keyframeRenderData.LeftTangentEnd.y - 4, 10, 10); var rightTangentRect = new Rect(keyframeRenderData.RightTangentEnd.x - 5, keyframeRenderData.RightTangentEnd.y - 4, 10, 10); if (keyframeRenderData.HasLeftTangent) { var inAngle = -Mathf.Rad2Deg * Mathf.Atan(keyframe.InTangent); Vector2 inDir = Quaternion.Euler(0, 0, inAngle) * Vector2.right; var ratio = DisplayArea.height / DisplayArea.width; inDir.y = inDir.y * ratio; inDir.Normalize(); keyframeRenderData.LeftTangentEnd = FinalRenderPosition + (-inDir * AnimationCurveEditorUtility.TANGENT_LENGTH); using (new GUIChangeColor(keyframeRenderData.LeftTangentColor)) GUI.Label(leftTangentRect, AnimationCurveEditorUtility.KeyframeTexture); } if (keyframeRenderData.HasRightTangent) { var outAngle = -Mathf.Rad2Deg * Mathf.Atan(keyframe.OutTangent); Vector2 outDir = Quaternion.Euler(0, 0, outAngle) * Vector2.right; var ratio = DisplayArea.height / DisplayArea.width; outDir.y = outDir.y * ratio; outDir.Normalize(); keyframeRenderData.RightTangentEnd = FinalRenderPosition + (outDir * AnimationCurveEditorUtility.TANGENT_LENGTH); using (new GUIChangeColor(keyframeRenderData.RightTangentColor)) GUI.Label(rightTangentRect, AnimationCurveEditorUtility.KeyframeTexture); } if (Event.current.type == EventType.MouseDown && leftTangentRect.Contains(Event.current.mousePosition)) { SelectTangent(keyframeRenderData, 0); } if (Event.current.type == EventType.MouseDown && rightTangentRect.Contains(Event.current.mousePosition)) { SelectTangent(keyframeRenderData, 1); } if (Event.current.type == EventType.MouseDrag && SelectedTangent != -1) { DragTangent(keyframeRenderData, 0); } if (Event.current.type == EventType.MouseDrag && SelectedTangent != -1) { DragTangent(keyframeRenderData, 1); } if (Event.current.rawType == EventType.MouseUp && SelectedTangent != -1) { Event.current.Use(); SelectedTangent = -1; } }
public void BuildInternalCurveFromAnimationCurve() { foreach (var keyframe in animationCurve.keys) { // Check we don't already have a key that is roughly equivilant to this key. USInternalKeyframe existingKeyframe = null; foreach (var internalKeyframe in internalKeyframes) { if (Mathf.Approximately(keyframe.time, internalKeyframe.Time)) { existingKeyframe = internalKeyframe; break; } } if (existingKeyframe) { existingKeyframe.ConvertFrom(keyframe); continue; } var internalkeyframe = CreateInstance <USInternalKeyframe>(); internalkeyframe.ConvertFrom(keyframe); internalkeyframe.curve = this; internalKeyframes.Add(internalkeyframe); internalKeyframes.Sort(KeyframeComparer); } }
/// <summary> /// A helper function to get the previous <see cref="WellFired.USInternalKeyframe"/>. /// if something goes wrong, this method returns null. /// </summary> /// <returns>The previous keyframe.</returns> /// <param name="keyframe">A Keyframe.</param> public USInternalKeyframe GetPrevKeyframe(USInternalKeyframe keyframe) { // There are no keys, bail. if (internalKeyframes.Count == 0) { return(null); } // this is the first keyframe, bail. if (internalKeyframes[0] == keyframe) { return(null); } // Find the next keyframe. var index = -1; for (var n = 0; n < internalKeyframes.Count; n++) { if (internalKeyframes[n] == keyframe) { index = n; } if (index != -1) { break; } } return(internalKeyframes[index - 1]); }
public bool Contains(USInternalKeyframe keyframe) { for (int i = this.Keys.Count - 1; i >= 0; i--) { if (this.Keys[i] == keyframe) { return(true); } } return(false); }
/// <summary> /// Compares two keyframes for equality. /// /// The method should return : /// if the first keyframe is greater, return 1, if they are equal, return 0, if the second keyframe is greater, return -1 /// </summary> /// <returns>The value result of the comparison</returns> /// <param name="a">The first keyframe.</param> /// <param name="b">The second keyframe.</param> public static int KeyframeComparer(USInternalKeyframe a, USInternalKeyframe b) { if (a == null) { return(-1); } if (b == null) { return(1); } return(a.Time.CompareTo(b.Time)); }
private void RebuildCachedKeyframeInformation(USInternalKeyframe keyframe) { cachedKeyframePositions[keyframe] = new AnimationKeyframeRenderData(); var keyframeIndex = keyframe.curve.Keys.ToList().FindIndex(element => element == keyframe); var keyframeRatio = (keyframe.Value - MinDisplayY) / (MaxDisplayY - MinDisplayY); var keyframePos = new Vector2(DisplayArea.width * (keyframe.Time / Duration), DisplayArea.height * keyframeRatio); keyframePos.y = DisplayArea.height - keyframePos.y; cachedKeyframePositions[keyframe].RenderPosition = keyframePos; cachedKeyframePositions[keyframe].RenderRect = new Rect(cachedKeyframePositions[keyframe].RenderPosition.x - 6, cachedKeyframePositions[keyframe].RenderPosition.y - 6, 16, 16); cachedKeyframePositions[keyframe].HasLeftTangent = false; cachedKeyframePositions[keyframe].HasRightTangent = false; // Evaluate, stepCount steps per curve section. if (keyframeIndex != 0) { var prevKeyframe = keyframe.curve.Keys[keyframeIndex - 1]; var timeDifference = keyframe.Time - prevKeyframe.Time; var timeStep = timeDifference / AnimationCurveEditorUtility.CURVE_KEYFRAME_STEP_COUNT; var startTime = prevKeyframe.Time; for (var n = 0; n <= AnimationCurveEditorUtility.CURVE_KEYFRAME_STEP_COUNT; n++) { var nextEvaluationTime = startTime + timeStep; var prevSampleValue = keyframe.curve.Evaluate(startTime); var sampleValue = keyframe.curve.Evaluate(nextEvaluationTime); var prevRatio = (prevSampleValue - MinDisplayY) / (MaxDisplayY - MinDisplayY); var nextRatio = (sampleValue - MinDisplayY) / (MaxDisplayY - MinDisplayY); var startPos = new Vector2(DisplayArea.width * (startTime / Duration), DisplayArea.height * prevRatio); var endPos = new Vector2(DisplayArea.width * (nextEvaluationTime / Duration), DisplayArea.height * nextRatio); startPos.y = DisplayArea.height - startPos.y; endPos.y = DisplayArea.height - endPos.y; cachedKeyframePositions[keyframe].CurveSegments.Add(startPos); startTime = nextEvaluationTime; } } if (keyframeIndex < keyframe.curve.Keys.Count - 1) { cachedKeyframePositions[keyframe].HasRightTangent = true; } if (keyframeIndex > 0) { cachedKeyframePositions[keyframe].HasLeftTangent = true; } }
/// <summary> /// Does this curve contain the passed <see cref="WellFired.USInternalKeyframe"/> /// </summary> /// <param name="keyframe">A Keyframe.</param> public bool Contains(USInternalKeyframe keyframe) { // Remove Keyframes that are too close together. for (var n = Keys.Count - 1; n >= 0; n--) { if (Keys[n] == keyframe) { return(true); } } return(false); }
public void RemoveKeyframe(USInternalKeyframe internalKeyframe) { for (int i = this.internalKeyframes.Count - 1; i >= 0; i--) { if (this.internalKeyframes[i] == internalKeyframe) { this.internalKeyframes.RemoveAt(i); } } this.internalKeyframes.Sort(new Comparison <USInternalKeyframe>(USInternalCurve.KeyframeComparer)); this.Duration = (from keyframe in this.internalKeyframes orderby keyframe.Time select keyframe).LastOrDefault <USInternalKeyframe>().Time; this.BuildAnimationCurveFromInternalCurve(); }
public void AddKeyframe(USInternalKeyframe internalKeyframe, USInternalCurve curve) { USUndoManager.PropertyChange(curve, "Add Keyframe"); curve.Keys.Add(internalKeyframe); if (Duration < internalKeyframe.Time) { Duration = internalKeyframe.Time; } curve.Keys.Sort(USInternalCurve.KeyframeComparer); curve.BuildAnimationCurveFromInternalCurve(); RebuildCachedCurveInformation(); AreCurvesDirty = true; }
public void LeftTangentLinear() { if (!this.curve) { return; } USInternalKeyframe prevKeyframe = this.curve.GetPrevKeyframe(this); if (prevKeyframe == null) { return; } this.inTangent = (prevKeyframe.value - this.value) / (prevKeyframe.time - this.time); this.brokenTangents = true; this.curve.BuildAnimationCurveFromInternalCurve(); }
public void RightTangentLinear() { if (!this.curve) { return; } USInternalKeyframe nextKeyframe = this.curve.GetNextKeyframe(this); if (nextKeyframe == null) { return; } this.outTangent = (nextKeyframe.value - this.value) / (nextKeyframe.time - this.time); this.brokenTangents = true; this.curve.BuildAnimationCurveFromInternalCurve(); }
/// <summary> /// Removes a <see cref="WellFired.USInternalKeyframe"/>. /// </summary> /// <param name="internalKeyframe">The keyframe to remove.</param> public void RemoveKeyframe(USInternalKeyframe internalKeyframe) { for (var n = internalKeyframes.Count - 1; n >= 0; n--) { if (internalKeyframes[n] == internalKeyframe) { internalKeyframes.RemoveAt(n); } } internalKeyframes.Sort(KeyframeComparer); Duration = internalKeyframes.OrderBy(keyframe => keyframe.Time).LastOrDefault().Time; BuildAnimationCurveFromInternalCurve(); }
public static void KeyframeLabel(USInternalKeyframe keyframe, AnimationKeyframeRenderData renderData, bool useCurrentValue, float xScroll, float xScale) { var isSelected = renderData.IsKeyframeSelected; var FinalRenderPosition = renderData.RenderPosition; FinalRenderPosition.x = (FinalRenderPosition.x * xScale) - xScroll; renderData.RenderRect = new Rect(FinalRenderPosition.x - 6, renderData.RenderPosition.y - 6, 16, 16); using (new Shared.GUIChangeColor(isSelected ? Color.yellow : (useCurrentValue ? Color.red : Color.white))) GUI.Label(renderData.RenderRect, AnimationCurveEditorUtility.KeyframeTexture); renderData.IsMouseOverKeyframe = false; if (renderData.RenderRect.Contains(Event.current.mousePosition)) { renderData.IsMouseOverKeyframe = true; } }
/// <summary> /// Adds a <see cref="WellFired.USInternalKeyframe"/> at the given time, with the given value. Allocates memory at runtime, it's advisable to use this only in edit mode. /// If this <see cref="WellFired.USInternalKeyframe"/> already exists, we will return the existing <see cref="WellFired.USInternalKeyframe"/>. /// </summary> /// <returns>The allocated keyframe.</returns> /// <param name="time">The keyframe time.</param> /// <param name="value">The value this keyframe should hold.</param> public USInternalKeyframe AddKeyframe(float time, float value) { // If a keyframe already exists at this time, use that one. USInternalKeyframe internalkeyframe = null; foreach (var keyframe in internalKeyframes) { if (Mathf.Approximately(keyframe.Time, time)) { internalkeyframe = keyframe; } if (internalkeyframe != null) { break; } } // Didn't find a keyframe create a new one. if (!internalkeyframe) { internalkeyframe = CreateInstance <USInternalKeyframe>(); internalKeyframes.Add(internalkeyframe); } Duration = Mathf.Max(internalKeyframes.OrderBy(keyframe => keyframe.Time).LastOrDefault().Time, time); internalkeyframe.curve = this; internalkeyframe.Time = time; internalkeyframe.Value = value; internalkeyframe.InTangent = 0.0f; internalkeyframe.OutTangent = 0.0f; internalKeyframes.Sort(KeyframeComparer); BuildAnimationCurveFromInternalCurve(); return(internalkeyframe); }
public void BreakKeyframeTangents(USInternalKeyframe keyframe, bool currentState) { USUndoManager.PropertyChange(keyframe, "Break Keyframe Tangent"); keyframe.BrokenTangents = !currentState; }
public static int KeyframeComparer(USInternalKeyframe a, USInternalKeyframe b) { return(a.Time.CompareTo(b.Time)); }
public void LeftKeyframeTangentLinear(USInternalKeyframe keyframe) { USUndoManager.PropertyChange(keyframe, "Left Tangent Linear"); keyframe.LeftTangentLinear(); }
public void LeftKeyframeTangentConstant(USInternalKeyframe keyframe) { USUndoManager.PropertyChange(keyframe, "Left Tangent Constant"); keyframe.LeftTangentConstant(); }
public void SmoothKeyframe(USInternalKeyframe keyframe) { USUndoManager.PropertyChange(keyframe, "Smooth Keyframe"); keyframe.Smooth(); }
public void FlattenKeyframe(USInternalKeyframe keyframe) { USUndoManager.PropertyChange(keyframe, "Flatten Keyframe"); keyframe.Flatten(); }
private USInternalKeyframe AddKeyframe(USInternalCurve curve, float time, float value) { // If a keyframe already exists at this time, use that one. USInternalKeyframe internalKeyframe = null; foreach (var keyframe in curve.Keys) { if (Mathf.Approximately(keyframe.Time, time)) { internalKeyframe = keyframe; } if (internalKeyframe != null) { break; } } // Didn't find a keyframe create a new one. if (!internalKeyframe) { internalKeyframe = CreateInstance <USInternalKeyframe>(); USUndoManager.RegisterCreatedObjectUndo(internalKeyframe, "Add New Keyframe"); USUndoManager.RegisterCompleteObjectUndo(curve, "Add Keyframe"); curve.Keys.Add(internalKeyframe); } USUndoManager.RegisterCompleteObjectUndo(internalKeyframe, "Add Keyframe"); internalKeyframe.curve = curve; internalKeyframe.Time = time; internalKeyframe.Value = value; internalKeyframe.InTangent = 0.0f; internalKeyframe.OutTangent = 0.0f; if (AutoTangentMode == CurveAutoTangentModes.Smooth) { internalKeyframe.Smooth(); } else if (AutoTangentMode == CurveAutoTangentModes.Flatten) { internalKeyframe.Flatten(); } else if (AutoTangentMode == CurveAutoTangentModes.RightLinear) { internalKeyframe.RightTangentLinear(); } else if (AutoTangentMode == CurveAutoTangentModes.RightConstant) { internalKeyframe.RightTangentConstant(); } else if (AutoTangentMode == CurveAutoTangentModes.LeftLinear) { internalKeyframe.LeftTangentLinear(); } else if (AutoTangentMode == CurveAutoTangentModes.LeftConstant) { internalKeyframe.LeftTangentConstant(); } else if (AutoTangentMode == CurveAutoTangentModes.BothLinear) { internalKeyframe.BothTangentLinear(); } else if (AutoTangentMode == CurveAutoTangentModes.BothConstant) { internalKeyframe.BothTangentConstant(); } curve.Keys.Sort(USInternalCurve.KeyframeComparer); curve.BuildAnimationCurveFromInternalCurve(); return(internalKeyframe); }
public void RemoveKeyframe(USInternalKeyframe keyframe) { USUndoManager.PropertyChange(keyframe.curve, "Remove Keyframe"); keyframe.curve.RemoveKeyframe(keyframe); }