示例#1
0
            public InsertCopyableUndoEntry(ISelectParent parent, Copyable copyable, int right, string action)
                : base($"{parent.ChildString} {action}", parent)
            {
                this.right = right;

                init = copyable.Contents;
            }
示例#2
0
        void Copyable_Insert(Copyable paste, int right, bool imported)
        {
            List <Track> pasted;

            try {
                pasted = paste.Contents.Cast <Track>().ToList();
            } catch (InvalidCastException) {
                return;
            }

            Program.Project.Undo.Add($"Track {(imported? "Imported" : "Pasted")}", () => {
                for (int i = paste.Contents.Count - 1; i >= 0; i--)
                {
                    Program.Project.Remove(right + i + 1);
                }
            }, () => {
                for (int i = 0; i < paste.Contents.Count; i++)
                {
                    Program.Project.Insert(right + i + 1, pasted[i].Clone());
                }
            }, () => {
                foreach (Track track in pasted)
                {
                    track.Dispose();
                }
                pasted = null;
            });

            for (int i = 0; i < paste.Contents.Count; i++)
            {
                Program.Project.Insert(right + i + 1, pasted[i].Clone());
            }
        }
示例#3
0
        private void Copyable_Insert(Copyable paste, int right, bool imported)
        {
            List <Device> pasted;

            try {
                pasted = paste.Contents.Cast <Device>().ToList();
            } catch (InvalidCastException) {
                return;
            }

            List <int> path = Track.GetPath(_chain);

            Program.Project.Undo.Add($"Device {(imported? "Imported" : "Pasted")}", () => {
                Chain chain = ((Chain)Track.TraversePath(path));

                for (int i = paste.Contents.Count - 1; i >= 0; i--)
                {
                    chain.Remove(right + i + 1);
                }
            }, () => {
                Chain chain = ((Chain)Track.TraversePath(path));

                for (int i = 0; i < paste.Contents.Count; i++)
                {
                    chain.Insert(right + i + 1, pasted[i].Clone());
                }
            });

            for (int i = 0; i < paste.Contents.Count; i++)
            {
                _chain.Insert(right + i + 1, pasted[i].Clone());
            }
        }
        /// <summary>
        /// Saves a shifted copy of an externally constructed cell.
        /// </summary>
        /// <param name="cell"></param>
        /// <param name="newX"></param>
        /// <param name="newY"></param>
        public void SaveExternalCell(MapCellType cell, int newX, int newY)
        {
            Point sp = new Point(newX, newY);
            Copyable <MapCellType> cc = (Copyable <MapCellType>)cell;

            savedCells[sp] = cc.Copy();
        }
示例#5
0
        public async void Paste(int right)
        {
            Copyable paste = await Copyable.DecodeClipboard();

            if (paste != null && Copyable_Insert(paste, right, out Action undo, out Action redo, out Action dispose))
            {
                Program.Project.Undo.Add("Track Pasted", undo, redo, dispose);
            }
        }
示例#6
0
        public static async void Paste(ISelectParent parent, int right)
        {
            Copyable paste = await Copyable.DecodeClipboard();

            if (paste != null && InsertCopyable(parent, paste, right, "Pasted", out InsertCopyableUndoEntry entry))
            {
                Program.Project.Undo.AddAndExecute(entry);
            }
        }
示例#7
0
        public object TryGetCopy(object obj)
        {
            Copyable copyable = obj as Copyable;

            if (copyable == null)
            {
                return(obj);
            }
            return(this.GetCopy(copyable));
        }
示例#8
0
        private static void Encode(BinaryWriter writer, Copyable o)
        {
            EncodeID(writer, typeof(Copyable));

            writer.Write(o.Contents.Count);
            for (int i = 0; i < o.Contents.Count; i++)
            {
                Encode(writer, (dynamic)o.Contents[i]);
            }
        }
