diff --git a/Artemis/Artemis/Utilities/GeneralHelpers.cs b/Artemis/Artemis/Utilities/GeneralHelpers.cs index c1c6f9395..25fd04a0e 100644 --- a/Artemis/Artemis/Utilities/GeneralHelpers.cs +++ b/Artemis/Artemis/Utilities/GeneralHelpers.cs @@ -2,9 +2,13 @@ using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; +using System.IO; using System.Reflection; +using System.Runtime.Serialization; +using System.Runtime.Serialization.Formatters.Binary; using System.Security.Principal; using System.Windows; +using System.Xml.Serialization; using static System.String; namespace Artemis.Utilities @@ -52,6 +56,28 @@ namespace Artemis.Utilities } } + /// + /// Perform a deep Copy of the object. + /// + /// The type of object being copied. + /// The object instance to copy. + /// The copied object. + public static T Clone(T source) + { + // Don't serialize a null object, simply return the default for that object + if (ReferenceEquals(source, null)) + return default(T); + + var serializer = new XmlSerializer(typeof(T)); + Stream stream = new MemoryStream(); + using (stream) + { + serializer.Serialize(stream, source); + stream.Seek(0, SeekOrigin.Begin); + return (T)serializer.Deserialize(stream); + } + } + public static object GetPropertyValue(object o, string path) { var propertyNames = path.Split('.'); diff --git a/Artemis/Artemis/ViewModels/ProfileEditorViewModel.cs b/Artemis/Artemis/ViewModels/ProfileEditorViewModel.cs index 25a33eb3a..5bf104d05 100644 --- a/Artemis/Artemis/ViewModels/ProfileEditorViewModel.cs +++ b/Artemis/Artemis/ViewModels/ProfileEditorViewModel.cs @@ -9,12 +9,14 @@ using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; +using System.Xml.Serialization; using Artemis.DAL; using Artemis.Events; using Artemis.KeyboardProviders; using Artemis.Managers; using Artemis.Models; using Artemis.Models.Profiles; +using Artemis.Utilities; using Artemis.ViewModels.LayerEditor; using Caliburn.Micro; using MahApps.Metro; @@ -194,12 +196,22 @@ namespace Artemis.ViewModels private KeyboardProvider ActiveKeyboard => _mainManager.KeyboardManager.ActiveKeyboard; + /// + /// Handles chaning the active keyboard, updating the preview image and profiles collection + /// + /// public void Handle(ActiveKeyboardChanged message) { NotifyOfPropertyChange(() => KeyboardImage); NotifyOfPropertyChange(() => PreviewSettings); + LoadProfiles(); } + /// + /// Handles refreshing the layer preview + /// + /// + /// private void PropertyChangeHandler(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == "KeyboardPreview") @@ -211,6 +223,9 @@ namespace Artemis.ViewModels ProfileProvider.AddOrUpdate(SelectedProfile); } + /// + /// Loads all profiles for the current game and keyboard + /// private void LoadProfiles() { Profiles.Clear(); @@ -218,6 +233,9 @@ namespace Artemis.ViewModels SelectedProfile = Profiles.FirstOrDefault(); } + /// + /// Adds a new profile to the current game and keyboard + /// public async void AddProfile() { var name = await _mainManager.DialogService.ShowInputDialog("Add new profile", @@ -254,6 +272,10 @@ namespace Artemis.ViewModels NotifyOfPropertyChange(() => KeyboardPreview); } + /// + /// Opens a new LayerEditorView for the given layer + /// + /// The layer to open the view for public void LayerEditor(LayerModel layer) { IWindowManager manager = new WindowManager(); @@ -262,8 +284,15 @@ namespace Artemis.ViewModels settings.Title = "Artemis | Edit " + layer.Name; manager.ShowDialog(_editorVm, null, settings); + + // Refresh the layer list and reselect the last layer + NotifyOfPropertyChange(() => Layers); + SelectedLayer = layer; } + /// + /// Adds a new layer to the profile and selects it + /// public void AddLayer() { if (_selectedProfile == null) @@ -275,6 +304,9 @@ namespace Artemis.ViewModels SelectedLayer = layer; } + /// + /// Removes the currently selected layer from the profile + /// public void RemoveLayer() { if (_selectedProfile == null || _selectedLayer == null) @@ -286,7 +318,11 @@ namespace Artemis.ViewModels SelectedProfile.FixOrder(); } - public void RemoveLayer(LayerModel layer) + /// + /// Removes the given layer from the profile + /// + /// + public void RemoveLayerFromMenu(LayerModel layer) { SelectedProfile.Layers.Remove(layer); Layers.Remove(layer); @@ -294,6 +330,23 @@ namespace Artemis.ViewModels SelectedProfile.FixOrder(); } + /// + /// Clones the given layer and adds it to the profile, on top of the original + /// + /// + public void CloneLayer(LayerModel layer) + { + var clone = GeneralHelpers.Clone(layer); + clone.Order = layer.Order - 1; + SelectedProfile.Layers.Add(clone); + Layers.Add(clone); + + SelectedProfile.FixOrder(); + } + + /// + /// Moves the currently selected layer up in the profile's layer tree + /// public void LayerUp() { if (SelectedLayer == null) @@ -310,6 +363,9 @@ namespace Artemis.ViewModels SelectedLayer = reorderLayer; } + /// + /// Moves the currently selected layer down in the profile's layer tree + /// public void LayerDown() { if (SelectedLayer == null) @@ -326,12 +382,21 @@ namespace Artemis.ViewModels SelectedLayer = reorderLayer; } + /// + /// Handler for clicking + /// + /// public void MouseDownKeyboardPreview(MouseButtonEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) _downTime = DateTime.Now; } + /// + /// Second handler for clicking, selects a the layer the user clicked on + /// if the used clicked on an empty spot, deselects the current layer + /// + /// public void MouseUpKeyboardPreview(MouseButtonEventArgs e) { var timeSinceDown = DateTime.Now - _downTime; @@ -347,6 +412,10 @@ namespace Artemis.ViewModels SelectedLayer = hoverLayer; } + /// + /// Handler for resizing and moving the currently selected layer + /// + /// public void MouseMoveKeyboardPreview(MouseEventArgs e) { var pos = e.GetPosition((Image) e.OriginalSource); @@ -377,6 +446,13 @@ namespace Artemis.ViewModels KeyboardPreviewCursor = Cursors.Hand; } + /// + /// Handles dragging the given layer + /// + /// + /// + /// + /// private void HandleDragging(MouseEventArgs e, double x, double y, LayerModel hoverLayer) { // Reset the dragging state on mouse release diff --git a/Artemis/Artemis/Views/ProfileEditorView.xaml b/Artemis/Artemis/Views/ProfileEditorView.xaml index 976417379..257b2444e 100644 --- a/Artemis/Artemis/Views/ProfileEditorView.xaml +++ b/Artemis/Artemis/Views/ProfileEditorView.xaml @@ -78,7 +78,8 @@ - + +