Skip to content

Commit 6edceca

Browse files
committed
added ability to create curves on the fly from 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
1 parent 5f948af commit 6edceca

File tree

14 files changed

+304
-28
lines changed

14 files changed

+304
-28
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ obj
1010
# built application files
1111
*.apk
1212
*.ap_
13+
.vs
1314

1415
# nuget packages
1516
packages

FreePIE.Core/FreePIE.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
<Compile Include="ScriptEngine\Globals\CurveGlobalProvider.cs" />
119119
<Compile Include="ScriptEngine\Globals\IGlobalProvider.cs" />
120120
<Compile Include="ScriptEngine\Globals\ScriptHelpersGlobalProvider.cs" />
121+
<Compile Include="ScriptEngine\Globals\ScriptHelpers\CurveHelper.cs" />
121122
<Compile Include="ScriptEngine\Globals\ScriptHelpers\DiagnosticHelper.cs" />
122123
<Compile Include="ScriptEngine\Globals\ScriptHelpers\FilterHelper.cs" />
123124
<Compile Include="ScriptEngine\Globals\ScriptHelpers\IScriptHelper.cs" />

FreePIE.Core/Model/Curve.cs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Linq;
45

56
namespace FreePIE.Core.Model
67
{
8+
79
public class Curve
810
{
9-
public Curve(List<Point> points) : this(null, points) {}
11+
public Curve(IEnumerable<Point> points) : this(null, points) {}
1012

11-
public Curve(string name, List<Point> points)
13+
public Curve(string name, IEnumerable<Point> points)
1214
{
1315
Name = name;
14-
Points = points;
16+
Points = points.ToList();
1517
ValidateCurve = true;
1618
}
1719

1820
public Curve() {}
1921

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

50+
public override string ToString()
51+
{
52+
return "[" + string.Join(", ", Points.Select(p => $"({p.X}, {p.Y})")) + "]";
53+
}
54+
55+
}
56+
[DebuggerDisplay("({X}, {Y})")]
4857
public struct Point
4958
{
5059
public Point(double x, double y) : this()
@@ -84,5 +93,26 @@ public bool Equals(Point other)
8493
{
8594
return X.Equals(other.X) && Y.Equals(other.Y);
8695
}
96+
97+
public void Deconstruct(out double x, out double y)
98+
{
99+
x = this.X;
100+
y = this.Y;
101+
}
102+
103+
public static implicit operator Point((double x, double y) tuple)
104+
{
105+
return new Point(tuple.x, tuple.y);
106+
}
107+
108+
public static implicit operator (double x, double y)(Point point)
109+
{
110+
return (point.X, point.Y);
111+
}
87112
}
113+
114+
115+
88116
}
117+
118+

FreePIE.Core/ScriptEngine/Globals/CurveGlobalProvider.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Diagnostics;
23
using System.Linq;
34
using FreePIE.Core.Common;
45
using FreePIE.Core.Contracts;
@@ -7,6 +8,7 @@
78

