mirror of
https://github.com/Artemis-RGB/Artemis
synced 2025-12-13 05:48:35 +00:00
Scripting - Enabled scripting (#638)
* Scripting - Simplified view model structure * Scripting - Removed Layer and LayerProperty scripts
This commit is contained in:
parent
ee16d69fb3
commit
631d8de2c3
@ -38,8 +38,6 @@ namespace Artemis.Core
|
||||
Profile = Parent.Profile;
|
||||
Name = name;
|
||||
Suspended = false;
|
||||
Scripts = new List<LayerScript>();
|
||||
ScriptConfigurations = new List<ScriptConfiguration>();
|
||||
|
||||
_general = new LayerGeneralProperties();
|
||||
_transform = new LayerTransformProperties();
|
||||
@ -64,8 +62,6 @@ namespace Artemis.Core
|
||||
|
||||
Profile = profile;
|
||||
Parent = parent;
|
||||
Scripts = new List<LayerScript>();
|
||||
ScriptConfigurations = new List<ScriptConfiguration>();
|
||||
|
||||
_general = new LayerGeneralProperties();
|
||||
_transform = new LayerTransformProperties();
|
||||
@ -82,16 +78,6 @@ namespace Artemis.Core
|
||||
/// </summary>
|
||||
public ReadOnlyCollection<ArtemisLed> Leds => _leds.AsReadOnly();
|
||||
|
||||
/// <summary>
|
||||
/// Gets a collection of all active scripts assigned to this layer
|
||||
/// </summary>
|
||||
public List<LayerScript> Scripts { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a collection of all script configurations assigned to this layer
|
||||
/// </summary>
|
||||
public List<ScriptConfiguration> ScriptConfigurations { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Defines the shape that is rendered by the <see cref="LayerBrush" />.
|
||||
/// </summary>
|
||||
@ -190,9 +176,6 @@ namespace Artemis.Core
|
||||
|
||||
Disposed = true;
|
||||
|
||||
while (Scripts.Count > 1)
|
||||
Scripts[0].Dispose();
|
||||
|
||||
LayerBrushStore.LayerBrushAdded -= LayerBrushStoreOnLayerBrushAdded;
|
||||
LayerBrushStore.LayerBrushRemoved -= LayerBrushStoreOnLayerBrushRemoved;
|
||||
|
||||
@ -269,11 +252,6 @@ namespace Artemis.Core
|
||||
ExpandedPropertyGroups.AddRange(LayerEntity.ExpandedPropertyGroups);
|
||||
LoadRenderElement();
|
||||
Adapter.Load();
|
||||
|
||||
foreach (ScriptConfiguration scriptConfiguration in ScriptConfigurations)
|
||||
scriptConfiguration.Script?.Dispose();
|
||||
ScriptConfigurations.Clear();
|
||||
ScriptConfigurations.AddRange(LayerEntity.ScriptConfigurations.Select(e => new ScriptConfiguration(e)));
|
||||
}
|
||||
|
||||
internal override void Save()
|
||||
@ -310,14 +288,7 @@ namespace Artemis.Core
|
||||
|
||||
// Adaption hints
|
||||
Adapter.Save();
|
||||
|
||||
LayerEntity.ScriptConfigurations.Clear();
|
||||
foreach (ScriptConfiguration scriptConfiguration in ScriptConfigurations)
|
||||
{
|
||||
scriptConfiguration.Save();
|
||||
LayerEntity.ScriptConfigurations.Add(scriptConfiguration.Entity);
|
||||
}
|
||||
|
||||
|
||||
SaveRenderElement();
|
||||
}
|
||||
|
||||
@ -349,10 +320,7 @@ namespace Artemis.Core
|
||||
{
|
||||
if (Disposed)
|
||||
throw new ObjectDisposedException("Layer");
|
||||
|
||||
foreach (LayerScript layerScript in Scripts)
|
||||
layerScript.OnLayerUpdating(deltaTime);
|
||||
|
||||
|
||||
UpdateDisplayCondition();
|
||||
UpdateTimeline(deltaTime);
|
||||
|
||||
@ -360,9 +328,6 @@ namespace Artemis.Core
|
||||
Enable();
|
||||
else if (Timeline.IsFinished)
|
||||
Disable();
|
||||
|
||||
foreach (LayerScript layerScript in Scripts)
|
||||
layerScript.OnLayerUpdated(deltaTime);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@ -512,10 +477,7 @@ namespace Artemis.Core
|
||||
{
|
||||
if (LayerBrush == null)
|
||||
throw new ArtemisCoreException("The layer is not yet ready for rendering");
|
||||
|
||||
foreach (LayerScript layerScript in Scripts)
|
||||
layerScript.OnLayerRendering(canvas, bounds, layerPaint);
|
||||
|
||||
|
||||
foreach (BaseLayerEffect baseLayerEffect in LayerEffects.Where(e => !e.Suspended))
|
||||
baseLayerEffect.PreProcess(canvas, bounds, layerPaint);
|
||||
|
||||
@ -531,9 +493,6 @@ namespace Artemis.Core
|
||||
|
||||
foreach (BaseLayerEffect baseLayerEffect in LayerEffects.Where(e => !e.Suspended))
|
||||
baseLayerEffect.PostProcess(canvas, bounds, layerPaint);
|
||||
|
||||
foreach (LayerScript layerScript in Scripts)
|
||||
layerScript.OnLayerRendered(canvas, bounds, layerPaint);
|
||||
}
|
||||
|
||||
finally
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Artemis.Core.ScriptingProviders;
|
||||
using Artemis.Storage.Entities.Profile;
|
||||
|
||||
namespace Artemis.Core
|
||||
@ -24,16 +23,6 @@ namespace Artemis.Core
|
||||
/// </summary>
|
||||
LayerPropertyGroup LayerPropertyGroup { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a collection of all active scripts assigned to this layer property
|
||||
/// </summary>
|
||||
List<PropertyScript> Scripts { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a collection of all script configurations assigned to this layer property
|
||||
/// </summary>
|
||||
public List<ScriptConfiguration> ScriptConfigurations { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the unique path of the property on the layer
|
||||
/// </summary>
|
||||
@ -81,8 +70,6 @@ namespace Artemis.Core
|
||||
/// <param name="timeline">The timeline to apply to the property</param>
|
||||
void Update(Timeline timeline);
|
||||
|
||||
#region Events
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when the layer property is disposed
|
||||
/// </summary>
|
||||
@ -137,7 +124,5 @@ namespace Artemis.Core
|
||||
/// Occurs when a data binding has been disabled
|
||||
/// </summary>
|
||||
public event EventHandler<LayerPropertyEventArgs>? DataBindingDisabled;
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@ -2,7 +2,6 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using Artemis.Core.ScriptingProviders;
|
||||
using Artemis.Storage.Entities.Profile;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
@ -31,8 +30,6 @@ namespace Artemis.Core
|
||||
Path = null!;
|
||||
Entity = null!;
|
||||
PropertyDescription = null!;
|
||||
Scripts = new List<PropertyScript>();
|
||||
ScriptConfigurations = new List<ScriptConfiguration>();
|
||||
|
||||
CurrentValue = default!;
|
||||
DefaultValue = default!;
|
||||
@ -60,9 +57,6 @@ namespace Artemis.Core
|
||||
{
|
||||
_disposed = true;
|
||||
|
||||
while (Scripts.Count > 1)
|
||||
Scripts[0].Dispose();
|
||||
|
||||
foreach (IDataBinding dataBinding in _dataBindings)
|
||||
dataBinding.Dispose();
|
||||
|
||||
@ -153,12 +147,6 @@ namespace Artemis.Core
|
||||
/// <inheritdoc />
|
||||
public PropertyDescriptionAttribute PropertyDescription { get; internal set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public List<PropertyScript> Scripts { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public List<ScriptConfiguration> ScriptConfigurations { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public string Path { get; private set; }
|
||||
|
||||
@ -171,18 +159,12 @@ namespace Artemis.Core
|
||||
if (_disposed)
|
||||
throw new ObjectDisposedException("LayerProperty");
|
||||
|
||||
foreach (PropertyScript propertyScript in Scripts)
|
||||
propertyScript.OnPropertyUpdating(timeline.Delta.TotalSeconds);
|
||||
|
||||
CurrentValue = BaseValue;
|
||||
|
||||
UpdateKeyframes(timeline);
|
||||
UpdateDataBindings(timeline);
|
||||
|
||||
OnUpdated();
|
||||
|
||||
foreach (PropertyScript propertyScript in Scripts)
|
||||
propertyScript.OnPropertyUpdated(timeline.Delta.TotalSeconds);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@ -762,11 +744,6 @@ namespace Artemis.Core
|
||||
if (dataBinding != null)
|
||||
_dataBindings.Add(dataBinding);
|
||||
}
|
||||
|
||||
foreach (ScriptConfiguration scriptConfiguration in ScriptConfigurations)
|
||||
scriptConfiguration.Script?.Dispose();
|
||||
ScriptConfigurations.Clear();
|
||||
ScriptConfigurations.AddRange(Entity.ScriptConfigurations.Select(e => new ScriptConfiguration(e)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -788,13 +765,6 @@ namespace Artemis.Core
|
||||
Entity.DataBindingEntities.Clear();
|
||||
foreach (IDataBinding dataBinding in _dataBindings)
|
||||
dataBinding.Save();
|
||||
|
||||
Entity.ScriptConfigurations.Clear();
|
||||
foreach (ScriptConfiguration scriptConfiguration in ScriptConfigurations)
|
||||
{
|
||||
scriptConfiguration.Save();
|
||||
Entity.ScriptConfigurations.Add(scriptConfiguration.Entity);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@ -0,0 +1,24 @@
|
||||
namespace Artemis.Core.ScriptingProviders
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a view model containing a script editor
|
||||
/// </summary>
|
||||
public interface IScriptEditorViewModel
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the script type this view model was created for
|
||||
/// </summary>
|
||||
ScriptType ScriptType { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the script this editor is editing
|
||||
/// </summary>
|
||||
Script? Script { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Called whenever the view model must display a different script
|
||||
/// </summary>
|
||||
/// <param name="script">The script to display or <see langword="null" /> if no script is to be displayed</param>
|
||||
void ChangeScript(Script? script);
|
||||
}
|
||||
}
|
||||
@ -9,6 +9,7 @@ namespace Artemis.Core.ScriptingProviders
|
||||
public class ScriptConfiguration : CorePropertyChanged, IStorageModel
|
||||
{
|
||||
private bool _hasChanges;
|
||||
private bool _isSuspended;
|
||||
private string _name;
|
||||
private string? _pendingScriptContent;
|
||||
private string? _scriptContent;
|
||||
@ -79,6 +80,16 @@ namespace Artemis.Core.ScriptingProviders
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Implement suspension
|
||||
/// <summary>
|
||||
/// [NYI] Gets or sets a boolean indicating whether this configuration is suspended
|
||||
/// </summary>
|
||||
public bool IsSuspended
|
||||
{
|
||||
get => _isSuspended;
|
||||
set => SetAndNotify(ref _isSuspended, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a boolean indicating whether this configuration has pending changes to it's
|
||||
/// <see cref="ScriptContent" />
|
||||
|
||||
@ -7,36 +7,10 @@ namespace Artemis.Core.ScriptingProviders
|
||||
/// <summary>
|
||||
/// Allows you to implement and register your own scripting provider.
|
||||
/// </summary>
|
||||
public abstract class ScriptingProvider<TGlobalScript, TProfileScript, TLayerScript, TPropertyScript> : ScriptingProvider
|
||||
public abstract class ScriptingProvider<TGlobalScript, TProfileScript> : ScriptingProvider
|
||||
where TGlobalScript : GlobalScript
|
||||
where TProfileScript : ProfileScript
|
||||
where TLayerScript : LayerScript
|
||||
where TPropertyScript : PropertyScript
|
||||
{
|
||||
/// <summary>
|
||||
/// Called when the UI needs a script editor for a <see cref="GlobalScript" />
|
||||
/// </summary>
|
||||
/// <param name="script">The script the editor must edit</param>
|
||||
public abstract IScriptEditorViewModel CreateGlobalScriptEditor(TGlobalScript script);
|
||||
|
||||
/// <summary>
|
||||
/// Called when the UI needs a script editor for a <see cref="ProfileScript" />
|
||||
/// </summary>
|
||||
/// <param name="script">The script the editor must edit</param>
|
||||
public abstract IScriptEditorViewModel CreateProfileScriptEditor(TProfileScript script);
|
||||
|
||||
/// <summary>
|
||||
/// Called when the UI needs a script editor for a <see cref="LayerScript" />
|
||||
/// </summary>
|
||||
/// <param name="script">The script the editor must edit</param>
|
||||
public abstract IScriptEditorViewModel CreateLayerScriptScriptEditor(TLayerScript script);
|
||||
|
||||
/// <summary>
|
||||
/// Called when the UI needs a script editor for a <see cref="PropertyScript" />
|
||||
/// </summary>
|
||||
/// <param name="script">The script the editor must edit</param>
|
||||
public abstract IScriptEditorViewModel CreatePropertyScriptEditor(TPropertyScript script);
|
||||
|
||||
#region Overrides of PluginFeature
|
||||
|
||||
/// <inheritdoc />
|
||||
@ -52,69 +26,11 @@ namespace Artemis.Core.ScriptingProviders
|
||||
|
||||
#region Overrides of ScriptingProvider
|
||||
|
||||
/// <inheritdoc />
|
||||
internal override Type GlobalScriptType => typeof(TGlobalScript);
|
||||
|
||||
/// <inheritdoc />
|
||||
internal override Type ProfileScriptType => typeof(TProfileScript);
|
||||
|
||||
/// <inheritdoc />
|
||||
internal override Type LayerScriptType => typeof(TLayerScript);
|
||||
|
||||
/// <inheritdoc />
|
||||
internal override Type PropertyScriptType => typeof(TPropertyScript);
|
||||
|
||||
/// <summary>
|
||||
/// Called when the UI needs a script editor for a <see cref="GlobalScript" />
|
||||
/// </summary>
|
||||
/// <param name="script">The script the editor must edit</param>
|
||||
public override IScriptEditorViewModel CreateGlobalScriptEditor(GlobalScript script)
|
||||
{
|
||||
if (script == null) throw new ArgumentNullException(nameof(script));
|
||||
if (script.GetType() != GlobalScriptType)
|
||||
throw new ArtemisCoreException($"This scripting provider only supports global scripts of type {GlobalScriptType.Name}");
|
||||
|
||||
return CreateGlobalScriptEditor((TGlobalScript) script);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when the UI needs a script editor for a <see cref="ProfileScript" />
|
||||
/// </summary>
|
||||
/// <param name="script">The script the editor must edit</param>
|
||||
public override IScriptEditorViewModel CreateProfileScriptEditor(ProfileScript script)
|
||||
{
|
||||
if (script == null) throw new ArgumentNullException(nameof(script));
|
||||
if (script.GetType() != ProfileScriptType)
|
||||
throw new ArtemisCoreException($"This scripting provider only supports profile scripts of type {ProfileScriptType.Name}");
|
||||
|
||||
return CreateProfileScriptEditor((TProfileScript) script);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when the UI needs a script editor for a <see cref="LayerScript" />
|
||||
/// </summary>
|
||||
/// <param name="script">The script the editor must edit</param>
|
||||
public override IScriptEditorViewModel CreateLayerScriptScriptEditor(LayerScript script)
|
||||
{
|
||||
if (script == null) throw new ArgumentNullException(nameof(script));
|
||||
if (script.GetType() != LayerScriptType)
|
||||
throw new ArtemisCoreException($"This scripting provider only supports layer scripts of type {LayerScriptType.Name}");
|
||||
|
||||
return CreateLayerScriptScriptEditor((TLayerScript) script);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when the UI needs a script editor for a <see cref="PropertyScript" />
|
||||
/// </summary>
|
||||
/// <param name="script">The script the editor must edit</param>
|
||||
public override IScriptEditorViewModel CreatePropertyScriptEditor(PropertyScript script)
|
||||
{
|
||||
if (script == null) throw new ArgumentNullException(nameof(script));
|
||||
if (script.GetType() != PropertyScriptType)
|
||||
throw new ArtemisCoreException($"This scripting provider only supports property scripts of type {PropertyScriptType.Name}");
|
||||
|
||||
return CreatePropertyScriptEditor((TPropertyScript) script);
|
||||
}
|
||||
internal override Type GlobalScriptType => typeof(TGlobalScript);
|
||||
|
||||
#endregion
|
||||
}
|
||||
@ -123,7 +39,7 @@ namespace Artemis.Core.ScriptingProviders
|
||||
/// Allows you to implement and register your own scripting provider.
|
||||
/// <para>
|
||||
/// Note: You can't implement this, implement
|
||||
/// <see cref="ScriptingProvider{TProfileScript,TLayerScript,TPropertyScript,TGlobalScript}" /> instead.
|
||||
/// <see cref="ScriptingProvider{TGlobalScript, TProfileScript}" /> instead.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public abstract class ScriptingProvider : PluginFeature
|
||||
@ -139,44 +55,13 @@ namespace Artemis.Core.ScriptingProviders
|
||||
public ReadOnlyCollection<Script> Scripts => InternalScripts.AsReadOnly();
|
||||
|
||||
internal abstract Type GlobalScriptType { get; }
|
||||
internal abstract Type PropertyScriptType { get; }
|
||||
internal abstract Type LayerScriptType { get; }
|
||||
internal abstract Type ProfileScriptType { get; }
|
||||
internal List<Script> InternalScripts { get; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Called when the UI needs a script editor for a <see cref="GlobalScript" />
|
||||
/// Called when the UI needs a script editor for the specified <paramref name="scriptType" />
|
||||
/// </summary>
|
||||
/// <param name="script">The script the editor must edit</param>
|
||||
public abstract IScriptEditorViewModel CreateGlobalScriptEditor(GlobalScript script);
|
||||
|
||||
/// <summary>
|
||||
/// Called when the UI needs a script editor for a <see cref="ProfileScript" />
|
||||
/// </summary>
|
||||
/// <param name="script">The script the editor must edit</param>
|
||||
public abstract IScriptEditorViewModel CreateProfileScriptEditor(ProfileScript script);
|
||||
|
||||
/// <summary>
|
||||
/// Called when the UI needs a script editor for a <see cref="LayerScript" />
|
||||
/// </summary>
|
||||
/// <param name="script">The script the editor must edit</param>
|
||||
public abstract IScriptEditorViewModel CreateLayerScriptScriptEditor(LayerScript script);
|
||||
|
||||
/// <summary>
|
||||
/// Called when the UI needs a script editor for a <see cref="PropertyScript" />
|
||||
/// </summary>
|
||||
/// <param name="script">The script the editor must edit</param>
|
||||
public abstract IScriptEditorViewModel CreatePropertyScriptEditor(PropertyScript script);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a view model containing a script editor
|
||||
/// </summary>
|
||||
public interface IScriptEditorViewModel
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the script this editor is editing
|
||||
/// </summary>
|
||||
Script Script { get; }
|
||||
/// <param name="scriptType">The type of script the editor will host</param>
|
||||
public abstract IScriptEditorViewModel CreateScriptEditor(ScriptType scriptType);
|
||||
}
|
||||
}
|
||||
@ -32,6 +32,9 @@ namespace Artemis.Core.ScriptingProviders
|
||||
|
||||
#region Overrides of Script
|
||||
|
||||
/// <inheritdoc />
|
||||
public override ScriptType ScriptType => ScriptType.Global;
|
||||
|
||||
/// <inheritdoc />
|
||||
internal override void InternalCleanup()
|
||||
{
|
||||
|
||||
@ -1,74 +0,0 @@
|
||||
using SkiaSharp;
|
||||
|
||||
namespace Artemis.Core.ScriptingProviders
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a script bound to a specific <see cref="Layer" /> processed by a <see cref="ScriptingProvider" />.
|
||||
/// </summary>
|
||||
public abstract class LayerScript : Script
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected LayerScript(Layer layer, ScriptConfiguration configuration) : base(configuration)
|
||||
{
|
||||
Layer = layer;
|
||||
lock (Layer.Scripts)
|
||||
{
|
||||
Layer.Scripts.Add(this);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the layer this script is bound to
|
||||
/// </summary>
|
||||
public Layer Layer { get; internal set; }
|
||||
|
||||
/// <summary>
|
||||
/// Called whenever the layer is about to update
|
||||
/// </summary>
|
||||
/// <param name="deltaTime">Seconds passed since last update</param>
|
||||
public virtual void OnLayerUpdating(double deltaTime)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called whenever the layer has been updated
|
||||
/// </summary>
|
||||
/// <param name="deltaTime">Seconds passed since last update</param>
|
||||
public virtual void OnLayerUpdated(double deltaTime)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called whenever the layer is about to render
|
||||
/// </summary>
|
||||
/// <param name="canvas">The layer canvas</param>
|
||||
/// <param name="bounds">The area to be filled, covers the shape</param>
|
||||
/// <param name="paint">The paint to be used to fill the shape</param>
|
||||
public virtual void OnLayerRendering(SKCanvas canvas, SKRect bounds, SKPaint paint)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called whenever the layer has been rendered
|
||||
/// </summary>
|
||||
/// <param name="canvas">The layer canvas</param>
|
||||
/// <param name="bounds">The area to be filled, covers the shape</param>
|
||||
/// <param name="paint">The paint to be used to fill the shape</param>
|
||||
public virtual void OnLayerRendered(SKCanvas canvas, SKRect bounds, SKPaint paint)
|
||||
{
|
||||
}
|
||||
|
||||
#region Overrides of Script
|
||||
|
||||
/// <inheritdoc />
|
||||
internal override void InternalCleanup()
|
||||
{
|
||||
lock (Layer.Scripts)
|
||||
{
|
||||
Layer.Scripts.Remove(this);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@ -58,6 +58,9 @@ namespace Artemis.Core.ScriptingProviders
|
||||
|
||||
#region Overrides of Script
|
||||
|
||||
/// <inheritdoc />
|
||||
public override ScriptType ScriptType => ScriptType.Profile;
|
||||
|
||||
/// <inheritdoc />
|
||||
internal override void InternalCleanup()
|
||||
{
|
||||
|
||||
@ -1,53 +0,0 @@
|
||||
namespace Artemis.Core.ScriptingProviders
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a script bound to a specific <see cref="LayerProperty{T}" /> processed by a
|
||||
/// <see cref="ScriptingProvider" />.
|
||||
/// </summary>
|
||||
public abstract class PropertyScript : Script
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected PropertyScript(ILayerProperty layerProperty, ScriptConfiguration configuration) : base(configuration)
|
||||
{
|
||||
LayerProperty = layerProperty;
|
||||
lock (LayerProperty.Scripts)
|
||||
{
|
||||
LayerProperty.Scripts.Add(this);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the layer property this script is bound to
|
||||
/// </summary>
|
||||
public ILayerProperty LayerProperty { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Called whenever the property is about to update
|
||||
/// </summary>
|
||||
/// <param name="deltaTime">Seconds passed since last update</param>
|
||||
public virtual void OnPropertyUpdating(double deltaTime)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called whenever the property has been updated
|
||||
/// </summary>
|
||||
/// <param name="deltaTime">Seconds passed since last update</param>
|
||||
public virtual void OnPropertyUpdated(double deltaTime)
|
||||
{
|
||||
}
|
||||
|
||||
#region Overrides of Script
|
||||
|
||||
/// <inheritdoc />
|
||||
internal override void InternalCleanup()
|
||||
{
|
||||
lock (LayerProperty.Scripts)
|
||||
{
|
||||
LayerProperty.Scripts.Remove(this);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@ -39,6 +39,11 @@ namespace Artemis.Core.ScriptingProviders
|
||||
/// </summary>
|
||||
public ScriptConfiguration ScriptConfiguration { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the script type of this script
|
||||
/// </summary>
|
||||
public abstract ScriptType ScriptType { get; }
|
||||
|
||||
#region Event handlers
|
||||
|
||||
private void ScriptConfigurationOnPropertyChanged(object? sender, PropertyChangedEventArgs e)
|
||||
@ -71,7 +76,7 @@ namespace Artemis.Core.ScriptingProviders
|
||||
ScriptConfiguration.PropertyChanged -= ScriptConfigurationOnPropertyChanged;
|
||||
ScriptConfiguration.Script = null;
|
||||
ScriptingProvider.InternalScripts.Remove(this);
|
||||
|
||||
|
||||
// Can't trust those pesky plugin devs!
|
||||
InternalCleanup();
|
||||
|
||||
@ -100,4 +105,20 @@ namespace Artemis.Core.ScriptingProviders
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a type of script
|
||||
/// </summary>
|
||||
public enum ScriptType
|
||||
{
|
||||
/// <summary>
|
||||
/// A global script that's always active
|
||||
/// </summary>
|
||||
Global,
|
||||
|
||||
/// <summary>
|
||||
/// A script tied to a <see cref="Profile" />
|
||||
/// </summary>
|
||||
Profile,
|
||||
}
|
||||
}
|
||||
@ -18,8 +18,7 @@ namespace Artemis.Core.Services
|
||||
/// </summary>
|
||||
/// <param name="scriptConfiguration">The script configuration of the script</param>
|
||||
/// <returns>
|
||||
/// If the <see cref="ScriptingProvider" /> was found an instance of the script; otherwise <see langword="null" />
|
||||
/// .
|
||||
/// If the <see cref="ScriptingProvider" /> was found an instance of the script; otherwise <see langword="null" />.
|
||||
/// </returns>
|
||||
GlobalScript? CreateScriptInstance(ScriptConfiguration scriptConfiguration);
|
||||
|
||||
@ -29,33 +28,10 @@ namespace Artemis.Core.Services
|
||||
/// <param name="profile">The profile the script configuration is configured for</param>
|
||||
/// <param name="scriptConfiguration">The script configuration of the script</param>
|
||||
/// <returns>
|
||||
/// If the <see cref="ScriptingProvider" /> was found an instance of the script; otherwise <see langword="null" />
|
||||
/// .
|
||||
/// If the <see cref="ScriptingProvider" /> was found an instance of the script; otherwise <see langword="null" />.
|
||||
/// </returns>
|
||||
ProfileScript? CreateScriptInstance(Profile profile, ScriptConfiguration scriptConfiguration);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a <see cref="LayerScript" /> instance for the given <paramref name="scriptConfiguration" />
|
||||
/// </summary>
|
||||
/// <param name="layer">The layer the script configuration is configured fo</param>
|
||||
/// <param name="scriptConfiguration">The script configuration of the script</param>
|
||||
/// <returns>
|
||||
/// If the <see cref="ScriptingProvider" /> was found an instance of the script; otherwise <see langword="null" />
|
||||
/// .
|
||||
/// </returns>
|
||||
LayerScript? CreateScriptInstance(Layer layer, ScriptConfiguration scriptConfiguration);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a <see cref="PropertyScript" /> instance for the given <paramref name="scriptConfiguration" />
|
||||
/// </summary>
|
||||
/// <param name="layerProperty">The layer property the script configuration is configured fo</param>
|
||||
/// <param name="scriptConfiguration">The script configuration of the script</param>
|
||||
/// <returns>
|
||||
/// If the <see cref="ScriptingProvider" /> was found an instance of the script; otherwise <see langword="null" />
|
||||
/// .
|
||||
/// </returns>
|
||||
PropertyScript? CreateScriptInstance(ILayerProperty layerProperty, ScriptConfiguration scriptConfiguration);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Deletes the provided global script by it's configuration
|
||||
/// </summary>
|
||||
|
||||
@ -12,11 +12,13 @@ namespace Artemis.Core.Services
|
||||
internal class ScriptingService : IScriptingService
|
||||
{
|
||||
private readonly IPluginManagementService _pluginManagementService;
|
||||
private readonly IProfileService _profileService;
|
||||
private List<ScriptingProvider> _scriptingProviders;
|
||||
|
||||
public ScriptingService(IPluginManagementService pluginManagementService, IProfileService profileService)
|
||||
{
|
||||
_pluginManagementService = pluginManagementService;
|
||||
_profileService = profileService;
|
||||
|
||||
InternalGlobalScripts = new List<GlobalScript>();
|
||||
|
||||
@ -27,7 +29,7 @@ namespace Artemis.Core.Services
|
||||
// No need to sub to Deactivated, scripts will deactivate themselves
|
||||
profileService.ProfileActivated += ProfileServiceOnProfileActivated;
|
||||
|
||||
foreach (ProfileConfiguration profileConfiguration in profileService.ProfileConfigurations)
|
||||
foreach (ProfileConfiguration profileConfiguration in _profileService.ProfileConfigurations)
|
||||
{
|
||||
if (profileConfiguration.Profile != null)
|
||||
InitializeProfileScripts(profileConfiguration.Profile);
|
||||
@ -54,6 +56,12 @@ namespace Artemis.Core.Services
|
||||
private void PluginManagementServiceOnPluginFeatureToggled(object? sender, PluginFeatureEventArgs e)
|
||||
{
|
||||
_scriptingProviders = _pluginManagementService.GetFeaturesOfType<ScriptingProvider>();
|
||||
|
||||
foreach (ProfileConfiguration profileConfiguration in _profileService.ProfileConfigurations)
|
||||
{
|
||||
if (profileConfiguration.Profile != null)
|
||||
InitializeProfileScripts(profileConfiguration.Profile);
|
||||
}
|
||||
}
|
||||
|
||||
private void ProfileServiceOnProfileActivated(object? sender, ProfileConfigurationEventArgs e)
|
||||
@ -67,30 +75,6 @@ namespace Artemis.Core.Services
|
||||
// Initialize the scripts on the profile
|
||||
foreach (ScriptConfiguration scriptConfiguration in profile.ScriptConfigurations.Where(c => c.Script == null))
|
||||
CreateScriptInstance(profile, scriptConfiguration);
|
||||
|
||||
foreach (Layer layer in profile.GetAllLayers())
|
||||
{
|
||||
// Initialize the scripts on the layers
|
||||
foreach (ScriptConfiguration scriptConfiguration in layer.ScriptConfigurations.Where(c => c.Script == null))
|
||||
CreateScriptInstance(layer, scriptConfiguration);
|
||||
|
||||
// Initialize the scripts on the layer properties of layers
|
||||
foreach (ILayerProperty layerProperty in layer.GetAllLayerProperties())
|
||||
{
|
||||
foreach (ScriptConfiguration scriptConfiguration in layerProperty.ScriptConfigurations.Where(c => c.Script == null))
|
||||
CreateScriptInstance(layerProperty, scriptConfiguration);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (Folder folder in profile.GetAllFolders())
|
||||
{
|
||||
// Initialize the scripts on the layer properties of folders
|
||||
foreach (ILayerProperty layerProperty in folder.GetAllLayerProperties())
|
||||
{
|
||||
foreach (ScriptConfiguration scriptConfiguration in layerProperty.ScriptConfigurations.Where(c => c.Script == null))
|
||||
CreateScriptInstance(layerProperty, scriptConfiguration);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public ReadOnlyCollection<GlobalScript> GlobalScripts => InternalGlobalScripts.AsReadOnly();
|
||||
@ -136,46 +120,6 @@ namespace Artemis.Core.Services
|
||||
return script;
|
||||
}
|
||||
|
||||
public LayerScript? CreateScriptInstance(Layer layer, ScriptConfiguration scriptConfiguration)
|
||||
{
|
||||
if (scriptConfiguration.Script != null)
|
||||
throw new ArtemisCoreException("The provided script configuration already has an active script");
|
||||
|
||||
ScriptingProvider? provider = _scriptingProviders.FirstOrDefault(p => p.Id == scriptConfiguration.ScriptingProviderId);
|
||||
if (provider == null)
|
||||
return null;
|
||||
|
||||
LayerScript script = (LayerScript) provider.Plugin.Kernel!.Get(
|
||||
provider.LayerScriptType,
|
||||
CreateScriptConstructorArgument(provider.LayerScriptType, layer),
|
||||
CreateScriptConstructorArgument(provider.LayerScriptType, scriptConfiguration)
|
||||
);
|
||||
|
||||
script.ScriptingProvider = provider;
|
||||
provider.InternalScripts.Add(script);
|
||||
return script;
|
||||
}
|
||||
|
||||
public PropertyScript? CreateScriptInstance(ILayerProperty layerProperty, ScriptConfiguration scriptConfiguration)
|
||||
{
|
||||
if (scriptConfiguration.Script != null)
|
||||
throw new ArtemisCoreException("The provided script configuration already has an active script");
|
||||
|
||||
ScriptingProvider? provider = _scriptingProviders.FirstOrDefault(p => p.Id == scriptConfiguration.ScriptingProviderId);
|
||||
if (provider == null)
|
||||
return null;
|
||||
|
||||
PropertyScript script = (PropertyScript) provider.Plugin.Kernel!.Get(
|
||||
provider.PropertyScriptType,
|
||||
CreateScriptConstructorArgument(provider.PropertyScriptType, layerProperty),
|
||||
CreateScriptConstructorArgument(provider.PropertyScriptType, scriptConfiguration)
|
||||
);
|
||||
|
||||
script.ScriptingProvider = provider;
|
||||
provider.InternalScripts.Add(script);
|
||||
return script;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void DeleteScript(ScriptConfiguration scriptConfiguration)
|
||||
{
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Artemis.Storage.Entities.General;
|
||||
using Artemis.Storage.Entities.Profile.Abstract;
|
||||
using Artemis.Storage.Entities.Profile.AdaptionHints;
|
||||
using LiteDB;
|
||||
@ -13,7 +12,6 @@ namespace Artemis.Storage.Entities.Profile
|
||||
{
|
||||
Leds = new List<LedEntity>();
|
||||
AdaptionHints = new List<IAdaptionHintEntity>();
|
||||
ScriptConfigurations = new List<ScriptConfigurationEntity>();
|
||||
PropertyEntities = new List<PropertyEntity>();
|
||||
LayerEffects = new List<LayerEffectEntity>();
|
||||
ExpandedPropertyGroups = new List<string>();
|
||||
@ -25,7 +23,6 @@ namespace Artemis.Storage.Entities.Profile
|
||||
|
||||
public List<LedEntity> Leds { get; set; }
|
||||
public List<IAdaptionHintEntity> AdaptionHints { get; set; }
|
||||
public List<ScriptConfigurationEntity> ScriptConfigurations { get; set; }
|
||||
|
||||
[BsonRef("ProfileEntity")]
|
||||
public ProfileEntity Profile { get; set; }
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using Artemis.Storage.Entities.General;
|
||||
using Artemis.Storage.Entities.Profile.DataBindings;
|
||||
|
||||
namespace Artemis.Storage.Entities.Profile
|
||||
@ -10,7 +9,6 @@ namespace Artemis.Storage.Entities.Profile
|
||||
{
|
||||
KeyframeEntities = new List<KeyframeEntity>();
|
||||
DataBindingEntities = new List<DataBindingEntity>();
|
||||
ScriptConfigurations = new List<ScriptConfigurationEntity>();
|
||||
}
|
||||
|
||||
public string FeatureId { get; set; }
|
||||
@ -21,6 +19,5 @@ namespace Artemis.Storage.Entities.Profile
|
||||
|
||||
public List<KeyframeEntity> KeyframeEntities { get; set; }
|
||||
public List<DataBindingEntity> DataBindingEntities { get; set; }
|
||||
public List<ScriptConfigurationEntity> ScriptConfigurations { get; set; }
|
||||
}
|
||||
}
|
||||
@ -8,13 +8,49 @@ namespace Artemis.UI.Shared.ScriptingProviders
|
||||
/// </summary>
|
||||
public class ScriptEditorViewModel : Screen, IScriptEditorViewModel
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public ScriptEditorViewModel(Script script)
|
||||
private Script? _script;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of <see cref="ScriptEditorViewModel" />
|
||||
/// </summary>
|
||||
/// <param name="scriptType">The script type this view model was created for</param>
|
||||
public ScriptEditorViewModel(ScriptType scriptType)
|
||||
{
|
||||
ScriptType = scriptType;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called just before the script is changed to a different one
|
||||
/// </summary>
|
||||
/// <param name="script">The script to display or <see langword="null" /> if no script is to be displayed</param>
|
||||
protected virtual void OnScriptChanging(Script? script)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called after the script was changed to a different one
|
||||
/// </summary>
|
||||
/// <param name="script">The script to display or <see langword="null" /> if no script is to be displayed</param>
|
||||
protected virtual void OnScriptChanged(Script? script)
|
||||
{
|
||||
Script = script;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Script Script { get; }
|
||||
public ScriptType ScriptType { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public Script? Script
|
||||
{
|
||||
get => _script;
|
||||
internal set => SetAndNotify(ref _script, value);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void ChangeScript(Script? script)
|
||||
{
|
||||
OnScriptChanging(script);
|
||||
Script = script;
|
||||
OnScriptChanged(script);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -113,8 +113,6 @@ namespace Artemis.UI.Ninject.Factories
|
||||
public interface IScriptVmFactory : IVmFactory
|
||||
{
|
||||
ScriptsDialogViewModel ScriptsDialogViewModel(Profile profile);
|
||||
ScriptsDialogViewModel ScriptsDialogViewModel(Layer layer);
|
||||
ScriptsDialogViewModel ScriptsDialogViewModel(ILayerProperty layerProperty);
|
||||
ScriptConfigurationViewModel ScriptConfigurationViewModel(ScriptConfiguration scriptConfiguration);
|
||||
}
|
||||
|
||||
|
||||
@ -234,7 +234,7 @@ namespace Artemis.UI.Screens.ProfileEditor.LayerProperties
|
||||
NotifyOfPropertyChange(nameof(TimeCaretPosition));
|
||||
}
|
||||
|
||||
private void ProfileEditorServiceOnSuspendEditingChanged(object? sender, EventArgs e)
|
||||
private void ProfileEditorServiceOnSuspendEditingChanged(object sender, EventArgs e)
|
||||
{
|
||||
NotifyOfPropertyChange(nameof(SuspendedEditing));
|
||||
}
|
||||
|
||||
@ -65,6 +65,9 @@
|
||||
<MenuItem Header="View Properties"
|
||||
Icon="{materialDesign:PackIcon Kind=Settings}"
|
||||
Command="{s:Action ViewProperties}"/>
|
||||
<MenuItem Header="_View Scripts"
|
||||
Icon="{materialDesign:PackIcon Kind=BookEdit}"
|
||||
Command="{s:Action ViewScripts}"/>
|
||||
<MenuItem Header="Adapt Profile"
|
||||
Icon="{materialDesign:PackIcon Kind=Magic}"
|
||||
Command="{s:Action AdaptProfile}"/>
|
||||
@ -110,19 +113,6 @@
|
||||
IsChecked="{Binding StopOnFocusLoss.Value}"
|
||||
InputGestureText="Shift+F5" />
|
||||
</MenuItem>
|
||||
<MenuItem Header="_Scripting" IsEnabled="False">
|
||||
<MenuItem Header="_Profile Scripts"
|
||||
Icon="{materialDesign:PackIcon Kind=BookEdit}"
|
||||
Command="{s:Action OpenProfileScripts}"/>
|
||||
<MenuItem Header="_Layer Scripts"
|
||||
Icon="{materialDesign:PackIcon Kind=Layers}"
|
||||
IsEnabled="{Binding HasSelectedElement}"
|
||||
Command="{s:Action OpenLayerScripts}"/>
|
||||
<MenuItem Header="_Property Scripts"
|
||||
Icon="{materialDesign:PackIcon Kind=FormTextbox}"
|
||||
IsEnabled="{Binding HasSelectedElement}"
|
||||
Command="{s:Action OpenLayerPropertyScripts}"/>
|
||||
</MenuItem>
|
||||
<MenuItem Header="_Options">
|
||||
<MenuItem Header="Focus Selected Layer"
|
||||
ToolTip="If enabled, displays only the layer you currently have selected"
|
||||
|
||||
@ -249,6 +249,11 @@ namespace Artemis.UI.Screens.ProfileEditor
|
||||
await _sidebarVmFactory.SidebarProfileConfigurationViewModel(_profileEditorService.SelectedProfileConfiguration).ViewProperties();
|
||||
}
|
||||
|
||||
public void ViewScripts()
|
||||
{
|
||||
_windowManager.ShowWindow(_scriptVmFactory.ScriptsDialogViewModel(ProfileConfiguration.Profile));
|
||||
}
|
||||
|
||||
public async Task AdaptProfile()
|
||||
{
|
||||
if (_profileEditorService.SelectedProfileConfiguration?.Profile == null)
|
||||
@ -304,22 +309,7 @@ namespace Artemis.UI.Screens.ProfileEditor
|
||||
_profileEditorService.PasteProfileElement(rootFolder, rootFolder.Children.Count);
|
||||
}
|
||||
}
|
||||
|
||||
public void OpenProfileScripts()
|
||||
{
|
||||
_windowManager.ShowWindow(_scriptVmFactory.ScriptsDialogViewModel(ProfileConfiguration.Profile));
|
||||
}
|
||||
|
||||
public void OpenLayerScripts()
|
||||
{
|
||||
if (_profileEditorService.SelectedProfileElement is Layer layer)
|
||||
_windowManager.ShowWindow(_scriptVmFactory.ScriptsDialogViewModel(layer));
|
||||
}
|
||||
|
||||
public void OpenLayerPropertyScripts()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public void OpenDebugger()
|
||||
{
|
||||
_debugService.ShowDebugger();
|
||||
|
||||
@ -391,7 +391,7 @@ namespace Artemis.UI.Screens.ProfileEditor.Visualization
|
||||
ActivateToolByIndex(2);
|
||||
}
|
||||
|
||||
private void ProfileEditorServiceOnSuspendEditingChanged(object? sender, EventArgs e)
|
||||
private void ProfileEditorServiceOnSuspendEditingChanged(object sender, EventArgs e)
|
||||
{
|
||||
NotifyOfPropertyChange(nameof(SuspendedEditing));
|
||||
UpdateCanSelectEditTool();
|
||||
|
||||
@ -3,16 +3,15 @@ using System.Threading.Tasks;
|
||||
using Artemis.Core.ScriptingProviders;
|
||||
using Artemis.Core.Services;
|
||||
using Artemis.UI.Screens.Scripting.Dialogs;
|
||||
using Artemis.UI.Shared.ScriptingProviders;
|
||||
using Artemis.UI.Shared.Services;
|
||||
using Stylet;
|
||||
|
||||
namespace Artemis.UI.Screens.Scripting
|
||||
{
|
||||
public class ScriptConfigurationViewModel : Conductor<IScriptEditorViewModel>
|
||||
public class ScriptConfigurationViewModel : PropertyChangedBase
|
||||
{
|
||||
private readonly IScriptingService _scriptingService;
|
||||
private readonly IDialogService _dialogService;
|
||||
private readonly IScriptingService _scriptingService;
|
||||
|
||||
public ScriptConfigurationViewModel(ScriptConfiguration scriptConfiguration, IScriptingService scriptingService, IDialogService dialogService)
|
||||
{
|
||||
@ -20,64 +19,9 @@ namespace Artemis.UI.Screens.Scripting
|
||||
_dialogService = dialogService;
|
||||
ScriptConfiguration = scriptConfiguration;
|
||||
Script = ScriptConfiguration.Script;
|
||||
|
||||
ActiveItem = Script switch
|
||||
{
|
||||
GlobalScript globalScript => Script.ScriptingProvider.CreateGlobalScriptEditor(globalScript),
|
||||
LayerScript layerScript => Script.ScriptingProvider.CreateLayerScriptScriptEditor(layerScript),
|
||||
ProfileScript profileScript => Script.ScriptingProvider.CreateProfileScriptEditor(profileScript),
|
||||
PropertyScript propertyScript => Script.ScriptingProvider.CreatePropertyScriptEditor(propertyScript),
|
||||
_ => new UnknownScriptEditorViewModel(null)
|
||||
};
|
||||
}
|
||||
|
||||
public Script Script { get; set; }
|
||||
public ScriptConfiguration ScriptConfiguration { get; }
|
||||
|
||||
public async Task ViewProperties()
|
||||
{
|
||||
object result = await _dialogService.ShowDialog<ScriptConfigurationEditViewModel>(new Dictionary<string, object>
|
||||
{
|
||||
{"scriptConfiguration", ScriptConfiguration}
|
||||
});
|
||||
|
||||
if (result is nameof(ScriptConfigurationEditViewModel.Delete))
|
||||
await Delete();
|
||||
}
|
||||
|
||||
private async Task Delete()
|
||||
{
|
||||
bool result = await _dialogService.ShowConfirmDialogAt("ScriptsDialog", "Delete script", $"Are you sure you want to delete '{ScriptConfiguration.Name}'?");
|
||||
if (!result)
|
||||
return;
|
||||
|
||||
switch (Script)
|
||||
{
|
||||
case GlobalScript globalScript:
|
||||
_scriptingService.DeleteScript(ScriptConfiguration);
|
||||
break;
|
||||
case LayerScript layerScript:
|
||||
layerScript.Layer.ScriptConfigurations.Remove(ScriptConfiguration);
|
||||
break;
|
||||
case ProfileScript profileScript:
|
||||
profileScript.Profile.ScriptConfigurations.Remove(ScriptConfiguration);
|
||||
break;
|
||||
case PropertyScript propertyScript:
|
||||
propertyScript.LayerProperty.ScriptConfigurations.Remove(ScriptConfiguration);
|
||||
break;
|
||||
}
|
||||
|
||||
ScriptConfiguration.DiscardPendingChanges();
|
||||
ScriptConfiguration.Script?.Dispose();
|
||||
RequestClose();
|
||||
}
|
||||
}
|
||||
|
||||
public class UnknownScriptEditorViewModel : ScriptEditorViewModel
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public UnknownScriptEditorViewModel(Script script) : base(script)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -25,6 +25,7 @@
|
||||
<ResourceDictionary.MergedDictionaries>
|
||||
<ResourceDictionary Source="../Sidebar/ArtemisSidebar.xaml" />
|
||||
</ResourceDictionary.MergedDictionaries>
|
||||
<shared:NullToVisibilityConverter x:Key="NullToVisibilityConverter" />
|
||||
</ResourceDictionary>
|
||||
</mde:MaterialWindow.Resources>
|
||||
<materialDesign:DialogHost IsTabStop="False" Focusable="False" Identifier="ScriptsDialog" DialogTheme="Inherit">
|
||||
@ -45,16 +46,16 @@
|
||||
Margin="0 10 0 0"
|
||||
ItemContainerStyle="{StaticResource SidebarListBoxItem}"
|
||||
HorizontalContentAlignment="Stretch"
|
||||
ItemsSource="{Binding Items}"
|
||||
SelectedItem="{Binding ActiveItem}">
|
||||
ItemsSource="{Binding ScriptConfigurations}"
|
||||
SelectedItem="{Binding SelectedScript}">
|
||||
<ListBox.ItemTemplate>
|
||||
<DataTemplate DataType="{x:Type local:ScriptConfigurationViewModel}">
|
||||
<Grid>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="Auto" />
|
||||
<ColumnDefinition Width="*" />
|
||||
<ColumnDefinition Width="Auto"/>
|
||||
<ColumnDefinition Width="Auto"/>
|
||||
<ColumnDefinition Width="Auto" />
|
||||
<ColumnDefinition Width="Auto" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition />
|
||||
@ -69,7 +70,7 @@
|
||||
Margin="0 0 10 0"
|
||||
VerticalAlignment="Center" />
|
||||
<TextBlock Grid.Row="0" Grid.Column="1" VerticalAlignment="Center" Text="{Binding ScriptConfiguration.Name}"
|
||||
Visibility="{Binding ScriptConfiguration.HasChanges, Converter={x:Static s:BoolToVisibilityConverter.InverseInstance}, Mode=OneWay}"/>
|
||||
Visibility="{Binding ScriptConfiguration.HasChanges, Converter={x:Static s:BoolToVisibilityConverter.InverseInstance}, Mode=OneWay}" />
|
||||
<TextBlock Grid.Row="0" Grid.Column="1" VerticalAlignment="Center"
|
||||
Visibility="{Binding ScriptConfiguration.HasChanges, Converter={x:Static s:BoolToVisibilityConverter.Instance}, Mode=OneWay}">
|
||||
<Run Text="{Binding ScriptConfiguration.Name}" FontWeight="Bold" />
|
||||
@ -81,7 +82,15 @@
|
||||
Foreground="{DynamicResource MaterialDesignBodyLight}"
|
||||
VerticalAlignment="Center" />
|
||||
|
||||
<Button Grid.Row="0" Grid.RowSpan="2" Grid.Column="2" ToolTip="View properties" Width="20" Height="20" Command="{s:Action ViewProperties}" s:View.ActionTarget="{Binding}" HorizontalAlignment="Right">
|
||||
<Button Grid.Row="0"
|
||||
Grid.RowSpan="2"
|
||||
Grid.Column="2"
|
||||
ToolTip="View properties"
|
||||
Width="20"
|
||||
Height="20"
|
||||
Command="{s:Action ViewProperties}"
|
||||
CommandParameter="{Binding}"
|
||||
HorizontalAlignment="Right">
|
||||
<Button.Style>
|
||||
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource MaterialDesignIconForegroundButton}">
|
||||
<Setter Property="Visibility" Value="Collapsed" />
|
||||
@ -94,7 +103,8 @@
|
||||
</Button.Style>
|
||||
<materialDesign:PackIcon Kind="Cog" Width="16" Height="16" />
|
||||
</Button>
|
||||
<ToggleButton Grid.Row="0" Grid.RowSpan="2" Grid.Column="3" ToolTip="Suspend profile" Width="18" Height="18" Margin="2 0 0 0" IsChecked="{Binding ScriptConfiguration.IsSuspended}">
|
||||
<ToggleButton Grid.Row="0" Grid.RowSpan="2" Grid.Column="3" ToolTip="Suspend profile" Width="18" Height="18" Margin="2 0 0 0"
|
||||
IsChecked="{Binding ScriptConfiguration.IsSuspended}">
|
||||
<ToggleButton.Style>
|
||||
<Style TargetType="{x:Type ToggleButton}" BasedOn="{StaticResource MaterialDesignFlatToggleButton}">
|
||||
<Setter Property="Visibility" Value="Collapsed" />
|
||||
@ -113,17 +123,31 @@
|
||||
</ListBox.ItemTemplate>
|
||||
</ListBox>
|
||||
|
||||
<Button Grid.Row="1"
|
||||
<StackPanel Grid.Row="0"
|
||||
Visibility="{Binding HasScripts, Converter={x:Static s:BoolToVisibilityConverter.InverseInstance}, Mode=OneWay}"
|
||||
VerticalAlignment="Center"
|
||||
HorizontalAlignment="Center" Margin="0 20 0 10">
|
||||
<TextBlock Style="{StaticResource MaterialDesignBody2TextBlock}" HorizontalAlignment="Center">Your scripts will be listed here</TextBlock>
|
||||
</StackPanel>
|
||||
<Button Grid.Row="1"
|
||||
Style="{StaticResource MaterialDesignOutlinedButton}"
|
||||
Content="ADD NEW SCRIPT"
|
||||
Margin="10 10 10 0"
|
||||
Content="ADD NEW SCRIPT"
|
||||
Margin="10 10 10 0"
|
||||
Command="{s:Action AddScriptConfiguration}"
|
||||
VerticalAlignment="Top" />
|
||||
</Grid>
|
||||
</Border>
|
||||
|
||||
<!-- Script editor -->
|
||||
<ContentControl Grid.Column="1" s:View.Model="{Binding ActiveItem.ActiveItem}" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" IsTabStop="False" />
|
||||
<ContentControl Grid.Column="1" s:View.Model="{Binding ActiveItem}" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" IsTabStop="False" />
|
||||
<StackPanel Grid.Column="1"
|
||||
Visibility="{Binding ActiveItem, Converter={StaticResource NullToVisibilityConverter}, ConverterParameter=Inverted}"
|
||||
VerticalAlignment="Center"
|
||||
HorizontalAlignment="Center">
|
||||
<materialDesign:PackIcon Kind="CodeGreaterThanOrEqual" Width="150" Height="150" HorizontalAlignment="Center" />
|
||||
<TextBlock Style="{StaticResource MaterialDesignHeadline4TextBlock}" HorizontalAlignment="Center">Get ready to start scripting!</TextBlock>
|
||||
<TextBlock Style="{StaticResource MaterialDesignSubtitle1TextBlock}" HorizontalAlignment="Center">Use the sidebar to create new scripts and manage existing scripts.</TextBlock>
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</materialDesign:DialogHost>
|
||||
</mde:MaterialWindow>
|
||||
@ -1,4 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Specialized;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Artemis.Core;
|
||||
@ -11,16 +13,15 @@ using Stylet;
|
||||
|
||||
namespace Artemis.UI.Screens.Scripting
|
||||
{
|
||||
public class ScriptsDialogViewModel : Conductor<ScriptConfigurationViewModel>.Collection.OneActive
|
||||
public class ScriptsDialogViewModel : Conductor<IScriptEditorViewModel>
|
||||
{
|
||||
private readonly IScriptingService _scriptingService;
|
||||
private readonly IDialogService _dialogService;
|
||||
private readonly IProfileService _profileService;
|
||||
private readonly IProfileEditorService _profileEditorService;
|
||||
private readonly IScriptVmFactory _scriptVmFactory;
|
||||
public Profile Profile { get; }
|
||||
public Layer Layer { get; }
|
||||
public ILayerProperty LayerProperty { get; }
|
||||
private readonly Dictionary<ScriptingProvider, IScriptEditorViewModel> _providerViewModels = new();
|
||||
private ScriptConfigurationViewModel _selectedScript;
|
||||
|
||||
public ScriptsDialogViewModel(Profile profile,
|
||||
IScriptingService scriptingService,
|
||||
@ -36,27 +37,56 @@ namespace Artemis.UI.Screens.Scripting
|
||||
_scriptVmFactory = scriptVmFactory;
|
||||
|
||||
DisplayName = "Artemis | Profile Scripts";
|
||||
ScriptType = ScriptType.Profile;
|
||||
Profile = profile ?? throw new ArgumentNullException(nameof(profile));
|
||||
|
||||
Items.AddRange(Profile.ScriptConfigurations.Select(scriptVmFactory.ScriptConfigurationViewModel));
|
||||
ScriptConfigurations.AddRange(Profile.ScriptConfigurations.Select(scriptVmFactory.ScriptConfigurationViewModel));
|
||||
ScriptConfigurations.CollectionChanged += ItemsOnCollectionChanged;
|
||||
}
|
||||
|
||||
public ScriptsDialogViewModel(Layer layer, IDialogService dialogService)
|
||||
{
|
||||
_dialogService = dialogService;
|
||||
DisplayName = "Artemins | Layer Scripts";
|
||||
Layer = layer ?? throw new ArgumentNullException(nameof(layer));
|
||||
public ScriptType ScriptType { get; }
|
||||
public Profile Profile { get; }
|
||||
public BindableCollection<ScriptConfigurationViewModel> ScriptConfigurations { get; } = new();
|
||||
public bool HasScripts => ScriptConfigurations.Any();
|
||||
|
||||
Items.AddRange(Layer.ScriptConfigurations.Select(_scriptVmFactory.ScriptConfigurationViewModel));
|
||||
public ScriptConfigurationViewModel SelectedScript
|
||||
{
|
||||
get => _selectedScript;
|
||||
set
|
||||
{
|
||||
if (!SetAndNotify(ref _selectedScript, value)) return;
|
||||
SetupScriptEditor(_selectedScript?.ScriptConfiguration);
|
||||
}
|
||||
}
|
||||
|
||||
public ScriptsDialogViewModel(ILayerProperty layerProperty, IDialogService dialogService)
|
||||
private void SetupScriptEditor(ScriptConfiguration scriptConfiguration)
|
||||
{
|
||||
_dialogService = dialogService;
|
||||
DisplayName = "Artemins | Layer Property Scripts";
|
||||
LayerProperty = layerProperty ?? throw new ArgumentNullException(nameof(layerProperty));
|
||||
if (scriptConfiguration == null)
|
||||
{
|
||||
ActiveItem = null;
|
||||
return;
|
||||
}
|
||||
|
||||
Items.AddRange(Layer.ScriptConfigurations.Select(_scriptVmFactory.ScriptConfigurationViewModel));
|
||||
// The script is null if the provider is missing
|
||||
if (scriptConfiguration.Script == null)
|
||||
{
|
||||
ActiveItem = null;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_providerViewModels.TryGetValue(scriptConfiguration.Script.ScriptingProvider, out IScriptEditorViewModel viewModel))
|
||||
{
|
||||
viewModel = scriptConfiguration.Script.ScriptingProvider.CreateScriptEditor(ScriptType);
|
||||
_providerViewModels.Add(scriptConfiguration.Script.ScriptingProvider, viewModel);
|
||||
}
|
||||
|
||||
ActiveItem = viewModel;
|
||||
ActiveItem.ChangeScript(scriptConfiguration.Script);
|
||||
}
|
||||
|
||||
private void ItemsOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
|
||||
{
|
||||
NotifyOfPropertyChange(nameof(HasScripts));
|
||||
}
|
||||
|
||||
public async Task AddScriptConfiguration()
|
||||
@ -70,30 +100,71 @@ namespace Artemis.UI.Screens.Scripting
|
||||
Profile.ScriptConfigurations.Add(scriptConfiguration);
|
||||
_scriptingService.CreateScriptInstance(Profile, scriptConfiguration);
|
||||
}
|
||||
else if (Layer != null)
|
||||
{
|
||||
Layer.ScriptConfigurations.Add(scriptConfiguration);
|
||||
_scriptingService.CreateScriptInstance(Layer, scriptConfiguration);
|
||||
}
|
||||
else if (LayerProperty != null)
|
||||
{
|
||||
LayerProperty.ScriptConfigurations.Add(scriptConfiguration);
|
||||
_scriptingService.CreateScriptInstance(LayerProperty, scriptConfiguration);
|
||||
}
|
||||
|
||||
Items.Add(_scriptVmFactory.ScriptConfigurationViewModel(scriptConfiguration));
|
||||
ScriptConfigurationViewModel viewModel = _scriptVmFactory.ScriptConfigurationViewModel(scriptConfiguration);
|
||||
ScriptConfigurations.Add(viewModel);
|
||||
SelectedScript = viewModel;
|
||||
}
|
||||
|
||||
#region Overrides of OneActive
|
||||
public async Task ViewProperties(ScriptConfigurationViewModel scriptConfigurationViewModel)
|
||||
{
|
||||
object result = await _dialogService.ShowDialogAt<ScriptConfigurationEditViewModel>(
|
||||
"ScriptsDialog",
|
||||
new Dictionary<string, object> {{"scriptConfiguration", scriptConfigurationViewModel.ScriptConfiguration}}
|
||||
);
|
||||
|
||||
if (result is nameof(ScriptConfigurationEditViewModel.Delete))
|
||||
await Delete(scriptConfigurationViewModel);
|
||||
}
|
||||
|
||||
private async Task Delete(ScriptConfigurationViewModel scriptConfigurationViewModel)
|
||||
{
|
||||
bool result = await _dialogService.ShowConfirmDialogAt(
|
||||
"ScriptsDialog",
|
||||
"Delete script",
|
||||
$"Are you sure you want to delete '{scriptConfigurationViewModel.ScriptConfiguration.Name}'?"
|
||||
);
|
||||
if (!result)
|
||||
return;
|
||||
|
||||
switch (scriptConfigurationViewModel.Script)
|
||||
{
|
||||
case GlobalScript:
|
||||
_scriptingService.DeleteScript(scriptConfigurationViewModel.ScriptConfiguration);
|
||||
break;
|
||||
case ProfileScript profileScript:
|
||||
profileScript.Profile.ScriptConfigurations.Remove(scriptConfigurationViewModel.ScriptConfiguration);
|
||||
break;
|
||||
}
|
||||
|
||||
scriptConfigurationViewModel.ScriptConfiguration.DiscardPendingChanges();
|
||||
scriptConfigurationViewModel.ScriptConfiguration.Script?.Dispose();
|
||||
|
||||
SelectedScript = null;
|
||||
ScriptConfigurations.Remove(scriptConfigurationViewModel);
|
||||
}
|
||||
|
||||
#region Overrides of Screen
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void OnInitialActivate()
|
||||
{
|
||||
SelectedScript = ScriptConfigurations.FirstOrDefault();
|
||||
base.OnInitialActivate();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void OnClose()
|
||||
{
|
||||
if (_profileEditorService.SelectedProfile == Profile)
|
||||
_profileEditorService.SaveSelectedProfileConfiguration();
|
||||
else
|
||||
_profileService.SaveProfile(Profile, false);
|
||||
if (Profile != null)
|
||||
{
|
||||
if (_profileEditorService.SelectedProfile == Profile)
|
||||
_profileEditorService.SaveSelectedProfileConfiguration();
|
||||
else
|
||||
_profileService.SaveProfile(Profile, false);
|
||||
}
|
||||
|
||||
ScriptConfigurations.CollectionChanged -= ItemsOnCollectionChanged;
|
||||
base.OnClose();
|
||||
}
|
||||
|
||||
|
||||
@ -154,7 +154,7 @@ namespace Artemis.UI.Screens.Sidebar
|
||||
VistaOpenFileDialog dialog = new()
|
||||
{
|
||||
Filter = "Artemis Profile|*.json",
|
||||
Title = "Export Artemis profile"
|
||||
Title = "Import Artemis profile"
|
||||
};
|
||||
bool? result = dialog.ShowDialog();
|
||||
if (result != true)
|
||||
|
||||
@ -92,9 +92,8 @@ namespace Artemis.UI.Screens.Sidebar
|
||||
ProfileConfigurationExportModel profileConfigurationExportModel = _profileService.ExportProfile(ProfileConfiguration);
|
||||
string json = JsonConvert.SerializeObject(profileConfigurationExportModel, IProfileService.ExportSettings);
|
||||
|
||||
if (!dialog.FileName.EndsWith(".json"))
|
||||
dialog.FileName += ".json";
|
||||
await File.WriteAllTextAsync(dialog.FileName, json);
|
||||
string path = Path.ChangeExtension(dialog.FileName, ".json");
|
||||
await File.WriteAllTextAsync(path, json);
|
||||
}
|
||||
|
||||
public void Duplicate()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user