Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions game/addons/tools/Code/Curves/CurveEditorPopup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,37 @@ public void AddPresets( SerializedProperty serializedProperty )
Presets.SetSizeMode( SizeMode.Default, SizeMode.CanShrink );
Presets.MaximumSize = new( 1000, 148 );
Presets.OnCurveClicked = c =>
{
var existingCurve = serializedProperty.GetValue<Curve>();
c.UpdateTimeRange( InheritRange( existingCurve.TimeRange, c.TimeRange ), false );
c.UpdateValueRange( InheritRange( existingCurve.ValueRange, c.ValueRange ), false );
serializedProperty.SetValue( c );
};
Presets.OnCurveRangesClicked = c =>
{
var existingCurve = serializedProperty.GetValue<Curve>();
if ( !Editor.CanEditTimeRange )
{
c.UpdateTimeRange( existingCurve.TimeRange, false );
c.UpdateTimeRange( InheritRange( existingCurve.TimeRange, c.TimeRange ), false );
}
if ( !Editor.CanEditValueRange )
{
c.UpdateValueRange( existingCurve.ValueRange, false );
c.UpdateValueRange( InheritRange( existingCurve.ValueRange, c.ValueRange ), false );
}
serializedProperty.SetValue( c );
};
Presets.CanApplyRanges = () => Editor.CanEditTimeRange || Editor.CanEditValueRange;
Presets.GetCurveToSave = () => serializedProperty.GetValue<Curve>();
Layout.Add( Presets );
}
}

/// <summary>
/// Keep the range of the curve being edited, unless it's degenerate (an unset curve), in which
/// case the preset's own range is the only sane thing to use.
/// </summary>
static Vector2 InheritRange( Vector2 existing, Vector2 fallback ) => existing.x == existing.y ? fallback : existing;

/// <summary>
/// Set this editor to be a range editor
/// </summary>
Expand Down
22 changes: 22 additions & 0 deletions game/addons/tools/Code/Curves/CurvePresets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,24 @@
public class CurvePresets : ListView
{
public Func<Curve?> GetCurveToSave { get; set; }

/// <summary>
/// Apply a preset to the curve being edited. The preset's own time/value ranges are ignored,
/// the edited curve keeps the ranges it already has.
/// </summary>
public Action<Curve> OnCurveClicked { get; set; }

/// <summary>
/// Apply a preset including its saved time/value ranges. Optional.
/// </summary>
public Action<Curve> OnCurveRangesClicked { get; set; }

/// <summary>
/// Whether <see cref="OnCurveRangesClicked"/> can do anything right now - ranges are locked
/// by the property being edited otherwise.
/// </summary>
public Func<bool> CanApplyRanges { get; set; }

List<Curve> curveList = new();

string CookieName;
Expand Down Expand Up @@ -98,6 +114,12 @@ private void ShowItemContext( object obj )

var m = new ContextMenu( this );

if ( OnCurveRangesClicked is not null && (CanApplyRanges?.Invoke() ?? true) )
{
m.AddOption( "Apply With Ranges", "open_in_full", () => OnCurveRangesClicked( c ) );
m.AddSeparator();
}

m.AddOption( "Copy Json", "content_copy", () =>
{
var json = JsonSerializer.Serialize( c );
Expand Down
Loading