示例#9
0
        static Copyable CreateCopyable(ISelectParent parent, int left, int right)
        {
            Copyable copy = new Copyable();

            for (int i = left; i <= right; i++)
            {
                copy.Contents.Add(parent.IChildren[i]);
            }

            return(copy);
        }
示例#10
0
        public bool TryCache(object obj)
        {
            Copyable copyable = obj as Copyable;

            if (copyable == null)
            {
                return(false);
            }
            this.Cache(copyable);
            return(true);
        }
示例#11
0
        async void ReadFile(string path, bool recovery)
        {
            Project  loaded   = null;
            Copyable imported = null;

            try {
                try {
                    using (FileStream file = File.Open(path, FileMode.Open, FileAccess.Read))
                        loaded = await Decoder.Decode(file, typeof(Project));

                    if (!recovery)
                    {
                        loaded.FilePath = path;
                        loaded.Undo.SavePosition();
                        Preferences.RecentsAdd(path);
                    }

                    Program.Project?.Dispose();
                    Program.Project = loaded;
                } catch (InvalidDataException) {
                    using (FileStream file = File.Open(path, FileMode.Open, FileAccess.Read))
                        imported = await Decoder.Decode(file, typeof(Copyable));

                    Program.Project?.Dispose();
                    Program.Project = new Project(
                        tracks: (imported.Type == typeof(Track))
                            ? imported.Contents.Cast <Track>().ToList()
                            : new List <Track>()
                    {
                        new Track(new Chain((imported.Type == typeof(Chain))
                                    ? new List <Device>()
                        {
                            new Group(imported.Contents.Cast <Chain>().ToList())
                        }
                                    : imported.Contents.Cast <Device>().ToList()
                                            ))
                    }
                        );
                }
            } catch {
                await MessageWindow.Create(
                    $"An error occurred while reading the file.\n\n" +
                    "You may not have sufficient privileges to read from the destination folder, or\n" +
                    "the file you're attempting to read is invalid.",
                    null, this
                    );

                return;
            }

            ProjectWindow.Create(this);
            Close();
        }
示例#12
0
 public Copyable GetCopy(Copyable obj)
 {
     for (int i = objectPool.Count - 1; i >= 0; --i)
     {
         Copyable o = objectPool[i];
         if (o.CopyFrom(obj))
         {
             objectPool.RemoveAt(i);
             return(o);
         }
     }
     return(obj.Copy());
 }
示例#13
0
        static bool InsertCopyable(ISelectParent parent, Copyable copyable, int right, string action, out InsertCopyableUndoEntry entry)
        {
            entry = null;

            if (!parent.ChildType.IsAssignableFrom(copyable.Type))
            {
                return(false);
            }

            entry = new InsertCopyableUndoEntry(parent, copyable, right, action);

            return(true);
        }
示例#14
0
        public static async void Replace(ISelectParent parent, int left, int right)
        {
            Copyable paste = await Copyable.DecodeClipboard();

            if (paste != null && InsertCopyable(parent, paste, right, "", out InsertCopyableUndoEntry insert))
            {
                Program.Project.Undo.AddAndExecute(new ReplaceUndoEntry(
                                                       parent,
                                                       new DeleteUndoEntry(parent, left, right),
                                                       insert
                                                       ));
            }
        }
示例#15
0
        bool Copyable_Insert(Copyable paste, int right, out Action undo, out Action redo, out Action dispose)
        {
            undo = redo = dispose = null;

            List <Device> pasted;

            try {
                pasted = paste.Contents.Cast <Device>().ToList();
            } catch (InvalidCastException) {
                return(false);
            }

            List <int> path = Track.GetPath(_chain);

            undo = () => {
                Chain chain = Track.TraversePath <Chain>(path);

                for (int i = paste.Contents.Count - 1; i >= 0; i--)
                {
                    chain.Remove(right + i + 1);
                }
            };

            redo = () => {
                Chain chain = Track.TraversePath <Chain>(path);

                for (int i = 0; i < paste.Contents.Count; i++)
                {
                    chain.Insert(right + i + 1, pasted[i].Clone());
                }

                Track.Get(chain).Window?.Selection.Select(chain[right + 1], true);
            };

            dispose = () => {
                foreach (Device device in pasted)
                {
                    device.Dispose();
                }
                pasted = null;
            };

            for (int i = 0; i < paste.Contents.Count; i++)
            {
                _chain.Insert(right + i + 1, pasted[i].Clone());
            }

            Track.Get(_chain).Window?.Selection.Select(_chain[right + 1], true);

            return(true);
        }
