1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-31 17:53:32 +00:00

Data bindings - Fixed some conversion issues

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

View File

@ -0,0 +1,14 @@
using System;
namespace Artemis.Core
{
public class LayerPropertyGroupUpdatingEventArgs : EventArgs
{
public LayerPropertyGroupUpdatingEventArgs(double deltaTime)
{
DeltaTime = deltaTime;
}
public double DeltaTime { get; }
}
}

View File

@ -132,11 +132,13 @@ namespace Artemis.Core
/// <returns></returns> /// <returns></returns>
public object GetValue(object baseValue) public object GetValue(object baseValue)
{ {
if (baseValue.GetType() != TargetProperty.PropertyType) // Validating this is kinda expensive, it'll fail on ChangeType later anyway ^^
{ // var targetType = TargetProperty.PropertyType;
throw new ArtemisCoreException($"The provided current value type ({baseValue.GetType().Name}) not match the " + // if (!targetType.IsCastableFrom(baseValue.GetType()))
$"target property type ({TargetProperty.PropertyType.Name})"); // {
} // throw new ArtemisCoreException($"The provided current value type ({baseValue.GetType().Name}) not match the " +
// $"target property type ({targetType.Name})");
// }
if (CompiledTargetAccessor == null) if (CompiledTargetAccessor == null)
return baseValue; return baseValue;
@ -145,7 +147,11 @@ namespace Artemis.Core
foreach (var dataBindingModifier in Modifiers) foreach (var dataBindingModifier in Modifiers)
dataBindingValue = dataBindingModifier.Apply(dataBindingValue); dataBindingValue = dataBindingModifier.Apply(dataBindingValue);
return dataBindingValue; return Convert.ChangeType(dataBindingValue, TargetProperty.PropertyType);
}
internal void Update(double deltaTime)
{
} }
internal void ApplyToEntity() internal void ApplyToEntity()
@ -210,20 +216,32 @@ namespace Artemis.Core
if (listType != null) if (listType != null)
throw new ArtemisCoreException($"Cannot create a regular accessor at path {SourcePropertyPath} because the path contains a list"); throw new ArtemisCoreException($"Cannot create a regular accessor at path {SourcePropertyPath} because the path contains a list");
var parameter = Expression.Parameter(typeof(object), "targetDataModel"); var parameter = Expression.Parameter(typeof(DataModel), "targetDataModel");
var accessor = SourcePropertyPath.Split('.').Aggregate<string, Expression>( var accessor = SourcePropertyPath.Split('.').Aggregate<string, Expression>(
Expression.Convert(parameter, SourceDataModel.GetType()), // Cast to the appropriate type Expression.Convert(parameter, SourceDataModel.GetType()), // Cast to the appropriate type
Expression.Property Expression.Property
); );
var lambda = Expression.Lambda<Func<DataModel, object>>(accessor); var returnValue = Expression.Convert(accessor, typeof(object));
var lambda = Expression.Lambda<Func<DataModel, object>>(returnValue, parameter);
CompiledTargetAccessor = lambda.Compile(); CompiledTargetAccessor = lambda.Compile();
} }
} }
/// <summary>
/// A mode that determines how the data binding is applied to the layer property
/// </summary>
public enum DataBindingMode public enum DataBindingMode
{ {
Override, /// <summary>
/// Replaces the layer property value with the data binding value
/// </summary>
Replace,
/// <summary>
/// Adds the data binding value to the layer property value
/// </summary>
Add Add
} }
} }

View File

@ -100,13 +100,6 @@ namespace Artemis.Core
/// <returns>The modified value</returns> /// <returns>The modified value</returns>
public object Apply(object currentValue) public object Apply(object currentValue)
{ {
var targetType = DataBinding.TargetProperty.GetType();
if (currentValue.GetType() != targetType)
{
throw new ArtemisCoreException("The current value of the data binding does not match the type of the target property." +
$" {targetType.Name} expected, received {currentValue.GetType().Name}.");
}
if (CompiledDynamicPredicate != null) if (CompiledDynamicPredicate != null)
return CompiledDynamicPredicate(currentValue, ParameterDataModel); return CompiledDynamicPredicate(currentValue, ParameterDataModel);
if (CompiledStaticPredicate != null) if (CompiledStaticPredicate != null)

View File

@ -20,11 +20,6 @@ namespace Artemis.Core
/// </summary> /// </summary>
public PluginInfo PluginInfo { get; internal set; } public PluginInfo PluginInfo { get; internal set; }
/// <summary>
/// Gets the data binding modifier this modifier type is applied to
/// </summary>
public DataBindingModifier Modifier { get; internal set; }
/// <summary> /// <summary>
/// Gets the types this modifier supports /// Gets the types this modifier supports
/// </summary> /// </summary>

View File

@ -79,7 +79,7 @@ namespace Artemis.Core
foreach (var baseLayerEffect in LayerEffects.Where(e => e.Enabled)) foreach (var baseLayerEffect in LayerEffects.Where(e => e.Enabled))
{ {
baseLayerEffect.BaseProperties?.Update(); baseLayerEffect.BaseProperties?.Update(deltaTime);
baseLayerEffect.Update(deltaTime); baseLayerEffect.Update(deltaTime);
} }
@ -125,7 +125,7 @@ namespace Artemis.Core
foreach (var baseLayerEffect in LayerEffects.Where(e => e.Enabled)) foreach (var baseLayerEffect in LayerEffects.Where(e => e.Enabled))
{ {
baseLayerEffect.BaseProperties?.Update(); baseLayerEffect.BaseProperties?.Update(delta);
baseLayerEffect.Update(delta); baseLayerEffect.Update(delta);
} }
} }

