1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2026-01-01 10:13:30 +00:00

Conditions - Implemented self referencing list conditions

This commit is contained in:
Robert 2020-10-14 19:04:56 +02:00
parent 879d19e4ea
commit 2946364463
4 changed files with 52 additions and 19 deletions

View File

@ -5,6 +5,7 @@ namespace Artemis.Core
{ {
internal class ListPredicateWrapperDataModel<T> : ListPredicateWrapperDataModel internal class ListPredicateWrapperDataModel<T> : ListPredicateWrapperDataModel
{ {
[DataModelProperty(Name = "List item", Description = "The current item in the list")]
public T Value => (UntypedValue is T typedValue ? typedValue : default)!; public T Value => (UntypedValue is T typedValue ? typedValue : default)!;
} }

View File

@ -61,8 +61,8 @@
HorizontalAlignment="Left" HorizontalAlignment="Left"
Click="PropertyButton_OnClick"> Click="PropertyButton_OnClick">
<Button.ContextMenu> <Button.ContextMenu>
<ContextMenu ItemsSource="{Binding DataModelViewModel.Children}" IsOpen="{Binding IsDataModelViewModelOpen, Mode=OneWayToSource}"> <ContextMenu IsOpen="{Binding IsDataModelViewModelOpen, Mode=OneWayToSource}">
<ContextMenu.ItemContainerStyle> <ContextMenu.Resources>
<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.SelectPropertyCommand, Source={StaticResource DataContextProxy}}" /> <Setter Property="Command" Value="{Binding Data.SelectPropertyCommand, Source={StaticResource DataContextProxy}}" />
@ -72,7 +72,14 @@
<Setter Property="IsSubmenuOpen" Value="{Binding IsVisualizationExpanded, Mode=TwoWay}" /> <Setter Property="IsSubmenuOpen" Value="{Binding IsVisualizationExpanded, Mode=TwoWay}" />
<Setter Property="HeaderTemplate" Value="{StaticResource DataModelDataTemplate}" /> <Setter Property="HeaderTemplate" Value="{StaticResource DataModelDataTemplate}" />
</Style> </Style>
</ContextMenu.ItemContainerStyle> </ContextMenu.Resources>
<ContextMenu.ItemsSource>
<CompositeCollection>
<CollectionContainer Collection="{Binding Data.ExtraDataModelViewModels, Source={StaticResource DataContextProxy}}" />
<Separator Visibility="{Binding Data.HasExtraDataModels, Source={StaticResource DataContextProxy}, Converter={x:Static s:BoolToVisibilityConverter.Instance}, Mode=OneWay}"/>
<CollectionContainer Collection="{Binding Data.DataModelViewModel.Children, Source={StaticResource DataContextProxy}}" />
</CompositeCollection>
</ContextMenu.ItemsSource>
</ContextMenu> </ContextMenu>
</Button.ContextMenu> </Button.ContextMenu>
<Grid> <Grid>

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Specialized;
using System.Linq; using System.Linq;
using System.Timers; using System.Timers;
using System.Windows.Media; using System.Windows.Media;
@ -21,11 +22,11 @@ namespace Artemis.UI.Shared.Input
private Brush _buttonBrush = new SolidColorBrush(Color.FromRgb(171, 71, 188)); private Brush _buttonBrush = new SolidColorBrush(Color.FromRgb(171, 71, 188));
private DataModelPath _dataModelPath; private DataModelPath _dataModelPath;
private DataModelPropertiesViewModel _dataModelViewModel; private DataModelPropertiesViewModel _dataModelViewModel;
private bool _displaySwitchButton;
private Type[] _filterTypes; private Type[] _filterTypes;
private bool _isDataModelViewModelOpen; private bool _isDataModelViewModelOpen;
private bool _isEnabled = true; private bool _isEnabled = true;
private string _placeholder = "Select a property"; private string _placeholder = "Select a property";
private bool _displaySwitchButton;
internal DataModelDynamicViewModel(Module module, ISettingsService settingsService, IDataModelUIService dataModelUIService) internal DataModelDynamicViewModel(Module module, ISettingsService settingsService, IDataModelUIService dataModelUIService)
{ {
@ -33,6 +34,7 @@ namespace Artemis.UI.Shared.Input
_dataModelUIService = dataModelUIService; _dataModelUIService = dataModelUIService;
_updateTimer = new Timer(500); _updateTimer = new Timer(500);
ExtraDataModelViewModels = new BindableCollection<DataModelPropertiesViewModel>();
ShowDataModelValues = settingsService.GetSetting<bool>("ProfileEditor.ShowDataModelValues"); ShowDataModelValues = settingsService.GetSetting<bool>("ProfileEditor.ShowDataModelValues");
SelectPropertyCommand = new DelegateCommand(ExecuteSelectPropertyCommand); SelectPropertyCommand = new DelegateCommand(ExecuteSelectPropertyCommand);
@ -73,6 +75,9 @@ namespace Artemis.UI.Shared.Input
} }
} }
public BindableCollection<DataModelPropertiesViewModel> ExtraDataModelViewModels { get; }
public bool HasExtraDataModels => ExtraDataModelViewModels.Any();
public DelegateCommand SelectPropertyCommand { get; } public DelegateCommand SelectPropertyCommand { get; }
public PluginSetting<bool> ShowDataModelValues { get; } public PluginSetting<bool> ShowDataModelValues { get; }
@ -144,22 +149,11 @@ namespace Artemis.UI.Shared.Input
// Get the data models // Get the data models
DataModelViewModel = _dataModelUIService.GetPluginDataModelVisualization(_module, true); DataModelViewModel = _dataModelUIService.GetPluginDataModelVisualization(_module, true);
DataModelViewModel.UpdateRequested += DataModelOnUpdateRequested; DataModelViewModel.UpdateRequested += DataModelOnUpdateRequested;
ExtraDataModelViewModels.CollectionChanged += ExtraDataModelViewModelsOnCollectionChanged;
_updateTimer.Start(); _updateTimer.Start();
_updateTimer.Elapsed += OnUpdateTimerOnElapsed; _updateTimer.Elapsed += OnUpdateTimerOnElapsed;
} }
private void DataModelOnUpdateRequested(object sender, EventArgs e)
{
DataModelViewModel.ApplyTypeFilter(true, FilterTypes);
}
private void OnUpdateTimerOnElapsed(object sender, ElapsedEventArgs e)
{
if (IsDataModelViewModelOpen)
DataModelViewModel.Update(_dataModelUIService);
}
private void ExecuteSelectPropertyCommand(object context) private void ExecuteSelectPropertyCommand(object context)
{ {
if (!(context is DataModelVisualizationViewModel selected)) if (!(context is DataModelVisualizationViewModel selected))
@ -178,6 +172,32 @@ namespace Artemis.UI.Shared.Input
DataModelPath?.Dispose(); DataModelPath?.Dispose();
} }
#region Event handlers
private void DataModelOnUpdateRequested(object sender, EventArgs e)
{
DataModelViewModel.ApplyTypeFilter(true, FilterTypes);
foreach (DataModelPropertiesViewModel extraDataModelViewModel in ExtraDataModelViewModels)
extraDataModelViewModel.ApplyTypeFilter(true, FilterTypes);
}
private void ExtraDataModelViewModelsOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
NotifyOfPropertyChange(nameof(HasExtraDataModels));
}
private void OnUpdateTimerOnElapsed(object sender, ElapsedEventArgs e)
{
if (!IsDataModelViewModelOpen)
return;
DataModelViewModel.Update(_dataModelUIService);
foreach (DataModelPropertiesViewModel extraDataModelViewModel in ExtraDataModelViewModels)
extraDataModelViewModel.Update(_dataModelUIService);
}
#endregion
#region Events #region Events
public event EventHandler<DataModelInputDynamicEventArgs> PropertySelected; public event EventHandler<DataModelInputDynamicEventArgs> PropertySelected;