89
namespace FreePIE.Core.ScriptEngine.Globals
910
{
11+
1012
public class CurveGlobalProvider : IGlobalProvider
1113
{
1214
private readonly ISettingsManager settingsManager;
@@ -20,6 +22,7 @@ public IEnumerable<object> ListGlobals()
2022
return settingsManager.Settings.Curves.Where(c => !string.IsNullOrEmpty(c.Name)).Select(c => new CurveGlobal(c));
2123
}
2224

25+
[DebuggerDisplay("{curve.Points}")]
2326
public class CurveGlobal : IGlobalNameProvider
2427
{
2528
private readonly Curve curve;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using FreePIE.Core.Contracts;
2+
using FreePIE.Core.Model;
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
8+
namespace FreePIE.Core.ScriptEngine.Globals.ScriptHelpers
9+
{
10+
[Global(Name = "curves")]
11+
public class CurveHelper : IScriptHelper
12+
{
13+
public CurveHelper()
14+
{
15+
16+
}
17+
18+
/// <summary>
19+
/// Create a curve from a list of points
20+
/// </summary>
21+
/// <param name="points">list of points in the format x,y,x,y,x,y,x,y...</param>
22+
/// <returns>a curve global</returns>
23+
public CurveGlobalProvider.CurveGlobal create(double minimum, double maximum, params double[] points)
24+
{
25+
26+
var pointz = new List<Point>() { new Point(minimum, minimum) };
27+
28+
// ensure that all of the points values are between the minimum and maximum
29+
30+
if (points.Any(p => p < minimum || p > maximum))
31+
throw new Exception("All points must be between the minimum and maximum values");
32+
33+
34+
pointz.AddRange(points.Select((x, i) => new { x, i }).GroupBy(p => p.i / 2).Select(g => new Point(g.First().x, g.Last().x)));
35+
36+
pointz.Add(new Point(maximum, maximum));
37+
38+
return new CurveGlobalProvider.CurveGlobal(new Curve(Guid.NewGuid().ToString(), pointz) { ValidateCurve = true });
39+
}
40+
41+
}
42+
43+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace FreePIE.GUI.Events
8+
{
9+
internal class SaveSettingsEvent
10+
{
11+
public SaveSettingsEvent()
12+
{
13+
14+
}
15+
}
16+
}

FreePIE.GUI/FreePIE.GUI.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
<Compile Include="Events\Command\RunEvent.cs" />
134134
<Compile Include="Events\Command\TrayEvent.cs" />
135135
<Compile Include="Events\DeleteCurveEvent.cs" />
136+
<Compile Include="Events\SaveSettingsEvent.cs" />
136137
<Compile Include="Events\ScriptDocumentAddedEvent.cs" />
137138
<Compile Include="Events\ExitingEvent.cs" />
138139
<Compile Include="Events\ScriptEvent.cs" />
Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,41 @@
11
<Window x:Name="ThisView" x:Class="FreePIE.GUI.Shells.Curves.CurveSettingsView"
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:curves="clr-namespace:FreePIE.GUI.Views.Curves"
44
Title="CurveSettingsView" Background="{DynamicResource WindowBackgroundBrush}" SizeToContent="WidthAndHeight" MinHeight="200" MinWidth="200"
55
Width="{Binding ElementName=ThisView, Path=WindowWidth}"
66
Height="{Binding ElementName=ThisView, Path=WindowHeight}"
77
Icon="{StaticResource IconCurve}">
88

9-
<Grid>
9+
<Grid Margin="10">
1010
<Grid.RowDefinitions>
1111
<RowDefinition Height="*"/>
1212
<RowDefinition Height="Auto"/>
1313
</Grid.RowDefinitions>
1414
<ScrollViewer>
15-
<ItemsControl x:Name="Curves" Grid.Row="0"></ItemsControl>
15+
<ItemsControl x:Name="Curves" Grid.Row="0">
16+
<ItemsControl.ItemsPanel>
17+
<ItemsPanelTemplate>
18+
<WrapPanel />
19+
</ItemsPanelTemplate>
20+
</ItemsControl.ItemsPanel>
21+
</ItemsControl>
1622
</ScrollViewer>
17-
<Button x:Name="AddCurve" Width="150" Grid.Row="1">Add new curve</Button>
23+
<StackPanel Orientation="Horizontal" Grid.Row="1">
24+
<Button x:Name="AddCurve" Width="150" >
25+
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
26+
<Path Stretch="Fill" Width="8" Height="8" Margin="5,0,5,0"
27+
Fill="White"
28+
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"/>
29+
<TextBlock> Add curve</TextBlock>
30+
</StackPanel>
31+
</Button>
32+
<Button x:Name="Save" Margin="5,0,0,0" Background="Green">
33+
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
34+
<Image Source="/Resources/save-16.png" Width="10"/>
35+
<TextBlock Margin="5,0,0,0">Save</TextBlock>
36+
</StackPanel>
37+
</Button>
38+
</StackPanel>
1839
</Grid>
40+
1941
</Window>

FreePIE.GUI/Shells/Curves/CurveSettingsViewModel.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using Caliburn.Micro;
5+
6+
using FreePIE.Core.Common.Events;
57
using FreePIE.Core.Persistence;
68
using FreePIE.GUI.Events;
79
using FreePIE.GUI.Result;
@@ -14,11 +16,14 @@ public class CurveSettingsViewModel : ShellPresentationModel, Core.Common.Events
1416
{
1517
private readonly ISettingsManager settingsManager;
1618
private readonly Func<CurveViewModel> curveModelFactory;
19+
private readonly IEventAggregator eventAggregator;
1720

1821
public CurveSettingsViewModel(IResultFactory resultFactory, ISettingsManager settingsManager, Func<CurveViewModel> curveModelFactory, IEventAggregator eventAggregator) : base(resultFactory)
1922
{
2023
this.settingsManager = settingsManager;
2124
this.curveModelFactory = curveModelFactory;
25+
this.eventAggregator = eventAggregator;
26+
2227
DisplayName = "Curve settings";
2328
CreateCurvesModel();
2429
eventAggregator.Subscribe(this);
@@ -57,5 +62,10 @@ public BindableCollection<CurveViewModel> Curves
5762
NotifyOfPropertyChange(() => Curves);
5863
}
5964
}
65+
66+
public void Save()
67+
{
68+
eventAggregator.Publish(new SaveSettingsEvent());
69+
}
6070
}
6171
}

FreePIE.GUI/Shells/MainShellViewModel.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace FreePIE.GUI.Shells
2323
{
2424
public class MainShellViewModel : ShellPresentationModel,
2525
Core.Common.Events.IHandle<ScriptDocumentAddedEvent>
26-
,Core.Common.Events.IHandle<ExitingEvent>
26+
,Core.Common.Events.IHandle<ExitingEvent>, Core.Common.Events.IHandle<SaveSettingsEvent>
2727
{
2828
private const string dockingConfig = "layout.config";
2929
private readonly IEventAggregator eventAggregator;
@@ -220,5 +220,10 @@ public void Handle(ExitingEvent message)
220220
var layoutSerializer = new XmlLayoutSerializer(DockingManager);
221221
layoutSerializer.Serialize(paths.GetDataPath(dockingConfig));
222222
}
223+
224+
void Core.Common.Events.IHandle<SaveSettingsEvent>.Handle(SaveSettingsEvent message)
225+
{
226+
this.settingsManager.Save();
227+
}
223228
}
224229
}

0 commit comments

Comments
 (0)