using System; using System.Collections.Generic; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Text.Json; using System.Text.Json.Serialization; namespace RokettoLaunch.Models.SaveFile { public class App4Options : AppOptionsBase { public class Group { public Guid Id { get; set; } = Guid.NewGuid(); public string Title { get; set; } public bool IsOpen { get; set; } = false; public List Items { get; set; } = new List(); [JsonConverter(typeof(RyzStudio.Text.Json.JsonSizeConverter))] public Size GridSize { get; set; } = new Size(0, 0); public Item AddItem(Item item) { var canPosition = this.CanPosition(item); if (!canPosition) { // Find next available position item.Position = this.GetNextPosition(); } // Resize-expand table if ((item.Position.Y + 1) > this.GridSize.Height) { this.GridSize = new Size(this.GridSize.Width, (item.Position.Y + 1)); //this.UpdateTable(table, this.GridSize); } // Add item this.Items.Add(item); return item; } public Group CopyOf() { var json = JsonSerializer.Serialize(this); var newGroupInfo = JsonSerializer.Deserialize(json); newGroupInfo.Id = Guid.NewGuid(); newGroupInfo.Title = "Copy of " + newGroupInfo.Title; // Resolve paths foreach (var item in newGroupInfo.Items ?? new List()) { item.Id = Guid.NewGuid(); if (item.IsGroup) { foreach (var subItem in item.Items ?? new List()) { subItem.Id = Guid.NewGuid(); subItem.ResolvePaths(); } } else { item.ResolvePaths(); } } return newGroupInfo; } public Size GetMaxGridSize() { var result = this.GridSize; var maxPosition = this.GetMaxPosition(); result.Width = Math.Max(result.Width, (maxPosition.X + 1)); result.Height = Math.Max(result.Height, (maxPosition.Y + 1)); return result; } public Point GetMaxPosition() { if (this.Items.Count <= 0) { return new Point(0, 0); } var result = new Point(0, 0); result.Y = this.Items.Select(x => x.Position.Y).Max(); result.X = this.Items.Where(x => x.Position.Y == result.Y)?.Select(x => x.Position.X)?.Max() ?? 0; return result; } public Point GetNextPosition() { if (this.Items.Count <= 0) { return new Point(0, 0); } var pos = this.GetMaxPosition(); pos.X++; if (pos.X > (this.GridSize.Width - 1)) { pos.X = 0; pos.Y++; } return pos; } public bool CanPosition(Item tile) { if (tile.Position == new Point(-1, -1)) { return false; } if (Items.Count <= 0) { return true; } return !Items.Where(x => x.Id != tile.Id && x.Position == tile.Position).Any(); } } public class Item { public static Item Create(App3Options.Item model) { var result = new Item(); result.Load(model); return result; } public Guid Id { get; set; } = Guid.NewGuid(); public string Title { get; set; } public string TargetPath { get; set; } public string Argument { get; set; } public string StartPath { get; set; } public ProcessWindowStyle WindowStyle { get; set; } = ProcessWindowStyle.Normal; public bool RunAsAdmin { get; set; } = false; [JsonConverter(typeof(RyzStudio.Text.Json.JsonPointConverter))] public Point Position { get; set; } public bool IsGroup { get; set; } = false; public List Items { get; set; } = new List(); [JsonIgnore] public string ResolvedTargetPath { get; private set; } [JsonIgnore] public string ResolvedArgument { get; private set; } [JsonIgnore] public string ResolvedStartPath { get; private set; } public Item CopyOf() { var json = JsonSerializer.Serialize(this); var newTileInfo = JsonSerializer.Deserialize(json); newTileInfo.Id = Guid.NewGuid(); newTileInfo.Title = "Copy of " + newTileInfo.Title; newTileInfo.Position = new Point(-1, -1); if (newTileInfo.IsGroup) { foreach (var subItem in newTileInfo.Items ?? new List()) { subItem.Id = Guid.NewGuid(); subItem.ResolvePaths(); } } else { newTileInfo.ResolvePaths(); } return newTileInfo; } public void Load(App3Options.Item model) { this.Title = model.Title; this.TargetPath = model.ProcessFilename; this.Argument = model.ProcessArgument; this.StartPath = model.ProcessWorkingDirectory; this.WindowStyle = model.ProcessWindowStyle; this.RunAsAdmin = model.ProcessAsAdmin; this.Position = model.Position; this.IsGroup = model.IsGroup; this.Items = model.Items.Select(x => Item.Create(x))?.ToList() ?? new List(); } public void Load(Item model) { this.Title = model.Title; this.TargetPath = model.TargetPath; this.Argument = model.Argument; this.StartPath = model.StartPath; this.WindowStyle = model.WindowStyle; this.RunAsAdmin = model.RunAsAdmin; this.Position = model.Position; this.IsGroup = model.IsGroup; this.Items = model.Items ?? new List(); this.ResolvedTargetPath = model.ResolvedTargetPath; this.ResolvedStartPath = model.ResolvedStartPath; this.ResolvedArgument = model.ResolvedArgument; this.ResolvePaths(); } public void ResolvePaths() { if (this.IsGroup) { foreach (var subItem in this.Items ?? new List()) { subItem.ResolvePaths(); } } else { this.ResolvedTargetPath = RyzStudio.IO.File.ResolvePath(this.TargetPath); this.ResolvedArgument = RyzStudio.IO.File.ResolvePath(this.Argument); this.ResolvedStartPath = RyzStudio.IO.File.ResolvePath(this.StartPath); } } public override string ToString() { return this.Title?.Trim() ?? string.Empty; } } public new int FileVersion { get; set; } = 4; public List Groups { get; set; } = new List(); public void Load(App3Options options) { this.TilesPerRow = options.TilesPerRow; this.ShowBigIcons = options.ShowBigIcons; this.ShowToggleHotkey = options.ShowToggleHotkey; this.HideOnClose = options.HideOnClose; this.HideOnExecute = options.HideOnExecute; this.AlwaysOnTop = options.AlwaysOnTop; this.StartPosition = options.StartPosition; this.Height = options.Height; foreach (var group in options.Groups) { var newGroup = new Group() { Title = group.Title, IsOpen = group.IsExpanded, GridSize = group.GridSize }; newGroup.Items = group.Items.Select(x => Item.Create(x))?.ToList() ?? new List(); this.Groups.Add(newGroup); } } public Item FindById(Guid id) { if (id == Guid.Empty) { return null; } foreach (var group in this.Groups) { foreach (var item in group.Items) { if (item.Id == id) { return item; } if (item.IsGroup) { foreach (var subItem in item.Items) { if (subItem.Id == id) { return subItem; } } } } } return null; } //public bool Update(Item value) //{ // var result = this.FindById(value.Id); // if (result == null) // { // return false; // } // result.Load(value); // return true; //} public bool Remove(Guid id) { if (id == Guid.Empty) { return false; } foreach (var group in this.Groups) { foreach (var item in group.Items) { if (item.Id == id) { return group.Items.Remove(item); } if (item.IsGroup) { foreach (var subItem in item.Items) { if (subItem.Id == id) { return item.Items.Remove(subItem); } } } } } return false; } } }