UI - Replaced tuples with custom classes for binding

As far as i know, it's not possible to have CompiledBindings with tuples, since tuple values are fields and not properties. This change allows for compiled bindings, and makes the intent behind the tuples clearer
This commit is contained in:
Diogo Trindade committed 2023-04-30 21:40:36 +01:00
1 parent 59f9479d9a
commit 5429346396
9 files changed
+120 -45

No files matched your search

@@ -3,10 +3,12 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
xmlns:vm="clr-namespace:Artemis.VisualScripting.Nodes.Operators.Screens"
x:DataType="vm:EnumEqualsNodeCustomViewModel"
x:Class="Artemis.VisualScripting.Nodes.Operators.Screens.EnumEqualsNodeCustomView">
<ComboBox IsEnabled="{Binding EnumValues.Count}"
ItemsSource="{Binding EnumValues}"
SelectedItem="{Binding CurrentValue}"
<ComboBox IsEnabled="{CompiledBinding EnumValues.Count}"
ItemsSource="{CompiledBinding EnumValues}"
SelectedItem="{CompiledBinding CurrentValue}"
PlaceholderText="Select a value"
Classes="condensed"
VerticalAlignment="Center">
@@ -17,7 +19,7 @@
</ComboBox.ItemsPanel>
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding [1]}" />
<TextBlock Text="{CompiledBinding Path=Name}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
@@ -42,15 +42,15 @@ public class EnumEqualsNodeCustomViewModel : CustomNodeViewModel
});
}
public ObservableCollection<(long, string)> EnumValues { get; } = new();
public ObservableCollection<EnumValueItem> EnumValues { get; } = new();
public (long, string) CurrentValue
public EnumValueItem CurrentValue
{
get => EnumValues.FirstOrDefault(v => v.Item1 == _node.Storage);
get => EnumValues.FirstOrDefault(v => v.Value == _node.Storage);
set
{
if (!Equals(_node.Storage, value.Item1))
_nodeEditorService.ExecuteCommand(Script, new UpdateStorage<long>(_node, value.Item1));
if (!Equals(_node.Storage, value.Value))
_nodeEditorService.ExecuteCommand(Script, new UpdateStorage<long>(_node, value.Value));
}
}
@@ -58,13 +58,38 @@ public class EnumEqualsNodeCustomViewModel : CustomNodeViewModel
{
Dispatcher.UIThread.Post(() =>
{
List<(long, string)> values = Enum.GetValues(type).Cast<Enum>().Select(e => (Convert.ToInt64(e), e.Humanize())).ToList();
List<EnumValueItem> values = Enum.GetValues(type).Cast<Enum>().Select(e => new EnumValueItem(value: Convert.ToInt64(e), name: e.Humanize())).ToList();
if (values.Count > 20)
EnumValues.AddRange(values.OrderBy(v => v.Item2));
EnumValues.AddRange(values.OrderBy(v => v.Name));
else
EnumValues.AddRange(Enum.GetValues(type).Cast<Enum>().Select(e => (Convert.ToInt64(e), e.Humanize())));
EnumValues.AddRange(values);
this.RaisePropertyChanged(nameof(CurrentValue));
}, DispatcherPriority.Background);
}
}
/// <summary>
/// Represents a single enum value
/// </summary>
public class EnumValueItem
{
/// <summary>
/// Creates a new instance of the <see cref="EnumValueItem" /> class.
/// </summary>
public EnumValueItem(long value, string name)
{
Value = value;
Name = name;
}
/// <summary>
/// The underlying value of the enum
/// </summary>
public long Value { get; set; }
/// <summary>
/// The name of the enum value
/// </summary>
public string Name { get; set; }
}