Profile editor - Moved layer property input to the Shared UI project

This commit is contained in:
SpoinkyNL committed 2020-06-02 22:23:09 +02:00
1 parent eda57f6d32
commit 92faafe1de
71 files changed
+1211 -434

No files matched your search

@@ -0,0 +1,22 @@
using System;
using Artemis.Core.Models.Profile;
namespace Artemis.UI.Shared.Events
{
public class ProfileElementEventArgs : EventArgs
{
public ProfileElementEventArgs(ProfileElement profileElement)
{
ProfileElement = profileElement;
}
public ProfileElementEventArgs(ProfileElement profileElement, ProfileElement previousProfileElement)
{
ProfileElement = profileElement;
PreviousProfileElement = previousProfileElement;
}
public ProfileElement ProfileElement { get; }
public ProfileElement PreviousProfileElement { get; }
}
}
@@ -0,0 +1,44 @@
using System;
using Artemis.Core;
using Artemis.Core.Plugins.Models;
using Artemis.UI.Shared.Services.Interfaces;
namespace Artemis.UI.Shared.PropertyInput
{
public class PropertyInputRegistration
{
private readonly IProfileEditorService _profileEditorService;
internal PropertyInputRegistration(IProfileEditorService profileEditorService, PluginInfo pluginInfo, Type supportedType, Type viewModelType)
{
_profileEditorService = profileEditorService;
PluginInfo = pluginInfo;
SupportedType = supportedType;
ViewModelType = viewModelType;
if (PluginInfo != Constants.CorePluginInfo)
PluginInfo.Instance.PluginDisabled += InstanceOnPluginDisabled;
}
public PluginInfo PluginInfo { get; }
public Type SupportedType { get; }
public Type ViewModelType { get; }
internal void Unsubscribe()
{
if (PluginInfo != Constants.CorePluginInfo)
PluginInfo.Instance.PluginDisabled -= InstanceOnPluginDisabled;
}
internal void Remove()
{
// It'll call Unsubscribe for us
_profileEditorService.RemovePropertyInput(this);
}
private void InstanceOnPluginDisabled(object sender, EventArgs e)
{
Remove();
}
}
}
@@ -0,0 +1,117 @@
using System;
using Artemis.Core.Models.Profile.LayerProperties;
using Artemis.UI.Shared.Services.Interfaces;
using Stylet;
namespace Artemis.UI.Shared.PropertyInput
{
public abstract class PropertyInputViewModel<T> : ValidatingModelBase, IDisposable
{
private T _inputValue;
protected PropertyInputViewModel(LayerProperty<T> layerProperty, IProfileEditorService profileEditorService)
{
LayerProperty = layerProperty;
ProfileEditorService = profileEditorService;
LayerProperty.Updated += LayerPropertyOnUpdated;
UpdateInputValue();
}
protected PropertyInputViewModel(LayerProperty<T> layerProperty, IProfileEditorService profileEditorService, IModelValidator validator) : base(validator)
{
LayerProperty = layerProperty;
ProfileEditorService = profileEditorService;
LayerProperty.Updated += LayerPropertyOnUpdated;
UpdateInputValue();
}
public LayerProperty<T> LayerProperty { get; }
public IProfileEditorService ProfileEditorService { get; }
public bool InputDragging { get; private set; }
public T InputValue
{
get => _inputValue;
set
{
_inputValue = value;
ApplyInputValue();
}
}
public virtual void Dispose()
{
LayerProperty.Updated -= LayerPropertyOnUpdated;
}
protected virtual void OnInputValueApplied()
{
}
protected virtual void OnInputValueChanged()
{
}
protected void ApplyInputValue()
{
// Force the validator to run
if (Validator != null)
Validate();
// Only apply the input value to the layer property if the validator found no errors
if (!HasErrors)
SetCurrentValue(_inputValue, !InputDragging);
OnInputValueChanged();
OnInputValueApplied();
}
private void LayerPropertyOnUpdated(object sender, EventArgs e)
{
UpdateInputValue();
}
private void UpdateInputValue()
{
// Avoid unnecessary UI updates and validator cycles
if (_inputValue != null && _inputValue.Equals(LayerProperty.CurrentValue) || _inputValue == null && LayerProperty.CurrentValue == null)
return;
// Override the input value
_inputValue = LayerProperty.CurrentValue;
// Notify a change in the input value
OnInputValueChanged();
NotifyOfPropertyChange(nameof(InputValue));
// Force the validator to run with the overridden value
if (Validator != null)
Validate();
}
#region Event handlers
public void InputDragStarted(object sender, EventArgs e)
{
InputDragging = true;
}
public void InputDragEnded(object sender, EventArgs e)
{
InputDragging = false;
ProfileEditorService.UpdateSelectedProfileElement();
}
private void SetCurrentValue(T value, bool saveChanges)
{
LayerProperty.SetCurrentValue(value, ProfileEditorService.CurrentTime);
if (saveChanges)
ProfileEditorService.UpdateSelectedProfileElement();
else
ProfileEditorService.UpdateProfilePreview();
}
#endregion
}
}
@@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using Artemis.Core.Models.Profile;
using Artemis.Core.Plugins.Abstract;
using Artemis.Core.Plugins.Models;
using Artemis.UI.Shared.Events;
using Artemis.UI.Shared.PropertyInput;
using Ninject;
namespace Artemis.UI.Shared.Services.Interfaces
{
public interface IProfileEditorService : IArtemisSharedUIService
{
Profile SelectedProfile { get; }
ProfileElement SelectedProfileElement { get; }
TimeSpan CurrentTime { get; set; }
int PixelsPerSecond { get; set; }
IReadOnlyList<PropertyInputRegistration> RegisteredPropertyEditors { get; }
IKernel Kernel { get; }
void ChangeSelectedProfile(Profile profile);
void UpdateSelectedProfile();
void ChangeSelectedProfileElement(ProfileElement profileElement);
void UpdateSelectedProfileElement();
void UpdateProfilePreview();
void UndoUpdateProfile(ProfileModule module);
void RedoUpdateProfile(ProfileModule module);
void StopRegularRender();
void ResumeRegularRender();
/// <summary>
/// Occurs when a new profile is selected
/// </summary>
event EventHandler<ProfileElementEventArgs> ProfileSelected;
/// <summary>
/// Occurs then the currently selected profile is updated
/// </summary>
event EventHandler<ProfileElementEventArgs> SelectedProfileUpdated;
/// <summary>
/// Occurs when a new profile element is selected
/// </summary>
event EventHandler<ProfileElementEventArgs> ProfileElementSelected;
/// <summary>
/// Occurs when the currently selected profile element is updated
/// </summary>
event EventHandler<ProfileElementEventArgs> SelectedProfileElementUpdated;
/// <summary>
/// Occurs when the current editor time is changed
/// </summary>
event EventHandler CurrentTimeChanged;
/// <summary>
/// Occurs when the pixels per second (zoom level) is changed
/// </summary>
event EventHandler PixelsPerSecondChanged;
/// <summary>
/// Occurs when the profile preview has been updated
/// </summary>
event EventHandler ProfilePreviewUpdated;
PropertyInputRegistration RegisterPropertyInput(PluginInfo pluginInfo, Type viewModelType);
void RemovePropertyInput(PropertyInputRegistration registration);
}
}
@@ -0,0 +1,240 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Artemis.Core.Models.Profile;
using Artemis.Core.Plugins.Abstract;
using Artemis.Core.Plugins.Exceptions;
using Artemis.Core.Plugins.Models;
using Artemis.Core.Services.Interfaces;
using Artemis.Core.Services.Storage.Interfaces;
using Artemis.UI.Shared.Events;
using Artemis.UI.Shared.PropertyInput;
using Artemis.UI.Shared.Services.Interfaces;
using Ninject;
namespace Artemis.UI.Shared.Services
{
public class ProfileEditorService : IProfileEditorService
{
private readonly ICoreService _coreService;
private readonly IProfileService _profileService;
private readonly List<PropertyInputRegistration> _registeredPropertyEditors;
private TimeSpan _currentTime;
private TimeSpan _lastUpdateTime;
private int _pixelsPerSecond;
public ProfileEditorService(ICoreService coreService, IProfileService profileService, IKernel kernel)
{
_coreService = coreService;
_profileService = profileService;
_registeredPropertyEditors = new List<PropertyInputRegistration>();
Kernel = kernel;
PixelsPerSecond = 31;
}
public IKernel Kernel { get; }
public IReadOnlyList<PropertyInputRegistration> RegisteredPropertyEditors => _registeredPropertyEditors.AsReadOnly();
public Profile SelectedProfile { get; private set; }
public ProfileElement SelectedProfileElement { get; private set; }
public TimeSpan CurrentTime
{
get => _currentTime;
set
{
if (_currentTime.Equals(value))
return;
_currentTime = value;
UpdateProfilePreview();
OnCurrentTimeChanged();
}
}
public int PixelsPerSecond
{
get => _pixelsPerSecond;
set
{
_pixelsPerSecond = value;
OnPixelsPerSecondChanged();
}
}
public void ChangeSelectedProfile(Profile profile)
{
ChangeSelectedProfileElement(null);
var profileElementEvent = new ProfileElementEventArgs(profile, SelectedProfile);
SelectedProfile = profile;
UpdateProfilePreview();
OnSelectedProfileChanged(profileElementEvent);
}
public void UpdateSelectedProfile()
{
_profileService.UpdateProfile(SelectedProfile, false);
UpdateProfilePreview();
OnSelectedProfileElementUpdated(new ProfileElementEventArgs(SelectedProfile));
}
public void ChangeSelectedProfileElement(ProfileElement profileElement)
{
var profileElementEvent = new ProfileElementEventArgs(profileElement, SelectedProfileElement);
SelectedProfileElement = profileElement;
OnSelectedProfileElementChanged(profileElementEvent);
}
public void UpdateSelectedProfileElement()
{
_profileService.UpdateProfile(SelectedProfile, true);
UpdateProfilePreview();
OnSelectedProfileElementUpdated(new ProfileElementEventArgs(SelectedProfileElement));
}
public void UpdateProfilePreview()
{
if (SelectedProfile == null)
return;
var delta = CurrentTime - _lastUpdateTime;
foreach (var layer in SelectedProfile.GetAllLayers())
{
layer.OverrideProgress(CurrentTime);
layer.LayerBrush?.Update(delta.TotalSeconds);
}
_lastUpdateTime = CurrentTime;
OnProfilePreviewUpdated();
}
public void UndoUpdateProfile(ProfileModule module)
{
var undid = _profileService.UndoUpdateProfile(SelectedProfile, module);
if (!undid)
return;
OnSelectedProfileChanged(new ProfileElementEventArgs(SelectedProfile, SelectedProfile));
if (SelectedProfileElement != null)
{
var elements = SelectedProfile.GetAllLayers().Cast<ProfileElement>().ToList();
elements.AddRange(SelectedProfile.GetAllFolders());
var element = elements.FirstOrDefault(l => l.EntityId == SelectedProfileElement.EntityId);
ChangeSelectedProfileElement(element);
}
UpdateProfilePreview();
}
public void RedoUpdateProfile(ProfileModule module)
{
var redid = _profileService.RedoUpdateProfile(SelectedProfile, module);
if (!redid)
return;
OnSelectedProfileChanged(new ProfileElementEventArgs(SelectedProfile, SelectedProfile));
if (SelectedProfileElement != null)
{
var elements = SelectedProfile.GetAllLayers().Cast<ProfileElement>().ToList();
elements.AddRange(SelectedProfile.GetAllFolders());
var element = elements.FirstOrDefault(l => l.EntityId == SelectedProfileElement.EntityId);
ChangeSelectedProfileElement(element);
}
UpdateProfilePreview();
}
public PropertyInputRegistration RegisterPropertyInput(PluginInfo pluginInfo, Type viewModelType)
{
// Bit ugly to do a name comparison but I don't know a nicer way right now
if (viewModelType.BaseType == null || viewModelType.BaseType.Name != typeof(PropertyInputViewModel<>).Name)
throw new ArtemisPluginException($"{nameof(viewModelType)} base type must be of type PropertyInputViewModel<T>");
lock (_registeredPropertyEditors)
{
var supportedType = viewModelType.BaseType.GetGenericArguments()[0];
var existing = _registeredPropertyEditors.FirstOrDefault(r => r.SupportedType == supportedType);
if (existing != null)
{
if (existing.PluginInfo != pluginInfo)
throw new ArtemisPluginException($"Cannot register property editor for type {supportedType.Name} because an editor was already registered by {pluginInfo.Name}");
return existing;
}
Kernel.Bind(viewModelType).ToSelf();
var registration = new PropertyInputRegistration(this, pluginInfo, supportedType, viewModelType);
_registeredPropertyEditors.Add(registration);
return registration;
}
}
public void RemovePropertyInput(PropertyInputRegistration registration)
{
lock (_registeredPropertyEditors)
{
if (_registeredPropertyEditors.Contains(registration))
{
registration.Unsubscribe();
_registeredPropertyEditors.Remove(registration);
Kernel.Unbind(registration.ViewModelType);
}
}
}
public event EventHandler<ProfileElementEventArgs> ProfileSelected;
public event EventHandler<ProfileElementEventArgs> SelectedProfileUpdated;
public event EventHandler<ProfileElementEventArgs> ProfileElementSelected;
public event EventHandler<ProfileElementEventArgs> SelectedProfileElementUpdated;
public event EventHandler CurrentTimeChanged;
public event EventHandler PixelsPerSecondChanged;
public event EventHandler ProfilePreviewUpdated;
public void StopRegularRender()
{
_coreService.ModuleUpdatingDisabled = true;
}
public void ResumeRegularRender()
{
_coreService.ModuleUpdatingDisabled = false;
}
protected virtual void OnSelectedProfileChanged(ProfileElementEventArgs e)
{
ProfileSelected?.Invoke(this, e);
}
protected virtual void OnSelectedProfileUpdated(ProfileElementEventArgs e)
{
SelectedProfileUpdated?.Invoke(this, e);
}
protected virtual void OnSelectedProfileElementChanged(ProfileElementEventArgs e)
{
ProfileElementSelected?.Invoke(this, e);
}
protected virtual void OnSelectedProfileElementUpdated(ProfileElementEventArgs e)
{
SelectedProfileElementUpdated?.Invoke(this, e);
}
protected virtual void OnCurrentTimeChanged()
{
CurrentTimeChanged?.Invoke(this, EventArgs.Empty);
}
protected virtual void OnPixelsPerSecondChanged()
{
PixelsPerSecondChanged?.Invoke(this, EventArgs.Empty);
}
protected virtual void OnProfilePreviewUpdated()
{
ProfilePreviewUpdated?.Invoke(this, EventArgs.Empty);
}
}
}