View File

@ -261,14 +261,14 @@ namespace Artemis.Core
if (TimelinePosition > TimelineLength) if (TimelinePosition > TimelineLength)
return; return;
General.Update(); General.Update(deltaTime);
Transform.Update(); Transform.Update(deltaTime);
LayerBrush.BaseProperties?.Update(); LayerBrush.BaseProperties?.Update(deltaTime);
LayerBrush.Update(deltaTime); LayerBrush.Update(deltaTime);
foreach (var baseLayerEffect in LayerEffects.Where(e => e.Enabled)) foreach (var baseLayerEffect in LayerEffects.Where(e => e.Enabled))
{ {
baseLayerEffect.BaseProperties?.Update(); baseLayerEffect.BaseProperties?.Update(deltaTime);
baseLayerEffect.Update(deltaTime); baseLayerEffect.Update(deltaTime);
} }
} }
@ -301,14 +301,14 @@ namespace Artemis.Core
var delta = (TimelinePosition - beginTime).TotalSeconds; var delta = (TimelinePosition - beginTime).TotalSeconds;
General.Update(); General.Update(delta);
Transform.Update(); Transform.Update(delta);
LayerBrush.BaseProperties?.Update(); LayerBrush.BaseProperties?.Update(delta);
LayerBrush.Update(delta); LayerBrush.Update(delta);
foreach (var baseLayerEffect in LayerEffects.Where(e => e.Enabled)) foreach (var baseLayerEffect in LayerEffects.Where(e => e.Enabled))
{ {
baseLayerEffect.BaseProperties?.Update(); baseLayerEffect.BaseProperties?.Update(delta);
baseLayerEffect.Update(delta); baseLayerEffect.Update(delta);
} }
} }

View File

