1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Condition VMs - Simplification WIPpepepepe

This commit is contained in:
SpoinkyNL 2020-10-08 22:21:17 +02:00
parent 805cdc6782
commit 94bde1fe38
11 changed files with 73 additions and 103 deletions

View File

@ -2,7 +2,6 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Artemis.Core.DataModelExpansions;
using Artemis.Storage.Entities.Profile.Abstract; using Artemis.Storage.Entities.Profile.Abstract;
using Artemis.Storage.Entities.Profile.Conditions; using Artemis.Storage.Entities.Profile.Conditions;
@ -73,47 +72,36 @@ namespace Artemis.Core
/// <summary> /// <summary>
/// Updates the list the predicate is evaluated on /// Updates the list the predicate is evaluated on
/// </summary> /// </summary>
/// <param name="dataModel">The data model of the list</param>
/// <param name="path">The path pointing to the list inside the list</param> /// <param name="path">The path pointing to the list inside the list</param>
public void UpdateList(DataModel? dataModel, string? path) public void UpdateList(DataModelPath? path)
{ {
if (_disposed) if (_disposed)
throw new ObjectDisposedException("DataModelConditionList"); throw new ObjectDisposedException("DataModelConditionList");
if (dataModel != null && path == null) if (path != null && !path.IsValid)
throw new ArtemisCoreException("If a data model is provided, a path is also required"); throw new ArtemisCoreException("Cannot update list to an invalid path");
if (dataModel == null && path != null)
throw new ArtemisCoreException("If path is provided, a data model is also required");
ListPath?.Dispose(); ListPath?.Dispose();
if (dataModel != null && path != null) ListPath = path;
{
DataModelPath newPath = new DataModelPath(dataModel, path);
if (!newPath.IsValid)
throw new ArtemisCoreException($"New left path '{newPath}' is invalid");
Type listType = newPath.GetPropertyType()!;
if (!typeof(IList).IsAssignableFrom(listType))
throw new ArtemisCoreException($"Data model of type {dataModel.GetType().Name} does not contain a list at path '{newPath}'");
ListPath = newPath; // Remove the old root group that was tied to the old data model
while (Children.Any())
RemoveChild(Children[0]);
if (path != null)
{
Type listType = path.GetPropertyType()!;
ListType = listType.GetGenericArguments()[0]; ListType = listType.GetGenericArguments()[0];
IsPrimitiveList = ListType.IsPrimitive || ListType.IsEnum || ListType == typeof(string); IsPrimitiveList = ListType.IsPrimitive || ListType.IsEnum || ListType == typeof(string);
// Create a new root group
AddChild(new DataModelConditionGroup(this));
} }
else else
{ {
ListPath = null; ListPath = null;
ListType = null; ListType = null;
} }
// Remove the old root group that was tied to the old data model
while (Children.Any())
RemoveChild(Children[0]);
if (dataModel == null)
return;
// Create a new root group
AddChild(new DataModelConditionGroup(this));
} }
#region IDisposable #region IDisposable

View File