示例#16
0
        async void ReadFile(string path, bool recovery)
        {
            Project  loaded   = null;
            Copyable imported = null;

            try {
                try {
                    using (FileStream file = File.Open(path, FileMode.Open, FileAccess.Read))
                        loaded = await Decoder.Decode <Project>(file);

                    if (!recovery)
                    {
                        loaded.FilePath = path;
                        loaded.Undo.SavePosition();
                        Preferences.RecentsAdd(path);
                    }

                    Program.Project?.Dispose();
                    Program.Project = loaded;
                } catch (InvalidDataException) {
                    using (FileStream file = File.Open(path, FileMode.Open, FileAccess.Read))
                        imported = await Decoder.Decode <Copyable>(file);

                    Program.Project?.Dispose();
                    Program.Project = new Project(
                        tracks: (imported.Type == typeof(Track))
                            ? imported.Contents.Cast <Track>().ToList()
                            : new List <Track>()
                    {
                        new Track(new Chain((imported.Type == typeof(Chain))
                                    ? new List <Device>()
                        {
                            new Group(imported.Contents.Cast <Chain>().ToList())
                        }
                                    : imported.Contents.Cast <Device>().ToList()
                                            ))
                    }
                        );
                }
            } catch {
                await MessageWindow.CreateReadError(this);

                Program.Project = null;

                return;
            }

            ProjectWindow.Create(this);
            Close();
        }
示例#17
0
        public async void Export(int left, int right)
        {
            Window sender = Track.Get(_chain).Window;

            SaveFileDialog sfd = new SaveFileDialog()
            {
                Filters = new List <FileDialogFilter>()
                {
                    new FileDialogFilter()
                    {
                        Extensions = new List <string>()
                        {
                            "apdev"
                        },
                        Name = "Apollo Device Preset"
                    }
                },
                Title = "Export Device Preset"
            };

            string result = await sfd.ShowAsync(sender);

            if (result != null)
            {
                string[] file = result.Split(Path.DirectorySeparatorChar);

                if (Directory.Exists(string.Join("/", file.Take(file.Count() - 1))))
                {
                    Copyable copy = new Copyable();

                    for (int i = left; i <= right; i++)
                    {
                        copy.Contents.Add(_chain[i]);
                    }

                    try {
                        File.WriteAllBytes(result, Encoder.Encode(copy).ToArray());
                    } catch (UnauthorizedAccessException) {
                        await MessageWindow.Create(
                            $"An error occurred while writing the file.\n\n" +
                            "You may not have sufficient privileges to write to the destination folder, or\n" +
                            "the current file already exists but cannot be overwritten.",
                            null, sender
                            );
                    }
                }
            }
        }
示例#18
0
        public void Copy(int left, int right, bool cut = false)
        {
            Copyable copy = new Copyable();

            for (int i = left; i <= right; i++)
            {
                copy.Contents.Add(Program.Project[i]);
            }

            copy.StoreToClipboard();

            if (cut)
            {
                Delete(left, right);
            }
        }