@ -19,7 +19,6 @@ namespace Artemis.Core
public class LayerProperty<T> : BaseLayerProperty public class LayerProperty<T> : BaseLayerProperty
{ {
private T _baseValue; private T _baseValue;
private T _currentValue;
private bool _isInitialized; private bool _isInitialized;
private List<LayerPropertyKeyframe<T>> _keyframes; private List<LayerPropertyKeyframe<T>> _keyframes;
@ -47,11 +46,7 @@ namespace Artemis.Core
/// <summary> /// <summary>
/// Gets the current value of this property as it is affected by it's keyframes, updated once every frame /// Gets the current value of this property as it is affected by it's keyframes, updated once every frame
/// </summary> /// </summary>
public T CurrentValue public T CurrentValue { get; set; }
{
get => !KeyframesEnabled || !KeyframesSupported ? BaseValue : _currentValue;
internal set => _currentValue = value;
}
/// <summary> /// <summary>
/// Gets or sets the default value of this layer property. If set, this value is automatically applied if the property /// Gets or sets the default value of this layer property. If set, this value is automatically applied if the property
@ -100,7 +95,7 @@ namespace Artemis.Core
currentKeyframe.Value = value; currentKeyframe.Value = value;
// Update the property so that the new keyframe is reflected on the current value // Update the property so that the new keyframe is reflected on the current value
Update(); Update(0);
} }
} }
@ -190,9 +185,19 @@ namespace Artemis.Core
} }
/// <summary> /// <summary>
/// Updates the property, applying keyframes to the current value /// Updates the property, applying keyframes and data bindings to the current value
/// </summary> /// </summary>
internal void Update() internal void Update(double deltaTime)
{
CurrentValue = BaseValue;
UpdateKeyframes();
UpdateDataBindings(deltaTime);
OnUpdated();
}
private void UpdateKeyframes()
{ {
if (!KeyframesSupported || !KeyframesEnabled) if (!KeyframesSupported || !KeyframesEnabled)
return; return;
@ -216,8 +221,15 @@ namespace Artemis.Core
var keyframeProgressEased = (float) Easings.Interpolate(keyframeProgress, CurrentKeyframe.EasingFunction); var keyframeProgressEased = (float) Easings.Interpolate(keyframeProgress, CurrentKeyframe.EasingFunction);
UpdateCurrentValue(keyframeProgress, keyframeProgressEased); UpdateCurrentValue(keyframeProgress, keyframeProgressEased);
} }
}
OnUpdated(); private void UpdateDataBindings(double deltaTime)
{
foreach (var dataBinding in DataBindings)
{
dataBinding.Update(deltaTime);
ApplyDataBinding(dataBinding);
}
} }
/// <summary> /// <summary>
@ -237,7 +249,7 @@ namespace Artemis.Core
PropertyEntity = entity; PropertyEntity = entity;
LayerPropertyGroup = layerPropertyGroup; LayerPropertyGroup = layerPropertyGroup;
LayerPropertyGroup.PropertyGroupUpdating += (sender, args) => Update(); LayerPropertyGroup.PropertyGroupUpdating += (sender, args) => Update(args.DeltaTime);
try try
{ {
@ -303,9 +315,7 @@ namespace Artemis.Core
/// <inheritdoc /> /// <inheritdoc />
protected override void ApplyDataBinding(DataBinding dataBinding) protected override void ApplyDataBinding(DataBinding dataBinding)
{ {
// The default implementation only supports simple types CurrentValue = (T) dataBinding.GetValue(CurrentValue);
if (dataBinding.TargetProperty.DeclaringType == GetType())
CurrentValue = (T) dataBinding.GetValue(CurrentValue);
} }
#endregion #endregion

View File

@ -227,11 +227,11 @@ namespace Artemis.Core
} }
} }
internal void Update() internal void Update(double deltaTime)
{ {
// Since at this point we don't know what properties the group has without using reflection, // Since at this point we don't know what properties the group has without using reflection,
// let properties subscribe to the update event and update themselves // let properties subscribe to the update event and update themselves
OnPropertyGroupUpdating(); OnPropertyGroupUpdating(new LayerPropertyGroupUpdatingEventArgs(deltaTime));
} }
private void InitializeProperty(RenderProfileElement profileElement, string path, BaseLayerProperty instance) private void InitializeProperty(RenderProfileElement profileElement, string path, BaseLayerProperty instance)
@ -264,7 +264,7 @@ namespace Artemis.Core
#region Events #region Events
internal event EventHandler PropertyGroupUpdating; internal event EventHandler<LayerPropertyGroupUpdatingEventArgs> PropertyGroupUpdating;
/// <summary> /// <summary>
/// Occurs when the property group has initialized all its children /// Occurs when the property group has initialized all its children
@ -282,9 +282,9 @@ namespace Artemis.Core
/// </summary> /// </summary>
public event EventHandler VisibilityChanged; public event EventHandler VisibilityChanged;
protected virtual void OnPropertyGroupUpdating() protected virtual void OnPropertyGroupUpdating(LayerPropertyGroupUpdatingEventArgs e)
{ {
PropertyGroupUpdating?.Invoke(this, EventArgs.Empty); PropertyGroupUpdating?.Invoke(this, e);
} }
protected virtual void OnVisibilityChanged() protected virtual void OnVisibilityChanged()

View File