@ -90,13 +90,10 @@ namespace Artemis.Core
/// and re-compiles the expression /// and re-compiles the expression
/// </summary> /// </summary>
/// <param name="path">The path pointing to the right side value inside the list</param> /// <param name="path">The path pointing to the right side value inside the list</param>
public void UpdateRightSideDynamic(string? path) public void UpdateRightSideDynamicList(DataModelPath? path)
{ {
RightPath?.Dispose(); RightPath?.Dispose();
if (path != null && DataModelConditionList.ListType != null) RightPath = path;
RightPath = new DataModelPath(ListPredicateWrapperDataModel.Create(DataModelConditionList.ListType), path);
else
RightPath = null;
PredicateType = ListRightSideType.DynamicList; PredicateType = ListRightSideType.DynamicList;
} }
@ -105,27 +102,11 @@ namespace Artemis.Core
/// Updates the right side of the predicate using path to a value in a data model, makes the predicate dynamic and /// Updates the right side of the predicate using path to a value in a data model, makes the predicate dynamic and
/// re-compiles the expression /// re-compiles the expression
/// </summary> /// </summary>
/// <param name="dataModel"></param>
/// <param name="path">The path pointing to the right side value inside the list</param> /// <param name="path">The path pointing to the right side value inside the list</param>
public void UpdateRightSideDynamic(DataModel? dataModel, string? path) public void UpdateRightSideDynamic(DataModelPath? path)
{ {
if (dataModel != null && path == null)
throw new ArtemisCoreException("If a data model is provided, a path is also required");
if (dataModel == null && path != null)
throw new ArtemisCoreException("If path is provided, a data model is also required");
RightPath?.Dispose(); RightPath?.Dispose();
if (dataModel != null) RightPath = path;
{
DataModelPath newPath = new DataModelPath(dataModel, path);
if (!newPath.IsValid)
throw new ArtemisCoreException($"New right path '{newPath}' is invalid");
RightPath = newPath;
}
else
{
RightPath = null;
}
PredicateType = ListRightSideType.Dynamic; PredicateType = ListRightSideType.Dynamic;
} }
@ -391,7 +372,7 @@ namespace Artemis.Core
Type rightSideType = RightPath.GetPropertyType()!; Type rightSideType = RightPath.GetPropertyType()!;
if (leftType != null && !leftType.IsCastableFrom(rightSideType)) if (leftType != null && !leftType.IsCastableFrom(rightSideType))
UpdateRightSideDynamic(null, null); UpdateRightSideDynamic(null);
} }
else if (PredicateType == ListRightSideType.DynamicList) else if (PredicateType == ListRightSideType.DynamicList)
{ {
@ -400,7 +381,7 @@ namespace Artemis.Core
Type rightSideType = RightPath.GetPropertyType()!; Type rightSideType = RightPath.GetPropertyType()!;
if (leftType != null && !leftType.IsCastableFrom(rightSideType)) if (leftType != null && !leftType.IsCastableFrom(rightSideType))
UpdateRightSideDynamic(null); UpdateRightSideDynamicList(null);
} }
else else
{ {

View File

@ -190,7 +190,7 @@ namespace Artemis.Core
/// </summary> /// </summary>
/// <param name="dataModel">The data model of the parameter</param> /// <param name="dataModel">The data model of the parameter</param>
/// <param name="path">The path pointing to the parameter inside the data model</param> /// <param name="path">The path pointing to the parameter inside the data model</param>
public void UpdateParameter(DataModel dataModel, string path) public void UpdateParameter(DataModel? dataModel, string? path)
{ {
if (_disposed) if (_disposed)
throw new ObjectDisposedException("DataBindingModifier"); throw new ObjectDisposedException("DataBindingModifier");

View File

@ -65,6 +65,19 @@ namespace Artemis.Core
SubscribeToDataModelStore(); SubscribeToDataModelStore();
} }
internal DataModelPath(DataModelPath dataModelPath)
{
Target = dataModelPath.Target;
Path = dataModelPath.Path;
Entity = new DataModelPathEntity();
_segments = new LinkedList<DataModelPathSegment>();
Save();
Initialize();
SubscribeToDataModelStore();
}
/// <summary> /// <summary>
/// Gets the data model at which this path starts /// Gets the data model at which this path starts
/// </summary> /// </summary>

View File

@ -137,10 +137,12 @@ namespace Artemis.Core.DataModelExpansions
return value as T; return value as T;
} }
internal bool ContainsPath(string path) internal bool ContainsPath(string? path)
{ {
if (path == null)
return false;
string[] parts = path.Split('.'); string[] parts = path.Split('.');
Type current = GetType(); Type? current = GetType();
foreach (string part in parts) foreach (string part in parts)
{ {
PropertyInfo? property = current?.GetProperty(part); PropertyInfo? property = current?.GetProperty(part);

View File

@ -50,10 +50,10 @@
</ContextMenu> </ContextMenu>
</Button.ContextMenu> </Button.ContextMenu>
<Grid> <Grid>
<TextBlock Text="{Binding SelectedPropertyViewModel.PropertyDescription.Name}" <TextBlock Text="{Binding DisplayValue}"
Visibility="{Binding SelectedPropertyViewModel, Converter={StaticResource NullToVisibilityConverter}}" /> Visibility="{Binding DataModelPath, Converter={StaticResource NullToVisibilityConverter}}" />
<TextBlock FontStyle="Italic" <TextBlock FontStyle="Italic"
Visibility="{Binding SelectedPropertyViewModel, Converter={StaticResource NullToVisibilityConverter}, ConverterParameter=Inverted}"> Visibility="{Binding DataModelPath, Converter={StaticResource NullToVisibilityConverter}, ConverterParameter=Inverted}">
<Run Text="« " /><Run Text="{Binding Placeholder}" /><Run Text=" »" /> <Run Text="« " /><Run Text="{Binding Placeholder}" /><Run Text=" »" />
</TextBlock> </TextBlock>
</Grid> </Grid>

View File

@ -1,8 +1,8 @@
using System; using System;
using System.Linq;
using System.Timers; using System.Timers;
using System.Windows.Media; using System.Windows.Media;
using Artemis.Core; using Artemis.Core;
using Artemis.Core.DataModelExpansions;
using Artemis.Core.Modules; using Artemis.Core.Modules;
using Artemis.Core.Services; using Artemis.Core.Services;
using Artemis.UI.Shared.Services; using Artemis.UI.Shared.Services;
@ -19,12 +19,12 @@ namespace Artemis.UI.Shared.Input
private readonly Module _module; private readonly Module _module;
private readonly Timer _updateTimer; private readonly Timer _updateTimer;
private Brush _buttonBrush = new SolidColorBrush(Color.FromRgb(171, 71, 188)); private Brush _buttonBrush = new SolidColorBrush(Color.FromRgb(171, 71, 188));
private DataModelPath _dataModelPath;
private DataModelPropertiesViewModel _dataModelViewModel; private DataModelPropertiesViewModel _dataModelViewModel;
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 DataModelVisualizationViewModel _selectedPropertyViewModel;
private Type[] _filterTypes;
internal DataModelDynamicViewModel(Module module, ISettingsService settingsService, IDataModelUIService dataModelUIService) internal DataModelDynamicViewModel(Module module, ISettingsService settingsService, IDataModelUIService dataModelUIService)
{ {
@ -81,19 +81,17 @@ namespace Artemis.UI.Shared.Input
set => SetAndNotify(ref _isDataModelViewModelOpen, value); set => SetAndNotify(ref _isDataModelViewModelOpen, value);
} }
public DataModelVisualizationViewModel SelectedPropertyViewModel public DataModelPath DataModelPath
{ {
get => _selectedPropertyViewModel; private get => _dataModelPath;
set => SetAndNotify(ref _selectedPropertyViewModel, value); set
{
if (!SetAndNotify(ref _dataModelPath, value)) return;
NotifyOfPropertyChange(nameof(DisplayValue));
}
} }
public void PopulateSelectedPropertyViewModel(DataModel datamodel, string path) public string DisplayValue => DataModelPath.GetPropertyDescription()?.Name ?? DataModelPath.Segments.LastOrDefault()?.Identifier;
{
if (datamodel == null)
SelectedPropertyViewModel = null;
else
SelectedPropertyViewModel = DataModelViewModel.GetChildByPath(datamodel.PluginInfo.Guid, path);
}
public void ChangeDataModel(DataModelPropertiesViewModel dataModel) public void ChangeDataModel(DataModelPropertiesViewModel dataModel)
{ {
@ -132,10 +130,16 @@ namespace Artemis.UI.Shared.Input
if (!(context is DataModelVisualizationViewModel selected)) if (!(context is DataModelVisualizationViewModel selected))
return; return;
SelectedPropertyViewModel = selected; DataModelPath = selected.DataModelPath;
OnPropertySelected(new DataModelInputDynamicEventArgs(selected)); OnPropertySelected(new DataModelInputDynamicEventArgs(DataModelPath));
} }
public void Dispose()
{
_updateTimer.Stop();
_updateTimer.Dispose();
_updateTimer.Elapsed -= OnUpdateTimerOnElapsed;
}
#region Events #region Events
@ -147,12 +151,5 @@ namespace Artemis.UI.Shared.Input
} }
#endregion #endregion
public void Dispose()
{
_updateTimer.Stop();
_updateTimer.Dispose();
_updateTimer.Elapsed -= OnUpdateTimerOnElapsed;
}
} }
} }

