//关键点的功能菜单 private void ShowKeyframeContextMenu(CustomCurveWrapper animationCurve, int i) { GenericMenu menu = new GenericMenu(); CustomKeyframeWrapper keyframeWrapper = animationCurve.GetKeyframeWrapper(i); KeyframeContext userData = new KeyframeContext(animationCurve, i, keyframeWrapper); Keyframe keyframe = animationCurve.GetKeyframe(i); menu.AddItem(new GUIContent("Delete Key"), false, new GenericMenu.MenuFunction2(this.DeleteKey), userData); menu.AddSeparator(string.Empty); menu.AddItem(new GUIContent("Auto"), animationCurve.IsAuto(i), new GenericMenu.MenuFunction2(this.SetKeyAuto), userData); menu.AddItem(new GUIContent("Free Smooth"), animationCurve.IsFreeSmooth(i), new GenericMenu.MenuFunction2(this.SetKeyFreeSmooth), userData); menu.AddItem(new GUIContent("Flat"), (animationCurve.IsFreeSmooth(i) && (keyframe.inTangent == 0f)) && (keyframe.outTangent == 0f), new GenericMenu.MenuFunction2(this.SetKeyFlat), userData); menu.AddItem(new GUIContent("Broken"), animationCurve.IsBroken(i), new GenericMenu.MenuFunction2(this.SetKeyBroken), userData); menu.AddSeparator(string.Empty); menu.AddItem(new GUIContent("Left Tangent/Free"), animationCurve.IsLeftFree(i), new GenericMenu.MenuFunction2(this.SetKeyLeftFree), userData); menu.AddItem(new GUIContent("Left Tangent/Linear"), animationCurve.IsLeftLinear(i), new GenericMenu.MenuFunction2(this.SetKeyLeftLinear), userData); menu.AddItem(new GUIContent("Left Tangent/Constant"), animationCurve.IsLeftConstant(i), new GenericMenu.MenuFunction2(this.SetKeyLeftConstant), userData); menu.AddItem(new GUIContent("Right Tangent/Free"), animationCurve.IsRightFree(i), new GenericMenu.MenuFunction2(this.SetKeyRightFree), userData); menu.AddItem(new GUIContent("Right Tangent/Linear"), animationCurve.IsRightLinear(i), new GenericMenu.MenuFunction2(this.SetKeyRightLinear), userData); menu.AddItem(new GUIContent("Right Tangent/Constant"), animationCurve.IsRightConstant(i), new GenericMenu.MenuFunction2(this.SetKeyRightConstant), userData); menu.AddItem(new GUIContent("Both Tangents/Free"), animationCurve.IsLeftFree(i) && animationCurve.IsRightFree(i), new GenericMenu.MenuFunction2(this.SetKeyBothFree), userData); menu.AddItem(new GUIContent("Both Tangents/Linear"), animationCurve.IsLeftLinear(i) && animationCurve.IsRightLinear(i), new GenericMenu.MenuFunction2(this.SetKeyBothLinear), userData); menu.AddItem(new GUIContent("Both Tangents/Constant"), animationCurve.IsLeftConstant(i) && animationCurve.IsRightConstant(i), new GenericMenu.MenuFunction2(this.SetKeyBothConstant), userData); menu.ShowAsContext(); }
private void DrawCurve(ControlState state, Rect curveArea, Vector2 baseWindow) { foreach (var wrapper in gradient.AnimationCurves) { //if(wrapper.KeyframeCount == 0) //{ // Handles.color = Color.yellow; // float temp = (baseWindow.y / 2 - curveArea.height + state.translation.y)*2 / baseWindow.y; // Handles.DrawDottedLine(new Vector3(0, temp), new Vector3(1, temp), 0.05f); //} //else if(wrapper.KeyframeCount == 1) //{ // CustomKeyframeWrapper kfWrapper = wrapper.GetKeyframeWrapper(0); // Handles.color = wrapper.Color; // Handles.DrawDottedLine(new Vector3(0, kfWrapper.ScreenPosition.y), new Vector3(curveArea.width, kfWrapper.ScreenPosition.y), 10f); //} for (int i = 0; i < (wrapper.KeyframeCount - 1); i++) { CustomKeyframeWrapper keyframeWrapper = wrapper.GetKeyframeWrapper(i); CustomKeyframeWrapper wrapper3 = wrapper.GetKeyframeWrapper(i + 1); Handles.DrawBezier(new Vector3(keyframeWrapper.ScreenPosition.x, keyframeWrapper.ScreenPosition.y), new Vector3(wrapper3.ScreenPosition.x, wrapper3.ScreenPosition.y), new Vector3(keyframeWrapper.OutTangentControlPointPosition.x, keyframeWrapper.OutTangentControlPointPosition.y), new Vector3(wrapper3.InTangentControlPointPosition.x, wrapper3.InTangentControlPointPosition.y), wrapper.Color, null, 4f); } } }
//画关键点 private void DrawKey() { foreach (var wrapper in memberCurveWrapper.AnimationCurves) { for (int i = 0; i < (wrapper.KeyframeCount); i++) { Keyframe keyframe = wrapper.GetKeyframe(i); CustomKeyframeWrapper keyframeWrapper = wrapper.GetKeyframeWrapper(i); bool flag = (this.selection.KeyId == i) && (this.selection.CurveId == wrapper.Id); //画菱形标志 Texture2D texture = Texture2D.whiteTexture; Rect rect3 = new Rect(keyframeWrapper.ScreenPosition.x - 4f, keyframeWrapper.ScreenPosition.y - 4f, 8f, 8f); GUI.Label(rect3, texture); //画左右切线 if (flag) { if (((i > 0) && !wrapper.IsAuto(i)) && (!wrapper.IsLeftLinear(i) && !wrapper.IsLeftConstant(i))) { Vector2 vector = new Vector2(keyframeWrapper.InTangentControlPointPosition.x - keyframeWrapper.ScreenPosition.x, keyframeWrapper.InTangentControlPointPosition.y - keyframeWrapper.ScreenPosition.y); vector.Normalize(); vector = (Vector2)(vector * 30f); Handles.color = Color.gray; Handles.DrawLine(new Vector3(keyframeWrapper.ScreenPosition.x, keyframeWrapper.ScreenPosition.y, 0f), new Vector3(keyframeWrapper.ScreenPosition.x + vector.x, keyframeWrapper.ScreenPosition.y + vector.y, 0f)); Rect rect = new Rect((keyframeWrapper.ScreenPosition.x + vector.x) - 4f, (keyframeWrapper.ScreenPosition.y + vector.y) - 4f, 8f, 8f); //依赖timelinetrackcontrl //GUI.Label(rect, string.Empty, TimelineTrackControl.styles.tangentStyle); GUI.Label(rect, texture); } if (((i < (wrapper.KeyframeCount - 1)) && !wrapper.IsAuto(i)) && (!wrapper.IsRightLinear(i) && !wrapper.IsRightConstant(i))) { Vector2 vector2 = new Vector2(keyframeWrapper.OutTangentControlPointPosition.x - keyframeWrapper.ScreenPosition.x, keyframeWrapper.OutTangentControlPointPosition.y - keyframeWrapper.ScreenPosition.y); vector2.Normalize(); vector2 = (Vector2)(vector2 * 30f); Handles.color = Color.gray; Handles.DrawLine(new Vector3(keyframeWrapper.ScreenPosition.x, keyframeWrapper.ScreenPosition.y, 0f), new Vector3(keyframeWrapper.ScreenPosition.x + vector2.x, keyframeWrapper.ScreenPosition.y + vector2.y, 0f)); Rect rect2 = new Rect((keyframeWrapper.ScreenPosition.x + vector2.x) - 4f, (keyframeWrapper.ScreenPosition.y + vector2.y) - 4f, 8f, 8f); GUI.Label(rect2, texture); } } } } }
//画曲线 protected void DrawCurve() { foreach (var wrapper in memberCurveWrapper.AnimationCurves) { for (int i = 0; i < (wrapper.KeyframeCount - 1); i++) { CustomKeyframeWrapper keyframeWrapper = wrapper.GetKeyframeWrapper(i); CustomKeyframeWrapper wrapper3 = wrapper.GetKeyframeWrapper(i + 1); Debug.Log("屏幕位置" + wrapper3.ScreenPosition); Debug.Log("屏幕位置" + keyframeWrapper.ScreenPosition); Debug.Log("逻辑位置" + wrapper.Curve[i].time); Debug.Log("逻辑位置" + wrapper.Curve[i + 1].time); Handles.DrawBezier(new Vector3(keyframeWrapper.ScreenPosition.x, keyframeWrapper.ScreenPosition.y), new Vector3(wrapper3.ScreenPosition.x, wrapper3.ScreenPosition.y), new Vector3(keyframeWrapper.OutTangentControlPointPosition.x, keyframeWrapper.OutTangentControlPointPosition.y), new Vector3(wrapper3.InTangentControlPointPosition.x, wrapper3.InTangentControlPointPosition.y), wrapper.Color, null, 4f); } } }
public KeyframeContext(CustomCurveWrapper curveWrapper, int key, CustomKeyframeWrapper ckw) { this.curveWrapper = curveWrapper; this.key = key; this.ckw = ckw; }
//调整左右切线的斜率 private void HandleKeyframeTangentInput(ControlState state, Rect curveArea) { scale = state.scale; translation = state.translation; foreach (var wrapper in memberCurveWrapper.AnimationCurves) { for (int i = 0; i < wrapper.KeyframeCount; i++) { int controlID; int num5; Keyframe keyframe = wrapper.GetKeyframe(i); CustomKeyframeWrapper keyframeWrapper = wrapper.GetKeyframeWrapper(i); bool flag = ((this.selection.KeyId == i)) && (this.selection.CurveId == wrapper.Id); if (!flag || wrapper.IsAuto(i)) { continue; } if (((i > 0) && !wrapper.IsLeftLinear(i)) && !wrapper.IsLeftConstant(i)) { Vector2 vector = new Vector2(keyframeWrapper.InTangentControlPointPosition.x - keyframeWrapper.ScreenPosition.x, keyframeWrapper.InTangentControlPointPosition.y - keyframeWrapper.ScreenPosition.y); vector.Normalize(); vector = (Vector2)(vector * 30f); Rect rect = new Rect((keyframeWrapper.ScreenPosition.x + vector.x) - 4f, (keyframeWrapper.ScreenPosition.y + vector.y) - 4f, 8f, 8f); controlID = GUIUtility.GetControlID("TangentIn".GetHashCode(), FocusType.Passive); switch (Event.current.GetTypeForControl(controlID)) { case EventType.MouseDown: if (rect.Contains(Event.current.mousePosition)) { GUIUtility.hotControl = controlID; Event.current.Use(); } break; case EventType.MouseUp: if (GUIUtility.hotControl == controlID) { GUIUtility.hotControl = 0; } break; case EventType.MouseDrag: goto Label_0213; } } goto Label_031E; Label_0213: if (GUIUtility.hotControl == controlID) { Vector2 vector2 = new Vector2((Event.current.mousePosition.x - translation.x) / scale.x, (curveArea.height - translation.y - Event.current.mousePosition.y) / scale.y); Vector2 vector3 = vector2 - new Vector2(keyframe.time, keyframe.value); float inTangent = vector3.y / vector3.x; float outTangent = keyframe.outTangent; if (wrapper.IsFreeSmooth(i)) { outTangent = inTangent; } Keyframe kf = new Keyframe(keyframe.time, keyframe.value, inTangent, outTangent) { tangentMode = keyframe.tangentMode }; wrapper.MoveKey(i, kf); } Label_031E: if (((i < (wrapper.KeyframeCount - 1)) && !wrapper.IsRightLinear(i)) && !wrapper.IsRightConstant(i)) { Vector2 vector4 = new Vector2(keyframeWrapper.OutTangentControlPointPosition.x - keyframeWrapper.ScreenPosition.x, keyframeWrapper.OutTangentControlPointPosition.y - keyframeWrapper.ScreenPosition.y); vector4.Normalize(); vector4 = (Vector2)(vector4 * 30f); Rect rect2 = new Rect((keyframeWrapper.ScreenPosition.x + vector4.x) - 4f, (keyframeWrapper.ScreenPosition.y + vector4.y) - 4f, 8f, 8f); num5 = GUIUtility.GetControlID("TangentOut".GetHashCode(), FocusType.Passive); switch (Event.current.GetTypeForControl(num5)) { case EventType.MouseDown: if (rect2.Contains(Event.current.mousePosition)) { GUIUtility.hotControl = num5; Event.current.Use(); } break; case EventType.MouseUp: if (GUIUtility.hotControl == num5) { GUIUtility.hotControl = 0; } break; case EventType.MouseDrag: if (GUIUtility.hotControl == num5) { Vector2 vector5 = new Vector2((Event.current.mousePosition.x - translation.x) / scale.x, (curveArea.height - translation.y - Event.current.mousePosition.y) / scale.y); Vector2 vector6 = new Vector2(keyframe.time, keyframe.value) - vector5; float num6 = vector6.y / vector6.x; float num7 = keyframe.inTangent; if (wrapper.IsFreeSmooth(i)) { num7 = num6; } Keyframe keyframe3 = new Keyframe(keyframe.time, keyframe.value, num7, num6) { tangentMode = keyframe.tangentMode }; wrapper.MoveKey(i, keyframe3); } break; } } continue; } } }