diff --git a/Source/Editor/GUI/CurveEditor.Contents.cs b/Source/Editor/GUI/CurveEditor.Contents.cs index c2831046b..75f37d457 100644 --- a/Source/Editor/GUI/CurveEditor.Contents.cs +++ b/Source/Editor/GUI/CurveEditor.Contents.cs @@ -522,6 +522,16 @@ namespace FlaxEditor.GUI cm.AddButton("Show whole curve", _editor.ShowWholeCurve); cm.AddButton("Reset view", _editor.ResetView); } + cm.AddSeparator(); + var presetCm = cm.AddChildMenu("Apply preset"); + foreach (var value in Enum.GetValues(typeof(CurvePreset))) + { + CurvePreset preset = (CurvePreset)value; + string name = Utilities.Utils.GetPropertyNameUI(preset.ToString()); + var b = presetCm.ContextMenu.AddButton(name, () => _editor.ApplyPreset(preset)); + b.Enabled = !(_editor is LinearCurveEditor && (preset != CurvePreset.Constant && preset != CurvePreset.Linear)); + } + _editor.OnShowContextMenu(cm, selectionCount); cm.Show(this, location); } @@ -619,6 +629,33 @@ namespace FlaxEditor.GUI } } + /// + /// A list of avaliable curve presets for the . + /// + public enum CurvePreset + { + /// + /// A curve where every point has the same value. + /// + Constant, + /// + /// A curve linear curve. + /// + Linear, + /// + /// A curve that starts a slowly and then accelerates until the end. + /// + EaseIn, + /// + /// A curve that starts a steep and then flattens until the end. + /// + EaseOut, + /// + /// A combination of the and preset. + /// + Smoothstep + } + /// public override void OnKeyframesDeselect(IKeyframesEditor editor) { diff --git a/Source/Editor/GUI/CurveEditor.cs b/Source/Editor/GUI/CurveEditor.cs index 706d07b32..d91eac18c 100644 --- a/Source/Editor/GUI/CurveEditor.cs +++ b/Source/Editor/GUI/CurveEditor.cs @@ -326,6 +326,28 @@ namespace FlaxEditor.GUI private Color _labelsColor; private Font _labelsFont; + /// + /// Preset values for to be applied to a . + /// + public Dictionary PresetValues = new Dictionary + { + { CurvePreset.Constant, new object[] { true, // LinearTangent + 0f, 0.5f, 0f, 0f, // Time, value, tangent in, tangent out + 1f, 0.5f, 0f, 0f } }, + { CurvePreset.EaseIn, new object[] { false, + 0f, 0f, 0f, 0f, + 1f, 1f, -1.4f, 0f } }, + { CurvePreset.EaseOut, new object[] { false, + 1f, 1f, 0f, 0f, + 0f, 0f, 0f, 1.4f } }, + { CurvePreset.Linear, new object[] { true, + 0f, 0f, 0f, 0f, + 1f, 1f, 0f, 0f } }, + { CurvePreset.Smoothstep, new object[] { false, + 0f, 0f, 0f, 0f, + 1f, 1f, 0f, 0f } }, + }; + /// /// The keyframe UI points. /// @@ -568,6 +590,28 @@ namespace FlaxEditor.GUI /// The list of indices of the keyframes to remove. protected abstract void RemoveKeyframesInternal(HashSet indicesToRemove); + /// + /// Tries to convert a float to the type of the type wildcard of the curve editor. + /// + /// The float. + /// The converted value. + public static object ConvertCurvePresetValueToCurveEditorType(float value) + { + if (typeof(T) == typeof(Float2)) + return new Float2(value); + if (typeof(T) == typeof(Float3)) + return new Float3(value); + if (typeof(T) == typeof(Float4)) + return new Float4(value); + if (typeof(T) == typeof(Vector2)) + return new Vector2(value); + if (typeof(T) == typeof(Vector3)) + return new Vector3(value); + if (typeof(T) == typeof(Vector4)) + return new Vector4(value); + return value; + } + /// /// Called when showing a context menu. Can be used to add custom buttons with actions. /// @@ -752,6 +796,17 @@ namespace FlaxEditor.GUI ShowCurve(false); } + /// + /// Applies a to the curve editor. + /// + /// The preset. + public virtual void ApplyPreset(CurvePreset preset) + { + // Remove existing keyframes + SelectAll(); + RemoveKeyframes(); + } + /// public override void Evaluate(out object result, float time, bool loop = false) { @@ -1580,6 +1635,22 @@ namespace FlaxEditor.GUI base.OnDestroy(); } + + /// + public override void ApplyPreset(CurvePreset preset) + { + base.ApplyPreset(preset); + + object[] data = PresetValues[preset]; + for (int i = 1; i < data.Length; i += 4) + { + float time = (float)data[i]; + object value = ConvertCurvePresetValueToCurveEditorType((float)data[i + 1]); + AddKeyframe(time, value); + } + + ShowWholeCurve(); + } } /// @@ -2396,5 +2467,28 @@ namespace FlaxEditor.GUI base.OnDestroy(); } + + /// + public override void ApplyPreset(CurvePreset preset) + { + base.ApplyPreset(preset); + + object[] data = PresetValues[preset]; + for (int i = 1; i < data.Length; i += 4) + { + float time = (float)data[i]; + object value = ConvertCurvePresetValueToCurveEditorType((float)data[i + 1]); + object tangentIn = ConvertCurvePresetValueToCurveEditorType((float)data[i + 2]); + object tangentOut = ConvertCurvePresetValueToCurveEditorType((float)data[i + 3]); + + AddKeyframe(time, value, tangentIn, tangentOut); + } + + SelectAll(); + if ((bool)data[0]) + SetTangentsLinear(); + + ShowWholeCurve(); + } } }