示例#19
0
        public async void Replace(int left, int right)
        {
            Copyable paste = await Copyable.DecodeClipboard();

            if (paste != null && Copyable_Insert(paste, right, out Action undo, out Action redo, out Action dispose))
            {
                Region_Delete(left, right, out Action undo2, out Action redo2, out Action dispose2);

                Program.Project.Undo.Add("Track Replaced",
                                         undo2 + undo,
                                         redo + redo2 + (() => Program.Project.Window?.Selection.Select(Program.Project[left + paste.Contents.Count - 1], true)),
                                         dispose2 + dispose
                                         );

                Selection.Select(Program.Project[left + paste.Contents.Count - 1], true);
            }
        }
示例#20
0
        static void Encode(BinaryWriter writer, Copyable o)
        {
            EncodeID(writer, typeof(Copyable));

            writer.Write(o.Contents.Count);
            for (int i = 0; i < o.Contents.Count; i++)
            {
                if (o.Contents[i] is Device d)
                {
                    Encode(writer, d);
                }
                else
                {
                    Encode(writer, (dynamic)o.Contents[i]);
                }
            }
        }
示例#21
0
        bool Copyable_Insert(Copyable paste, int right, out Action undo, out Action redo, out Action dispose)
        {
            undo = redo = dispose = null;

            List <Track> pasted;

            try {
                pasted = paste.Contents.Cast <Track>().ToList();
            } catch (InvalidCastException) {
                return(false);
            }

            undo = () => {
                for (int i = paste.Contents.Count - 1; i >= 0; i--)
                {
                    Program.Project.Remove(right + i + 1);
                }
            };

            redo = () => {
                for (int i = 0; i < paste.Contents.Count; i++)
                {
                    Program.Project.Insert(right + i + 1, pasted[i].Clone());
                }

                Program.Project.Window?.Selection.Select(Program.Project[right + 1], true);
            };

            dispose = () => {
                foreach (Track track in pasted)
                {
                    track.Dispose();
                }
                pasted = null;
            };

            for (int i = 0; i < paste.Contents.Count; i++)
            {
                Program.Project.Insert(right + i + 1, pasted[i].Clone());
            }

            Selection.Select(Program.Project[right + 1], true);

            return(true);
        }
示例#22
0
        /// <summary>Creates a deep copy of the object, reusing the given object.</summary>
        /// <param name="into">The object to copy into.</param>
        /// <returns>The copy.</returns>
        public void CopyInto(SpatialHashedQuadTree <T> into)
        {
            Copyable.CopyInto(this, into);

            into._cells.Clear();
            foreach (var entry in _cells)
            {
                var treeCopy = entry.Value.NewInstance();
                entry.Value.CopyInto(treeCopy);
                into._cells.Add(entry.Key, treeCopy);
            }

            into._entryBounds.Clear();
            foreach (var bound in _entryBounds)
            {
                into._entryBounds.Add(bound.Key, bound.Value);
            }
        }
示例#23
0
        public async void Copy(int left, int right, bool cut = false)
        {
            Copyable copy = new Copyable();

            for (int i = left; i <= right; i++)
            {
                copy.Contents.Add(_chain[i]);
            }

            string b64 = Convert.ToBase64String(Encoder.Encode(copy).ToArray());

            if (cut)
            {
                Delete(left, right);
            }

            await Application.Current.Clipboard.SetTextAsync(b64);
        }
示例#24
0
        /// <summary>
        /// Constructs a shifted copy of the specified block, located
        /// at the specified (upper left) corner, and saves the result.
        /// </summary>
        /// <param name="block"></param>
        /// <param name="xmin"></param>
        /// <param name="ymin"></param>
        public void SaveExternalBlock(MapCellType[,] block, int xmin, int ymin)
        {
            int width  = block.GetLength(0);
            int height = block.GetLength(1);

            MapCellType cell;

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    Copyable <MapCellType> copyable = block[x, y];
                    cell = copyable.Copy();

                    Point sp = new Point(x + xmin, y + ymin);
                    savedCells[sp] = cell;
                }
            }
        }