@ -1,4 +1,5 @@
using Artemis.Core; using System.Reflection;
using Artemis.Core;
using Artemis.Core.Modules; using Artemis.Core.Modules;
using Artemis.UI.Screens.Modules; using Artemis.UI.Screens.Modules;
using Artemis.UI.Screens.Modules.Tabs; using Artemis.UI.Screens.Modules.Tabs;
@ -7,6 +8,7 @@ using Artemis.UI.Screens.ProfileEditor.DisplayConditions;
using Artemis.UI.Screens.ProfileEditor.DisplayConditions.Abstract; using Artemis.UI.Screens.ProfileEditor.DisplayConditions.Abstract;
using Artemis.UI.Screens.ProfileEditor.LayerProperties; using Artemis.UI.Screens.ProfileEditor.LayerProperties;
using Artemis.UI.Screens.ProfileEditor.LayerProperties.Abstract; using Artemis.UI.Screens.ProfileEditor.LayerProperties.Abstract;
using Artemis.UI.Screens.ProfileEditor.LayerProperties.DataBindings;
using Artemis.UI.Screens.ProfileEditor.LayerProperties.LayerEffects; using Artemis.UI.Screens.ProfileEditor.LayerProperties.LayerEffects;
using Artemis.UI.Screens.ProfileEditor.LayerProperties.Timeline; using Artemis.UI.Screens.ProfileEditor.LayerProperties.Timeline;
using Artemis.UI.Screens.ProfileEditor.LayerProperties.Tree; using Artemis.UI.Screens.ProfileEditor.LayerProperties.Tree;
@ -17,6 +19,7 @@ using Artemis.UI.Screens.Settings.Debug;
using Artemis.UI.Screens.Settings.Tabs.Devices; using Artemis.UI.Screens.Settings.Tabs.Devices;
using Artemis.UI.Screens.Settings.Tabs.Plugins; using Artemis.UI.Screens.Settings.Tabs.Plugins;
using Stylet; using Stylet;
using Module = Artemis.Core.Modules.Module;
namespace Artemis.UI.Ninject.Factories namespace Artemis.UI.Ninject.Factories
{ {
@ -75,6 +78,13 @@ namespace Artemis.UI.Ninject.Factories
DisplayConditionListPredicateViewModel DisplayConditionListPredicateViewModel(DisplayConditionListPredicate displayConditionListPredicate, DisplayConditionViewModel parent); DisplayConditionListPredicateViewModel DisplayConditionListPredicateViewModel(DisplayConditionListPredicate displayConditionListPredicate, DisplayConditionViewModel parent);
} }
public interface IDataBindingsVmFactory : IVmFactory
{
DataBindingsViewModel DataBindingsViewModel(BaseLayerProperty layerProperty);
DataBindingViewModel DataBindingViewModel(BaseLayerProperty layerProperty, PropertyInfo targetProperty);
DataBindingModifierViewModel DataBindingModifierViewModel(DataBindingModifier modifier);
}
public interface ILayerPropertyVmFactory : IVmFactory public interface ILayerPropertyVmFactory : IVmFactory
{ {
LayerPropertyGroupViewModel LayerPropertyGroupViewModel(LayerPropertyGroup layerPropertyGroup, PropertyGroupDescriptionAttribute propertyGroupDescription); LayerPropertyGroupViewModel LayerPropertyGroupViewModel(LayerPropertyGroup layerPropertyGroup, PropertyGroupDescriptionAttribute propertyGroupDescription);

View File

@ -6,9 +6,30 @@
xmlns:local="clr-namespace:Artemis.UI.Screens.ProfileEditor.LayerProperties.DataBindings" xmlns:local="clr-namespace:Artemis.UI.Screens.ProfileEditor.LayerProperties.DataBindings"
xmlns:s="https://github.com/canton7/Stylet" xmlns:s="https://github.com/canton7/Stylet"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" 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" mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800" d:DesignHeight="450" d:DesignWidth="800"
d:DataContext="{d:DesignInstance local:DataBindingModifierViewModel}"> 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>
<Grid.ColumnDefinitions> <Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" />
@ -41,10 +62,10 @@
Style="{StaticResource DisplayConditionButtonLeftClickMenu}" Style="{StaticResource DisplayConditionButtonLeftClickMenu}"
Background="#7B7B7B" Background="#7B7B7B"
BorderBrush="#7B7B7B" BorderBrush="#7B7B7B"
Content="{Binding SelectedOperator.Description}"> Content="{Binding SelectedModifierType.Description}">
<!-- Click="PropertyButton_OnClick" --> <!-- Click="PropertyButton_OnClick" -->
<Button.ContextMenu> <Button.ContextMenu>
<ContextMenu ItemsSource="{Binding Operators}"> <ContextMenu ItemsSource="{Binding ModifierTypes}">
<ContextMenu.ItemTemplate> <ContextMenu.ItemTemplate>
<DataTemplate> <DataTemplate>
<StackPanel Orientation="Horizontal"> <StackPanel Orientation="Horizontal">
@ -55,7 +76,7 @@
</ContextMenu.ItemTemplate> </ContextMenu.ItemTemplate>
<ContextMenu.ItemContainerStyle> <ContextMenu.ItemContainerStyle>
<Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource MaterialDesignMenuItem}"> <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="CommandParameter" Value="{Binding}" />
<Setter Property="CommandTarget" Value="{Binding}" /> <Setter Property="CommandTarget" Value="{Binding}" />
</Style> </Style>
@ -63,18 +84,19 @@
</ContextMenu> </ContextMenu>
</Button.ContextMenu> </Button.ContextMenu>
</Button> </Button>
<Button Grid.Column="3" <Button Grid.Column="3"
Background="#ab47bc" Background="#ab47bc"
BorderBrush="#ab47bc" BorderBrush="#ab47bc"
Style="{StaticResource DisplayConditionButton}" Style="{StaticResource DisplayConditionButton}"
ToolTip="{Binding SelectedLeftSideProperty.DisplayPropertyPath}" ToolTip="{Binding SelectedParameterProperty.DisplayPropertyPath}"
HorizontalAlignment="Left"> HorizontalAlignment="Left">
<Button.ContextMenu> <Button.ContextMenu>
<ContextMenu ItemsSource="{Binding LeftSideDataModel.Children}" IsOpen="{Binding LeftSideDataModelOpen, Mode=OneWayToSource}"> <ContextMenu ItemsSource="{Binding ParameterDataModel.Children}" IsOpen="{Binding ParameterDataModelOpen, Mode=OneWayToSource}">
<ContextMenu.ItemContainerStyle> <ContextMenu.ItemContainerStyle>
<Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource MaterialDesignMenuItem}"> <Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource MaterialDesignMenuItem}">
<Setter Property="ItemsSource" Value="{Binding Children}" /> <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="CommandParameter" Value="{Binding}" />
<Setter Property="CommandTarget" Value="{Binding}" /> <Setter Property="CommandTarget" Value="{Binding}" />
<Setter Property="IsEnabled" Value="{Binding IsMatchingFilteredTypes}" /> <Setter Property="IsEnabled" Value="{Binding IsMatchingFilteredTypes}" />
@ -85,11 +107,11 @@
</ContextMenu> </ContextMenu>
</Button.ContextMenu> </Button.ContextMenu>
<Grid> <Grid>
<TextBlock Text="{Binding SelectedLeftSideProperty.PropertyDescription.Name}" <TextBlock Text="{Binding SelectedParameterProperty.PropertyDescription.Name}"
Visibility="{Binding SelectedLeftSideProperty, Converter={StaticResource NullToVisibilityConverter}}" /> Visibility="{Binding SelectedParameterProperty, Converter={StaticResource NullToVisibilityConverter}}" />
<TextBlock Text="« Select a property »" <TextBlock Text="« Select a property »"
FontStyle="Italic" FontStyle="Italic"
Visibility="{Binding SelectedLeftSideProperty, Converter={StaticResource NullToVisibilityConverter}, ConverterParameter=Inverted}" /> Visibility="{Binding SelectedParameterProperty, Converter={StaticResource NullToVisibilityConverter}, ConverterParameter=Inverted}" />
</Grid> </Grid>
</Button> </Button>
</Grid> </Grid>