View File

@ -87,7 +87,7 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
public void Initialize() public void Initialize()
{ {
DataModelVisualizationViewModel listDataModel = GetListDataModel(); DataModelPropertiesViewModel listDataModel = GetListDataModel();
if (listDataModel.Children.Count == 1 && listDataModel.Children.First() is DataModelListPropertyViewModel) if (listDataModel.Children.Count == 1 && listDataModel.Children.First() is DataModelListPropertyViewModel)
_isPrimitiveList = true; _isPrimitiveList = true;
else else
@ -97,7 +97,7 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
if (!_isPrimitiveList) if (!_isPrimitiveList)
{ {
LeftSideSelectionViewModel = _dataModelUIService.GetDynamicSelectionViewModel(_profileEditorService.GetCurrentModule()); LeftSideSelectionViewModel = _dataModelUIService.GetDynamicSelectionViewModel(_profileEditorService.GetCurrentModule());
LeftSideSelectionViewModel.ChangeDataModel((DataModelPropertiesViewModel) listDataModel); LeftSideSelectionViewModel.ChangeDataModel(listDataModel);
LeftSideSelectionViewModel.PropertySelected += LeftSideOnPropertySelected; LeftSideSelectionViewModel.PropertySelected += LeftSideOnPropertySelected;
} }
@ -194,7 +194,7 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
Update(); Update();
} }
private DataModelVisualizationViewModel GetListDataModel() private DataModelPropertiesViewModel GetListDataModel()
{ {
if (DataModelConditionListPredicate.DataModelConditionList.ListPath?.DataModelGuid == null) if (DataModelConditionListPredicate.DataModelConditionList.ListPath?.DataModelGuid == null)
throw new ArtemisUIException("Failed to retrieve the list data model VM for this list predicate because it has no list path"); throw new ArtemisUIException("Failed to retrieve the list data model VM for this list predicate because it has no list path");
@ -273,6 +273,11 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
RightSideSelectionViewModel.DisplaySwitchButton = true; RightSideSelectionViewModel.DisplaySwitchButton = true;
RightSideSelectionViewModel.PropertySelected += RightSideOnPropertySelected; RightSideSelectionViewModel.PropertySelected += RightSideOnPropertySelected;
RightSideSelectionViewModel.SwitchToStaticRequested += RightSideSelectionViewModelOnSwitchToStaticRequested; RightSideSelectionViewModel.SwitchToStaticRequested += RightSideSelectionViewModelOnSwitchToStaticRequested;
// Add an extra data model to the selection VM to allow self-referencing the current item
// The safe cast prevents adding this extra VM on primitive lists where they serve no purpose
if (GetListDataModel()?.Children?.FirstOrDefault() is DataModelPropertiesViewModel listValue)
RightSideSelectionViewModel.ExtraDataModelViewModels.Add(listValue);
} }
private void CreateRightSideInputViewModel(Type leftSideType) private void CreateRightSideInputViewModel(Type leftSideType)