Skip to content

Commit

Permalink
remove save button fromadded ability to create curves on the fly from…
Browse files Browse the repository at this point in the history
… python via the new CurveHelper

added ability to generate python script for curve creation from the Curves Setting GUI
add new button to save curves instead of relying on exit of Freepie to auto save.
added some debugger display attributes to Point and Curve classes for easier debugging
add Unit Tests for CurveHelper
improve layout of  Curve editor to flow instead of just using a single column
  • Loading branch information
drowhunter committed Nov 3, 2024
1 parent 5f948af commit c4ef84f
Show file tree
Hide file tree
Showing 14 changed files with 298 additions and 28 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ obj
# built application files
*.apk
*.ap_
.vs

# nuget packages
packages
Expand Down
1 change: 1 addition & 0 deletions FreePIE.Core/FreePIE.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
<Compile Include="ScriptEngine\Globals\CurveGlobalProvider.cs" />
<Compile Include="ScriptEngine\Globals\IGlobalProvider.cs" />
<Compile Include="ScriptEngine\Globals\ScriptHelpersGlobalProvider.cs" />
<Compile Include="ScriptEngine\Globals\ScriptHelpers\CurveHelper.cs" />
<Compile Include="ScriptEngine\Globals\ScriptHelpers\DiagnosticHelper.cs" />
<Compile Include="ScriptEngine\Globals\ScriptHelpers\FilterHelper.cs" />
<Compile Include="ScriptEngine\Globals\ScriptHelpers\IScriptHelper.cs" />
Expand Down
38 changes: 34 additions & 4 deletions FreePIE.Core/Model/Curve.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace FreePIE.Core.Model
{

public class Curve
{
public Curve(List<Point> points) : this(null, points) {}
public Curve(IEnumerable<Point> points) : this(null, points) {}

public Curve(string name, List<Point> points)
public Curve(string name, IEnumerable<Point> points)
{
Name = name;
Points = points;
Points = points.ToList();
ValidateCurve = true;
}

public Curve() {}

[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
public List<Point> Points { get; set; }
public string Name { get; set; }
public bool? ValidateCurve { get; set; }
Expand All @@ -43,8 +46,14 @@ private static List<Point> CalculateDefault(double yAxisMinValue, double yAxisMa
.Select(value => new Point(value, value))
.ToList();
}
}

public override string ToString()
{
return "[" + string.Join(", ", Points.Select(p => $"({p.X}, {p.Y})")) + "]";
}

}
[DebuggerDisplay("({X}, {Y})")]
public struct Point
{
public Point(double x, double y) : this()
Expand Down Expand Up @@ -84,5 +93,26 @@ public bool Equals(Point other)
{
return X.Equals(other.X) && Y.Equals(other.Y);
}

public void Deconstruct(out double x, out double y)
{
x = this.X;
y = this.Y;
}

public static implicit operator Point((double x, double y) tuple)
{
return new Point(tuple.x, tuple.y);
}

public static implicit operator (double x, double y)(Point point)
{
return (point.X, point.Y);
}
}



}


3 changes: 3 additions & 0 deletions FreePIE.Core/ScriptEngine/Globals/CurveGlobalProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using FreePIE.Core.Common;
using FreePIE.Core.Contracts;
Expand All @@ -7,6 +8,7 @@