View File

@ -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; using Stylet;
namespace Artemis.UI.Screens.ProfileEditor.LayerProperties.DataBindings namespace Artemis.UI.Screens.ProfileEditor.LayerProperties.DataBindings
{ {
public class DataBindingModifierViewModel : PropertyChangedBase 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; 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 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);
}
} }
} }

View File

@ -52,15 +52,16 @@
<Button Background="#ab47bc" <Button Background="#ab47bc"
BorderBrush="#ab47bc" BorderBrush="#ab47bc"
Style="{StaticResource DisplayConditionButton}" Style="{StaticResource DisplayConditionButton}"
ToolTip="{Binding SelectedLeftSideProperty.DisplayPropertyPath}" ToolTip="{Binding SelectedSourceProperty.DisplayPropertyPath}"
IsEnabled="{Binding IsDataBindingEnabled}" IsEnabled="{Binding IsDataBindingEnabled}"
HorizontalAlignment="Left"> HorizontalAlignment="Left"
Click="PropertyButton_OnClick">
<Button.ContextMenu> <Button.ContextMenu>
<ContextMenu ItemsSource="{Binding LeftSideDataModel.Children}" IsOpen="{Binding LeftSideDataModelOpen, Mode=OneWayToSource}"> <ContextMenu ItemsSource="{Binding SourceDataModel.Children}" IsOpen="{Binding SourceDataModelOpen, Mode=OneWayToSource}">
<ContextMenu.ItemContainerStyle> <ContextMenu.ItemContainerStyle>
<Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource MaterialDesignMenuItem}"> <Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource MaterialDesignMenuItem}">
<Setter Property="ItemsSource" Value="{Binding Children}" /> <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="CommandParameter" Value="{Binding}" />
<Setter Property="CommandTarget" Value="{Binding}" /> <Setter Property="CommandTarget" Value="{Binding}" />
<Setter Property="IsEnabled" Value="{Binding IsMatchingFilteredTypes}" /> <Setter Property="IsEnabled" Value="{Binding IsMatchingFilteredTypes}" />
@ -71,11 +72,11 @@
</ContextMenu> </ContextMenu>
</Button.ContextMenu> </Button.ContextMenu>
<Grid> <Grid>
<TextBlock Text="{Binding SelectedLeftSideProperty.PropertyDescription.Name}" <TextBlock Text="{Binding SelectedSourceProperty.PropertyDescription.Name}"
Visibility="{Binding SelectedLeftSideProperty, Converter={StaticResource NullToVisibilityConverter}}" /> Visibility="{Binding SelectedSourceProperty, Converter={StaticResource NullToVisibilityConverter}}" />
<TextBlock Text="« Select a property »" <TextBlock Text="« Select a property »"
FontStyle="Italic" FontStyle="Italic"
Visibility="{Binding SelectedLeftSideProperty, Converter={StaticResource NullToVisibilityConverter}, ConverterParameter=Inverted}" /> Visibility="{Binding SelectedSourceProperty, Converter={StaticResource NullToVisibilityConverter}, ConverterParameter=Inverted}" />
</Grid> </Grid>
</Button> </Button>
</StackPanel> </StackPanel>
@ -142,10 +143,10 @@
<RowDefinition /> <RowDefinition />
</Grid.RowDefinitions> </Grid.RowDefinitions>
<TextBlock Margin="0 2">Input</TextBlock> <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="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>
</Grid> </Grid>
</materialDesign:Card> </materialDesign:Card>
@ -167,7 +168,8 @@
FontSize="12" FontSize="12"
Padding="6 4" Padding="6 4"
Height="22" Height="22"
IsEnabled="{Binding IsDataBindingEnabled}"> IsEnabled="{Binding IsDataBindingEnabled}"
Command="{s:Action AddModifier}">
<StackPanel Orientation="Horizontal"> <StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Margin="0 -1 4 0" Kind="Plus" /> <materialDesign:PackIcon Margin="0 -1 4 0" Kind="Plus" />
<TextBlock> <TextBlock>