View File

@ -1,14 +1,15 @@
using System; using System;
using Artemis.Core;
namespace Artemis.UI.Shared namespace Artemis.UI.Shared
{ {
public class DataModelInputDynamicEventArgs : EventArgs public class DataModelInputDynamicEventArgs : EventArgs
{ {
public DataModelVisualizationViewModel DataModelVisualizationViewModel { get; } public DataModelPath DataModelPath { get; }
public DataModelInputDynamicEventArgs(DataModelVisualizationViewModel dataModelVisualizationViewModel) public DataModelInputDynamicEventArgs(DataModelPath dataModelPath)
{ {
DataModelVisualizationViewModel = dataModelVisualizationViewModel; DataModelPath = dataModelPath;
} }
} }
} }

View File

@ -120,15 +120,12 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
// Lists use a different color // Lists use a different color
LeftSideSelectionViewModel.ButtonBrush = new SolidColorBrush(Color.FromRgb(71, 108, 188)); LeftSideSelectionViewModel.ButtonBrush = new SolidColorBrush(Color.FromRgb(71, 108, 188));
LeftSideSelectionViewModel.FilterTypes = _supportedInputTypes.ToArray(); LeftSideSelectionViewModel.FilterTypes = _supportedInputTypes.ToArray();
LeftSideSelectionViewModel.PopulateSelectedPropertyViewModel( LeftSideSelectionViewModel.DataModelPath = DataModelConditionListPredicate.LeftPath;
DataModelConditionListPredicate.LeftPath?.Target,
DataModelConditionListPredicate.LeftPath?.Path
);
} }
Type leftSideType = _isPrimitiveList Type leftSideType = _isPrimitiveList
? DataModelConditionListPredicate.DataModelConditionList.ListType ? DataModelConditionListPredicate.DataModelConditionList.ListType
: LeftSideSelectionViewModel.SelectedPropertyViewModel?.DataModelPath?.GetPropertyType(); : LeftSideSelectionViewModel.DataModelPath?.GetPropertyType();
// Get the supported operators // Get the supported operators
Operators.Clear(); Operators.Clear();
@ -158,10 +155,7 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
} }
RightSideSelectionViewModel.FilterTypes = new[] {leftSideType}; RightSideSelectionViewModel.FilterTypes = new[] {leftSideType};
RightSideSelectionViewModel.PopulateSelectedPropertyViewModel( LeftSideSelectionViewModel.DataModelPath = DataModelConditionListPredicate.RightPath;
DataModelConditionListPredicate.RightPath?.Target,
DataModelConditionListPredicate.RightPath?.Path
);
} }
else if (SelectedOperator.SupportsRightSide) else if (SelectedOperator.SupportsRightSide)
{ {
@ -181,7 +175,7 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
public void ApplyLeftSide() public void ApplyLeftSide()
{ {
DataModelConditionListPredicate.UpdateLeftSide(LeftSideSelectionViewModel.SelectedPropertyViewModel.Path); DataModelConditionListPredicate.UpdateLeftSide(LeftSideSelectionViewModel.DataModelPath.Path);
_profileEditorService.UpdateSelectedProfileElement(); _profileEditorService.UpdateSelectedProfileElement();
SelectedOperator = DataModelConditionListPredicate.Operator; SelectedOperator = DataModelConditionListPredicate.Operator;
@ -191,12 +185,9 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
public void ApplyRightSideDynamic() public void ApplyRightSideDynamic()
{ {
if (DataModelConditionListPredicate.PredicateType == ListRightSideType.Dynamic) if (DataModelConditionListPredicate.PredicateType == ListRightSideType.Dynamic)
DataModelConditionListPredicate.UpdateRightSideDynamic( DataModelConditionListPredicate.UpdateRightSideDynamic(RightSideSelectionViewModel.DataModelPath);
RightSideSelectionViewModel.SelectedPropertyViewModel.DataModel,
RightSideSelectionViewModel.SelectedPropertyViewModel.Path
);
else if (DataModelConditionListPredicate.PredicateType == ListRightSideType.DynamicList) else if (DataModelConditionListPredicate.PredicateType == ListRightSideType.DynamicList)
DataModelConditionListPredicate.UpdateRightSideDynamic(RightSideSelectionViewModel.SelectedPropertyViewModel.Path); DataModelConditionListPredicate.UpdateRightSideDynamicList(RightSideSelectionViewModel.DataModelPath);
_profileEditorService.UpdateSelectedProfileElement(); _profileEditorService.UpdateSelectedProfileElement();
Update(); Update();

View File

@ -96,10 +96,7 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
public void ApplyList() public void ApplyList()
{ {
DataModelConditionList.UpdateList( DataModelConditionList.UpdateList(TargetSelectionViewModel.DataModelPath);
TargetSelectionViewModel.SelectedPropertyViewModel.DataModel,
TargetSelectionViewModel.SelectedPropertyViewModel.Path
);
_profileEditorService.UpdateSelectedProfileElement(); _profileEditorService.UpdateSelectedProfileElement();
Update(); Update();
@ -107,7 +104,7 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
public override void Update() public override void Update()
{ {
TargetSelectionViewModel.PopulateSelectedPropertyViewModel(DataModelConditionList.ListPath?.Target, DataModelConditionList.ListPath?.Path); TargetSelectionViewModel.DataModelPath = DataModelConditionList.ListPath;
NotifyOfPropertyChange(nameof(SelectedListOperator)); NotifyOfPropertyChange(nameof(SelectedListOperator));
// Remove VMs of effects no longer applied on the layer // Remove VMs of effects no longer applied on the layer

View File

@ -105,7 +105,7 @@ namespace Artemis.UI.Screens.ProfileEditor.LayerProperties.DataBindings.DirectDa
private void ParameterSelectionViewModelOnPropertySelected(object sender, DataModelInputDynamicEventArgs e) private void ParameterSelectionViewModelOnPropertySelected(object sender, DataModelInputDynamicEventArgs e)
{ {
Modifier.UpdateParameter(e.DataModelVisualizationViewModel.DataModel, e.DataModelVisualizationViewModel.Path); Modifier.UpdateParameter(e.DataModelPath.Target, e.DataModelPath.Path);
_profileEditorService.UpdateSelectedProfileElement(); _profileEditorService.UpdateSelectedProfileElement();
} }