using System.Collections.Generic; using System.IO; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework; using System.Linq; using Myra.Graphics2D.UI; using Myra.Graphics2D.Brushes; namespace Sledgemapper { public static class ExtensionMethods { public static Dictionary LoadContentFolder(this ContentManager contentManager, string contentFolder) { DirectoryInfo dir = new(contentManager.RootDirectory + "/" + contentFolder); if (!dir.Exists) throw new DirectoryNotFoundException(); Dictionary result = new(); FileInfo[] files = dir.GetFiles("*.*"); foreach (FileInfo file in files.Where(f => f.Extension != ".ttf" && f.Extension != ".otf")) { result.Add(file.Name.Split('.')[0], contentManager.Load(contentFolder + "/" + file.Name.Split('.')[0])); } return result; } public static IEnumerable Split(this string str, int chunkSize) => Enumerable.Range(0, str.Length / chunkSize) .Select(i => str.Substring(i * chunkSize, chunkSize)); public static Point AbsPoint(this Point point) { return new Point(System.Math.Abs(point.X), System.Math.Abs(point.Y)); } public static (Window Window, C Content) GetParentContentInWindow(this Widget widget) where C : Widget { Container container = widget.Parent; while (!(container is Window)) { container = container.Parent; } var localWindow = (Window)container; var localContent = localWindow.Content as C; return (localWindow, localContent); } public static Window GetContainingWindow(this Widget widget) { var container = widget.Parent; while (!(container is Window) || container is null) { container = container.Parent; } return container as Window; } public static bool ValidateTextbox(this TextBox textBox) { var valid = !string.IsNullOrWhiteSpace(textBox.Text); if (!valid) { textBox.Background = new SolidBrush(Color.Red); } else { textBox.Background = new SolidBrush(new Color(51, 51, 51)); ; } return valid; } public static void ShowInModalWindow(this Widget widget, Desktop desktop, string title) { Window window = new() { Title = title, Content=widget }; window.ShowModal(desktop); } } //public interface IExtendedWidget //{ // void ShowInModalWindow(this Widget widget, string title) //} }