Data bindings - Fixed some conversion issues

Data bindings - Started hooking up the UI
This commit is contained in:
Robert committed 2020-09-04 23:12:48 +02:00
1 parent 59e1f37aec
commit 18225ca6fa
16 files changed
+357 -90

No files matched your search

@@ -6,9 +6,30 @@
xmlns:local="clr-namespace:Artemis.UI.Screens.ProfileEditor.LayerProperties.DataBindings"
xmlns:s="https://github.com/canton7/Stylet"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:converters="clr-namespace:Artemis.UI.Converters"
xmlns:utilities="clr-namespace:Artemis.UI.Utilities"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
d:DataContext="{d:DesignInstance local:DataBindingModifierViewModel}">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Artemis.UI;component/ResourceDictionaries/DisplayConditions.xaml" />
<ResourceDictionary>
<converters:NullToVisibilityConverter x:Key="NullToVisibilityConverter" />
<utilities:BindingProxy x:Key="DataContextProxy" Data="{Binding}" />
<DataTemplate x:Key="DataModelDataTemplate">
<Control x:Name="TemplateControl" Focusable="False" Template="{StaticResource DataModelSelectionTemplate}" />
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Data.ShowDataModelValues.Value, Source={StaticResource DataContextProxy}}" Value="True">
<Setter TargetName="TemplateControl" Property="Template" Value="{StaticResource DataModelSelectionTemplateWithValues}" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
@@ -41,10 +62,10 @@
Style="{StaticResource DisplayConditionButtonLeftClickMenu}"
Background="#7B7B7B"
BorderBrush="#7B7B7B"
Content="{Binding SelectedOperator.Description}">
Content="{Binding SelectedModifierType.Description}">
<!-- Click="PropertyButton_OnClick" -->
<Button.ContextMenu>
<ContextMenu ItemsSource="{Binding Operators}">
<ContextMenu ItemsSource="{Binding ModifierTypes}">
<ContextMenu.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
@@ -55,7 +76,7 @@
</ContextMenu.ItemTemplate>
<ContextMenu.ItemContainerStyle>
<Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource MaterialDesignMenuItem}">
<Setter Property="Command" Value="{Binding Data.SelectOperatorCommand, Source={StaticResource DataContextProxy}}" />
<Setter Property="Command" Value="{Binding Data.SelectModifierTypeCommand, Source={StaticResource DataContextProxy}}" />
<Setter Property="CommandParameter" Value="{Binding}" />
<Setter Property="CommandTarget" Value="{Binding}" />
</Style>
@@ -63,18 +84,19 @@
</ContextMenu>
</Button.ContextMenu>
</Button>
<Button Grid.Column="3"
Background="#ab47bc"
BorderBrush="#ab47bc"
Style="{StaticResource DisplayConditionButton}"
ToolTip="{Binding SelectedLeftSideProperty.DisplayPropertyPath}"
ToolTip="{Binding SelectedParameterProperty.DisplayPropertyPath}"
HorizontalAlignment="Left">
<Button.ContextMenu>
<ContextMenu ItemsSource="{Binding LeftSideDataModel.Children}" IsOpen="{Binding LeftSideDataModelOpen, Mode=OneWayToSource}">
<ContextMenu ItemsSource="{Binding ParameterDataModel.Children}" IsOpen="{Binding ParameterDataModelOpen, Mode=OneWayToSource}">
<ContextMenu.ItemContainerStyle>
<Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource MaterialDesignMenuItem}">
<Setter Property="ItemsSource" Value="{Binding Children}" />
<Setter Property="Command" Value="{Binding Data.SelectLeftPropertyCommand, Source={StaticResource DataContextProxy}}" />
<Setter Property="Command" Value="{Binding Data.SelectModifierTypeCommand, Source={StaticResource DataContextProxy}}" />
<Setter Property="CommandParameter" Value="{Binding}" />
<Setter Property="CommandTarget" Value="{Binding}" />
<Setter Property="IsEnabled" Value="{Binding IsMatchingFilteredTypes}" />
@@ -85,11 +107,11 @@
</ContextMenu>
</Button.ContextMenu>
<Grid>
<TextBlock Text="{Binding SelectedLeftSideProperty.PropertyDescription.Name}"
Visibility="{Binding SelectedLeftSideProperty, Converter={StaticResource NullToVisibilityConverter}}" />
<TextBlock Text="{Binding SelectedParameterProperty.PropertyDescription.Name}"
Visibility="{Binding SelectedParameterProperty, Converter={StaticResource NullToVisibilityConverter}}" />
<TextBlock Text="« Select a property »"
FontStyle="Italic"
Visibility="{Binding SelectedLeftSideProperty, Converter={StaticResource NullToVisibilityConverter}, ConverterParameter=Inverted}" />
Visibility="{Binding SelectedParameterProperty, Converter={StaticResource NullToVisibilityConverter}, ConverterParameter=Inverted}" />
</Grid>
</Button>
</Grid>
@@ -1,15 +1,127 @@
using Artemis.Core;
using System;
using System.Threading.Tasks;
using System.Timers;
using Artemis.Core;
using Artemis.Core.Services;
using Artemis.UI.Shared;
using Artemis.UI.Shared.Services;
using Artemis.UI.Utilities;
using Stylet;
namespace Artemis.UI.Screens.ProfileEditor.LayerProperties.DataBindings
{
public class DataBindingModifierViewModel : PropertyChangedBase
{
public DataBindingModifierViewModel(DataBindingModifier modifier)
private readonly IDataBindingService _dataBindingService;
private readonly IDataModelUIService _dataModelUIService;
private readonly IProfileEditorService _profileEditorService;
private readonly Timer _updateTimer;
private DataModelPropertiesViewModel _parameterDataModel;
private bool _parameterDataModelOpen;
private DataBindingModifierType _selectedModifierType;
private DataModelVisualizationViewModel _selectedParameterProperty;
public DataBindingModifierViewModel(DataBindingModifier modifier,
IDataBindingService dataBindingService,
ISettingsService settingsService,
IDataModelUIService dataModelUIService,
IProfileEditorService profileEditorService)
{
_dataBindingService = dataBindingService;
_dataModelUIService = dataModelUIService;
_profileEditorService = profileEditorService;
_updateTimer = new Timer(500);
ShowDataModelValues = settingsService.GetSetting<bool>("ProfileEditor.ShowDataModelValues");
Modifier = modifier;
ModifierTypes = new BindableCollection<DataBindingModifierType>();
SelectModifierTypeCommand = new DelegateCommand(ExecuteSelectModifierTypeCommand);
// Initialize async, no need to wait for it
Task.Run(Initialize);
}
public DelegateCommand SelectModifierTypeCommand { get; set; }
public PluginSetting<bool> ShowDataModelValues { get; }
public DataBindingModifier Modifier { get; }
public BindableCollection<DataBindingModifierType> ModifierTypes { get; }
public DataBindingModifierType SelectedModifierType
{
get => _selectedModifierType;
set => SetAndNotify(ref _selectedModifierType, value);
}
public DataModelPropertiesViewModel ParameterDataModel
{
get => _parameterDataModel;
set => SetAndNotify(ref _parameterDataModel, value);
}
public bool ParameterDataModelOpen
{
get => _parameterDataModelOpen;
set => SetAndNotify(ref _parameterDataModelOpen, value);
}
public DataModelVisualizationViewModel SelectedParameterProperty
{
get => _selectedParameterProperty;
set => SetAndNotify(ref _selectedParameterProperty, value);
}
private void Initialize()
{
// Get the data models
ParameterDataModel = _dataModelUIService.GetMainDataModelVisualization();
if (!_dataModelUIService.GetPluginExtendsDataModel(_profileEditorService.GetCurrentModule()))
ParameterDataModel.Children.Add(_dataModelUIService.GetPluginDataModelVisualization(_profileEditorService.GetCurrentModule()));
ParameterDataModel.UpdateRequested += ParameterDataModelOnUpdateRequested;
Update();
_updateTimer.Start();
_updateTimer.Elapsed += OnUpdateTimerOnElapsed;
}
private void Update()
{
// Modifier type
ModifierTypes.Clear();
ModifierTypes.AddRange(_dataBindingService.GetCompatibleModifierTypes(Modifier.DataBinding.TargetProperty.PropertyType));
SelectedModifierType = Modifier.ModifierType;
// Determine the parameter property
if (Modifier.ParameterDataModel == null)
SelectedParameterProperty = null;
else
SelectedParameterProperty = ParameterDataModel.GetChildByPath(Modifier.ParameterDataModel.PluginInfo.Guid, Modifier.ParameterPropertyPath);
}
private void ExecuteSelectModifierTypeCommand(object context)
{
if (!(context is DataBindingModifierType dataBindingModifierType))
return;
Modifier.UpdateModifierType(dataBindingModifierType);
_profileEditorService.UpdateSelectedProfileElement();
Update();
}
private void ParameterDataModelOnUpdateRequested(object sender, EventArgs e)
{
ParameterDataModel.ApplyTypeFilter(true, Modifier.DataBinding.TargetProperty.PropertyType);
}
private void OnUpdateTimerOnElapsed(object sender, ElapsedEventArgs e)
{
if (ParameterDataModelOpen)
ParameterDataModel.Update(_dataModelUIService);
}
}
}
@@ -52,15 +52,16 @@
<Button Background="#ab47bc"
BorderBrush="#ab47bc"
Style="{StaticResource DisplayConditionButton}"
ToolTip="{Binding SelectedLeftSideProperty.DisplayPropertyPath}"
ToolTip="{Binding SelectedSourceProperty.DisplayPropertyPath}"
IsEnabled="{Binding IsDataBindingEnabled}"
HorizontalAlignment="Left">
HorizontalAlignment="Left"
Click="PropertyButton_OnClick">
<Button.ContextMenu>
<ContextMenu ItemsSource="{Binding LeftSideDataModel.Children}" IsOpen="{Binding LeftSideDataModelOpen, Mode=OneWayToSource}">
<ContextMenu ItemsSource="{Binding SourceDataModel.Children}" IsOpen="{Binding SourceDataModelOpen, Mode=OneWayToSource}">
<ContextMenu.ItemContainerStyle>
<Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource MaterialDesignMenuItem}">
<Setter Property="ItemsSource" Value="{Binding Children}" />
<Setter Property="Command" Value="{Binding Data.SelectLeftPropertyCommand, Source={StaticResource DataContextProxy}}" />
<Setter Property="Command" Value="{Binding Data.SelectSourcePropertyCommand, Source={StaticResource DataContextProxy}}" />
<Setter Property="CommandParameter" Value="{Binding}" />
<Setter Property="CommandTarget" Value="{Binding}" />
<Setter Property="IsEnabled" Value="{Binding IsMatchingFilteredTypes}" />
@@ -71,11 +72,11 @@
</ContextMenu>
</Button.ContextMenu>
<Grid>
<TextBlock Text="{Binding SelectedLeftSideProperty.PropertyDescription.Name}"
Visibility="{Binding SelectedLeftSideProperty, Converter={StaticResource NullToVisibilityConverter}}" />
<TextBlock Text="{Binding SelectedSourceProperty.PropertyDescription.Name}"
Visibility="{Binding SelectedSourceProperty, Converter={StaticResource NullToVisibilityConverter}}" />
<TextBlock Text="« Select a property »"
FontStyle="Italic"
Visibility="{Binding SelectedLeftSideProperty, Converter={StaticResource NullToVisibilityConverter}, ConverterParameter=Inverted}" />
Visibility="{Binding SelectedSourceProperty, Converter={StaticResource NullToVisibilityConverter}, ConverterParameter=Inverted}" />
</Grid>
</Button>
</StackPanel>
@@ -142,10 +143,10 @@
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Margin="0 2">Input</TextBlock>
<TextBlock Grid.Row="0" Grid.Column="1" Margin="0 2" FontFamily="Consolas">1250</TextBlock>
<TextBlock Grid.Row="0" Grid.Column="1" Margin="0 2" FontFamily="Consolas" Text="{Binding TestInputValue}"/>
<TextBlock Grid.Row="1" Grid.Column="0" Margin="0 2">Output</TextBlock>
<TextBlock Grid.Row="1" Grid.Column="1" Margin="0 2" FontFamily="Consolas">909</TextBlock>
<TextBlock Grid.Row="1" Grid.Column="1" Margin="0 2" FontFamily="Consolas" Text="{Binding TestResultValue}"/>
</Grid>
</Grid>
</materialDesign:Card>
@@ -167,7 +168,8 @@
FontSize="12"
Padding="6 4"
Height="22"
IsEnabled="{Binding IsDataBindingEnabled}">
IsEnabled="{Binding IsDataBindingEnabled}"
Command="{s:Action AddModifier}">
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Margin="0 -1 4 0" Kind="Plus" />
<TextBlock>
@@ -0,0 +1,26 @@
using System.Windows;
using System.Windows.Controls;
namespace Artemis.UI.Screens.ProfileEditor.LayerProperties.DataBindings
{
/// <summary>
/// Interaction logic for DataBindingView.xaml
/// </summary>
public partial class DataBindingView : UserControl
{
public DataBindingView()
{
InitializeComponent();
}
private void PropertyButton_OnClick(object sender, RoutedEventArgs e)
{
// DataContext is not set when using left button, I don't know why but there it is
if (sender is Button button && button.ContextMenu != null)
{
button.ContextMenu.DataContext = button.DataContext;
button.ContextMenu.IsOpen = true;
}
}
}
}
@@ -6,42 +6,45 @@ using System.Threading.Tasks;
using System.Timers;
using Artemis.Core;
using Artemis.Core.Services;
using Artemis.UI.Ninject.Factories;
using Artemis.UI.Shared;
using Artemis.UI.Shared.Services;
using Artemis.UI.Utilities;
using Stylet;
namespace Artemis.UI.Screens.ProfileEditor.LayerProperties.DataBindings
{
public class DataBindingViewModel : PropertyChangedBase
{
private readonly IDataModelService _dataModelService;
private readonly IDataModelUIService _dataModelUIService;
private readonly IDataBindingsVmFactory _dataBindingsVmFactory;
private readonly IProfileEditorService _profileEditorService;
private readonly ISettingsService _settingsService;
private readonly Timer _updateTimer;
private DataBinding _dataBinding;
private bool _isDataBindingEnabled;
private DataModelPropertiesViewModel _sourceDataModel;
private DataModelVisualizationViewModel _selectedSourceProperty;
private bool _sourceDataModelOpen;
private object _testInputValue;
private object _testResultValue;
public DataBindingViewModel(
BaseLayerProperty layerProperty,
public DataBindingViewModel(BaseLayerProperty layerProperty,
PropertyInfo targetProperty,
IProfileEditorService profileEditorService,
IDataModelUIService dataModelUIService,
IDataModelService dataModelService,
ISettingsService settingsService)
ISettingsService settingsService,
IDataBindingsVmFactory dataBindingsVmFactory)
{
_profileEditorService = profileEditorService;
_dataModelUIService = dataModelUIService;
_dataModelService = dataModelService;
_settingsService = settingsService;
_dataBindingsVmFactory = dataBindingsVmFactory;
_updateTimer = new Timer(500);
LayerProperty = layerProperty;
TargetProperty = targetProperty;
DisplayName = TargetProperty.Name.ToUpper();
SelectSourcePropertyCommand = new DelegateCommand(ExecuteSelectSourceProperty);
ModifierViewModels = new BindableCollection<DataBindingModifierViewModel>();
DataBinding = layerProperty.DataBindings.FirstOrDefault(d => d.TargetProperty == targetProperty);
@@ -53,14 +56,18 @@ namespace Artemis.UI.Screens.ProfileEditor.LayerProperties.DataBindings
Task.Run(Initialize);
}
public bool SourceDataModelOpen { get; set; }
public DataModelPropertiesViewModel SourceDataModel
{
get => _sourceDataModel;
set => SetAndNotify(ref _sourceDataModel, value);
}
public bool SourceDataModelOpen
{
get => _sourceDataModelOpen;
set => SetAndNotify(ref _sourceDataModelOpen, value);
}
public DataModelVisualizationViewModel SelectedSourceProperty
{
get => _selectedSourceProperty;
@@ -73,6 +80,8 @@ namespace Artemis.UI.Screens.ProfileEditor.LayerProperties.DataBindings
public string DisplayName { get; }
public BindableCollection<DataBindingModifierViewModel> ModifierViewModels { get; }
public DelegateCommand SelectSourcePropertyCommand { get; }
public bool IsDataBindingEnabled
{
get => _isDataBindingEnabled;
@@ -122,7 +131,7 @@ namespace Artemis.UI.Screens.ProfileEditor.LayerProperties.DataBindings
var modifier = new DataBindingModifier(ProfileRightSideType.Dynamic);
DataBinding.AddModifier(modifier);
ModifierViewModels.Add(new DataBindingModifierViewModel(modifier));
ModifierViewModels.Add(_dataBindingsVmFactory.DataBindingModifierViewModel(modifier));
}
private void Initialize()
@@ -134,10 +143,24 @@ namespace Artemis.UI.Screens.ProfileEditor.LayerProperties.DataBindings
SourceDataModel.UpdateRequested += SourceDataModelOnUpdateRequested;
Update();
_updateTimer.Start();
_updateTimer.Elapsed += OnUpdateTimerOnElapsed;
}
private void Update()
{
if (DataBinding == null || SourceDataModel == null)
return;
// Determine the source property
if (DataBinding.SourceDataModel == null)
SelectedSourceProperty = null;
else
SelectedSourceProperty = SourceDataModel.GetChildByPath(DataBinding.SourceDataModel.PluginInfo.Guid, DataBinding.SourcePropertyPath);
}
private void SourceDataModelOnUpdateRequested(object sender, EventArgs e)
{
SourceDataModel.ApplyTypeFilter(true, DataBinding.TargetProperty.PropertyType);
@@ -145,10 +168,33 @@ namespace Artemis.UI.Screens.ProfileEditor.LayerProperties.DataBindings
private void OnUpdateTimerOnElapsed(object sender, ElapsedEventArgs e)
{
UpdateTestResult();
if (SourceDataModelOpen)
SourceDataModel.Update(_dataModelUIService);
}
private void UpdateTestResult()
{
var currentValue = SelectedSourceProperty?.GetCurrentValue();
if (currentValue == null && TargetProperty.PropertyType.IsValueType)
currentValue = Activator.CreateInstance(TargetProperty.PropertyType);
TestInputValue = Convert.ChangeType(currentValue, TargetProperty.PropertyType);
TestResultValue = DataBinding?.GetValue(TestInputValue);
}
public object TestInputValue
{
get => _testInputValue;
set => SetAndNotify(ref _testInputValue, value);
}
public object TestResultValue
{
get => _testResultValue;
set => SetAndNotify(ref _testResultValue, value);
}
private void UpdateModifierViewModels()
{
ModifierViewModels.Clear();
@@ -156,7 +202,18 @@ namespace Artemis.UI.Screens.ProfileEditor.LayerProperties.DataBindings
return;
foreach (var dataBindingModifier in DataBinding.Modifiers)
ModifierViewModels.Add(new DataBindingModifierViewModel(dataBindingModifier));
ModifierViewModels.Add(_dataBindingsVmFactory.DataBindingModifierViewModel(dataBindingModifier));
}
private void ExecuteSelectSourceProperty(object context)
{
if (!(context is DataModelVisualizationViewModel selected))
return;
DataBinding.UpdateSource(selected.DataModel, selected.PropertyPath);
_profileEditorService.UpdateSelectedProfileElement();
Update();
}
}
}
@@ -1,16 +1,19 @@
using System.Linq;
using Artemis.Core;
using Artemis.UI.Ninject.Factories;
using Stylet;
namespace Artemis.UI.Screens.ProfileEditor.LayerProperties.DataBindings
{
public class DataBindingsViewModel : PropertyChangedBase
{
private readonly IDataBindingsVmFactory _dataBindingsVmFactory;
private DataBindingsTabsViewModel _dataBindingsTabsViewModel;
private DataBindingViewModel _dataBindingViewModel;
public DataBindingsViewModel(BaseLayerProperty layerProperty)
public DataBindingsViewModel(BaseLayerProperty layerProperty, IDataBindingsVmFactory dataBindingsVmFactory)
{
_dataBindingsVmFactory = dataBindingsVmFactory;
LayerProperty = layerProperty;
Initialise();
}
@@ -41,12 +44,12 @@ namespace Artemis.UI.Screens.ProfileEditor.LayerProperties.DataBindings
// Create a data binding VM for each data bindable property. These VMs will be responsible for retrieving
// and creating the actual data bindings
if (properties.Count == 1)
DataBindingViewModel = new DataBindingViewModel(LayerProperty, properties.First());
DataBindingViewModel = _dataBindingsVmFactory.DataBindingViewModel(LayerProperty, properties.First());
else
{
DataBindingsTabsViewModel = new DataBindingsTabsViewModel();
foreach (var dataBindingProperty in properties)
DataBindingsTabsViewModel.Tabs.Add(new DataBindingViewModel(LayerProperty, dataBindingProperty));
DataBindingsTabsViewModel.Tabs.Add(_dataBindingsVmFactory.DataBindingViewModel(LayerProperty, dataBindingProperty));
}
}
}
@@ -23,6 +23,7 @@ namespace Artemis.UI.Screens.ProfileEditor.LayerProperties
public class LayerPropertiesViewModel : ProfileEditorPanelViewModel, IDropTarget
{
private readonly ILayerPropertyVmFactory _layerPropertyVmFactory;
private readonly IDataBindingsVmFactory _dataBindingsVmFactory;
private LayerPropertyGroupViewModel _brushPropertyGroup;
private DataBindingsViewModel _dataBindingsViewModel;
private EffectsViewModel _effectsViewModel;
@@ -38,10 +39,14 @@ namespace Artemis.UI.Screens.ProfileEditor.LayerProperties
private TimelineViewModel _timelineViewModel;
private TreeViewModel _treeViewModel;
public LayerPropertiesViewModel(IProfileEditorService profileEditorService, ICoreService coreService, ISettingsService settingsService,
ILayerPropertyVmFactory layerPropertyVmFactory)
public LayerPropertiesViewModel(IProfileEditorService profileEditorService,
ICoreService coreService,
ISettingsService settingsService,
ILayerPropertyVmFactory layerPropertyVmFactory,
IDataBindingsVmFactory dataBindingsVmFactory)
{
_layerPropertyVmFactory = layerPropertyVmFactory;
_dataBindingsVmFactory = dataBindingsVmFactory;
ProfileEditorService = profileEditorService;
CoreService = coreService;
@@ -236,7 +241,7 @@ namespace Artemis.UI.Screens.ProfileEditor.LayerProperties
if (ProfileEditorService.SelectedDataBinding != null)
{
RightSideIndex = 1;
DataBindingsViewModel = new DataBindingsViewModel(ProfileEditorService.SelectedDataBinding);
DataBindingsViewModel = _dataBindingsVmFactory.DataBindingsViewModel(ProfileEditorService.SelectedDataBinding);
}
else
RightSideIndex = 0;