namespace FreePIE.Core.ScriptEngine.Globals
{

public class CurveGlobalProvider : IGlobalProvider
{
private readonly ISettingsManager settingsManager;
Expand All @@ -20,6 +22,7 @@ public IEnumerable<object> ListGlobals()
return settingsManager.Settings.Curves.Where(c => !string.IsNullOrEmpty(c.Name)).Select(c => new CurveGlobal(c));
}

[DebuggerDisplay("{curve.Points}")]
public class CurveGlobal : IGlobalNameProvider
{
private readonly Curve curve;
Expand Down
43 changes: 43 additions & 0 deletions FreePIE.Core/ScriptEngine/Globals/ScriptHelpers/CurveHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using FreePIE.Core.Contracts;
using FreePIE.Core.Model;

using System;
using System.Collections.Generic;
using System.Linq;

namespace FreePIE.Core.ScriptEngine.Globals.ScriptHelpers
{
[Global(Name = "curves")]
public class CurveHelper : IScriptHelper
{
public CurveHelper()
{

}

/// <summary>
/// Create a curve from a list of points
/// </summary>
/// <param name="points">list of points in the format x,y,x,y,x,y,x,y...</param>
/// <returns>a curve global</returns>
public CurveGlobalProvider.CurveGlobal create(double minimum, double maximum, params double[] points)
{

var pointz = new List<Point>() { new Point(minimum, minimum) };

// ensure that all of the points values are between the minimum and maximum

if (points.Any(p => p < minimum || p > maximum))
throw new Exception("All points must be between the minimum and maximum values");


pointz.AddRange(points.Select((x, i) => new { x, i }).GroupBy(p => p.i / 2).Select(g => new Point(g.First().x, g.Last().x)));

pointz.Add(new Point(maximum, maximum));

return new CurveGlobalProvider.CurveGlobal(new Curve(Guid.NewGuid().ToString(), pointz) { ValidateCurve = true });
}

}

}
16 changes: 16 additions & 0 deletions FreePIE.GUI/Events/SaveSettingsEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FreePIE.GUI.Events
{
internal class SaveSettingsEvent
{
public SaveSettingsEvent()
{

}
}
}
1 change: 1 addition & 0 deletions FreePIE.GUI/FreePIE.GUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
<Compile Include="Events\Command\RunEvent.cs" />
<Compile Include="Events\Command\TrayEvent.cs" />
<Compile Include="Events\DeleteCurveEvent.cs" />
<Compile Include="Events\SaveSettingsEvent.cs" />
<Compile Include="Events\ScriptDocumentAddedEvent.cs" />
<Compile Include="Events\ExitingEvent.cs" />
<Compile Include="Events\ScriptEvent.cs" />
Expand Down
30 changes: 26 additions & 4 deletions FreePIE.GUI/Shells/Curves/CurveSettingsView.xaml
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
<Window x:Name="ThisView" x:Class="FreePIE.GUI.Shells.Curves.CurveSettingsView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:curves="clr-namespace:FreePIE.GUI.Views.Curves"
Title="CurveSettingsView" Background="{DynamicResource WindowBackgroundBrush}" SizeToContent="WidthAndHeight" MinHeight="200" MinWidth="200"
Width="{Binding ElementName=ThisView, Path=WindowWidth}"
Height="{Binding ElementName=ThisView, Path=WindowHeight}"
Icon="{StaticResource IconCurve}">

<Grid>
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ScrollViewer>
<ItemsControl x:Name="Curves" Grid.Row="0"></ItemsControl>
<ItemsControl x:Name="Curves" Grid.Row="0">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</ScrollViewer>
<Button x:Name="AddCurve" Width="150" Grid.Row="1">Add new curve</Button>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Grid.Row="1">
<Button x:Name="AddCurve" Width="150" >
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Path Stretch="Fill" Width="8" Height="8" Margin="5,0,5,0"
Fill="White"
Data="M4.1561281,2.2702953 L4.8524521,2.2702954 4.8509674,3.963097 5.8969377,3.9630803 5.8969378,5.0916036 4.8524628,5.1061913 4.8524521,6.7843885 4.1561281,6.7843887 4.1559771,5.0877741 3.1116421,5.0916036 3.1116421,3.9630803 4.1556735,3.9654722 4.1561281,2.2702953 z"/>
<TextBlock> Add curve</TextBlock>
</StackPanel>
</Button>
<Button x:Name="Save" Margin="5,0,0,0" Background="Green">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Image Source="/Resources/save-16.png" Width="10"/>
<TextBlock Margin="5,0,0,0">Save</TextBlock>
</StackPanel>
</Button>
</StackPanel>
</Grid>

</Window>
10 changes: 10 additions & 0 deletions FreePIE.GUI/Shells/Curves/CurveSettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Collections.Generic;
using System.Linq;
using Caliburn.Micro;

using FreePIE.Core.Common.Events;
using FreePIE.Core.Persistence;
using FreePIE.GUI.Events;
using FreePIE.GUI.Result;
Expand All @@ -14,11 +16,14 @@ public class CurveSettingsViewModel : ShellPresentationModel, Core.Common.Events
{
private readonly ISettingsManager settingsManager;
private readonly Func<CurveViewModel> curveModelFactory;
private readonly IEventAggregator eventAggregator;

public CurveSettingsViewModel(IResultFactory resultFactory, ISettingsManager settingsManager, Func<CurveViewModel> curveModelFactory, IEventAggregator eventAggregator) : base(resultFactory)
{
this.settingsManager = settingsManager;
this.curveModelFactory = curveModelFactory;
this.eventAggregator = eventAggregator;

DisplayName = "Curve settings";
CreateCurvesModel();
eventAggregator.Subscribe(this);
Expand Down Expand Up @@ -57,5 +62,10 @@ public BindableCollection<CurveViewModel> Curves
NotifyOfPropertyChange(() => Curves);
}
}

public void Save()
{
eventAggregator.Publish(new SaveSettingsEvent());
}
}
}
7 changes: 6 additions & 1 deletion FreePIE.GUI/Shells/MainShellViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace FreePIE.GUI.Shells
{
public class MainShellViewModel : ShellPresentationModel,
Core.Common.Events.IHandle<ScriptDocumentAddedEvent>
,Core.Common.Events.IHandle<ExitingEvent>
,Core.Common.Events.IHandle<ExitingEvent>, Core.Common.Events.IHandle<SaveSettingsEvent>
{
private const string dockingConfig = "layout.config";
private readonly IEventAggregator eventAggregator;
Expand Down Expand Up @@ -220,5 +220,10 @@ public void Handle(ExitingEvent message)
var layoutSerializer = new XmlLayoutSerializer(DockingManager);
layoutSerializer.Serialize(paths.GetDataPath(dockingConfig));
}

void Core.Common.Events.IHandle<SaveSettingsEvent>.Handle(SaveSettingsEvent message)
{
this.settingsManager.Save();
}
}
}
35 changes: 23 additions & 12 deletions FreePIE.GUI/Views/Curves/CurveView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
xmlns:Charts="clr-namespace:Visiblox.Charts;assembly=Visiblox.Charts"
xmlns:Visiblox="clr-namespace:FreePIE.GUI.Common.Visiblox"
xmlns:Curves="clr-namespace:FreePIE.GUI.Views.Curves">