View File

@ -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;
}
}
}
}

View File

@ -6,42 +6,45 @@ using System.Threading.Tasks;
using System.Timers; using System.Timers;
using Artemis.Core; using Artemis.Core;
using Artemis.Core.Services; using Artemis.Core.Services;
using Artemis.UI.Ninject.Factories;
using Artemis.UI.Shared; using Artemis.UI.Shared;
using Artemis.UI.Shared.Services; using Artemis.UI.Shared.Services;
using Artemis.UI.Utilities;
using Stylet; using Stylet;
namespace Artemis.UI.Screens.ProfileEditor.LayerProperties.DataBindings namespace Artemis.UI.Screens.ProfileEditor.LayerProperties.DataBindings
{ {
public class DataBindingViewModel : PropertyChangedBase public class DataBindingViewModel : PropertyChangedBase
{ {
private readonly IDataModelService _dataModelService;
private readonly IDataModelUIService _dataModelUIService; private readonly IDataModelUIService _dataModelUIService;
private readonly IDataBindingsVmFactory _dataBindingsVmFactory;
private readonly IProfileEditorService _profileEditorService; private readonly IProfileEditorService _profileEditorService;
private readonly ISettingsService _settingsService;
private readonly Timer _updateTimer; private readonly Timer _updateTimer;
private DataBinding _dataBinding; private DataBinding _dataBinding;
private bool _isDataBindingEnabled; private bool _isDataBindingEnabled;
private DataModelPropertiesViewModel _sourceDataModel; private DataModelPropertiesViewModel _sourceDataModel;
private DataModelVisualizationViewModel _selectedSourceProperty; private DataModelVisualizationViewModel _selectedSourceProperty;
private bool _sourceDataModelOpen;
private object _testInputValue;
private object _testResultValue;
public DataBindingViewModel( public DataBindingViewModel(BaseLayerProperty layerProperty,
BaseLayerProperty layerProperty,
PropertyInfo targetProperty, PropertyInfo targetProperty,
IProfileEditorService profileEditorService, IProfileEditorService profileEditorService,
IDataModelUIService dataModelUIService, IDataModelUIService dataModelUIService,
IDataModelService dataModelService, ISettingsService settingsService,
ISettingsService settingsService) IDataBindingsVmFactory dataBindingsVmFactory)
{ {
_profileEditorService = profileEditorService; _profileEditorService = profileEditorService;
_dataModelUIService = dataModelUIService; _dataModelUIService = dataModelUIService;
_dataModelService = dataModelService; _dataBindingsVmFactory = dataBindingsVmFactory;
_settingsService = settingsService;
_updateTimer = new Timer(500); _updateTimer = new Timer(500);
LayerProperty = layerProperty; LayerProperty = layerProperty;
TargetProperty = targetProperty; TargetProperty = targetProperty;
DisplayName = TargetProperty.Name.ToUpper(); DisplayName = TargetProperty.Name.ToUpper();
SelectSourcePropertyCommand = new DelegateCommand(ExecuteSelectSourceProperty);
ModifierViewModels = new BindableCollection<DataBindingModifierViewModel>(); ModifierViewModels = new BindableCollection<DataBindingModifierViewModel>();
DataBinding = layerProperty.DataBindings.FirstOrDefault(d => d.TargetProperty == targetProperty); DataBinding = layerProperty.DataBindings.FirstOrDefault(d => d.TargetProperty == targetProperty);
@ -53,14 +56,18 @@ namespace Artemis.UI.Screens.ProfileEditor.LayerProperties.DataBindings
Task.Run(Initialize); Task.Run(Initialize);
} }
public bool SourceDataModelOpen { get; set; }
public DataModelPropertiesViewModel SourceDataModel public DataModelPropertiesViewModel SourceDataModel
{ {
get => _sourceDataModel; get => _sourceDataModel;
set => SetAndNotify(ref _sourceDataModel, value); set => SetAndNotify(ref _sourceDataModel, value);
} }
public bool SourceDataModelOpen
{
get => _sourceDataModelOpen;
set => SetAndNotify(ref _sourceDataModelOpen, value);
}
public DataModelVisualizationViewModel SelectedSourceProperty public DataModelVisualizationViewModel SelectedSourceProperty
{ {
get => _selectedSourceProperty; get => _selectedSourceProperty;
@ -73,6 +80,8 @@ namespace Artemis.UI.Screens.ProfileEditor.LayerProperties.DataBindings
public string DisplayName { get; } public string DisplayName { get; }
public BindableCollection<DataBindingModifierViewModel> ModifierViewModels { get; } public BindableCollection<DataBindingModifierViewModel> ModifierViewModels { get; }
public DelegateCommand SelectSourcePropertyCommand { get; }
public bool IsDataBindingEnabled public bool IsDataBindingEnabled
{ {
get => _isDataBindingEnabled; get => _isDataBindingEnabled;
@ -122,7 +131,7 @@ namespace Artemis.UI.Screens.ProfileEditor.LayerProperties.DataBindings
var modifier = new DataBindingModifier(ProfileRightSideType.Dynamic); var modifier = new DataBindingModifier(ProfileRightSideType.Dynamic);
DataBinding.AddModifier(modifier); DataBinding.AddModifier(modifier);
ModifierViewModels.Add(new DataBindingModifierViewModel(modifier)); ModifierViewModels.Add(_dataBindingsVmFactory.DataBindingModifierViewModel(modifier));
} }
private void Initialize() private void Initialize()
@ -134,10 +143,24 @@ namespace Artemis.UI.Screens.ProfileEditor.LayerProperties.DataBindings
SourceDataModel.UpdateRequested += SourceDataModelOnUpdateRequested; SourceDataModel.UpdateRequested += SourceDataModelOnUpdateRequested;
Update();
_updateTimer.Start(); _updateTimer.Start();
_updateTimer.Elapsed += OnUpdateTimerOnElapsed; _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) private void SourceDataModelOnUpdateRequested(object sender, EventArgs e)
{ {
SourceDataModel.ApplyTypeFilter(true, DataBinding.TargetProperty.PropertyType); SourceDataModel.ApplyTypeFilter(true, DataBinding.TargetProperty.PropertyType);
@ -145,10 +168,33 @@ namespace Artemis.UI.Screens.ProfileEditor.LayerProperties.DataBindings
private void OnUpdateTimerOnElapsed(object sender, ElapsedEventArgs e) private void OnUpdateTimerOnElapsed(object sender, ElapsedEventArgs e)
{ {
UpdateTestResult();
if (SourceDataModelOpen) if (SourceDataModelOpen)
SourceDataModel.Update(_dataModelUIService); 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() private void UpdateModifierViewModels()
{ {
ModifierViewModels.Clear(); ModifierViewModels.Clear();
@ -156,7 +202,18 @@ namespace Artemis.UI.Screens.ProfileEditor.LayerProperties.DataBindings
return; return;
foreach (var dataBindingModifier in DataBinding.Modifiers) 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();
} }
} }
} }

View File

@ -1,16 +1,19 @@
using System.Linq; using System.Linq;
using Artemis.Core; using Artemis.Core;
using Artemis.UI.Ninject.Factories;
using Stylet; using Stylet;
namespace Artemis.UI.Screens.ProfileEditor.LayerProperties.DataBindings namespace Artemis.UI.Screens.ProfileEditor.LayerProperties.DataBindings
{ {
public class DataBindingsViewModel : PropertyChangedBase public class DataBindingsViewModel : PropertyChangedBase
{ {
private readonly IDataBindingsVmFactory _dataBindingsVmFactory;
private DataBindingsTabsViewModel _dataBindingsTabsViewModel; private DataBindingsTabsViewModel _dataBindingsTabsViewModel;
private DataBindingViewModel _dataBindingViewModel; private DataBindingViewModel _dataBindingViewModel;
public DataBindingsViewModel(BaseLayerProperty layerProperty) public DataBindingsViewModel(BaseLayerProperty layerProperty, IDataBindingsVmFactory dataBindingsVmFactory)
{ {
_dataBindingsVmFactory = dataBindingsVmFactory;
LayerProperty = layerProperty; LayerProperty = layerProperty;
Initialise(); 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 // Create a data binding VM for each data bindable property. These VMs will be responsible for retrieving
// and creating the actual data bindings // and creating the actual data bindings
if (properties.Count == 1) if (properties.Count == 1)
DataBindingViewModel = new DataBindingViewModel(LayerProperty, properties.First()); DataBindingViewModel = _dataBindingsVmFactory.DataBindingViewModel(LayerProperty, properties.First());
else else
{ {
DataBindingsTabsViewModel = new DataBindingsTabsViewModel(); DataBindingsTabsViewModel = new DataBindingsTabsViewModel();
foreach (var dataBindingProperty in properties) foreach (var dataBindingProperty in properties)
DataBindingsTabsViewModel.Tabs.Add(new DataBindingViewModel(LayerProperty, dataBindingProperty)); DataBindingsTabsViewModel.Tabs.Add(_dataBindingsVmFactory.DataBindingViewModel(LayerProperty, dataBindingProperty));
} }
} }
} }

View File

@ -23,6 +23,7 @@ namespace Artemis.UI.Screens.ProfileEditor.LayerProperties
public class LayerPropertiesViewModel : ProfileEditorPanelViewModel, IDropTarget public class LayerPropertiesViewModel : ProfileEditorPanelViewModel, IDropTarget
{ {
private readonly ILayerPropertyVmFactory _layerPropertyVmFactory; private readonly ILayerPropertyVmFactory _layerPropertyVmFactory;
private readonly IDataBindingsVmFactory _dataBindingsVmFactory;
private LayerPropertyGroupViewModel _brushPropertyGroup; private LayerPropertyGroupViewModel _brushPropertyGroup;
private DataBindingsViewModel _dataBindingsViewModel; private DataBindingsViewModel _dataBindingsViewModel;
private EffectsViewModel _effectsViewModel; private EffectsViewModel _effectsViewModel;
@ -38,10 +39,14 @@ namespace Artemis.UI.Screens.ProfileEditor.LayerProperties
private TimelineViewModel _timelineViewModel; private TimelineViewModel _timelineViewModel;
private TreeViewModel _treeViewModel; private TreeViewModel _treeViewModel;
public LayerPropertiesViewModel(IProfileEditorService profileEditorService, ICoreService coreService, ISettingsService settingsService, public LayerPropertiesViewModel(IProfileEditorService profileEditorService,
ILayerPropertyVmFactory layerPropertyVmFactory) ICoreService coreService,
ISettingsService settingsService,
ILayerPropertyVmFactory layerPropertyVmFactory,
IDataBindingsVmFactory dataBindingsVmFactory)
{ {
_layerPropertyVmFactory = layerPropertyVmFactory; _layerPropertyVmFactory = layerPropertyVmFactory;
_dataBindingsVmFactory = dataBindingsVmFactory;
ProfileEditorService = profileEditorService; ProfileEditorService = profileEditorService;
CoreService = coreService; CoreService = coreService;
@ -236,7 +241,7 @@ namespace Artemis.UI.Screens.ProfileEditor.LayerProperties
if (ProfileEditorService.SelectedDataBinding != null) if (ProfileEditorService.SelectedDataBinding != null)
{ {
RightSideIndex = 1; RightSideIndex = 1;
DataBindingsViewModel = new DataBindingsViewModel(ProfileEditorService.SelectedDataBinding); DataBindingsViewModel = _dataBindingsVmFactory.DataBindingsViewModel(ProfileEditorService.SelectedDataBinding);
} }
else else
RightSideIndex = 0; RightSideIndex = 0;