Skip to content

Commit

Permalink
Filter node removal
Browse files Browse the repository at this point in the history
  • Loading branch information
VoidXH committed Apr 24, 2024
1 parent 013e768 commit 76cffe9
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 15 deletions.
5 changes: 1 addition & 4 deletions Cavern.QuickEQ.Format/ConfigurationFile/ConfigurationFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ protected void Optimize() {
bool Optimize(FilterGraphNode node) {
bool optimized = false;
if (node.Filter == null) {
IReadOnlyList<FilterGraphNode> parents = node.Parents;
while (parents.Count != 0) {
parents[0].DetachChild(node, true);
}
node.DetachFromGraph();
optimized = true;
}

Expand Down
13 changes: 11 additions & 2 deletions Cavern/Filters/Utilities/FilterGraphNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,18 @@ public void DetachParents() {
}

/// <summary>
/// Remove this node from the filter graph, including both parents and children.
/// Remove this node from the filter graph, from both parents and children.
/// </summary>
public void DetachFromGraph() {
/// <param name="mergeConnections">Connect the parents and children together</param>
public void DetachFromGraph(bool mergeConnections = true) {
if (mergeConnections) {
for (int i = 0, c = parents.Count; i < c; i++) {
for (int j = 0, c2 = children.Count; j < c2; j++) {
parents[i].AddChild(children[j]);
}
}
}

DetachChildren();
DetachParents();
}
Expand Down
3 changes: 1 addition & 2 deletions CavernSamples/CavernizeGUI/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,7 @@ public MainWindow() {
/// <summary>
/// Displays an error message.
/// </summary>
static void Error(string message) =>
MessageBox.Show(message, (string)language["Error"], MessageBoxButton.OK, MessageBoxImage.Error);
static void Error(string message) => MessageBox.Show(message, (string)language["Error"], MessageBoxButton.OK, MessageBoxImage.Error);

/// <summary>
/// Perform one-time UI updates after the window is initialized and displayed.
Expand Down
4 changes: 3 additions & 1 deletion CavernSamples/FilterStudio/Graphs/Parsing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ public static Graph ParseConfigurationFile((string name, FilterGraphNode root)[]
static void AddToGraph(string parent, FilterGraphNode source, Graph target) {
string uid = source.GetHashCode().ToString();
if (target.FindNode(uid) == null) {
Node node = new StyledNode(uid, source.ToString());
StyledNode node = new StyledNode(uid, source.ToString()) {
Filter = source
};
target.AddNode(node);
}

Expand Down
7 changes: 7 additions & 0 deletions CavernSamples/FilterStudio/Graphs/StyledNode.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Microsoft.Msagl.Drawing;

using Cavern.Filters.Utilities;

namespace FilterStudio.Graphs {
/// <summary>
/// An MSAGL <see cref="Node"/> with display properties aligned for this application.
Expand All @@ -18,6 +20,11 @@ public Color Foreground {
}
}

/// <summary>
/// If this node represents a filter and not a label, this property is set to that specific node.
/// </summary>
public FilterGraphNode Filter { get; set; }

/// <summary>
/// An MSAGL <see cref="Node"/> with display properties aligned for this application.
/// </summary>
Expand Down
3 changes: 3 additions & 0 deletions CavernSamples/FilterStudio/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
<MenuItem Header="{StaticResource MFile}">
<MenuItem Header="{StaticResource OpCfg}" Click="LoadConfiguration"/>
</MenuItem>
<MenuItem Header="{StaticResource MGrap}">
<MenuItem Header="{StaticResource OpDel}" Click="DeleteNode"/>
</MenuItem>
</Menu>
<DockPanel DockPanel.Dock="Right" Width="250">
<TextBlock x:Name="selectedNode" DockPanel.Dock="Top" Style="{StaticResource Header}" Margin="5" Text="{StaticResource NNode}"/>
Expand Down
42 changes: 37 additions & 5 deletions CavernSamples/FilterStudio/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.Msagl.Drawing;
using Microsoft.Win32;
using Microsoft.Win32;
using System.Linq;
using System.Windows;
using System.Windows.Input;
Expand All @@ -21,6 +20,11 @@ public partial class MainWindow : Window {
/// </summary>
static readonly ResourceDictionary language = Consts.Language.GetMainWindowStrings();

/// <summary>
/// The currently selected filter's node.
/// </summary>
StyledNode SelectedFilter => (StyledNode)graph.Graph?.Nodes.FirstOrDefault(x => x.Attr.LineWidth > 1);

/// <summary>
/// Each channel's full filter graph.
/// </summary>
Expand All @@ -33,17 +37,31 @@ public MainWindow() {
InitializeComponent();
}

/// <summary>
/// Displays an error message.
/// </summary>
static void Error(string message) => MessageBox.Show(message, (string)language["Error"], MessageBoxButton.OK, MessageBoxImage.Error);

/// <summary>
/// When selecting a <paramref name="node"/>, open it for modification.
/// </summary>
void OnNodeSelected(Node node) {
void OnNodeSelected() {
StyledNode node = SelectedFilter;
if (node == null) {
selectedNode.Text = (string)language["NNode"];
return;
}
selectedNode.Text = node.LabelText;
}

/// <summary>
/// Updates the graph based on the <see cref="rootNodes"/>.
/// </summary>
void ReloadGraph() {
graph.Graph = Parsing.ParseConfigurationFile(rootNodes, Parsing.ParseBackground((SolidColorBrush)Background));
OnNodeSelected();
}

/// <summary>
/// Open a configuration file of known formats.
/// </summary>
Expand All @@ -55,16 +73,30 @@ void LoadConfiguration(object _, RoutedEventArgs e) {
ConfigurationFile file = new EqualizerAPOConfigurationFile(dialog.FileName, Listener.DefaultSampleRate);

rootNodes = file.InputChannels;
graph.Graph = Parsing.ParseConfigurationFile(rootNodes, Parsing.ParseBackground((SolidColorBrush)Background));
ReloadGraph();
}
}

/// <summary>
/// Delete the currently selected node.
/// </summary>
void DeleteNode(object _, RoutedEventArgs e) {
StyledNode node = SelectedFilter;
if (node == null || node.Filter == null) {
Error((string)language["NFNod"]);
return;
}

node.Filter.DetachFromGraph();
ReloadGraph();
}

/// <summary>
/// Hack to provide a Click event for MSAGL's WPF library.
/// </summary>
void GraphClick(object _, MouseButtonEventArgs e) {
Dispatcher.BeginInvoke(() => { // Call after the graph has handled it
OnNodeSelected(graph.Graph?.Nodes.FirstOrDefault(x => x.Attr.LineWidth > 1));
OnNodeSelected();
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
xmlns:system="clr-namespace:System;assembly=mscorlib">
<system:String x:Key="MFile">_Fájl</system:String>
<system:String x:Key="OpCfg">_Konfigurációs fájl megnyitása</system:String>
<system:String x:Key="OpFil">Equalizer APO konfigurációk|*.txt</system:String>
<system:String x:Key="MGrap">_Gráf</system:String>
<system:String x:Key="OpDel">Szűrő _törlése</system:String>
<system:String x:Key="OpFil">Equalizer APO konfigurációk|*.txt</system:String>
<system:String x:Key="Error">Hiba</system:String>
<system:String x:Key="NNode">Nincs kiválasztva csúcspont.</system:String>
<system:String x:Key="NFNod">Kérlek, előbb jelölj ki egy szűrő csúcspontot.</system:String>
</ResourceDictionary>
4 changes: 4 additions & 0 deletions CavernSamples/FilterStudio/Resources/MainWindowStrings.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
xmlns:system="clr-namespace:System;assembly=mscorlib">
<system:String x:Key="MFile">_File</system:String>
<system:String x:Key="OpCfg">_Open configuration file</system:String>
<system:String x:Key="MGrap">_Graph</system:String>
<system:String x:Key="OpDel">_Delete filter</system:String>
<system:String x:Key="OpFil">Equalizer APO configurations|*.txt</system:String>
<system:String x:Key="Error">Error</system:String>
<system:String x:Key="NNode">No node selected.</system:String>
<system:String x:Key="NFNod">Please select a filter node first.</system:String>
</ResourceDictionary>

0 comments on commit 76cffe9

Please sign in to comment.