示例#25
0
        public async void Import(int right, string path = null)
        {
            Window sender = Track.Get(_chain).Window;

            if (path == null)
            {
                OpenFileDialog ofd = new OpenFileDialog()
                {
                    AllowMultiple = false,
                    Filters       = new List <FileDialogFilter>()
                    {
                        new FileDialogFilter()
                        {
                            Extensions = new List <string>()
                            {
                                "apdev"
                            },
                            Name = "Apollo Device Preset"
                        }
                    },
                    Title = "Import Device Preset"
                };

                string[] result = await ofd.ShowAsync(sender);

                if (result.Length > 0)
                {
                    path = result[0];
                }
                else
                {
                    return;
                }
            }

            Copyable loaded = await Copyable.DecodeFile(path, sender);

            if (loaded != null && Copyable_Insert(loaded, right, out Action undo, out Action redo, out Action dispose))
            {
                Program.Project.Undo.Add("Device Imported", undo, redo, dispose);
            }
        }
示例#26
0
        public static async void Import(ISelectParent parent, int right, string[] paths = null)
        {
            if (parent.ChildFileExtension == null)
            {
                return;
            }

            Window sender = parent.IWindow;

            paths = paths ?? await CreateOFD(parent).ShowAsync(sender);

            if (!paths.Any())
            {
                return;
            }

            Copyable loaded = await Copyable.DecodeFile(paths, sender, parent.ChildType);

            if (loaded != null && InsertCopyable(parent, loaded, right, "Imported", out InsertCopyableUndoEntry entry))
            {
                Program.Project.Undo.AddAndExecute(entry);
            }
        }
示例#27
0
        public async void Replace(int left, int right)
        {
            Copyable paste = await Copyable.DecodeClipboard();

            if (paste != null && Copyable_Insert(paste, right, out Action undo, out Action redo, out Action dispose))
            {
                Region_Delete(left, right, out Action undo2, out Action redo2, out Action dispose2);

                List <int> path = Track.GetPath(_chain);

                Program.Project.Undo.Add("Device Replaced",
                                         undo2 + undo,
                                         redo + redo2 + (() => {
                    Chain chain = Track.TraversePath <Chain>(path);

                    Track.Get(chain).Window?.Selection.Select(chain[left + paste.Contents.Count - 1], true);
                }),
                                         dispose2 + dispose
                                         );

                Track.Get(_chain).Window?.Selection.Select(_chain[left + paste.Contents.Count - 1], true);
            }
        }
示例#28
0
        /// <summary>
        ///     Creates a deep copy of the system. The passed system must be of the same type.
        ///     <para>
        ///         This clones any contained data types to return an instance that represents a complete copy of the one passed
        ///         in.
        ///     </para>
        /// </summary>
        /// <param name="into">The instance to copy into.</param>
        /// <remarks>The manager for the system to copy into must be set to the manager into which the system is being copied.</remarks>
        public virtual void CopyInto(AbstractSystem into)
        {
            // Don't allow identity copying.
            Debug.Assert(into != this, "Cannot copy into self, is this intentional?");
            if (into == this)
            {
                return;
            }

            // Manager must be re-set to new owner before copying.
            if (into.Manager == null)
            {
                throw new ArgumentException("Target must have a Manager.", "into");
            }

            // Systems should never have to be copied inside the same context.
            if (into.Manager == Manager)
            {
                throw new ArgumentException("Target must have a different Manager.", "into");
            }

            // Use dynamic function to do basic copying.
            Copyable.CopyInto(this, into);
        }
示例#29
0
 /// <summary>Creates a deep copy of the object, reusing the given object.</summary>
 /// <param name="into">The object to copy into.</param>
 /// <returns>The copy.</returns>
 public void CopyInto(JointEdge into)
 {
     Copyable.CopyInto(this, into);
 }
示例#30
0
 /// <summary>Creates a deep copy of the object, reusing the given object.</summary>
 /// <param name="into">The object to copy into.</param>
 /// <returns>The copy.</returns>
 public virtual void CopyInto(Behavior into)
 {
     Copyable.CopyInto(this, into);
 }