using System; using Artemis.Core.Models.Profile; using Artemis.Core.Services.Storage.Interfaces; using Artemis.UI.Services.Interfaces; namespace Artemis.UI.Services { public class ProfileEditorService : IProfileEditorService { private readonly IProfileService _profileService; private TimeSpan _currentTime; private TimeSpan _lastUpdateTime; public ProfileEditorService(IProfileService profileService) { _profileService = profileService; } 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 void ChangeSelectedProfile(Profile profile) { SelectedProfile = profile; UpdateProfilePreview(); OnSelectedProfileChanged(); } public void UpdateSelectedProfile() { _profileService.UpdateProfile(SelectedProfile, false); UpdateProfilePreview(); OnSelectedProfileElementUpdated(); } public void ChangeSelectedProfileElement(ProfileElement profileElement) { SelectedProfileElement = profileElement; OnSelectedProfileElementChanged(); } public void UpdateSelectedProfileElement() { _profileService.UpdateProfile(SelectedProfile, true); UpdateProfilePreview(); OnSelectedProfileElementUpdated(); } private void UpdateProfilePreview() { if (SelectedProfile == null) return; var delta = CurrentTime - _lastUpdateTime; foreach (var layer in SelectedProfile.GetAllLayers()) { // Override keyframe progress foreach (var baseLayerProperty in layer.Properties) baseLayerProperty.KeyframeEngine?.OverrideProgress(CurrentTime); // Force layer shape to redraw layer.LayerShape?.CalculateRenderProperties(layer.PositionProperty.GetCurrentValue(), layer.SizeProperty.GetCurrentValue()); // Update the brush with the delta (which can now be negative ^^) layer.Update(delta.TotalSeconds); } _lastUpdateTime = CurrentTime; } public event EventHandler SelectedProfileChanged; public event EventHandler SelectedProfileUpdated; public event EventHandler SelectedProfileElementChanged; public event EventHandler SelectedProfileElementUpdated; public event EventHandler CurrentTimeChanged; protected virtual void OnSelectedProfileElementUpdated() { SelectedProfileElementUpdated?.Invoke(this, EventArgs.Empty); } protected virtual void OnSelectedProfileElementChanged() { SelectedProfileElementChanged?.Invoke(this, EventArgs.Empty); } protected virtual void OnSelectedProfileUpdated() { SelectedProfileUpdated?.Invoke(this, EventArgs.Empty); } protected virtual void OnSelectedProfileChanged() { SelectedProfileChanged?.Invoke(this, EventArgs.Empty); } protected virtual void OnCurrentTimeChanged() { CurrentTimeChanged?.Invoke(this, EventArgs.Empty); } } }