<Grid Margin="5">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
Expand All @@ -14,9 +15,9 @@

<CheckBox x:Name="ValidateCurve" Margin="5,3,5,0">Validate Curve</CheckBox>
<Button x:Name="Reset">Reset</Button>
<Button x:Name="Delete">Delete</Button>
<Button x:Name="Delete" Margin="5,0,0,0" Background="Red">Delete</Button>
</StackPanel>
<Charts:Chart Background="White" Width="600" Height="400" Margin="0,2,0,0">
<Charts:Chart Background="White" Width="600" Height="400" Margin="0,2,0,0" >
<Charts:Chart.Behaviour>
<Charts:BehaviourManager AllowMultipleEnabled="True">
<Visiblox:MovePointBehaviour OnPointMove="OnPointDragged" OnPointSelected="OnPointSelected" />
Expand Down Expand Up @@ -47,18 +48,28 @@
</Charts:LineSeries>
</Charts:Chart.Series>
</Charts:Chart>
<GroupBox Header="Selected point" IsEnabled="{ Binding HasSelectedPoint }">
<GroupBox Header="Selected point">
<Border Padding="5">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<Label Margin="0,0,5,0" VerticalAlignment="Center" Width="30">In</Label>
<TextBox x:Name="SelectedPointX" IsEnabled="{ Binding CanSetSelectedPointX }" Width="80" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Margin="0,0,5,0" VerticalAlignment="Center" Width="30">Out</Label>
<TextBox x:Name="SelectedPointY" Width="80" />
<StackPanel Orientation="Horizontal" >
<StackPanel Orientation="Horizontal" IsEnabled="{ Binding HasSelectedPoint }">
<StackPanel Orientation="Horizontal">
<Label Margin="5,0,5,0" VerticalAlignment="Center" Width="30">In</Label>
<TextBox x:Name="SelectedPointX" IsEnabled="{ Binding CanSetSelectedPointX }" Width="80" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Margin="5,0,5,0" VerticalAlignment="Center" Width="30">Out</Label>
<TextBox x:Name="SelectedPointY" Width="80" />
</StackPanel>
<Button x:Name="ApplyNewValuesToSelectedPoint" HorizontalAlignment="Left" Width="Auto" Margin="5,0,5,0">Apply</Button>

</StackPanel>
<Button x:Name="ApplyNewValuesToSelectedPoint" HorizontalAlignment="Left" Width="Auto" Margin="0,0,5,0">Apply</Button>
<Button x:Name="Script" HorizontalAlignment="Left" Width="Auto" Margin="5,0,5,0" IsEnabled="{ Binding HasPoints }" Background="DarkGoldenrod" ToolTip="Copy curve as python script to clipboard">
<StackPanel Orientation="Horizontal">
<Image Source="/Resources/copy-16.png" Margin="0,0,5,0"/>
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center">Copy </TextBlock>
</StackPanel>
</Button>

</StackPanel>
</Border>
</GroupBox>
Expand Down
Loading

0 comments on commit c4ef84f

Please sign in to comment.